我有一个形状文件(Sample.shp)以及另外两个文件(Sample.shx和Sample.dbf),它们具有为Bombay的15个密码定义的几何(多边形)。
我可以使用快速入门教程查看.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 / Polygon对象,这样我就可以使用Geometry.contains()来查找某个点是否属于特定的Geometry / Polygon。
我试过了:
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
非常感谢任何帮助
答案 0 :(得分:1)
实际上,您不需要提取自己的几何 - 只需创建一个过滤器并遍历过滤后的集合。在您的情况下,可能只返回一个功能。
Filter pointInPolygon = CQL.toFilter("CONTAINS(the_geom, POINT(1 2))");
SimpleFeatureCollection features = source.getFeatures(filter);
SimpleFeatureIterator iterator = features.features();
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
Geometry geom = (Geometry) feature.getDefaultGeometry();
/*... do something here */
}
} finally {
iterator.close(); // IMPORTANT
}
有关查询数据存储的完整讨论,请参阅Query Lab。
答案 1 :(得分:0)
我使用了上述解决方案并尝试了几种组合。刚刚将“THE_GEOM”更改为小写,POINT按顺序(Lon Lat)
Filter filter = CQL.toFilter("CONTAINS(the_geom, POINT(72.82916 18.942883))");
SimpleFeatureCollection collection=featureSource.getFeatures(filter);
SimpleFeatureIterator iterator = collection.features();
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
.....
}
} finally {
iterator.close(); // IMPORTANT
}