如何修复地理空间数据以在MongoDB中创建2dsphere索引
MongoDB 2dsphere索引创建中的多边形自相交问题及批量修复需求
问题背景
我有一个存储地理空间数据的MongoDB集合,计划为集合中的location字段创建2dsphere索引。以下是集合中的示例数据(仅展示核心地理空间相关部分):
[ { "entity_number": "Q1765236089-1", "location": { "type": "Polygon", "coordinates": [ [ [66.94697700059068, 39.37199847874891, 0], [66.94713546519371, 39.37185880644635, 0], [66.94738281423123, 39.37200984329424, 0], [66.94721290635363, 39.37215291517258, 0], [66.94721755688605, 39.37215367622946, 0], [66.94697700059068, 39.37199847874891, 0] ] ] } }, { "entity_number": "J1765212045-9", "location": { "type": "Polygon", "coordinates": [ [ [66.64272448285637, 40.01845655540586, 0], [66.64280212857551, 40.01849759761946, 0], [66.64289492226342, 40.01841983943562, 0], [66.6428077685561, 40.01837167238775, 0], [66.64281005851667, 40.01837164697973, 0], [66.64272448285637, 40.01845655540586, 0] ] ] } } ]
索引创建操作
执行的索引创建命令如下:
db.Coordinates.createIndex({location:"2dsphere"})
遇到的错误
创建索引过程中,针对entity_number为Q1765236089-1的文档抛出错误:
"Edges 2 and 4 cross. Edge locations in degrees: [39.3720098,
66.9473828]-[39.3721529, 66.9472129] and [39.3721537, 66.9472176]-[39.3719985, 66.9469770]"
临时修复方案
根据错误提示,移除坐标数组中索引为4的条目后,该文档的索引创建问题得到解决。修改后的坐标结构如下:
{ "coordinates": [ [ [66.94697700059068, 39.37199847874891, 0], [66.94713546519371, 39.37185880644635, 0], [66.94738281423123, 39.37200984329424, 0], [66.94721290635363, 39.37215291517258, 0], [66.94697700059068, 39.37199847874891, 0] ] ] }
批量修复需求
集合中其他文档也存在类似的多边形边自相交问题,每次移除对应的冗余坐标条目就能解决问题。现在我想编写一个函数来批量校验并修复所有数据,需要了解MongoDB创建2dsphere索引时的几何校验逻辑,希望能得到相关的帮助或建议。(注:集合中的多边形边数不一,部分超过4条,最多的有50条边)
内容的提问来源于stack exchange,提问作者ABDULLOKH MUKHAMMADJONOV
相关产品推荐
相关产品推荐

