rainwalk/map.py
2023-04-07 08:22:59 -04:00

88 lines
2.8 KiB
Python

import geopy.distance
import overpy
from shapely.geometry import Point, LineString
import folium
from math import radians, cos, sin, sqrt, atan2, asin
from decimal import Decimal
import pandas as pd
R =6371
def distance(lat1, lon1, lat2, lon2):
return geopy.distance.geodesic((lat1, lon1), (lat2, lon2)).m
def get_subline(points, length):
subline = []
subline_length = 0
# Iterate through the points and add them to the subline until its length is equal to or greater than the desired length
for i in range(len(points) - 1):
p1 = points[i]
p2 = points[i+1]
segment_length = distance(p1[0], p1[1], p2[0], p2[1])
#print(f"Points {p1} -> {p2} : Distance {segment_length}")
# If adding the current segment would make the subline too long, interpolate a point on the segment and add that instead
if subline_length + segment_length > length:
remaining_length = length - subline_length
ratio = Decimal(remaining_length / segment_length)
subline.append(p1)
subline.append((Decimal(p1[0]) + (Decimal(p2[0]) - Decimal(p1[0])) * ratio, p1[1] + (p2[1] - p1[1]) * ratio))
return subline
subline.append(p1)
subline_length += segment_length
# If we get to the end of the line and still haven't reached the desired length, just return the whole line
subline.append(points[-1])
return subline
df = pd.read_csv('data2.csv')
clat = df['Latitude Point'][0]
clon = df['Longitude Point'][0]
m = folium.Map(location=[clat, clon], zoom_start=20)
api = overpy.Overpass()
iters = 0
for index, row in df.iterrows():
print(row)
lat = row['Latitude Point']
lon = row['Longitude Point']
length = row['Sidewalk Shed/Linear Feet'] * 0.3048
# coordinates and length of line in feet
#lat = 40.68941
#lon = -73.97881
#length = 260
# convert lat/lon to Point object
coords = Point(lat, lon)
# use overpass API to get nearest sidewalk
result = api.query(f'way["highway"="footway"](around:100,{lat},{lon});out;')
if len(result.ways) == 0:
continue
sidewalk_coords = [(node.lat, node.lon) for node in result.ways[0].get_nodes(resolve_missing=True)]
# create LineString object from sidewalk coordinates
new_line = get_subline(sidewalk_coords, length)
# create folium map centered at given coordinates
map_center = [lat, lon]
# add sidewalk and line to map
folium.PolyLine(sidewalk_coords, color='orange', weight=10).add_to(m)
for i in sidewalk_coords:
folium.CircleMarker(location=[i[0], i[1]],
radius=2,
weight=5, tooltip=f"{i[0]}, {i[1]}").add_to(m)
folium.PolyLine(new_line, color='red').add_to(m)
iters = iters + 1
if iters%50 == 0:
m.save('your_map.html')
# show map
m.save('your_map.html')