JTS Geometry 常用方法总结

1.缓冲区(Buffer):计算具有给定宽度的几何图形周围的缓冲区几何图形。

  /**
     * 几何图形指定距离(扩大或者缩小)后的几何图形
     *
     * @param a
     * @param distance
     * @return
     */
    public Geometry bufferGeometry(Geometry a, double distance) {
        return a.buffer(distance);
    }

JTS Geometry 常用方法总结_第1张图片

 JTS Geometry 常用方法总结_第2张图片

2, equals:两个几何对象是否重叠

/**
     *  两个几何对象是否完全重叠
     * @return
     * @throws ParseException
     */
    public void equalsGeometry() throws ParseException{
        WKTReader reader = new WKTReader( geometryFactory );
        String wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        String wkt2 = ""LINESTRING(3 0, 1 0)";
        LineString geometry1 = (LineString) reader.read(wkt1);
        LineString geometry2 = (LineString) reader.read(wkt2);
        boolean b1 = geometry1.equals(geometry2);//false
        
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = ""LINESTRING(10 0, 0 0)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b1 = geometry1.equals(geometry2);//true

    }

3:Crosses:几何对象是否交叉(两个对象共享一些点,但不是所有的内部点)

/**
     *  两个几何对象是否交叉
     * @return
     * @throws ParseException
     */
    public void crossGeometry() throws ParseException{
        GeometryFactory geometryFactory = new GeometryFactory();
        //情景1
        WKTReader reader = new WKTReader(geometryFactory);
        String wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        String wkt2 = "LINESTRING(3 0, 1 0)";
        LineString geometry1 = (LineString) reader.read(wkt1);
        LineString geometry2 = (LineString) reader.read(wkt2);
        boolean b1 = geometry1.crosses(geometry2);//false
        System.out.println(b1);
        //情景2
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = "LINESTRING(3 0, 1 1)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b2 = geometry1.crosses(geometry2);//false
        System.out.println(b2);
        //情景3
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = "LINESTRING(4 0, -1 -1)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b3 = geometry1.crosses(geometry2);//false
        System.out.println(b3);
        //情景4
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = "LINESTRING(0 4, 3 0, -1 -1)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b4 = geometry1.crosses(geometry2);//true
        System.out.println(b4);

    }

JTS Geometry 常用方法总结_第3张图片

4,intersects :两个对象是否空间相交(至少有一个点)

/**
     * 至少一个公共点(相交)
     * @return
     * @throws ParseException
     */
    public boolean intersectsGeo() throws ParseException{
        GeometryFactory geometryFactory = new GeometryFactory();
        WKTReader reader = new WKTReader(geometryFactory);
        //情景1
        String wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        String wkt2 = "LINESTRING(3 0, 1 0)";
        LineString geometry1 = (LineString) reader.read(wkt1);
        LineString geometry2 = (LineString) reader.read(wkt2);
        boolean b1 = geometry1.intersects(geometry2);//true
        System.out.println(b1);
        //情景2
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = "LINESTRING(3 0, 1 1)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b2 = geometry1.intersects(geometry2);//true
        System.out.println(b2);
        //情景3
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = "LINESTRING(4 0, -1 -1)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b3 = geometry1.intersects(geometry2);//true
        System.out.println(b3);
        //情景4
        wkt1 = "LINESTRING(0 0, 3 0, 10 0)";
        wkt2 = "LINESTRING(0 4, 3 0, -1 -1)";
        geometry1 = (LineString) reader.read(wkt1);
        geometry2 = (LineString) reader.read(wkt2);
        boolean b4 = geometry1.intersects(geometry2);//true
        System.out.println(b4);
    }

JTS Geometry 常用方法总结_第4张图片

5,within: 判断当前几何图形是否在指定几何图形内。

    /**
     * 判断点point(x,y)是否在geometry表示的Polygon中
     * @param x
     * @param y
     * @param geometry wkt格式
     * @return
     */
    public boolean withinGeo(double x,double y,String geometry) throws ParseException {

        Coordinate coord = new Coordinate(x,y);
        Point point = geometryFactory.createPoint( coord );

        WKTReader reader = new WKTReader( geometryFactory );
        Polygon polygon = (Polygon) reader.read(geometry);
        return point.within(polygon);
    }

    /**
     * lineString是否在Polygon中
     * @return
     */
    public boolean withinGeo(LineString lineString, Geometry polygon) throws ParseException {
        return lineString.within(polygon);
    }

6, disjoint: 几何形状没有共有的点(相邻)

   /**
     * 几何对象没有交点(相邻)
     * @return
     * @throws ParseException
     */
    public boolean disjointGeo() throws ParseException{
        GeometryFactory geometryFactory = new GeometryFactory();
        //情景1
        WKTReader reader = new WKTReader(geometryFactory);
        LineString geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
        LineString geometry2 = (LineString) reader.read("LINESTRING(0 1, 0 2)");
        System.out.println(geometry1.disjoint(geometry2));//true
        //情景2
        geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
        geometry2 = (LineString) reader.read("LINESTRING(0 1, -1 -1)");
        System.out.println(geometry1.disjoint(geometry2));//true
        //情景3
        geometry1 = (LineString) reader.read("LINESTRING(0 0, 2 0, 5 0)");
        geometry2 = (LineString) reader.read("LINESTRING(0 0, -1 -1)");
        System.out.println(geometry1.disjoint(geometry2));//false
}

JTS Geometry 常用方法总结_第5张图片

你可能感兴趣的:(Geometry,java)