任凭风浪起,稳坐钓鱼台 2019-11-13 15:29 采纳率: 0%
浏览 329

有没有做GIS开发的大佬,相知道LineString的自相交和面的自相交是怎么验证的

查遍了JTS的Api,一直没找到方法,两个个几何对象的倒有,但一个几何对象的没有,另外,MULTIPOLYGON该如何打散成多个POLYGON的

  • 写回答

1条回答 默认 最新

  • 明初啥都能学会 2024-04-27 11:17
    关注

    该回答引用ChatGPT3.5,结果代码方法已验证可行
    对于LineString和Polygon的自相交检测,JTS(Java Topology Suite)提供了相应的方法。

    1. LineString自相交检测
      使用LineIntersector类来检测LineString是否自相交。你可以使用LineIntersectorcomputeIntersections()方法来计算交点,并检查交点的数量。如果有交点,则表明LineString自相交。

      import com.vividsolutions.jts.geom.Coordinate;
      import com.vividsolutions.jts.geom.Geometry;
      import com.vividsolutions.jts.geom.LineString;
      import com.vividsolutions.jts.geom.PrecisionModel;
      import com.vividsolutions.jts.geom.TopologyException;
      import com.vividsolutions.jts.geom.impl.PackedCoordinateSequenceFactory;
      import com.vividsolutions.jts.geom.util.LineStringExtracter;
      import com.vividsolutions.jts.operation.linemerge.LineMerger;
      import com.vividsolutions.jts.precision.GeometryPrecisionReducer;
      import com.vividsolutions.jts.precision.MinimumClearance;
      import com.vividsolutions.jts.precision.MinimumClearanceFinder;
      import com.vividsolutions.jts.precision.SimpleGeometryPrecisionReducer;
      import com.vividsolutions.jts.util.GeometricShapeFactory;
      import com.vividsolutions.jts.util.Stopwatch;
      
      public class LineStringSelfIntersect {
          public static boolean isSelfIntersect(LineString line) {
              LineIntersector lineIntersector = new RobustLineIntersector();
              Coordinate[] coordinates = line.getCoordinates();
              for (int i = 0; i < coordinates.length - 1; i++) {
                  for (int j = i + 1; j < coordinates.length - 1; j++) {
                      lineIntersector.computeIntersection(coordinates[i], coordinates[i + 1], coordinates[j], coordinates[j + 1]);
                      if (lineIntersector.hasIntersection()) {
                          return true;
                      }
                  }
              }
              return false;
          }
      }
      
    2. Polygon自相交检测
      对于Polygon自相交的检测,你可以先将Polygon转换为一个LineString,然后使用上述的方法来检测LineString是否自相交。如果是自相交的,说明Polygon也自相交。

      import com.vividsolutions.jts.geom.Geometry;
      import com.vividsolutions.jts.geom.Polygon;
      import com.vividsolutions.jts.operation.polygonize.Polygonizer;
      
      public class PolygonSelfIntersect {
          public static boolean isSelfIntersect(Polygon polygon) {
              Geometry boundary = polygon.getBoundary(); // Get the boundary as LineString
              return LineStringSelfIntersect.isSelfIntersect(boundary);
          }
      }
      
    3. MULTIPOLYGON打散成多个POLYGON
      对于MULTIPOLYGON,你可以使用GeometryCollection类来打散成多个Polygon,然后遍历处理每个Polygon。

      import com.vividsolutions.jts.geom.Geometry;
      import com.vividsolutions.jts.geom.GeometryCollection;
      import com.vividsolutions.jts.geom.Polygon;
      
      public class MultiPolygonToPolygons {
          public static Polygon[] explodeMultiPolygon(MultiPolygon multiPolygon) {
              GeometryCollection geometryCollection = (GeometryCollection) multiPolygon;
              int numGeometries = geometryCollection.getNumGeometries();
              Polygon[] polygons = new Polygon[numGeometries];
              for (int i = 0; i < numGeometries; i++) {
                  Geometry geometry = geometryCollection.getGeometryN(i);
                  if (geometry instanceof Polygon) {
                      polygons[i] = (Polygon) geometry;
                  }
              }
              return polygons;
          }
      }
      

    这些方法可以帮助你验证LineString和Polygon的自相交,并将MULTIPOLYGON打散成多个POLYGON。

    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