Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Merge various file-based geodata and output as geojson
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import optparse
import mapnik
import sys
import os
parser = optparse.OptionParser(usage="""%prog datasource files (like .geojson, .shp, .csv, .sqlite, .osm, .kml)
Merge various geodata and output as geojson
$ %prog -h (or --help for possible options)
$ %prog one.shp two.json | python -mjson.tool > merged.geojson
""", version='%prog ')
mapping = {
def detect_mapnik_type(filename):
ext = os.path.splitext(filename)
if not ext[1]:
sys.stderr.write('Could not detect file extension for %s\n' % filename)
ext = ext[1][1:]
if not mapping.has_key(ext):
sys.stderr.write('No known mapping from this type of file "%s" to a known Mapnik datasource\n' % ext)
ds_type = mapping[ext]
if ds_type not in mapnik.DatasourceCache.plugin_names():
sys.stderr.write('Whoops, looks like you a missing a great Mapnik datasource plugin (%s) to be able to read %s\n' % (ds_type,filename))
params = {'type':ds_type}
if ds_type == 'ogr':
params['layer_by_index'] = 0
params['file'] = filename
return params
if __name__ == "__main__":
(options, args) = parser.parse_args()
if not len(args) > 0:
parser.error("Please provide the path to a one or more datasource files")
print '''{
"type": "FeatureCollection",
"features": ['''
for idx, filename in enumerate(args):
if idx > 0:
print ','
ds = mapnik.Datasource(**detect_mapnik_type(filename))
e = ds.envelope();
query = mapnik.Query(e)
for fld in ds.fields():
fs = ds.features(query)
feat =
while feat:
print ' ' + feat.to_geojson(),
feat =
print ','
except StopIteration:
feat = False
print '''\n ]

This comment has been minimized.

Show comment Hide comment

springmeyer Oct 14, 2012


springmeyer commented Oct 14, 2012

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