2017-10-06 63 views
2

这是我在stackoverflow中的第一个问题,所以我希望我以正确的方式做到这一点。如何使用GeoTools获得shapeFile .shp中多边形的所有点?

我正在使用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的四个界限,但不包含多边形的点,是有可能做到这一点?

谢谢。

+0

当我说点我的意思是该图的各点的纬度和经度 –

+0

'SimpleFeatureIterator'并检查几何形状,[示例](http://docs.geotools.org/。稳定/ userguide /教程/几何/ geometrycrs.html)? – trashgod

回答

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); 
    } 
相关问题