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
# requirements.txt | |
geopandas==0.10.2 | |
jupyter==1.0.0 | |
keplergl==0.3.2 | |
matplotlib==3.5.1 | |
osmnx==1.1.2 | |
pandas==1.4.2 | |
seaborn==0.11.2 |
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
number_of_offices = len(df_commuter.groupby(["workplace_lng", "workplace_lat"]).count()) | |
number_of_residences = len(df_commuter.groupby(["residence_lng", "residence_lat"]).count()) | |
number_of_offices_in_london_and_city = len(df_commuter_london_office.groupby(["workplace_lng", "workplace_lat"]).count()) | |
number_of_residences_commuting_to_london_and_city = len(df_commuter_london_office.groupby(["residence_lng", "residence_lat"]).count()) | |
commuters_office_ratio = number_of_residences / number_of_offices | |
commuters_office_ratio_in_london_and_city = number_of_residences_commuting_to_london_and_city / number_of_offices_in_london_and_city | |
print(f"Number of offices in london and the city {number_of_offices_in_london_and_city} ({number_of_offices_in_london_and_city / number_of_offices} %)") |
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
import seaborn as sns | |
sns.set_style("darkgrid", {"grid.color": ".6", "grid.linestyle": ":"}) | |
def radar_histogram(ax, df): | |
""" | |
Input: | |
df with at least 2 columns distance_km and bearing_deg. | |
Output: radar histogram plot. |
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
from typing import Tuple | |
from math import radians | |
def haversine(lng1: float, lat1: float, lng2: float, lat2: float) -> Tuple[float, float]: | |
""" returns (haversine distance in km, bearing in degrees from point 1 to point 2), vectorised """ | |
avg_earth_radius_km = 6371.0072 | |
lng1, lat1, lng2, lat2 = map(np.deg2rad, [lng1, lat1, lng2, lat2]) |
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
# -- St Luke office | |
# narrow dataset to the geometry | |
mask_st_luke_office = gdf_commuters_workplace.intersects(shape(polygon_st_luke_office)) | |
df_commuters_st_luke_office = df_commuter[mask_st_luke_office] | |
# embed shape into a geopandas to visualise in kepler | |
gdf_st_luke_geometry = gpd.GeoDataFrame({'geometry':[shape(polygon_st_luke_office)], "display_name": ["St Luke's Close Office"]}) | |
# -- Same for Albert Road office | |
mask_albert_road = gdf_commuters_workplace.intersects(shape(polygon_albert_road)) |
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
polygon_st_luke_office = { | |
"type": "Polygon", | |
"coordinates": [ | |
[ | |
[-0.0930210043528368, 51.52553386809767], | |
[-0.09362754938510826, 51.5257442611004], | |
[-0.09398505401347826, 51.52546150215205], | |
[-0.09363181940230854, 51.525218817282784], | |
[-0.09313761642997592, 51.52527679524477], | |
[-0.0930210043528368, 51.52553386809767], |
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
# -- about 17 seconds -- | |
gdf_commuters_workplace = gpd.GeoDataFrame(df_commuter.copy(), geometry=gpd.points_from_xy(df_commuter.workplace_lng, df_commuter.workplace_lat)) | |
# -- about 120 seconds: points in polygon | |
mask_points_in_city = gdf_commuters_workplace.intersects(gdf.geometry.iloc[0]) | |
mask_points_in_london = gdf_commuters_workplace.intersects(gdf.geometry.iloc[1]) | |
num_total_rows = len(gdf_commuters_workplace) | |
num_rows_in_city = len(mask_points_in_city[mask_points_in_city == True]) | |
num_rows_in_london = len(mask_points_in_london[mask_points_in_london == True]) | |
print(f"Number of rows for offices in the city {num_rows_in_city} ({100 * num_rows_in_city / num_total_rows} %)") |
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
conda create -n geods python=3.9 | |
conda activate geods |
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
import contextily as cx | |
import geopandas as gpd | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import osmnx | |
import pandas as pd | |
from keplergl import KeplerGl | |
from shapely.geometry import shape |
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
# About 10 seconds | |
df_commuter = pd.read_csv("https://github.com/uber-web/kepler.gl-data/raw/master/ukcommute/data.csv") | |
df_commuter.head() |
NewerOlder