Skip to content

Instantly share code, notes, and snippets.

View evandiewald's full-sized avatar

Evan Diewald evandiewald

View GitHub Profile
@evandiewald
evandiewald / distance-vs-rssi.sql
Created July 4, 2022 23:11
Distance vs. RSSI query for DeWi ETL as of 7-4-2022 (poc_receipts_v2)
with hashes as
(select transaction_hash, actor from transaction_actors where
actor = {{address}}
and actor_role = 'witness'
and block > (select max(height) - {{n_blocks}} from blocks)),
target_transactions as
(select fields, hash from transactions where
(type = 'poc_receipts_v2' or type = 'poc_receipts_v1')
def load_map(place: str, graph_only: bool = False) -> (nx.MultiDiGraph, dict):
"""
Load OSM trail data for a given region. Initially check if the graph has already been cached on disk, otherwise it will be downloaded.
Args:
place: The geocode of the region of interest, e.g. 'Shenandoah National Park, Virginia, USA'
graph_only: If true, return only the NetworkX graph, not the geojson.
Returns: The dataset as a NetworkX MultiGraph, the nodes geojson, the edges geojson
"""
def get_elevation_profile_of_segment(dataset: rasterio.DatasetReader, coords: list[list]):
"""
Get the elevation profile (distance vs. altitude) of a path segment from the list of coordinates.
Args:
dataset: The opened rasterio dataset for the SRTM global topography data.
coords: The path coordinates in [[lon1, lat1], [lon2, lat2], ...] format.
Returns: The distance (in miles) and elevation (in feet) vectors.
"""
# coordinates are [lon, lat], flip for rasterio
@callback(
Output("route-path", "data"),
Input("map", "click_lat_lng"),
Input("distance-target", "value"),
Input("elevation-min", "value"),
Input("mode-select", "value"),
State("route-path", "data")
)
def cycles(click_lat_lng, distance_target: Optional[Union[int, float]], elevation_min: Optional[Union[int, float]], mode, path):
if click_lat_lng is None:
def find_best_loop(G: nx.Graph, root, target_dist, tol=1.0, min_elev: Optional[Union[int, float]] = None,
dataset: Optional[rasterio.DatasetReader] = None):
if min_elev and not dataset:
raise ValueError("If asking for elevation data, you must include a rasterio dataset")
error = 1e8
best_path = []
for n in G.nodes():
if nx.has_path(G, root, n):
shortest_path = nx.shortest_path(G, root, n)
paths = nx.all_simple_paths(G, root, n, cutoff=10)
def layout(place=None):
if place:
global G, G2
G, _, edges = load_map(place)
G2 = nx.Graph(G)
polyline = dl.Polyline(id="route-line", positions=[], color="red", weight=10, fillColor="red", fillOpacity=1.0)
patterns = [dict(repeat='100',
arrowHead=dict(pixelSize=15, polygon=False, pathOptions=dict(color="red", stroke=True)),
line=dict(pixelSize=10, pathOptions=dict(color='#f00', weight=20)))]
@evandiewald
evandiewald / connection.py
Created September 30, 2022 15:24
Utility to create a SQLAlchemy connection to a Postgres db via SSH tunnel
from dotenv import load_dotenv
from sshtunnel import SSHTunnelForwarder
from sqlalchemy.engine import create_engine, Engine
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine
from typing import Union
import os
from sqlalchemy.pool import NullPool
load_dotenv()