【问题标题】:How I can get all the points of a polygon in a shapeFile .shp using GeoTools?如何使用 GeoTools 在 shapeFile .shp 中获取多边形的所有点?
【发布时间】:2018-03-18 10:06:04
【问题描述】:

这是我在 stackoverflow 中的第一个问题,所以我希望我以正确的方式进行。

我正在使用 geotools 读取 shapeFile (.shp),但找不到获取多边形所有点的函数。现在我有以下代码:

public class ImportShapeFileService implements ImportShapeFileServiceInterface {

    @Override
    public void loadShapeFile(File shapeFile) throws MdfException {

        FileDataStore store;
        try {
            store = FileDataStoreFinder.getDataStore(shapeFile);
            SimpleFeatureSource featureSource = store.getFeatureSource();
            SimpleFeatureCollection collection = featureSource.getFeatures();

            ReferencedEnvelope env = collection.getBounds();
            double left = env.getMinX();
            double right = env.getMaxX();
            double top = env.getMaxY();
            double bottom = env.getMinY();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我得到了 shapFile 的四个边界,但不是包含多边形的点,是否可以这样做?

谢谢。

【问题讨论】:

  • 我所说的点是指图中每个点的经纬度。
  • SimpleFeatureIterator 并检查几何,对于 example?

标签: java javafx shapefile geotools


【解决方案1】:

根据你想对积分做什么,我会做类似的事情:

try(SimpleFeatureIterator itr1 = features.features()){
  while(itr1.hasNext()){
     SimpleFeature f = itr1.next();
     Geometry g = (Geometry)f.getDefaultGeometry();
     Coordinate[] coords = g.getCoordinates();
     // do something with the coordinates
   }
}

如果你必须有Point 而不仅仅是坐标(并且你确定你有多边形,那么你可以使用:

  Geometry g = (Geometry)f.getDefaultGeometry();
  for(int i=0;i<g.getNumPoints();i++) {
    Point p=((Polygon)g).getExteriorRing().getPointN(i);
  }

【讨论】:

  • g.getCoordinates() 在“23-SNAPSHOT”版本中不再存在。 ://
  • 看来locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/… Geometry 是 JTS 对象而不是 GeoTools 对象。
  • 我明白了,谢谢!你碰巧知道如何从中获取CoordinateXYMZ 对象吗? g.getCoordinate() 返回一个普通的 Coordinate 具有 M 值但它是 NaN,即使 shapefile 已正确设置 M 值。
  • 你需要问一个新问题
  • 是的,如果你不知道答案,我打算这样做。
猜你喜欢
  • 2014-03-09
  • 2020-11-25
  • 1970-01-01
  • 2018-10-06
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多