GeoJSON 在 Node.js 中使用 Mongoose/Mongodb

GeoJSON working with Mongoose/Mongodb in Node.js

我正在使用 Node.js、Express、Mongo、Mongoose 和 Mapbox api.[=15= 使用 MEEN 堆栈构建地图网络应用程序]

到目前为止,一切都令人兴奋。我要显示的数据是来自具有关联属性的 shapefile 的多边形。

到目前为止,我已经设法将数据加载到 Mongodb 中,并使用模式配置了 Mongoose 模型。在节点应用程序中,我可以访问数据但不能将其加载到地图中 api.

我收到错误 "Uncaught Error: Invalid GeoJSON object."

在 devtools 控制台中检查对象,我可以看到几何记录是空白的。我认为这是由于模式配置错误造成的。

下面是来自 Mongodb 命令行的一条记录。出于隐私目的,我删除了几个元素。看起来符合预期:

    {
    "_id" : ObjectId("56c77259336aac299181a207"),
    "type" : "Feature",
    "properties" : {
        "TNRTPCD" : "P",
        "TNRSBTPCD" : "C",
        "PRCNTOWNER" : 100,
        "NTRTMSTMP" : "20140908161622",
        "TNRSBTPDSC" : "CLAIM",
        "TRMNTNTPDS" : null,
        "TAG_NUMBER" : null,
        "OBJECTID" : 40495,
        "NTRSRD" : "MTA_ONLINE",
        "NUM_OWNERS" : 1,
        "PROTECTED" : "N",
        "PDTSRD" : "MTA_ONLINE",
        "TNRTPDSCRP" : "Placer",
        "TNRNMBRD" : 1030842,
        "RVSNNMBR" : 0,
        "FCODE" : null,
        "TRMNTNDT" : null,
        "RNHCTRS" : 20.3496,
        "TTLTPCD" : "PCX"
    },
    "geometry" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    -121.88883540217063,
                    50.97489195799901,
                    0
                ],
                [
                    -121.88883523174192,
                    50.97072525131302,
                    0
                ],
                [
                    -121.8950854466247,
                    50.97072527980969,
                    0
                ],
                [
                    -121.89508560169767,
                    50.97489198254216,
                    0
                ],
                [
                    -121.88883540217063,
                    50.97489195799901,
                    0
                ]
            ]
        ]
    }
}

这是我的模型文件中的代码:

    //Claim Model

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// Schema Defined Here
var claimSchema = new Schema({
  gid: Number,
  tnrtpcd: String,
  ttltpdsc: String,
  tnrsbtpcd: String,
  prcntowner: Number,
  ntrtmstmp: String,
  tnrsbtpdsc: String,
  owner_name: String,
  trmntntpds: String,
  tag_number: String,
  objectid: Number,
  gdtdt: String,
  ntrsrd: String,  
  claim_name: String,
  num_owners: Number,
  clientnum: Number,
  issue_date: String,
  protected: String,
  pdtsrd: String,
  pdttmstmp: String,
  tnrtpdscrp: String,
  tnrnmbrd: Number,
  rvsnnmbr: Number,
  fcode: String,
  trmntndt: String,
  rnhctrs: Number,
  ttltpcd: String,
  geometry: { type: [String], index: '2dsphere'}
}, { collection : 'docs' });

mongoose.model('Claim', claimSchema);

路由似乎在 app.js 中工作,因为我能够访问数据。这是我的视图文件中代码的相关部分:

script.
L.mapbox.accessToken = 'pk.[ommited]';

//- var geojson = mongoose.model('Claim').find({'properties.CLIENTNUM': 278107});
 var geojson = $.getJSON('/claims/nick',function(result){
        return result;
    });
L.mapbox.map('map', 'mapbox.outdoors')
  .featureLayer.setGeoJSON(geojson);

所以这里的大问题是如何正确定义几何数据的模式?假设这是我的问题。

看起来更容易混淆 "type" 作为字段名称和 "type" 作为 mongoose 模式的声明。

为了灵活地允许几种 GeoJSON 类型,您基本上希望 "geometry" 字段为:

"geometry": {
    "type": { "type": String },
    "coodinates": []
}

如果需要,您可以为 "Point"、"Polygon" 等添加 enum,但通常松散的 "coordinates" 作为数组 [] 匹配中的规则那些案例。

稍微搜索一下就会发现人们已经为此创建了 "types"。

好的,现在解决了。感谢 Blakes Seven 的建议。

我按照 Blakes Seven 的建议更改了架构。我还必须更改 mapbox 调用,以便它从 getJSON 回调内部调用。看起来像这样:

var geojson = $.getJSON('/claims/placer',function(result){
        L.mapbox.map('map', 'mapbox.outdoors')
        .setView([49.72902, -121.43253], 12)
        .featureLayer.setGeoJSON(result);

    });

不可以在地图上显示查询到的数据!