Skip to content

Instantly share code, notes, and snippets.

How to add / edit an attribute in a OpenStreetMap (OSM) PBF file

OSM2psql is a handy tool for importing OSM data into a PostgreSQL database. Though, the created database cannot be converted back to a PBF file. For example, to use a routing algorithm (e.g., Graphhopper) with a custom weighting, the PBF format is needed. For this, I used Osmosis.

For the task of editing a PBF file and re-creating a new PBF file I used the following process:

1. Install PostgreSQL, PostGIS and Osmosis.

# snap GPS trajectories to OSM road network
# expected csv format: tid,lon,lat
# where tid is the trip id, where all coordinates of the same trip share the same id
# the order of coordinates is expected to be in correct order
# setup OSRM server: https://hub.docker.com/r/osrm/osrm-backend/
import csv
import requests
# A Script to
# - download the GeoLife (https://www.microsoft.com/en-us/research/publication/geolife-gps-trajectory-dataset-user-guide/) data
# - transform it to a .csv file
# - and cut records to given outlines
# not very performant bc of preprocessing with pandas (takes about 30 min)
# produces a 1,8 GB output csv file
import os
from pathlib import Path

Python packages for mobility data

Analysis of mobility data

  • MovingPandas implements a Trajectory class and corresponding methods based on GeoPandas.

Fokus on human mobility data

  • scikit-mobility is a library for human mobility analysis in Python.
  • trackintel is a library for the analysis of spatio-temporal tracking data with a focus on human mobility. The core of trackintel is the hierachical data model for movement data that is used in GIS, transport planning and related fields. We provide functionalities for the full life-cycle of human mobility data analysis: import and export of tracking data of different types (e.g, trackpoints, check-ins, trajectories), preprocessing, data quality assessment, semantic enrichment, quantitative analysis and mining tasks, and visualization of data and results.

Routing and travel time matrices

Freeze env for documentation and sharing:

pip list --format=freeze > requirements.txt

from haversine import haversine, Unit
# construct the cost matrix
def get_cost_matrix(gdf1, gdf2):
gdf1_centroids = gdf1.to_crs(3395).centroid.to_crs(4326)
gdf2_centroids = gdf2.to_crs(3395).centroid.to_crs(4326)
coords_sig1 = list(zip(gdf1_centroids.y, gdf1_centroids.x))
coords_sig2 = list(zip(gdf2_centroids.y, gdf2_centroids.x))
#get all potential combinations between all points from sig1 and sig2
import numpy as np
import geopandas as gpd
import cv2
# read in data
example = gpd.read_file("data/example.gpkg")
scenario1 = gpd.read_file("data/example_similar.gpkg")
scenario2 = gpd.read_file("data/example_different.gpkg")
def signature_opt1(gdf, crs):

Setting up the conda env for a python project with jupyter lab

1. Setup new python project

1. create new environment

conda create --name myenv

with specific python version:

library(sf)
xmin <- -2.3743
xmax <- 0.5589
ymin <- 37.3397
ymax <- 40.8679
st_polygon(list(rbind(
c(xmin, ymin),
c(xmin, ymax),
@AlexandraKapp
AlexandraKapp / osrm_docker_windows.md
Last active September 30, 2024 13:54
How to set up your own OSRM backend with Docker on Windows

How to set up your own OSRM backend with Docker on Windows

The OSRM docker quick start provides a great explanation on how to set up the container: https://hub.docker.com/r/osrm/osrm-backend/

Yet - for Docker on Windows minor changes were necessary for me (otherwise I'd get "File not found" or "Permission" errors).

This is how it worked for me:

1. Pull the image