Skip to content

Instantly share code, notes, and snippets.

@ischneider
Last active September 18, 2023 20:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ischneider/2606d53ac2e6374dc47c to your computer and use it in GitHub Desktop.
Save ischneider/2606d53ac2e6374dc47c to your computer and use it in GitHub Desktop.
multiprocessing example using fiona and shapely
from multiprocessing import Pool
import sys
import fiona
from fiona.transform import transform_geom
from shapely.geometry import mapping, shape
import json
def reproject(f, srs_crs, dest_crs):
f['geometry'] = transform_geom(srs_crs, dest_crs, f['geometry'],
antimeridian_cutting=True,
precision=-1)
return f
def buffer(f, v):
f['geometry'] = mapping(shape(f['geometry']).buffer(v))
return f
class Process:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
def __call__(self, f):
return reproject(buffer(reproject(f, self.crs, 'EPSG:3857'), 1E+5), 'EPSG:3857', 'EPSG:4326')
if __name__ == '__main__':
pool = Pool()
with fiona.open(sys.argv[1], 'r') as source:
p = Process(crs=source.crs)
res = pool.map(p, source)
print json.dumps({
'type': 'FeatureCollection',
'features': res
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment