public
Last active

Merge various file-based geodata and output as geojson

  • Download Gist
merge.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#!/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 ]
}'''

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.