-
-
Save AlexArcPy/9eb33a8ba238b9ec08baf3b8c21219c4 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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