Skip to content

Instantly share code, notes, and snippets.

@lmeulen
lmeulen / traveltime_getdata.py
Created May 5, 2021 19:31
traveltime_getdata
import pandas as pd
import geopandas as gpd
import numpy as np
import os, urllib, json, csv, zipfile, math
INPUT='input_ut.csv'
OUTPUT='output_ut.csv'
OUTPUT_PC='distances_pc4_ut.json'
with zipfile.Zipfile('gtfs-nl.zip') as z:
@lmeulen
lmeulen / traveltime_get_traveltimes.py
Created May 6, 2021 06:29
traveltime_get_traveltimes
URL = 'http://localhost:8080/otp/routers/default/plan?'
outputFile = open(OUTPUT, 'w')
writer = csv.writer(outputFile)
writer.writerow(['StartDate','StartTime','StartLat','StartLon','EndLat',
'EndLon', 'DurationMin'])
# Takes CSV input, creates URLs, stores data locally in row array
for c, d in df.iterrows():
params = {'date' : d['Date'],
@lmeulen
lmeulen / traveltime_traveltime_per_pc4
Created May 6, 2021 06:35
traveltime_traveltime_per_pc4
pc = gpd.read_file(os.path.join('PC_4-shp','PC4.shp'))
df = pd.read_csv(OUTPUT)
gdf = gpd.GeoDataFrame(df,
geometry=gpd.points_from_xy(df.EndLon, df.EndLat))
[['geometry', 'DurationMin']]
def get_avg_traveltime(pc4):
v = gdf[gdf.within(pc4['geometry'] )].mean(skipna=True)
if math.isnan(v):
return np.NaN
@lmeulen
lmeulen / traveltime_save_json
Created May 6, 2021 06:39
traveltime_save_json
pc.to_file(OUTPUT_PC, driver="GeoJSON")
@lmeulen
lmeulen / traveltime_unreachables.py
Last active May 6, 2021 06:40
traveltime_unreachables
# Unreachable? Closest other postal area and add 15 min
for index, pcode in pc.iterrows():
if math.isnan(pcode.Traveltime):
# get 'not disjoint' countries
dst=pc[pc.geometry.touches(pcode.geometry)]['Traveltime'].mean(skipna=True)
# add names of neighbors as NEIGHBORS value
if ~math.isnan(dst) and (dst > 0):
pc.at[index, "Traveltime"] = int(dst + 15)
else:
pc.at[index, "Traveltime"] = pc.Traveltime.max()
@lmeulen
lmeulen / traveltime_start_otp.bash
Last active May 7, 2021 18:45
traveltime_start_otp
Java -Xmx12G -jar otp-2.0.0-shaded.jar --build --serve .
@lmeulen
lmeulen / AllPT2UT_otp.sh
Created May 25, 2021 17:27
AllPT2UT_otp
java -Xmx12G -jar otp-2.0.0-shaded.jar --build --serve .
@lmeulen
lmeulen / AllPT2UT_input.py
Created May 25, 2021 17:55
AllPT2UT_input
import pandas as pd
import geopandas as gpd
import numpy as np
import urllib, json, csv, zipfile, sys
import matplotlib.pyplot as plt
from geopandas.tools import sjoin
from shapely.geometry import LineString, Point
with zipfile.ZipFile('gtfs-nl.zip') as z:
with z.open('stops.txt') as f:
@lmeulen
lmeulen / UT2ALL_create_gpd.py
Created May 25, 2021 19:29
UT2ALL_create_gpd
segments_df['line'] = segments_df.apply(lambda x: LineString([Point(x["lon_x"], x["lat_x"]),
Point(x["lon_y"], x["lat_y"])]),
axis=1)
geodata = gpd.GeoDataFrame(segments_df, geometry=segments_df['line'])
geodata['width'] = 5 * (geodata['count']) / (geodata['count'].max())
@lmeulen
lmeulen / UT2ALL_plot.py
Created May 25, 2021 19:47
UT2ALL_plot
geodata.plot(figsize=(15, 15), linewidth=np.minimum(np.maximum(df5['width'], 0.1), 2.75))