Skip to content

Instantly share code, notes, and snippets.

View joaofig's full-sized avatar

João Paulo Figueira joaofig

View GitHub Profile
def predict(max_branch: int = 3,
max_length: int = 10) -> FeatureGroup | None:
fg = folium.FeatureGroup(name="polylines")
if "token_list" in st.session_state:
hex_list = st.session_state["token_list"]
seed = hex_list[-3:-1]
if len(seed) > 1:
paths = expand_seed(seed[0], seed[1],
max_branch=max_branch,
def get_successors(h0: int, h1: int) -> Counter:
cnt = get_cache_successors(h0, h1)
if cnt is None:
db = TrajDb()
sql = "select t2 from triple where t0=? and t1=?"
successors = [r[0] for r in db.query(sql, [int(h0), int(h1)])]
cnt = Counter(successors)
set_cache_successors(h0, h1, cnt)
return cnt
def compute_probability(token_list: list[int]) -> float:
nodes = token_list[1:-1]
prob = 0.0
if len(nodes) > 2:
prob = 1.0
for i in range(len(nodes)-2):
t0, t1, t2 = nodes[i:i+3]
cnt = get_successors(int(t0), int(t1))
if len(cnt):
prob *= cnt[t2] / cnt.total()
@joaofig
joaofig / match-trips_insert_h3_nodes.py
Created July 2, 2023 17:37
Inserts the mapping between H3 tokens and map node coordinates
def insert_h3_nodes(h3_nodes: list[tuple[int,tuple[float,float]]]):
db = TrajDb()
sql = "insert or ignore into h3_node (h3, lat, lon) values (?, ?, ?)"
db.execute_sql(sql, [(n[0], n[1][1], n[1][0]) for n in h3_nodes], many=True)
@joaofig
joaofig / match-trips_insert_triples.py
Created July 2, 2023 17:35
Inserts the expanded triples list
def insert_triples(traj_id: int,
triples: list[(int,int,int)]):
db = TrajDb()
sql = "insert into triple (traj_id, t0, t1, t2) values (?, ?, ?, ?)"
params = [(traj_id, t0, t1, t2) for t0, t1, t2 in triples]
db.execute_sql(sql, params, many=True)
@joaofig
joaofig / match-trips_insert_h3.py
Created July 2, 2023 17:32
Inserts the trajectory H3 token list
def insert_h3(traj_id: int,
h3_list: list[int]) -> None:
db = TrajDb()
sql = "insert into traj_h3 (traj_id, h3) values (?, ?)"
params = [[traj_id, int(h)] for h in h3_list]
db.execute_sql(sql, params, many=True)
@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