Skip to content

Instantly share code, notes, and snippets.

View SebastianoF's full-sized avatar

Sebastiano Ferraris SebastianoF

  • London
View GitHub Profile
@SebastianoF
SebastianoF / b001_13.py
Created April 24, 2022 16:59
b001_13.py
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} %)")
@SebastianoF
SebastianoF / b001_13.py
Last active April 24, 2022 16:53
b001_13.py
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.
@SebastianoF
SebastianoF / b001_12.py
Created April 24, 2022 16:50
b001_12.py
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])
@SebastianoF
SebastianoF / b001_11.py
Last active April 24, 2022 15:01
b001_11.py
# -- 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))
@SebastianoF
SebastianoF / b001_10.py
Created April 24, 2022 14:57
b001_10.py
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],
@SebastianoF
SebastianoF / b001_09.py
Created April 24, 2022 14:49
b001_09.py
# -- 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} %)")
@SebastianoF
SebastianoF / b001_08.py
Created April 24, 2022 14:42
b001_08.py
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)
@SebastianoF
SebastianoF / b001_07.py
Last active April 24, 2022 14:43
b001_07.py
gdf_epsg = gdf.to_crs(epsg=3857)
ax = gdf_epsg.plot(figsize=(10, 10), alpha=0.5, edgecolor='k')
cx.add_basemap(ax)
@SebastianoF
SebastianoF / b001_06.py
Last active April 24, 2022 14:43
b001_06.py
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)])
@SebastianoF
SebastianoF / b001_05.py
Last active April 24, 2022 14:44
b001_05.py
config = {
'version': 'v1',
'config': {
'mapState': {
'latitude': 51.536265,
'longitude': -0.039740,
'zoom': 10
}
}
}