我有一個形狀文件(Sample.shp)以及兩個其他文件(Sample.shx和Sample.dbf),它具有爲Bombay的15個pincode定義的幾何(多邊形)。使用Geotools從shapefile中提取多邊形
我可以使用快速入門教程查看.shp文件。
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
// Create a map content and add our shapefile to it
MapContent map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
map.addLayer(layer);
// Now display the map
JMapFrame.showMap(map);
現在,我想這15個pincodes的幾何形狀轉換爲15幾何/多邊形對象,以便我可以使用Geometry.contains()找到一個點是否落在特定的幾何/多邊形。
我想:
ShapefileReader r = new ShapefileReader(new ShpFiles(file),true,false,geometryFactory);
System.out.println(r.getCount(0)); >> returns 51
System.out.println(r.hasNext()); >> returns false
任何幫助,非常感謝
感謝您的回覆。看起來這應該爲我工作。我試過featureSource.getFeatures()。size()>> 15(這是正確的,因爲我有15個多邊形)。但是當我做Filter filter = CQL.toFilter(「CONTAINS(THE_GEOM,POINT(72.83 18.94))」);然後featureSource.getFeatures(filter)>> NULL(這應該返回1的大小,因爲此點位於其中一個多邊形中)。 Google的座標是(18.94,72.83)有什麼建議嗎? – Manisha
嘗試POINT(72.83 18.94) - 緯度/經度和x/y不直觀 –
嘿感謝您的解決方案..我只是改變了「THE_GEOM」爲「the_geom」,它的工作。 – Manisha