问题描述
数据库是 Multipolygons,例如代表 SpatialIndexFeatureCollection 中的城市街区。传入的请求数据是一个 shapefile,其中包含一个表示例如建筑物的 MultiPoint 对象。我们的目标是返回所有包含至少 n 个输入数据点的多边形。
在没有最低要求的情况下,我们有一个可行的解决方案,即使用 FilterFactory2.dwithin() 作为过滤器查询集合,并将多点拆分为一组点作为输入以创建 SimpleFeatureCollection。不过,这种方法只返回每个找到的多面体一次。因此我们无法按出现次数过滤结果。单独过滤每个点的集合似乎效率很低。
有没有办法处理多点和多边形之间的交集?
解决方法
听起来好像需要依次查询每个多边形的点集合,并保留返回集合大于 N 的点集合。
int N = 3;
FileDataStore pointsDS = new ShapefileDataStore(URLs.fileToUrl(new File("/data/natural_earth/110m_cultural/110m_populated_places.shp")));
FileDataStore polyDS = new ShapefileDataStore(URLs.fileToUrl(new File("/data/natural_earth/110m_cultural/110m_admin_0_countries.shp")));
SimpleFeatureCollection points = pointsDS.getFeatureSource().getFeatures();
SimpleFeatureCollection polys = polyDS.getFeatureSource().getFeatures();
FilterFactory2 filterFactory = CommonFactoryFinder.getFilterFactory2();
Expression propertyName = filterFactory.property(points.getSchema()
.getGeometryDescriptor().getName());
ArrayList<SimpleFeature> results = new ArrayList<>();
try(SimpleFeatureIterator itr = polys.features()){
while (itr.hasNext()) {
SimpleFeature poly = itr.next();
Filter filter = filterFactory.within(propertyName,filterFactory.literal(poly.getDefaultGeometry()));
SimpleFeatureCollection sub = points.subCollection(filter);
if(sub.size()>N){
results.add(poly);
}
}
}
for(SimpleFeature f:results){
System.out.println(f.getAttribute("NAME"));
}