-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly_utils.py
266 lines (186 loc) · 7.88 KB
/
poly_utils.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import shapely.geometry as sg
import shapely.ops as so
from centerline.geometry import Centerline
import numpy as np
import pandas as pd
import geopandas as gpd
from geopandas import GeoDataFrame
from upcp.utils import las_utils
def tilecode_to_poly(tilecode):
((x1, y2), (x2, y1)) = las_utils.get_bbox_from_tile_code(
tilecode, padding=0)
return sg.box(x1, y1, x2, y2)
def fix_invalid(poly):
orig_multi = type(poly) == sg.MultiPolygon
if ~poly.is_valid:
poly = poly.buffer(0)
if type(poly) == sg.Polygon and orig_multi:
poly = sg.MultiPolygon([poly])
return poly
def extract_interior(poly):
if poly.interiors:
int_polys = sg.MultiPolygon([sg.Polygon(list(lr.coords))
for lr in poly.interiors])
return sg.Polygon(list(poly.exterior.coords)), int_polys
else:
return poly, sg.MultiPolygon()
def get_centerlines(polygon):
''' Save a NaN value when centerline calculation fails. '''
try:
x = Centerline(polygon)
except Exception as e:
print(e) # TODO also print rows.name[0] ??
x = np.nan
return x
def remove_short_lines(line, min_se_length=5):
if line.type == 'MultiLineString':
passing_lines = []
for i, linestring in enumerate(line.geoms):
other_lines = sg.MultiLineString(
[x for j, x in enumerate(line.geoms) if j != i])
p0 = sg.Point(linestring.coords[0])
p1 = sg.Point(linestring.coords[-1])
is_deadend = False
if p0.disjoint(other_lines):
is_deadend = True
if p1.disjoint(other_lines):
is_deadend = True
if not is_deadend or linestring.length > min_se_length:
passing_lines.append(linestring)
return sg.MultiLineString(passing_lines)
if line.type == 'LineString':
return line
def linestring_to_segments(linestring):
return [sg.LineString([linestring.coords[i], linestring.coords[i+1]])
for i in range(len(linestring.coords) - 1)]
def get_segments(line):
line_segments = []
if line.type == 'MultiLineString':
for linestring in line.geoms:
line_segments.extend(linestring_to_segments(linestring))
if line.type == 'LineString':
line_segments.extend(linestring_to_segments(line))
return line_segments
def interpolate_by_distance(linestring, resolution=1):
all_points = []
count = round(linestring.length / resolution) + 1
if count == 1:
all_points.append(linestring.interpolate(linestring.length / 2))
else:
for i in range(count):
all_points.append(linestring.interpolate(resolution * i))
return all_points
def interpolate(line, resolution=1):
if line.type == 'MultiLineString':
all_points = []
for linestring in line:
all_points.extend(interpolate_by_distance(linestring, resolution))
return sg.MultiPoint(all_points)
if line.type == 'LineString':
return sg.MultiPoint(interpolate_by_distance(line, resolution))
def polygon_to_multilinestring(polygon):
return sg.MultiLineString([polygon.exterior]
+ [line for line in polygon.interiors])
def get_avg_width(poly, segments, resolution=1, precision=2):
avg_width = []
min_width = []
sidewalk_lines = polygon_to_multilinestring(poly)
for segment in segments:
points = interpolate(segment, resolution)
distances = []
for point in points.geoms:
p1, p2 = so.nearest_points(sidewalk_lines, point)
distances.append(p1.distance(p2))
avg_width.append(sum(distances) / len(distances) * 2)
min_width.append(min(distances) * 2)
return np.round(avg_width, precision), np.round(min_width, precision)
def get_avg_width_cl(poly, segments, resolution=1, precision=2):
sidewalk_lines = polygon_to_multilinestring(poly)
points = interpolate(segments, resolution)
distances = []
for point in points.geoms:
p1, p2 = so.nearest_points(sidewalk_lines, point)
distances.append(p1.distance(p2))
avg_width = sum(distances) / len(distances) * 2
min_width = min(distances) * 2
return pd.Series([np.round(avg_width, precision), np.round(min_width, precision)])
def get_route_color(route_weight):
if route_weight == 0:
final_color = 'black'
elif (route_weight > 0) & (route_weight < 1000):
final_color = 'green'
elif (route_weight >= 1000) & (route_weight < 1000000):
final_color = 'lightgreen'
elif (route_weight >= 1000000) & (route_weight < 1000000000):
final_color = 'orange'
elif (route_weight >= 1000000000) & (route_weight < 1000000000000):
final_color = 'red'
elif route_weight == 1000000000000:
final_color = 'darkred'
elif route_weight > 1000000000000:
final_color = 'grey'
else:
final_color = 'purple'
return final_color
def create_df_centerlines(centerline):
centerline_list = []
# Create list of all sub-centerlines
if centerline.type == 'LineString':
centerline_list.append(centerline)
if centerline.type == 'MultiLineString':
for line in centerline:
centerline_list.append(line)
# Create dataframe from list
centerline_df = gpd.GeoDataFrame(centerline_list, columns=['geometry'])
# Add length and route weight columns
centerline_df['length'] = centerline_df['geometry'].length
centerline_df['route_weight'] = np.nan
return centerline_df
def cut(line, distance):
# Cut a line in two at a distance from its starting point
if distance <= 0.0 or distance >= line.length:
return [sg.LineString(line)]
coords = list(line.coords)
for i, p in enumerate(coords):
pd = line.project(sg.Point(p))
if pd == distance:
return [
sg.LineString(coords[:i+1]),
sg.LineString(coords[i:])]
if pd > distance:
cp = line.interpolate(distance)
return [
sg.LineString(coords[:i] + [(cp.x, cp.y)]),
sg.LineString([(cp.x, cp.y)] + coords[i:])]
def shorten_linestrings(centerline_df, max_ls_length):
# Check if we have a linestring that is too long
while centerline_df['length'].max() > max_ls_length:
# Select longest linestring
id_longest_ls = centerline_df['length'].idxmax()
longest_ls = centerline_df.iloc[[id_longest_ls]]['centerlines'].values[0]
# Cut linestring
cut_ls = cut(longest_ls, max_ls_length-0.01)
# Create dataframe from cut linestrings, including length
cut_ls_df = gpd.GeoDataFrame(cut_ls, columns=['centerlines']).set_geometry('centerlines')
cut_ls_df['length'] = cut_ls_df['centerlines'].length
# Add index back
cut_ls_df['index'] = centerline_df['index'][id_longest_ls]
# Remove long linestring that was cut from original dataframe
centerline_df = centerline_df.drop(index=id_longest_ls)
# Add dataframe with cut linestrings to original dataframe
centerline_df = centerline_df.append(cut_ls_df).reset_index(drop=True)
return centerline_df
def remove_interiors(polygon, eps):
list_interiors = []
for interior in polygon.interiors:
p = sg.Polygon(interior)
if p.area > eps:
list_interiors.append(interior)
return(sg.Polygon(polygon.exterior.coords, holes=list_interiors))
def create_mls_per_sidewalk(df, crs):
mls_list = []
for sidewalk_id in df['sidewalk_id'].unique():
df_segments_id = df[df['sidewalk_id'] == sidewalk_id]
mls_id = sg.MultiLineString(list(df_segments_id['geometry']))
mls_list.append(mls_id)
return(GeoDataFrame(geometry=mls_list, crs=crs))