-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
173 lines (151 loc) · 6.28 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from io import BytesIO
import simplejson as json
from pathlib import Path
from typing import Generator, List, Tuple, Type
from urllib.request import urlopen
import ijson.backends.yajl2_cffi as ijson
from rtree import index
from shapely.geometry import shape
from shapely.errors import TopologicalError
def geojson_gen(buildings: Generator) -> Generator:
""" takes in Generator of features from ijson and yields a tuple for bulk loading into rtree index in the form (id, geometry, object)
http://toblerity.org/rtree/tutorial.html#using-rtree-as-a-cheapo-spatial-database
"""
for i, building in enumerate(buildings):
yield (i, shape(building["geometry"]).bounds, building)
def intersection_candidates(features: Generator, idx: Type[index.Index]) -> Generator:
""" finds all features which do not intersect with building """
for _, bbox, feature in geojson_gen(features):
buildings = [n.object for n in idx.intersection(bbox, objects=True)]
if len(buildings):
yield (feature, buildings)
def non_intersection_candidates(
features: Generator, idx: Type[index.Index]
) -> Generator:
""" finds all features which do not intersect with building"""
for _, bbox, feature in geojson_gen(features):
buildings = [n.object for n in idx.intersection(bbox, objects=True)]
if not len(buildings):
yield (feature, buildings)
def building_block_intersection(overlaps_candidates: Generator) -> Generator:
""" """
for candidate in overlaps_candidates:
buildings = []
for building in candidate[1]:
try:
intersect = shape(building["geometry"]).intersection(
shape(candidate[0]["geometry"])
)
except TopologicalError:
continue
if intersect:
buildings.append(building)
if len(buildings):
yield (candidate[0]["properties"]["TRACTCE10"], buildings)
def block_building_intersection(overlaps_candidates: Generator) -> Generator:
""" """
for candidate in overlaps_candidates:
for building in candidate[1]:
try:
intersect = shape(building["geometry"]).intersection(
shape(candidate[0]["geometry"])
)
except TopologicalError:
pass
if intersect:
yield candidate[0]
def census_wo_intersection(overlaps_candidates: Generator) -> Generator:
""" """
for candidate in overlaps_candidates:
contains = False
for building in candidate[1]:
intersect = shape(building["geometry"]).intersection(
shape(candidate[0]["geometry"])
)
if intersect:
contains = True
if not contains:
yield candidate[0]
def bbox_filter(in_gen: Generator, bbox: Tuple[float, float]) -> Generator:
""" filters input Generator to bounding box coordinates. Bounding box in format (left, bottom, right, top) """
for item in in_gen:
feature = shape(item["geometry"])
xs, ys = feature.exterior.coords.xy
x_in_bounds = False
y_in_bounds = False
for x in xs:
if x < bbox[2] and x > bbox[0]:
x_in_bounds = True
for y in ys:
if y < bbox[3] and y > bbox[1]:
y_in_bounds = True
if x_in_bounds and y_in_bounds:
yield item
BBOX = (
-106.791_915_893_554_69,
34.980_502_445_365_2,
-106.414_260_864_257_81,
35.288_787_158_819_86,
)
def main():
""" main function """
with open("./data/abq_building.geojson", "rb") as building_file, open(
"./data/NewMexico.json", "rb"
) as ms_buildings, open("./data/blocks_wgs84.geojson", "rb") as block_file:
abq_features = ijson.items(building_file, "features.item")
census_blocks = ijson.items(block_file, "features.item")
ms_features = ijson.items(ms_buildings, "features.item")
ms_features = bbox_filter(ms_features, BBOX)
ms_gen = geojson_gen(ms_features)
print("indexing ms buildings")
ms_idx = index.Index(ms_gen, filename="ms_building_idx")
abq_gen = geojson_gen(abq_features)
print("indexing osm buildings")
abq_idx = index.Index(abq_gen, filename="building_idx")
empty_candidates = non_intersection_candidates(census_blocks, abq_idx)
empty_blocks = census_wo_intersection(empty_candidates)
ms_candidates = intersection_candidates(empty_blocks, ms_idx)
ms_blocks = building_block_intersection(ms_candidates)
geojson = """{
"type": "FeatureCollection",
"features": [\n"""
print("writing tract files")
for block in ms_blocks:
for building in block[1]:
building["properties"] = {"building": "yes"}
buildings = [json.dumps(building) for building in block[1]]
filename = f"./out/tract_{block[0]}.geojson"
if Path(filename).is_file():
with open(filename, "a+") as f:
data = f.read()
for building in buildings:
f.write(building)
f.write(",")
else:
with open(filename, "w") as f:
f.write(geojson)
for building in buildings:
f.write(building)
f.write(",")
files = [path for path in Path("./out").iterdir()]
for item in files:
content = b""
with item.open(mode="rb") as f:
data = f.read()
content = data[:-1]
with item.open(mode="wb") as f:
content += b"]\n}"
f.write(content)
for item in files:
with item.open(mode="r+") as f:
data = f.read()
json_data = json.loads(data)
features = json_data["features"]
buildings = list(set([json.dumps(building) for building in features]))
deduped = [json.loads(building) for building in buildings]
json_data["features"] = deduped
f.seek(0)
f.write(json.dumps(json_data))
f.truncate()
if __name__ == "__main__":
main()