4th tab for street grids
Browse files- app.py +4 -1
- strgrid_tab/__init__.py +6 -0
- strgrid_tab/strgrid_logic.py +61 -0
- strgrid_tab/strgrid_setup.py +53 -0
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import logging
|
|
| 8 |
from inference_tab import get_inference_widgets, run_inference,georefImg
|
| 9 |
from annotation_tab import get_annotation_widgets
|
| 10 |
from map_tab import get_map_widgets
|
|
|
|
| 11 |
|
| 12 |
# setup logging
|
| 13 |
logging.basicConfig(level=logging.DEBUG)
|
|
@@ -21,7 +22,9 @@ with gr.Blocks() as demo:
|
|
| 21 |
with gr.Tab("Annotation"):
|
| 22 |
get_annotation_widgets(selected_tile_state)
|
| 23 |
with gr.Tab("Map"):
|
| 24 |
-
get_map_widgets(city_name)
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
|
|
|
|
| 8 |
from inference_tab import get_inference_widgets, run_inference,georefImg
|
| 9 |
from annotation_tab import get_annotation_widgets
|
| 10 |
from map_tab import get_map_widgets
|
| 11 |
+
from strgrid_tab import get_strgrid_widgets
|
| 12 |
|
| 13 |
# setup logging
|
| 14 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
| 22 |
with gr.Tab("Annotation"):
|
| 23 |
get_annotation_widgets(selected_tile_state)
|
| 24 |
with gr.Tab("Map"):
|
| 25 |
+
get_map_widgets(city_name)
|
| 26 |
+
with gr.Tab("Street Grid"):
|
| 27 |
+
get_strgrid_widgets()
|
| 28 |
|
| 29 |
|
| 30 |
|
strgrid_tab/__init__.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .strgrid_setup import get_strgrid_widgets
|
| 2 |
+
from .strgrid_logic import run_strgrid_logic
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
__all__ = ["get_strgrid_widgets", "run_strgrid_logic"]
|
strgrid_tab/strgrid_logic.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import geopandas as gpd
|
| 2 |
+
import osmnx as ox
|
| 3 |
+
from pyproj import Transformer
|
| 4 |
+
from config import OUTPUT_DIR
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def extract_osm_from_shp_extent(shp_path):
|
| 9 |
+
gdf = gpd.read_file(shp_path)
|
| 10 |
+
gdf = gdf.to_crs(epsg=4326)
|
| 11 |
+
|
| 12 |
+
bbox = gdf.total_bounds
|
| 13 |
+
|
| 14 |
+
G = ox.graph_from_bbox(bbox,network_type="all")
|
| 15 |
+
G_proj = ox.project_graph(G, to_crs="EPSG:3395")
|
| 16 |
+
|
| 17 |
+
edges = ox.graph_to_gdfs(
|
| 18 |
+
G_proj,
|
| 19 |
+
nodes=False,
|
| 20 |
+
edges=True,
|
| 21 |
+
fill_edge_geometry=True
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
edges = edges[["name", "geometry"]]
|
| 25 |
+
edges = edges[edges["name"].notnull()]
|
| 26 |
+
edges["name"] = edges["name"].apply(
|
| 27 |
+
lambda x: x[0] if isinstance(x, list) else x
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return edges
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def find_nearest_osm_roads(input_shp,osm_roads):
|
| 34 |
+
roads = gpd.read_file(input_shp)
|
| 35 |
+
roads = roads.to_crs(epsg=3395)
|
| 36 |
+
|
| 37 |
+
nearest = gpd.sjoin_nearest(
|
| 38 |
+
roads,
|
| 39 |
+
osm_roads,
|
| 40 |
+
how="left",
|
| 41 |
+
distance_col="distance_m"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
nearest.rename(columns={"name_left":"strgrid_name","name_right":"osm_name"}, inplace=True)
|
| 46 |
+
nearest = nearest.sort_values("distance_m").drop_duplicates(subset=["strgrid_name","geometry"])
|
| 47 |
+
|
| 48 |
+
return nearest
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def write_nearest_roads_csv(nearest_gdf):
|
| 52 |
+
OSM_NEAR_PATH = os.path.join(OUTPUT_DIR, "strgrid_osm_nearest.csv")
|
| 53 |
+
nearest_gdf.to_csv(OSM_NEAR_PATH, index=False)
|
| 54 |
+
return OSM_NEAR_PATH
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def run_strgrid_logic(input_shp):
|
| 58 |
+
osm_roads = extract_osm_from_shp_extent(input_shp)
|
| 59 |
+
nearest = find_nearest_osm_roads(input_shp,osm_roads)
|
| 60 |
+
csv_path = write_nearest_roads_csv(nearest)
|
| 61 |
+
return csv_path
|
strgrid_tab/strgrid_setup.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from .strgrid_logic import run_strgrid_logic
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def get_strgrid_widgets():
|
| 6 |
+
with gr.Row():
|
| 7 |
+
with gr.Column(scale=1, min_width=500):
|
| 8 |
+
shp_input = gr.File(
|
| 9 |
+
label="Upload Zipped Shapefile with existing street grid"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
run_osm_btn = gr.Button(
|
| 13 |
+
"Extract Nearest OSM Roads",
|
| 14 |
+
interactive=False
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
osm_log = gr.Textbox(
|
| 18 |
+
label="Progress",
|
| 19 |
+
lines=6,
|
| 20 |
+
interactive=False
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
osm_download = gr.File(
|
| 24 |
+
label="Download CSV",
|
| 25 |
+
file_types=[".csv"],
|
| 26 |
+
type="filepath"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def enable_run_btn(shp):
|
| 32 |
+
return gr.update(interactive=bool(shp))
|
| 33 |
+
|
| 34 |
+
shp_input.change(
|
| 35 |
+
fn=enable_run_btn,
|
| 36 |
+
inputs=shp_input,
|
| 37 |
+
outputs=run_osm_btn
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def run_osm_wrapper(shp_file):
|
| 42 |
+
csv_path = run_strgrid_logic(
|
| 43 |
+
shp_file.name
|
| 44 |
+
)
|
| 45 |
+
return "Find nearest OSM roads is complete.", csv_path
|
| 46 |
+
|
| 47 |
+
run_osm_btn.click(
|
| 48 |
+
fn=run_osm_wrapper,
|
| 49 |
+
inputs=[shp_input],
|
| 50 |
+
outputs=[osm_log, osm_download]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return shp_input, run_osm_btn, osm_log, osm_download
|