Last active
May 28, 2021 14:12
-
-
Save lmeulen/d106471c870c23e4596ecd235df6dac4 to your computer and use it in GitHub Desktop.
UT2ALL_segments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pop_val(datastring, index): | |
b = None | |
res = shift = 0 | |
while b is None or b >= 0x20: | |
b = ord(datastring[index]) - 63 | |
res |= ( (b & 0x1f) << shift ) | |
index += 1 | |
shift += 5 | |
if res & 1: | |
return ~(res >> 1), index | |
return (res >> 1), index | |
def decode(datastring): | |
coordinates = [] | |
lat = lon = 0 | |
idx = 0 | |
while idx < len(datastring): | |
delta_lat, idx = pop_val(datastring, idx) | |
lat += delta_lat / 100000.0 | |
delta_lon, idx = pop_val(datastring, idx) | |
lon += delta_lon / 100000.0 | |
coordinates.append((lat, lon)) | |
return coordinates | |
segments = [] | |
for c, g in legs_df.iterrows(): | |
cnt = g['count'] | |
lat_prev = 0.0 | |
lon_prev = 0.0 | |
for s in decode(g['geometryPoints']): | |
segments.append([lat_prev, lon_prev, s[0], s[1], cnt]) | |
lat_prev = s[0] | |
lon_prev = s[1] | |
segments_df = pd.DataFrame(segments, columns = ['lat_x', 'lon_x', 'lat_y', 'lon_y', 'count']) | |
segments_df = segments_df[segments_df.lat_x > 0.0] | |
segments_df = segments_df.groupby(['lat_x', 'lon_x', 'lat_y', 'lon_y']).sum().reset_index() | |
segments_df = segments_df.sort_values('count') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment