Last active
March 10, 2017 18:23
-
-
Save tasseff/fbc5ffdd63b922535ab9bc0a45cb2668 to your computer and use it in GitHub Desktop.
Create a GeoJSON file with circular polygons.
This file contains hidden or 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
| #!/usr/bin/env python | |
| import argparse | |
| import geojson | |
| import shapely.geometry.point | |
| def write_circle_geojson(centers, radii, output_path): | |
| centers = zip(centers[0::2], centers[1::2]) | |
| feature_array = [] | |
| for i, center in enumerate(centers): | |
| p = shapely.geometry.point.Point(center[0], center[1]) | |
| circle = p.buffer(radii[i]) | |
| properties = {'name': 'circle' + str(i)} | |
| feature = geojson.Feature(geometry = circle, properties = properties) | |
| feature_array.append(feature) | |
| with open(output_path, 'w') as outfile: | |
| geojson.dump(geojson.FeatureCollection(feature_array), outfile) | |
| if __name__ == "__main__": | |
| description = 'Create a GeoJSON file with circular polygons.' | |
| parser = argparse.ArgumentParser(description = description) | |
| parser.add_argument('--centers', '-c', type = float, nargs = '+', | |
| metavar = 'centers', | |
| help = '(x, y) pairs defining circle centers') | |
| parser.add_argument('--radii', '-r', type = float, nargs = '+', | |
| metavar = 'radii', | |
| help = 'floats defining circle radii') | |
| parser.add_argument('--output_path', '-o', type = str, nargs = 1, | |
| metavar = 'outputPath', | |
| help = 'path to output JSON file') | |
| args = parser.parse_args() | |
| centers = args.centers | |
| radii = args.radii | |
| output_path = args.output_path[0] | |
| write_circle_geojson(centers, radii, output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment