-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean-spiky-hulls.py
executable file
·56 lines (44 loc) · 1.77 KB
/
clean-spiky-hulls.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python
'''
Orphan objects in spiky communities
whose concave hull area is much less than convex one.
'''
import argparse
import logging
import geojson
from shapely.geometry import asShape
log = logging.getLogger(__name__)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'input', metavar='communities.hulls.json',
help='GeoJSON input concave hulls')
parser.add_argument(
'output', metavar='communities.cleaned.hulls.json',
help='Cleaned output concave hulls')
parser.add_argument('--convexity', type=float, metavar='x', default=0.5,
help='Minimum on the ratio of '
'the concave/convex area. Default %(default)f')
args = parser.parse_args()
log.info("Writing output to %s", args.output)
with open(args.input, 'r') as inputfd:
features = geojson.load(inputfd)
output_features = []
for feature in features['features']:
if feature['geometry'] is None:
log.error("Geometry is null! Skipping: %s", repr(feature))
continue
shape = asShape(feature['geometry'])
concave_area = shape.area
convex_area = shape.convex_hull.area
if concave_area < convex_area * args.convexity:
log.info("Cluster %i is to concave (%g/%g = %g < %0.2f)",
feature['properties']['clust'],
concave_area, convex_area,
concave_area / convex_area, args.convexity)
else:
output_features.append(feature)
with open(args.output, 'w') as outputfd:
log.info("Writing output to %s", args.output)
geojson.dump(geojson.FeatureCollection(output_features),
outputfd, indent=2)