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_02.txt
Last active April 25, 2022 08:22
b001_02.txt
# 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
@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_1.bash
Last active April 24, 2022 14:46
b001_1.bash
conda create -n geods python=3.9
conda activate geods
@SebastianoF
SebastianoF / b001_03.py
Last active April 24, 2022 14:45
b001_03.py
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
@SebastianoF
SebastianoF / b001_04.py
Last active April 24, 2022 14:44
b001_04.py
# About 10 seconds
df_commuter = pd.read_csv("https://github.com/uber-web/kepler.gl-data/raw/master/ukcommute/data.csv")
df_commuter.head()