【问题标题】:Unable to use MongoDB's $geoIntersect to match a Polygon to a Point it contains无法使用 MongoDB $geoIntersects 将多边形与其包含的点匹配
【发布时间】:2021-02-18 06:20:27
【问题描述】:

我已将一组 geoJSON 多边形导入到我的 mongoDB 集合中,这些多边形相当于宾夕法尼亚州的立法区。这些数据来自宾夕法尼亚州立大学网站上的geoJSON file,不同的工具确实从文件中正确提取,这告诉我原始数据是有效的。

我通过将数据加载到数组中并遍历每个条目并填充 mongoosejs 架构中的相应 geometry 字段来导入它。一个地区的架构如下所示:

const polygonSchema = new Schema({
    type: {
      type: String,
      enum: ['Polygon'],
      required: true
    },
    coordinates: {
      type: [[[Number]]],
      required: true
    }
  });

var districtSchema = new Schema({

    jurisdiction: {type: String, enum:['state', 'us']},
    chamber: {type: String, enum: ['house', 'senate']},

    districtNumber: {type: Number},

    contact: {
        "firstName": String,
        "lastName": String,
        "party": String,
        "url": String,
    },

    geometry: {type: polygonSchema, index: '2dsphere'},
    
    deleted: {type: Boolean, default: false}
});

一个示例条目看起来像这样(多边形包含大量点,所以我截断了它)

{
  "_id": "602dc87f9f8326e4e5825b70",
  "chamber": "house",
  "districtNumber": 5,
  "jurisdiction": "us",
  "__v": 0,
  "contact": {
    "firstName": "Mary Gay",
    "lastName": "Scanlon",
    "party": "D",
    "url": "https://scanlon.house.gov"
  },
  "geometry": {
    "coordinates": [
      [
        [
          -75.59557557627258,
          39.83744617888617
        ],
        [
          -75.5965303118867,
          39.8380378976056
        ],
        [
          -75.59689506526837,
          39.8383438227424
        ],
        [
          -75.59720859368865,
          39.83857630244814
        ],
        [
          -75.59754671724104,
          39.83897169444833
        ],
        [
          -75.59780380953957,
          39.83936864305231
        ]
      ]
    ],
    "type": "Polygon"
  }
}

为了测试数据,我试图找到包含特定坐标对的地区。但是,当我知道这是一组有效的坐标时,我没有得到任何结果。

这是我正在使用 mongoose 运行的测试查询;当它应该返回上面采样的地区时,它没有找到匹配项并且没有返回错误:

models.district.findOne(
{"geometry": {
  "$geoIntersects": { "$geometry": { "type": "Point", "coordinates": [39.948164,-75.339984] }
  }
}
}).exec((err, result) => console.log("Coordinate test", err,result));

有趣的是,我还使用了 mongoDB 在their geospatial tutorial 中使用的演示数据库,它们可以处理查询。在结构上,坐标似乎位于相同的位置;我能发现的唯一区别是我的多边形有很多点。

【问题讨论】:

    标签: mongodb geospatial geojson


    【解决方案1】:

    这是 RTFM 与疲劳引起的监督相混合的案例。当我写好我的测试坐标时,我在查询中手写了它们,并且没有注意到 geoJSON 版本中的顺序,我假设顺序是纬度、经度。原来spec has them inverted.

    当我反转我的测试坐标时,查询会按照我的预期工作。

    【讨论】:

      猜你喜欢
      • 2017-03-12
      • 1970-01-01
      • 2015-10-13
      • 2014-07-18
      • 2013-02-04
      • 2016-09-14
      • 2016-12-08
      • 2013-12-08
      • 1970-01-01
      相关资源
      最近更新 更多