【GeoTools】JTS几何关系判断

GeoTools JTS的几何关系判断

maven:


    org.locationtech.jts
    jts-core
    1.16.1

几何关系
相等(equals)
脱节(disjoint)
相交(intersects)
接触(touches)
交叉(crosses)
内含(within)
包含(contains)
覆盖(overlaps)
  1. 相等(equals)
/**
 * 判断两个几何图形是否相等
 * @throws ParseException
 */
public void equals() throws ParseException {
    WKTReader reader = new WKTReader();
    String wktPolygon1 = "POLYGON ((113.5052490234375 34.397844946449865, 114.03259277343749 34.397844946449865, 114.03259277343749 34.82507742547058, 113.5052490234375 34.82507742547058, 113.5052490234375 34.397844946449865))";
//        String wktPolygon2 = "POLYGON ((113.5052490234375 34.397844946449865, 114.03259277343749 34.397844946449865, 114.03259277343749 34.82507742547058, 113.5052490234375 34.82507742547058, 113.5052490234375 34.397844946449865))";
    String wktPolygon2 = "POLYGON ((114.345703125 33.8521697014074, 115.257568359375 33.8521697014074, 115.257568359375 34.53371242139564, 114.345703125 34.53371242139564, 114.345703125 33.8521697014074))";
    Geometry read1 = reader.read(wktPolygon1);
    Geometry read2 = reader.read(wktPolygon2);
    System.out.println(read1.equals(read2));
}

输出结果为false,当两个图形的坐标完全相同时输出true

图形:
【GeoTools】JTS几何关系判断_第1张图片
2. 脱节(disjoint)

/**
* 判断两个几何图形是否不相交
* @throws Exception
*/
public void disjoint() throws Exception {
   WKTReader reader = new WKTReader();
   String wktPolygon1 = "POLYGON ((113.5052490234375 34.397844946449865, 114.03259277343749 34.397844946449865, 114.03259277343749 34.82507742547058, 113.5052490234375 34.82507742547058, 113.5052490234375 34.397844946449865))";
   String wktPolygon2 = "POLYGON ((114.345703125 33.8521697014074, 115.257568359375 33.8521697014074, 115.257568359375 34.53371242139564, 114.345703125 34.53371242139564, 114.345703125 33.8521697014074))";
   Geometry read1 = reader.read(wktPolygon1);
   Geometry read2 = reader.read(wktPolygon2);
   System.out.println(read1.disjoint(read2));
}

输出true,说明两个图形没有相交

图形:
【GeoTools】JTS几何关系判断_第2张图片

  1. 相交
/**
* 判断两个几何图形是否相交(和脱节刚好相反)
* @throws Exception
*/
public void intersects() throws Exception {
   WKTReader reader = new WKTReader();
   String wktPolygon1 = "POLYGON ((114.345703125 33.8521697014074, 115.257568359375 33.8521697014074, 115.257568359375 34.53371242139564, 114.345703125 34.53371242139564, 114.345703125 33.8521697014074))";
   String wktPolygon2 = "POLYGON ((114.85107421875 33.57343808567733, 115.60913085937499 33.57343808567733, 115.60913085937499 34.134541681937364, 114.85107421875 34.134541681937364, 114.85107421875 33.57343808567733))";
   Geometry read1 = reader.read(wktPolygon1);
   Geometry read2 = reader.read(wktPolygon2);
   System.out.println(read1.intersects(read2));
}

输出true,说明连个图形相交了

图形:
【GeoTools】JTS几何关系判断_第3张图片

  1. 接触(touches)
/**
* 判断几何体是否接触(边界是否有共同的点或边界线)
* @throws Exception
*/
public void touches() throws Exception {
   WKTReader reader = new WKTReader();
   String wktPolygon1 = "POLYGON ((115.6087875366211 33.11282400421533, 116.1416244506836 33.11282400421533, 116.1416244506836 33.57172177544194, 115.60913085937499 33.57343808567733, 115.6087875366211 33.11282400421533))";
   String wktPolygon2 = "POLYGON ((114.85107421875 33.57343808567733, 115.60913085937499 33.57343808567733, 115.60913085937499 34.134541681937364, 114.85107421875 34.134541681937364, 114.85107421875 33.57343808567733))";
   Geometry read1 = reader.read(wktPolygon1);
   Geometry read2 = reader.read(wktPolygon2);
   System.out.println(read1.touches(read2));
}

输出为true,两个图形的边界的点或线的坐标一样,并且图形内部没有公共的点。使用intersects方法输出也为true,和intersects不同的是touches只允许两个图形的边界点有公共点,不允许内部有公共点。

