Skip to content

Instantly share code, notes, and snippets.

@jwass
Created August 10, 2013 21:13
Show Gist options
  • Save jwass/6202165 to your computer and use it in GitHub Desktop.
Save jwass/6202165 to your computer and use it in GitHub Desktop.
Simple script used to convert some shapefiles to GeoJSON, leaving out a few undesired properties. Requires shapely 1.2.18.
import functools
import fiona
import geojson
import pyproj
import shapely.geometry
import shapely.ops
omit = ['SHAPE_AREA', 'SHAPE_LEN']
def convert(f_in, f_out):
with fiona.open(f_in) as source:
# Use the recipe from the Shapely documentation:
# http://toblerity.org/shapely/manual.html
project = functools.partial(pyproj.transform,
pyproj.Proj(**source.crs),
pyproj.Proj(init='epsg:4326'))
features = []
for f in source:
shape = shapely.geometry.shape(f['geometry'])
projected_shape = shapely.ops.transform(project, shape)
# Remove the properties we don't want
props = f['properties'] # props is a reference
for k in omit:
if k in props:
del props[k]
feature = geojson.Feature(id=f['id'],
geometry=projected_shape,
properties=props)
features.append(feature)
fc = geojson.FeatureCollection(features)
with open(f_out, 'w') as f:
f.write(geojson.dumps(fc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment