graphPaname/app/preprocessing.py

39 lines
1.2 KiB
Python
Raw Normal View History

from folium import Map, Marker, PolyLine
2020-06-12 19:21:50 +02:00
from pandas import DataFrame, json_normalize
2020-06-13 18:23:34 +02:00
from app.data_request import request_dataset
2020-06-14 00:58:52 +02:00
from constants import COLUMNS, COORDINATES
2020-05-27 20:13:45 +02:00
2020-06-05 13:48:47 +02:00
def create_dataframe(dataset) -> DataFrame:
"""
2020-06-14 00:58:52 +02:00
Creates a DataFrame from a JSON response
2020-06-05 13:48:47 +02:00
"""
json = request_dataset(dataset)
2020-06-05 15:24:16 +02:00
df = json_normalize(data=json, record_path=["records"], errors="ignore",)
2020-06-10 21:48:44 +02:00
filtered_df = df.filter(items=COLUMNS[dataset])
return filtered_df
2020-06-14 00:58:52 +02:00
def reverse_coordinates(row):
"""
Reverses each tuples coordinates to ensure folium can parse them correctly
"""
2020-06-15 20:02:20 +02:00
coord = [tuple(reversed(t)) for t in row]
return coord
2020-06-14 00:58:52 +02:00
def create_map(df):
"""
Creates a Map with markers or lines from the DataFrame
2020-06-14 00:58:52 +02:00
"""
m = Map(location=COORDINATES, zoom_start=12, tiles="Stamen Terrain")
2020-06-14 00:58:52 +02:00
for index, row in df.iterrows():
if row["fields.geo_shape.type"] == "LineString":
coord = reverse_coordinates(row["fields.geo_shape.coordinates"])
PolyLine(locations=coord, color="blue", opacity=0.5).add_to(m)
else:
lng, lat = row["fields.geo_shape.coordinates"]
Marker(location=[lat, lng]).add_to(m)
2020-06-14 00:58:52 +02:00
m.save("app/templates/map.html")