Skip to content

Instantly share code, notes, and snippets.

View joaofig's full-sized avatar

João Paulo Figueira joaofig

View GitHub Profile
@joaofig
joaofig / generate_triples_match-trips.py
Created July 2, 2023 11:13
Generates the list of triples from a token list
def generate_triples(hex_list: list[int]) -> list[(int,int,int)]:
triples = []
if len(hex_list) > 2:
for i in range(len(hex_list) - 2):
t0, t1, t2 = hex_list[i:i + 3]
triples.append((t0, t1, t2))
return triples
@joaofig
joaofig / pyvalhalla_setup.py
Created June 30, 2023 15:20
Setting up PyValhalla in Python
from valhalla import Actor, get_config
config = get_config(tile_extract='./valhalla/custom_files/valhalla_tiles.tar',
verbose=True)
actor = Actor(config)
@joaofig
joaofig / pip_install_pyvalhalla.sh
Created June 30, 2023 15:18
Install PyValhalla through PIP
pip install pyvalhalla
@joaofig
joaofig / start_valhalla_docker.sh
Created June 30, 2023 15:08
Runs the Valhalla docker image
docker run -it --rm --name valhalla_gis-ops -p 8002:8002 \
-v $PWD/valhalla/custom_files:/custom_files \
-e tile_urls=http://download.geofabrik.de/north-america/us/michigan-latest.osm.pbf \
-e serve_tiles=True -e build_admins=True \
docker.pkg.github.com/gis-ops/docker-valhalla/valhalla:3.3.0
@joaofig
joaofig / valhalla_docker.sh
Created June 30, 2023 15:05
Pulls Valhalla's image from the package manager
docker login docker.pkg.github.com
docker pull docker.pkg.github.com/gis-ops/docker-valhalla/valhalla:3.3.0
@joaofig
joaofig / match-trips_insert_geometry.py
Created June 18, 2023 14:50
Inserts the geometry string
def insert_geometry(traj_id: int,
geometry: str) -> None:
db = TrajDb()
sql = "insert into traj_match (traj_id, geometry) values (?, ?)"
db.execute_sql(sql, [traj_id, geometry])
@joaofig
joaofig / match-trips_main.py
Last active July 2, 2023 10:37
The main map-matching data processing loop.
def main():
tiles = './valhalla/custom_files/valhalla_tiles.tar'
config = get_config(tile_extract=tiles, verbose=True)
max_traj_id = get_max_traj_id()
max_match = get_max_match()
for traj_id in range(max_match + 1, max_traj_id + 1):
actor = Actor(config)
@joaofig
joaofig / match-trips_load_trajectory_points.py
Created June 17, 2023 11:31
Loads the GPS trajectory points from the database.
def load_trajectory_points(traj_id):
db = EVedDb()
sql = f"""
select distinct
s.latitude as lat
, s.longitude as lon
, min(s.time_stamp) / 1000 as time
from signal s
inner join trajectory t on s.vehicle_id = t.vehicle_id and s.trip_id = t.trip_id
@joaofig
joaofig / match-trips_map_match.py
Created June 17, 2023 11:29
Map-matches a set of GPS locations to the digital map using Valhalla.
def map_match(actor: Actor,
df: pd.DataFrame) -> str:
param = {
"use_timestamps": True,
"shortest": True,
"shape_match": "walk_or_snap",
"shape": df.to_dict(orient='records'),
"costing": "auto",
"format": "osrm",
"directions_options": {
@joaofig
joaofig / GrapgTrajectory_get_matching_links.py
Created November 21, 2022 09:30
This function returns all the matching links for a query trajectory