我需要一些帮助: how to insert geojson polygons into a mongodb database and query, based on the coordinates of a certain point, all the polygons which are located at a certain distance from that point
这是我将使用的geojson样本:
var testJson = {
"type" : "Polygon",
"properties": {},
"geometry" : [
[
[ 40.8003, -73.9580 ],
[ 40.7968, -73.9498 ],
[ 40.7648, -73.9737 ],
[ 40.7681, -73.9814 ],
[ 40.8003, -73.9580 ]
]
]
};
我想将它保存到mongodb数据库并能够查询它 . 为此,我执行以下操作:
- 我创建了一个猫鼬模式:
var GeoSchema = mongoose.Schema({“type”:{“type”:String},“properties”:{“type”:Object},“geometry”:{“type”:Array,“index”:'2dsphere' }};
- 我创建了一个猫鼬模型:
var GeoModel = mongoose.model('GeoModel',GeoSchema);
- 我将geojson保存到我的数据库:
var post = new GeoModel(testJson);
post.save(function(err,doc){if(err)throw err; console.log(“After Save:\ n%s”,JSON.stringify(doc,undefined,4));});
- 最后,我搜索并找到记录:
GeoModel.find({geometry:[[40.8003,-73.9580],[40.7968,-73.9498],[40.7648,-73.9737],[40.7681,-73.9814],[40.8003,-73.9580]]},函数(错误,记录){if(err)return console.log(err); console.log(records);});
The issues I need help with:
我将以多边形的形式加载大量的geojson数据 .
可以说我位于以下坐标: 40.8003, -73.9580
对于x公里的距离,我希望能够使用范围内的所有多边形 .
所以,我正在考虑做一个类似于此的查询,当然这不起作用:):
GeoModel.find(
{
geometry: {
$near : {
$geometry : {
type : "Point" ,
coordinates : [40.8003, -73.9580]
},
$maxDistance : 20000
}
},
}, function(err, records) {
if (err) return console.log(err);
console.log(records);
}
);
我收到这个错误:
{ [MongoError: Unable to execute query: error processing query: ns=coverageMap.geomodels limit=1000 skip=0
Tree: GEONEAR field=geometry maxdist=20000 isNearSphere=0
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query]
name: 'MongoError',
message: 'Unable to execute query: error processing query: ns=coverageMap.geomodels limit=1000 skip=0\nTree: GEONEAR field=geometry maxdist=20000 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
'$err': 'Unable to execute query: error processing query: ns=coverageMap.geomodels limit=1000 skip=0\nTree: GEONEAR field=geometry maxdist=20000 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 17007 }
关于如何解决这个问题的任何想法?
谢谢!