gpt4 book ai didi

Mongodb $geoIntersect 问题

转载 作者:可可西里 更新时间:2023-11-01 10:29:47 25 4
gpt4 key购买 nike

我有一个集合,其属性为“place”,如下所示。

"place" : {
"name" : "Central Region",
"country" : "Country_A",
"coor_type" : "Polygon",
"coordinates" : [
[
[
103.749959507073,
1.2123138339349
],
[
103.918426999964,
1.2123138339349
],
[
103.918426999964,
1.36874499902569
],
[
103.749959507073,
1.36874499902569
]
]
]
}

我正在尝试使用 $geoInterset 运算符来获取文档列表,其中 place.coordinates 与由两点定义的矩形相交。

[ [103.591247, 1.222136], [104.040313, 1.477497] ] 

第一个点是矩形的左下角,另一个点是矩形的右上角。

我写了一个查询:

db.Document.find(
{'place.coordinates':
{$geoIntersects:
{$geometry:
{type:'Polygon'
, coordinates:[[103.591247,1.222136],[104.040313,1.477497]]
}
}
}
}
);

然而,mongodb 一直报错

error: {
"$err" : "Malformed geo query: { $geoIntersects: { $geometry:
{ type:\"Polygon\",
coordinates:[[103.591247,1.222136],[104.040313,1.477497]]
}}}",
"code" : 16677}

我尝试过不同的类型,例如“Box”和“LineString”。 Box 会给出类似的错误。 LineString 不会提示,但也不会返回任何结果。

我在这里做错了什么?有什么建议吗?

最佳答案

主要问题是 place 文档和查询不是正确的 Polygon GeoJSON 文档。

这是修复它的方法。

1)

LinearRing 的第一个和最后一个坐标必须相同。

Polygons consist of an array of GeoJSON LinearRing coordinate arrays. These LinearRings are closed LineStrings. Closed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates. -- MongoDb doc - Polygon

2)

coor_type 重命名为 type 以符合 GeoJSON 标准。

{ <location field>: { type: "<GeoJSON type>" , coordinates: <coordinates> } }

In order to index GeoJSON data, you must store the data in a location field that you name. The location field contains a subdocument with a type field specifying the GeoJSON object type and a coordinates field specifying the object’s coordinates. Always store coordinates in longitude, latitude order. Mongodb docs - geojson polygon

示例 GeoJSON 多边形文档插入:

var obj = {
"place": {
"name": "Central Region",
"country": "Country_A",
"type": "Polygon",
"coordinates": [
[
[
103.749959507073,
1.2123138339349
],
[
103.918426999964,
1.2123138339349
],
[
103.918426999964,
1.36874499902569
],
[
103.749959507073,
1.36874499902569
],
[
103.749959507073,
1.2123138339349
]
]
]
}
};

db.geo.drop();
db.geo.insert( obj );

//Use the 2dsphere index to validate your GeoJSON format
// and speed up queries
db.geo.ensureIndex({ "place" : "2dsphere" })

3)

确保您的查询具有正确的 Polygon 值,并且在包含 typecoordinates 字段的字段上进行搜索。您正在搜索字段 place.coordinates,而它本应是 place

示例 $geoIntersects 查询

db.geo.find({
place: {
$geoIntersects: {
$geometry: {
type: "Polygon",
coordinates: [
[
[
103.749959507073,
1.2123138339349
],
[
103.918426999964,
1.2123138339349
],
[
103.918426999964,
1.36874499902569
],
[
103.749959507073,
1.36874499902569
],
[
103.749959507073,
1.2123138339349
]
]
]
}
}
}
})

关于Mongodb $geoIntersect 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28163987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com