Skip to content

Instantly share code, notes, and snippets.

@anhphamt
Last active September 6, 2017 13:12
Show Gist options
  • Save anhphamt/8e7704081053f180b2b6f22d465fb799 to your computer and use it in GitHub Desktop.
Save anhphamt/8e7704081053f180b2b6f22d465fb799 to your computer and use it in GitHub Desktop.
Import SHP to MongoDB on MAC

Try this link

http://isticktoit.net/?p=1444

Install GDAL and ogr2ogr tool

brew install gdal

After installing the tool, convert the SHP file to geojson first by this command

ogr2ogr -f geoJSON adrtirol.json adr_epsg4326.shp

After cleaning GEOJSON file (remove , at the end line ...)

Import file JSON to MongoDB

mongoimport --drop --host 127.0.0.1:27017 --db geodatatest --collection adrtirol < /var/mongodata/adrtirol.json

Fix geojson rewind issue

Install this tool

npm install -g geojson-rewind
geojson-rewind foo.geojson

This tool is only work with small GEOJSON file.

Try to re-create GEO DATA and ignore errors for indexing 2dsphere

db.plan_overlays.renameCollection('plan_overlays_bk')

db.plan_overlays.insert(db.plan_overlays_bk.findOne()) 
db.plan_overlays_error.insert(db.plan_overlays_bk.findOne()) 

db.plan_overlays.createIndex({geometry:"2dsphere"}) 

db.plan_overlays.remove({})
db.plan_overlays_error.remove({})

db.plan_overlays_bk.find().forEach(function(x){
    try {
      db.plan_overlays.insert(x);
    } catch(err) {
      db.plan_overlays_error.insert(x);
    }
})

Find closest Geometry

db.plan_zone.find({"geometry":{$near:{$geometry: {type:"Point", coordinates:[144.9605271 , -37.8186165]}, $maxDistance:500}}})

Split large JSON file into smaller files

jq -cM --argjson sublen '50' 'range(0; length; $sublen) as $i | .[$i:$i+$sublen]'
plan_ugb.json | gsplit -l 1 -da 3 - meta2_ --additional-suffix=.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment