2017-10-06 67 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); 
    } 
相關問題