-
Notifications
You must be signed in to change notification settings - Fork 2
/
aggregate_shapes.py
73 lines (47 loc) · 1.69 KB
/
aggregate_shapes.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
import os
import glob
import json
import fiona
project_dir = "/sciclone/aiddata10/REU/projects/afghanistan_gie"
area_path_list = glob.glob(
"{0}/mapping/mapping_uploads/*/*_poly*.shp".format(project_dir))
canal_path_list = glob.glob(
"{0}/mapping/mapping_uploads/*/*_line*.shp".format(project_dir))
# make sure all projects have area and canal
assert len(area_path_list) == len(canal_path_list) == 87
# -----------------------------------------------------------------------------
# combine all lines (no project information needed)
all_canal_features = []
for path in canal_path_list:
project_id = os.path.basename(path)[:4]
with fiona.open(path, 'r') as feat_src:
for feature in feat_src:
feature['properties']['project_id'] = project_id
del feature['properties']['id']
all_canal_features.append(feature)
canal_geo = {
"type": "FeatureCollection",
"features": all_canal_features
}
canal_path = os.path.expanduser(
"~/git/afghanistan_gie/canal_data/canal_lines.geojson")
canal_file = open(canal_path, "w")
json.dump(canal_geo, canal_file)
canal_file.close()
all_area_features = []
for path in area_path_list:
project_id = os.path.basename(path)[:4]
with fiona.open(path, 'r') as feat_src:
for feature in feat_src:
feature['properties']['project_id'] = project_id
del feature['properties']['id']
all_area_features.append(feature)
area_geo = {
"type": "FeatureCollection",
"features": all_area_features
}
area_path = os.path.expanduser(
"~/git/afghanistan_gie/canal_data/canal_polygons.geojson")
area_file = open(area_path, "w")
json.dump(area_geo, area_file)
area_file.close()