我有一个充满来自奥地利的州的GeoJson,我想做一个查询,将某些州拦截我的多边形作为输出。
这是我的查询:
db.GeoAustria.find(
{
'features.geometry':{
$geoIntersects:{
$geometry:{
type: "Polygon",
coordinates: [
[
[
16.21685028076172,
48.007381433478855
],
[
16.24225616455078,
47.98716432210271
],
[
16.256675720214844,
48.00669234420252
],
[
16.21685028076172,
48.007381433478855
]
]
]
}
}
}
}
)
但是它为我提供了所有要素,包括那些不与多边形重叠的要素...在此查询中我的错误在哪里?
基本数组匹配的误解在这里。输入集是在单个FeatureCollection
对象的数组中具有95个多边形的单个文档。当您对此类事情执行find()时,任何相交的地理区域都将导致整个文档作为匹配项返回。这与以下内容完全相同:
> db.foo.insert({x:["A","B","C"]})
WriteResult({ "nInserted" : 1 })
> db.foo.find({x:"A"});
{ "_id" : ObjectId("5fb1845b08c09fb8dfe8d1c1"), "x" : [ "A", "B", "C" ] }
返回整个文档,而不仅仅是元素“ A”。
假设您的集合中可能有多个大型文档。该管道产生Baden的单个目标几何体(我在您的输入集上对其进行了测试):
var Xcoords = [
[
[
16.21685028076172,
48.007381433478855
],
[
16.24225616455078,
47.98716432210271
],
[
16.256675720214844,
48.00669234420252
],
[
16.21685028076172,
48.007381433478855
]
]
];
var targ = {type: "Polygon", coordinates: Xcoords};
db.geo1.aggregate([
// First, eliminate any docs where the geometry array has zero intersects. In this
// context, features.geometry means "for each element of array features get the
// geometry field from the object there", almost like saying "features.?.geometry"
{$match: {"features.geometry": {$geoIntersects: {$geometry: targ}} }}
// Next, break up any passing docs of 95 geoms into 95 docs of 1 geom...
,{$unwind: "$features"}
// .. and run THE SAME $match as before to match just the one we are looking for.
// In this context, the array is gone and "features.geometry" means get JUST the
// object named geometry:
,{$match: {"features.geometry": {$geoIntersects: {$geometry: targ}} }}
]);
除此之外,我可能建议将其FeatureCollection
分解为既可索引(FeatureCollection
在MongoDB中不可索引)又易于处理的东西。例如,针对您的单文档/多多边形设计的小脚本将在95个文档中将其转换为额外信息:
db.geo2.drop();
mainDoc = db.geo1.findOne(); // the one Austria doc
mainDoc['features'].forEach(function(oneFeature) {
var qq = {
country: "Austria",
crs: mainDoc['crs'],
properties: oneFeature['properties'],
geometry: oneFeature['geometry']
};
db.geo2.insert(qq);
});
db.geo2.aggregate([
{$match: {"geometry": {$geoIntersects: {$geometry: targ}} }}
]);
// yields same single doc output (Baden)
这使得匹配和过滤变得容易。有关FeatureCollection
与vs的更多信息,GeometryCollection
请参见https://www.moschetti.org/rants/hurricane.html。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句