A simple tutorial on the use of the python-geojson library.

The requirement: to convert a bunch of point, line and polygon coordinates into GeoJSON. The coordinate values exist as arrays stored as a string in a CSV file. I have included an additional step of adding the GeoJSON objects to MongoDB.

The library is easy to use and perfect for building GeoJSON output. Here are the steps I took.

 

Step1 - get your CSV content in shape
Storing the coordinate value pairs in-line with the format expected by the python-geojson library saves a lot of hassle. In many cases you won't have the luxury of dictating the source format, so some massaging will most likely be required to get it ready for use. You could also handle this on the Python side. For polygon coordinates, remember the right hand rule. I am not going to cover prepping the coordinate values, but essentially they need to be in the following format for use in the python-geojson functions:

Point: (-115.81, 37.24)
LineString: [(8.919, 44.4074), (8.923, 44.4075)]
Polygon: [[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38, 57.322)]]

I stored them in this format in the CSV bearing in mind that they are String objects and will need to be converted at some point.

There are other GeoJSON objects that I have not covered like MultiLineString and MultiPolygon, if you are interested in these, take a look at the documentation to learn more.

You can include as many fields as you wish in the CSV file, I will cover how to include these as properties in the GeoJSON output.

 

Step2 - loop through the rows in your CSV file
Create a Python script to open the CSV file and print out the values of the first (id) and fourth (coordinates) columns, remembering that we are working with a zero based array.

import csv

with open('pointdemo.csv', 'r') as f:
    next(f) #ignore headers line - start on second line
    reader = csv.reader(f)
    for row in reader:
        print("id: " + row[0] + " - point: " + row[3])

 

Step3 - use the python-geojson library to convert the CSV content to GeoJSON
Extend the script above to include the python-geojson library.

import csv
import ast
from geojson import Point, Feature, dump, FeatureCollection

pointfeatures = []
with open('pointdemo.csv', 'r') as f:
    next(f) #ignore headers line - start on second line
    reader = csv.reader(f)
    for row in reader:
        coords = ast.literal_eval(row[3])
        point = Point(coords)      
        pointjson = Feature(name="cv"+row[0], geometry=point, style={"color": "#ff46b5"}, properties={"direction":row[1], "status":row[2]})
        pointfeatures.append(pointjson)

feature_collection = FeatureCollection(pointfeatures)

# populate file to confirm content 
with open('point.geojson', 'w') as f:
   dump(feature_collection, f)

Note the ast library, we will use this to convert the coordinate String object into a tuple as expected by the python-geojson library functions.

Help on function literal_eval in module ast:

literal_eval(node_or_string)Safely evaluate an expression node or a string
containing a Python expression.  The string or node provided may only consist of the
following Python literal structures: strings, bytes, numbers, tuples, lists,
dicts,sets, booleans, and None.
(END)

Import Point, Feature, dump and FeatureCollection modules from the python-geojson library.

Create a variable (coords) and assign it to the result of the conversion from String to Tuple ready for use by the python-geojson library.

Create a variable (point) and assign it to the result of the python-geojson Point function - the Point GeoJSON object.

At this stage we have a Point GeoJSON object with no properties. To add properties, we need to add the Point GeoJSON object to a Feature, we assign this to a variable named pointjson. Notice how the python-geojson Feature function makes it really easy to add property, name and style content to the GeoJSON object.

We now have a Feature GeoJSON object, but this object is replaced each time the loop moves to the next row of the CSV so we need a way to store these objects and add them to a FeatureCollection GeoJSON object. To do this we use a List (pointfeatures) - we then append each pointjson feature to the pointfeatures List. Finally, we assign a variable called feature_collection the value of FeatureCollection with the argument of the pointfeatures List

Lastly we can create a geojson output file and populate it with our feature_collection GeoJSON object. GeoJSONLint is a great place to test you GeoJSON output.

 

Step4 - export GeoJSON features to MongoDB
Yout can easily add GeoJSON objects directly to MongoDB using the pymongo library. You can then either export each Feature or a single FeatureCollection GeoJSON object.

import csv
import ast
import pymongo
from geojson import Point, Feature, dump, FeatureCollection

myclient = pymongo.MongoClient("mongodb://<user>:<password>@ds152000.mlab.com:52000/<client>")
mydb = myclient["<client>"]
mycol = mydb["<database>"]

pointfeatures = []
with open('pointdemo.csv', 'r') as f:
    next(f) #ignore headers line - start on second line
    reader = csv.reader(f)
    for row in reader:
        coords = ast.literal_eval(row[3])
        point = Point(coords)      
        pointjson = Feature(name="cv"+row[0], geometry=point, style={"color": "#ff46b5"}, properties={"direction":row[1], "status":row[2]})
        # export single Feature to MongoDB
        # x = mycol.insert_one(pointjson)
        pointfeatures.append(pointjson)

feature_collection = FeatureCollection(pointfeatures)
# export FeatureCollection to MongoDB
x = mycol.insert_one(feature_collection)

Remember to replace <user>, <password>, <client> and <database> with the correct values for your MongoDB connection
 

Script examples and CSV files for this tutorial can be found on GitHub.