Tag Archives: geospatial

Loading Shapefiles in to MongoDB

I’ve been playing a bit recently with a small geospatial/location based app. After haggling with a bunch of tools and MongoDB a bunch, here are a few tips on importing a set of ESRI Shapefiles in to a MongoDB. I’m looking at SF Street Sweeping data but you can use any Shapefiles you wish.

Get the shapefiles

For example, grab the SF Street Sweeping data, and download and unzip those.

Convert Shapefiles to WGS84 Projection

The SF shapefiles are in Northern California specific projection (2227). Lat/long coordinates are in WGS84 projection (4326). Download the GDAL tools to get access to the ogr2ogr tool to directly convert them. Or use QGIS to load the shapefile as a vector, then export it out using the WGS84 projection.

Convert Shapefile to GeoJSON

ogr2ogr -f geoJSON sweeping.json sfsweeproutes_in_wgs84.shp

Clean up the resulting GeoJSON

MongoImport doesn’t like the ogr2ogr generated GeoJSON. Remove the first two lines:

{
"type": "FeatureCollection",

and the last line:

}

and save that to `sweeping_clean.json`

Import the data to Mongo

mongoimport --db sfstreets --collection streets < sweeping_clean.json

Create a 2dsphere spatial index

Mongo needs an index to query on geospatial data. To create it from the mongo command line:


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

Where streets is your collection name and `geometry` is the object in your document that contains the GeoJSON location data.

That’s it!
You now have in the sfstreets database a streets streets collection. I’ll follow up in the next post on how to query this data.<