forked from ungarj/label_centerlines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_features.py
executable file
·73 lines (47 loc) · 1.77 KB
/
merge_features.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
import os
import fiona
from shapely.geometry import shape, mapping, box, Point
from shapely.ops import cascaded_union
from pdb import set_trace
STARTDIR = "/home/rbraswell/repo/label_centerlines/test"
INFILE = "roads_mixed_gtet050.shp"
OUTFILE = "roads_mixed_gtet050_merged.shp"
CHECK_PT = Point((286787.855, 107588.807))
if __name__ == "__main__":
features = fiona.open(os.path.join(STARTDIR, INFILE))
schema = features.schema
crs = features.crs
driver = features.driver
#out_schema = features.schema.copy()
geoms = []
for feature in features:
fid = feature['id']
geom = shape(feature['geometry'])
assert geom.is_valid, "geom not valid"
assert geom.geometryType() == "Polygon", "geom not polygon"
assert geom.contains(CHECK_PT) is False, "oops"
#geom = geom.buffer(1.e-9)
geom = geom.buffer(0.1)
geom = geom.buffer(-0.1)
area = geom.area
length = geom.length
n_inner = len(geom.interiors)
bbox = box(*geom.bounds)
bbarea = bbox.area
print int(fid), n_inner, area, length, length/area, bbarea
geoms.append(geom)
geom_all = cascaded_union(geoms)
assert geom_all.contains(CHECK_PT) is False, "crap"
#set_trace()
out_schema = {'geometry': 'MultiPolygon', 'properties': {'id': 'int'}}
outfile = os.path.join(STARTDIR, OUTFILE)
out_features = fiona.open(outfile, 'w', schema=out_schema,
crs=crs, driver=driver)
out_feature = {}
out_feature['geometry'] = mapping(geom_all)
out_feature['type'] = 'Feature'
out_feature['id'] = '0'
out_feature['properties'] = {'id': 0}
out_features.write(out_feature)
out_features.close()