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
try: | |
from kepler_config import config_map_2 | |
except ImportError: | |
config_map_2 = config | |
map_2 = KeplerGl(data={'london' :gdf_epsg}, config=config_map_2, height=800) # kepler knows what to do when fed with a geodataframe | |
display(map_2b) |
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
gdf_epsg = gdf.to_crs(epsg=3857) | |
ax = gdf_epsg.plot(figsize=(10, 10), alpha=0.5, edgecolor='k') | |
cx.add_basemap(ax) |
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
osmnx.config(use_cache=True, log_console=True) | |
def gdf_concat(list_gdf: list): | |
return gpd.GeoDataFrame( pd.concat(list_gdf, ignore_index=True)) | |
query_city = {'city': 'City of London'} | |
query_london = {'city': 'London'} | |
gdf = gdf_concat([osmnx.geocode_to_gdf(query_city), osmnx.geocode_to_gdf(query_london)]) |
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
config = { | |
'version': 'v1', | |
'config': { | |
'mapState': { | |
'latitude': 51.536265, | |
'longitude': -0.039740, | |
'zoom': 10 | |
} | |
} | |
} |
NewerOlder