~qeef/damn-client.py

ref: 008f97255d638613faebc379be8b52f6531aa2e5 damn-client.py/convex_hull.py -rwxr-xr-x 877 bytes
008f9725Jiri Vlasak Update requirements 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
"""This script creates convex hull of all the features geometries.

Example usage:

    ./convex_hull.py buildings.geojson
"""
import json
import sys
from shapely.geometry import mapping
from shapely.geometry import shape
from shapely.ops import unary_union

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(__doc__)
        sys.exit(1)
    fin = None
    with open(sys.argv[1]) as f:
        fin = json.load(f)
    assert(fin != None)
    shapes = []
    for f in fin["features"]:
        shapes.append(shape(f["geometry"]))
    shapes = unary_union(shapes)
    shapes = shapes.convex_hull
    print(json.dumps({
        "type": "FeatureCollection",
        "generator": "damn-client.py",
        "features": [{
            "type": "Feature",
            "properties": {},
            "geometry": mapping(shapes),
        }],
    }))