Skip to content

Instantly share code, notes, and snippets.

@tasseff
Last active March 10, 2017 18:23
Show Gist options
  • Select an option

  • Save tasseff/fbc5ffdd63b922535ab9bc0a45cb2668 to your computer and use it in GitHub Desktop.

Select an option

Save tasseff/fbc5ffdd63b922535ab9bc0a45cb2668 to your computer and use it in GitHub Desktop.
Create a GeoJSON file with circular polygons.
#!/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