Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created September 21, 2012 00:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springmeyer/3759113 to your computer and use it in GitHub Desktop.
Save springmeyer/3759113 to your computer and use it in GitHub Desktop.
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 = {
'txt':'csv',
'csv':'csv',
'tsv':'csv',
'osm':'osm',
'shp':'shape',
'db':'sqlite',
'sqlite3':'sqlite',
'sqlite':'sqlite',
'kml':'ogr',
'gml':'ogr',
'gpx':'ogr',
'geojson':'ogr',
'json':'ogr'
}
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)
sys.exit(1)
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)
sys.exit(1)
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))
sys.exit(1)
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():
query.add_property_name(fld)
fs = ds.features(query)
feat = fs.next()
while feat:
try:
print ' ' + feat.to_geojson(),
feat = fs.next()
print ','
except StopIteration:
feat = False
print '''\n ]
}'''
@springmeyer
Copy link
Author

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