图形:
【GeoTools】JTS几何关系判断_第4张图片
下面这种图形输出也为ture,但是Geometry对象不能同时为线(可以是线,但是结果为false),只能是面和面、面和线进行判断。(没有测试点)
【GeoTools】JTS几何关系判断_第5张图片

  1. 交叉(crosses)
/**
* 判断几何图形是否交叉(穿过)
* @throws Exception
*/
public void crosses() throws Exception {
   WKTReader reader = new WKTReader();
   String wktPolygon1 = "LINESTRING (115.90164184570312 33.18123802491263, 116.08840942382812 33.005208549965474, 116.43447875976561 33.16284622181141)";
   String wktPolygon2 = "LINESTRING (115.82748413085936 33.042629907076225, 116.08840942382812 33.005208549965474, 116.38710021972655 33.00981511270531)";
   Geometry read1 = reader.read(wktPolygon1);
   Geometry read2 = reader.read(wktPolygon2);
   System.out.println(read1.crosses(read2));
}

输出为true,intersects方法输出也为true,但是和intersects不同的是如果两个Geometry 都为面时crosses输出为false。一个为面,一个为线的两个Geometry交叉时crosses输出也为ture。

图形:
【GeoTools】JTS几何关系判断_第6张图片
下面这种图形crosses输出为true,但是Geometry对象不能同时为面(可以是面,但是结果为false),只能是线和线、面和线进行判断。(没有测试点)
【GeoTools】JTS几何关系判断_第7张图片

  1. 内含(within)
/**
* 内含
* @throws Exception
*/
public void within() throws Exception {
   WKTReader reader = new WKTReader();
   String wktPolygon1 = "POLYGON ((112.87353515625 31.970803930433096, 114.45556640625 31.970803930433096, 114.45556640625 33.30298618122413, 112.87353515625 33.30298618122413, 112.87353515625 31.970803930433096))";
   String wktPolygon2 = "POLYGON ((111.357421875 30.95876857077987, 115.90576171874999 30.95876857077987, 115.90576171874999 34.361576287484176, 111.357421875 34.361576287484176, 111.357421875 30.95876857077987))";
   Geometry read1 = reader.read(wktPolygon1);
   Geometry read2 = reader.read(wktPolygon2);
   System.out.println(read1.within(read2));
}

输出true,如图,A图形内含在B图形里。

图片:
【GeoTools】JTS几何关系判断_第8张图片

  1. 包含(contains)
/**
 * 包含
 * @throws Exception
 */
public void contains() throws Exception {
    WKTReader reader = new WKTReader();
    String wktPolygon1 = "POLYGON ((112.87353515625 31.970803930433096, 114.45556640625 31.970803930433096, 114.45556640625 33.30298618122413, 112.87353515625 33.30298618122413, 112.87353515625 31.970803930433096))";
    String wktPolygon2 = "POLYGON ((111.357421875 30.95876857077987, 115.90576171874999 30.95876857077987, 115.90576171874999 34.361576287484176, 111.357421875 34.361576287484176, 111.357421875 30.95876857077987))";
    Geometry read1 = reader.read(wktPolygon1);
    Geometry read2 = reader.read(wktPolygon2);
    System.out.println(read2.contains(read1));
}

输出为true,如图,B图形包含A图形,和内含刚好相反。

图形:
【GeoTools】JTS几何关系判断_第9张图片

  1. 覆盖(overlaps)
/**
 * 覆盖
 * @throws Exception
 */
public void overlaps() throws Exception {
    WKTReader reader = new WKTReader();
    String wktPolygon1 = "POLYGON ((111.357421875 30.95876857077987, 115.90576171874999 30.95876857077987, 115.90576171874999 34.361576287484176, 111.357421875 34.361576287484176, 111.357421875 30.95876857077987))";
    String wktPolygon2 = "POLYGON ((114.75219726562499 30.420256142845158, 117.72949218749999 30.420256142845158, 117.72949218749999 32.58384932565662, 114.75219726562499 32.58384932565662, 114.75219726562499 30.420256142845158))";
    Geometry read1 = reader.read(wktPolygon1);
    Geometry read2 = reader.read(wktPolygon2);
    System.out.println(read1.overlaps(read2));
}

输出为true,两个几何图形必须有公共的点但不是所有的点。就是说两个图形要有相交的部分,也要有不相交的部分。和intersects方法不同的是当两个图形的一个点或者边界相交时,输出为false。

图形:
【GeoTools】JTS几何关系判断_第10张图片

你可能感兴趣的:(GeoTools,其他)