Friday, August 7, 2015

import ESRI shape file into mongoDB

import ESRI shape file into mongoDB


1) Go to http://www.ogr2gui.ca/ download GDAL ogr GUI tools

2) use tool to convert shape to geojson ( geoJSON use latitude and longitude, project code is
      4326: WGS84)

3) mongoimport will see it as single document, however I need multi document for each record, I need to modify the geojson file, first remove head portion (selected text), then remove foot portion( selected text)




   
4) remove , at each line end. each line should be a json object, it looks like {......} without ","
    So you should replace ] ] ] } },  with ] ] ] } } as show below


5) run mongoimport as
 
C:\Program Files\MongoDB\Server\3.0\bin>mongoimport --db civilgis --collection p
arks C:\Users\hu_j\Downloads\parks.geojson

--db : database name
--collection : table name
then the geojson file

this case it import 41 document


6) index the coordinate to speed up the performance
db.zoning.createIndex({"geometry":"2dsphere"})
db.parks.ensureIndex({"geometry":"2dsphere"})
db.collection.createIndex( { <location field> : "2dsphere" } )




Note: keep the format of "coordinate [[[ ]]]"  do not change to coordinate [[ ]],
otherwise, db.xxx.ensureIndex({"geometry": "2dsphere"}) will fail.


Error and fix

mongoimport error tells you location at line number #14683, in this case you need to replace
"geometry": null }, without ","




Error 2 and fix


  db.parcels.ensureIndex({geometry:"2dsphere"})

     
       "errmsg" : "exception: Can't extract geo keys:.......
          .....
            ] ] Duplicate vertices: 29 and 47",

fix by remove the duplicate coordinate(vertice). start from 0, the 29th point and 47th point are duplicate, remove 47th, note that last coordinate before ]]duplicate vertices is NOT the 29th nor 47th, you have to manually count 29th point and delete it.

first find by APNLINK: \"41051111\" copy that record to new document, then find the duplicate coordinate by count 29th

        "errmsg" : "exception: Can't extract geo keys: { _id: ObjectId('55ca7991
21167708171b06ba'), type: \"Feature\", properties: { CITYSTZPRP: \"Ca\", OWNER_N
AME: \"Casden Lakes\", Legal_Desc: \"N TR 12341 LOT A\", HOUSE_NO: null, APN: \"
410-511-11\", APNLINK: \"41051111\", ADDR_NUMBE: null, STREET: null, SUFFIX: nul
l, OBJECTID: 981, FID_points: 41051111, ADDRESS: null }, geometry: { coordinates
: [ [ [ -117.8772983067213, 33.69316895497129 ], [ -117.8771896001406, 33.693156


after fix the whole parcels.geojson, you have to drop current parcels in mongoDB, re-import parcels.geojson then create index

================================================================
http://zaiste.net/2012/08/importing_json_into_mongodb/


Importing JSON data into MongoDB can be tricky. By default, monogoimport assumes a special structure for a file to import from: similar to JSON format except that only one document per line is allowed with no comma after each of them - something like:
{ name: "Widget 1", desc: "This is Widget 1" }
{ name: "Widget 2", desc: "This is Widget 2" }
It would be easier, however, to use a traditional JSON instead of adapting it to this special format required by MongoDB. Luckily, we can force mongoimport to import the data as a JSON array using --jsonArray parameter.
Let's suppose we have a seed.json file with the following content:
[
    { name: "Widget 1", desc: "This is Widget 1" },
    { name: "Widget 2", desc: "This is Widget 2" }
]
We can import it to our local MongoDB database using following command:
λ mongoimport --db  --collection  --type json --file seed.json
--jsonArray
If the specified collection doesn't exist, it will be automatically created; otherwise new documents will be appended to the existing one.

No comments: