Creating a Shapefile From a MongoDB Using Python

14 Nov

I am always looking for ways to store data and create shapefiles, or other filetypes I need, from it. Just picked up a book on MongoDB last night and have thrown together a quick example of how to enter some point data using Long and Lat and then retrieving the data and sending it to This is the first example, if I get around to it, the next example will query for 10 points near another then spit that out to a shapefile. But I need to walk before I run, so here are my first steps.

  1. Install Mongo
  2. Install pymongo
  3. Install Check out this blog for more info – this guy is awesome, it is where I got the code to write the projection: GeoSpatial Python.
  4. Run mongod
  5. You can use mongo, a full JavaScript Shell, to enter data, but I used Python.
  6. Enter some data. I entered two points (35,-106) and (35.8,-106.8)
  7. write a python program to convert this data to a shapefile.

Here is the python program to retrieve these two points, write a shapefile and a PRJ:

from pymongo import Connection,GEO2D
import shapefile


for x in db.places.find():

prj = open(“Web.prj”, “w”)
epsg = ‘GEOGCS[“WGS 84”,’
epsg += ‘DATUM[“WGS_1984”,’
epsg += ‘SPHEROID[“WGS 84”,6378137,298.257223563]]’
epsg += ‘,PRIMEM[“Greenwich”,0],’
epsg += ‘UNIT[“degree”,0.0174532925199433]]’


Now you should have a shapefile with 2 points in WGS84


One Response to “Creating a Shapefile From a MongoDB Using Python”


  1. MongoDB with a slice of CherryPy: Running a Query and Displaying Results « Architecture and Planning - November 15, 2012

    […] CherryPy. Recently, I have been playing with MongoDB and pymongo. In my last post I showed how to create a shapefile from a mongoDB. In this post, I will show how to connect to mongoDB through Python using CherryPy and spitting out […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: