如何将Geojson文件导入MongoDB



由于Geojson是实际的json,我认为我可以使用mongoimport将数据从.geojson文件加载到MongoDB数据库中。

但是我收到以下错误:

exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0

该文件是25MB,这是它的片段:

{
"type": "FeatureCollection",
"features": [
{
    "type": "Feature",
    "id": "node/2661561690",
    "properties": {
        "timestamp": "2014-02-08T17:58:24Z",
        "version": "1",
        "changeset": "20451306",
        "user": "Schandlers",
        "uid": "51690",
        "natural": "tree",
        "id": "node/2661561690"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [
            -66.9162255,
            10.5056439
        ]
    }
},
// ... Omitted data
{
    "type": "Feature",
    "id": "node/2664472516",
    "properties": {
        "timestamp": "2014-02-10T04:27:30Z",
        "version": "2",
        "changeset": "20477473",
        "user": "albertoq",
        "uid": "527105",
        "name": "Distribuidora Brithijos (Aceites)",
        "shop": "car_parts",
        "id": "node/2664472516"
    },
    "geometry": {
        "type": "Point",
        "coordinates": [
            -66.9388903,
            10.4833647
        ]
    }
}
]
}

下载jq(它是类似sed的程序,但用于JSON)

然后运行:

jq --compact-output ".features" input.geojson > output.geojson

然后

mongoimport --db dbname -c collectionname --file "output.geojson" --jsonArray

现在你有一系列的功能。MongoDB会认为这是一个文档。尝试从 geojson 的开头删除以下内容:

{
"type": "FeatureCollection",
"features": [

此外,从 geojson 的末尾删除以下内容:

]
}

编辑 - 此外,mongo 希望每行一个文档。因此,请确保您的唯一 在文档之间!例如

...    
},n
    {
        "type": "Feature",
        "id": "node/2664472516",
...

ParoX 的想法效果很好,但有 16MB 的限制。

蒙戈德文档

-

-jsonArray 接受在单个 JSON 数组中导入用多个 MongoDB 文档表示的数据。仅限于导入 16 MB 或 较小。

如果文件大小大于16MB,您可以这样做

jq --compact-output ".features[]" input.geojson> output.geojson

这将为一个对象提供一行,末尾没有逗号。

{.....}
{.......}
{...}
{"type":"Feature","geometry":{"type":"Point","coordinates":[-80.87088507656375,35.21515162500578]},"properties":{"name":"ABBOTT NEIGHBORHOOD PARK","address":"1300  SPRUCE ST"}}
{"type":"Feature","geometry":{"type":"Point","coordinates":[-80.83775386582222,35.24980190252168]},"properties":{"name":"DOUBLE OAKS CENTER","address":"1326 WOODWARD AV"}}
{"type":"Feature","geometry":{"type":"Point","coordinates":[-80.83827000459532,35.25674709224663]},"properties":{"name":"DOUBLE OAKS NEIGHBORHOOD PARK","address":"2605  DOUBLE OAKS RD"}}
{"type":"Feature","geometry":{"type":"Point","coordinates":[-80.83697759172735,35.25751734669229]},"properties":{"name":"DOUBLE OAKS POOL","address":"1200 NEWLAND RD"}}
{"type":"Feature","geometry":{"type":"Point","coordinates":[-80.81647652154736,35.40148708491418]},"properties":{"name":"DAVID B. WAYMER FLYING REGIONAL PARK","address":"15401 HOLBROOKS RD"}}
{"type":"Feature","geometry":{"type":"Point","coordinates":[-80.83556459443902,35.39917224760999]},"properties":{"name":"DAVID B. WAYMER COMMUNITY PARK","address":"302 HOLBROOKS RD"}}
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-80.72487831115721,35.26545403190955],[-80.72135925292969,35.26727607954368],[-80.71517944335938,35.26769654625573],[-80.7125186920166,35.27035945142482],[-80.70857048034668,35.268257165144064],[-80.70479393005371,35.268397319259996],[-80.70324897766113,35.26503355355979],[-80.71088790893555,35.2553619492954],[-80.71681022644043,35.2553619492954],[-80.7150936126709,35.26054831539319],[-80.71869850158691,35.26026797976481],[-80.72032928466797,35.26061839914875],[-80.72264671325684,35.26033806376283],[-80.72487831115721,35.26545403190955]]]},"properties":{"name":"Plaza Road Park"}}

mongoimport --db dbname -c collectionname --file "output.geojson" --jsonArray

这个Python脚本旨在通过一个步骤将GeoJSON文件导入MongoDB:https://github.com/rtbigdata/geojson-mongo-import.py

首先,为了验证你的GeoJSON文件是否准确,你可以使用Geojsonlint,QGIS等。

之后,要将您的数据导入您的集合,请使用 Mongoimport:

mongoimport --db MY_DATABASE_NAME -c MY_COLLECTION_NAME --type json --file "MY_GEOJSON_FILENAME"

将上面的 3 个变量替换为您的有效名称。显然,请确保您的当前目录包含该文件。

如果问题是您的文档集大小优于 16Mb,您可以使用 batchSize 选项,该选项设置批处理中的文档数量。例如:

mongoimport -d mydb -c mycol data.json -j 4 --batchSize=100

请注意 -j 选项,该选项有助于通过使用多个工作器来增加数据库的输出。

奇怪的是,batchSize选项没有使用"mongoimport"的"--help"选项进行记录,去图!

最新更新