TJR*_*TJR 18 javascript mongodb meteor
我尝试使用以下方法准备我的数据库字段以进行地理编码:
MyCollection._ensureIndex({'data.address.located':'2dsphere'});
Run Code Online (Sandbox Code Playgroud)
但是这个错误来了:
MongoError: Can't extract geo keys from object, malformed geometry?:
{ type: "Point", coordinates: [ 32.4586858, -110.8571443 ] }
Run Code Online (Sandbox Code Playgroud)
我看不出这个领域有什么问题?任何的想法 ?
当我去看一下这个就说明了这一点:
The following example stores a GeoJSON Point:
{ loc: { type: "Point", coordinates: [ 40, 5 ] } }
Run Code Online (Sandbox Code Playgroud)
go-*_*leg 52
问题是
[ 32.4586858, -110.8571443 ]
Run Code Online (Sandbox Code Playgroud)
不是有效的坐标.顺序应该是经度,然后是纬度,而该坐标看起来是相反的(通过有效纬度范围是-90到90并且-110.8571443在其之外的事实来判断).
我想你的意思是:
{ type: "Point", coordinates: [ -110.8571443, 32.4586858 ] }
Run Code Online (Sandbox Code Playgroud)
或者还有其他一些输入错误.
和指数预计坐标是在该范围.
但是,如果您尝试在已导入的数据上应用索引并发现交换坐标,则可能需要将其交换回来而不是重新导入整个集合.为此,您可以使用下一个mongoshell脚本.
假设你已经有了良好
GeoJSON的类型Point.
db.coll.find().forEach(function (e) {
// assuming that `geoJson` is our field containing `coordinates`
e.geoJson.coordinates = [ // Swapping Lat/Lon
e.geoJson.coordinates[1], // Longitude goes first
e.geoJson.coordinates[0] // Latitude goes last
];
db.coll.save(e);
// Some `print(e.name)` can go here just to understand the progress
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18433 次 |
| 最近记录: |