Spaces:
Sleeping
Sleeping
Initial commit
Browse files- app.py +187 -0
- data/hospitals.dbf +0 -0
- data/hospitals.prj +1 -0
- data/hospitals.shp +0 -0
- data/hospitals.shx +0 -0
- data/police.dbf +0 -0
- data/police.prj +1 -0
- data/police.shp +0 -0
- data/police.shx +0 -0
- data/serp.dbf +0 -0
- data/serp.prj +1 -0
- data/serp.shp +0 -0
- data/serp.shx +0 -0
app.py
ADDED
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
|
6 |
+
import sys
|
7 |
+
|
8 |
+
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "SatSeg"))
|
9 |
+
from satseg.geo_tools import (
|
10 |
+
shapefile_to_latlong,
|
11 |
+
shapefile_to_grid_indices,
|
12 |
+
points_to_shapefile,
|
13 |
+
get_cached_grid_indices,
|
14 |
+
)
|
15 |
+
|
16 |
+
DATA_DIR = "data/"
|
17 |
+
MASK_PATH = os.path.join(DATA_DIR, "serp.shp")
|
18 |
+
HOSP_PATH = os.path.join(DATA_DIR, "hospitals.shp")
|
19 |
+
POLICE_PATH = os.path.join(DATA_DIR, "police.shp")
|
20 |
+
OUT_DIR = "out/"
|
21 |
+
|
22 |
+
|
23 |
+
def gr_generate_map(
|
24 |
+
token: str,
|
25 |
+
side_len: str,
|
26 |
+
show_grid: bool,
|
27 |
+
show_mask: bool,
|
28 |
+
show_hospitals: bool = True,
|
29 |
+
show_police: bool = True,
|
30 |
+
region: str = "None",
|
31 |
+
):
|
32 |
+
token = "pk.eyJ1IjoiZGlsaXRoIiwiYSI6ImNsaDQ3NXF3ZDAxdDMzZXMxeWJic2h1cDQifQ.DDczQCDfTgQEUt6pGvjUAg"
|
33 |
+
side_len = float(side_len)
|
34 |
+
|
35 |
+
scattermaps = []
|
36 |
+
if show_grid:
|
37 |
+
grid_path = MASK_PATH[: -len(".shp")] + f"-len={side_len}.shp"
|
38 |
+
if not os.path.exists(grid_path):
|
39 |
+
indices = shapefile_to_grid_indices(MASK_PATH, side_len)
|
40 |
+
points_to_shapefile(indices, grid_path)
|
41 |
+
else:
|
42 |
+
indices, _ = get_cached_grid_indices(grid_path)
|
43 |
+
box = go.Scattermapbox(
|
44 |
+
lat=indices[:, 1],
|
45 |
+
lon=indices[:, 0],
|
46 |
+
mode="markers",
|
47 |
+
marker=go.scattermapbox.Marker(size=6),
|
48 |
+
)
|
49 |
+
box.name = "Grids"
|
50 |
+
scattermaps.append(box)
|
51 |
+
if show_mask:
|
52 |
+
contours = shapefile_to_latlong(MASK_PATH)
|
53 |
+
for contour in contours:
|
54 |
+
lons = contour[:, 0]
|
55 |
+
lats = contour[:, 1]
|
56 |
+
scattermaps.append(
|
57 |
+
go.Scattermapbox(
|
58 |
+
fill="toself",
|
59 |
+
lat=lats,
|
60 |
+
lon=lons,
|
61 |
+
mode="markers",
|
62 |
+
marker=go.scattermapbox.Marker(size=6),
|
63 |
+
)
|
64 |
+
)
|
65 |
+
|
66 |
+
if show_hospitals:
|
67 |
+
indices, labels = get_cached_grid_indices(HOSP_PATH)
|
68 |
+
box = go.Scattermapbox(
|
69 |
+
lat=indices[:, 1],
|
70 |
+
lon=indices[:, 0],
|
71 |
+
mode="markers+text",
|
72 |
+
text=labels,
|
73 |
+
marker=go.scattermapbox.Marker(size=10),
|
74 |
+
)
|
75 |
+
box.name = "Hospitals"
|
76 |
+
box.textfont.update({"color": "White"})
|
77 |
+
scattermaps.append(box)
|
78 |
+
|
79 |
+
if show_police:
|
80 |
+
indices, labels = get_cached_grid_indices(POLICE_PATH)
|
81 |
+
box = go.Scattermapbox(
|
82 |
+
lat=indices[:, 1],
|
83 |
+
lon=indices[:, 0],
|
84 |
+
mode="markers+text",
|
85 |
+
text=labels,
|
86 |
+
marker=go.scattermapbox.Marker(size=10),
|
87 |
+
)
|
88 |
+
box.name = "Police Stations"
|
89 |
+
box.textfont.update({"color": "White"})
|
90 |
+
scattermaps.append(box)
|
91 |
+
|
92 |
+
fig = go.Figure(scattermaps)
|
93 |
+
|
94 |
+
center = (7.753769, 80.691730)
|
95 |
+
if region == "Ussangoda":
|
96 |
+
center = (6.0994295, 80.9860763)
|
97 |
+
elif region == "Indikolapelessa":
|
98 |
+
center = (6.3602253, 80.9371957)
|
99 |
+
elif region == "Ginigalpelessa":
|
100 |
+
center = (6.3846744, 80.8868755)
|
101 |
+
elif region == "Yudhaganawa":
|
102 |
+
center = (7.665643, 80.9529867)
|
103 |
+
if token:
|
104 |
+
fig.update_layout(
|
105 |
+
mapbox=dict(
|
106 |
+
style="satellite-streets",
|
107 |
+
accesstoken=token,
|
108 |
+
center=go.layout.mapbox.Center(lat=center[0], lon=center[1]),
|
109 |
+
pitch=0,
|
110 |
+
zoom=6 if region == "None" else 13,
|
111 |
+
),
|
112 |
+
mapbox_layers=[
|
113 |
+
{
|
114 |
+
# "below": "traces",
|
115 |
+
"sourcetype": "raster",
|
116 |
+
"sourceattribution": "United States Geological Survey",
|
117 |
+
"source": [
|
118 |
+
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
|
119 |
+
],
|
120 |
+
}
|
121 |
+
],
|
122 |
+
)
|
123 |
+
else:
|
124 |
+
fig.update_layout(
|
125 |
+
mapbox_style="open-street-map",
|
126 |
+
hovermode="closest",
|
127 |
+
mapbox=dict(
|
128 |
+
bearing=0,
|
129 |
+
center=go.layout.mapbox.Center(lat=center[0], lon=center[1]),
|
130 |
+
pitch=0,
|
131 |
+
zoom=6 if region == "None" else 13,
|
132 |
+
),
|
133 |
+
)
|
134 |
+
|
135 |
+
return fig
|
136 |
+
|
137 |
+
|
138 |
+
with gr.Blocks() as demo:
|
139 |
+
gr.Markdown("""# SatSeg""")
|
140 |
+
|
141 |
+
with gr.Tab("Sampling"):
|
142 |
+
grid_token = gr.Textbox(
|
143 |
+
value="", label="Mapbox Token (https://account.mapbox.com/)"
|
144 |
+
)
|
145 |
+
grid_side_len = gr.Textbox(value="100", label="Sampling Gap (m)")
|
146 |
+
|
147 |
+
grid_show_grid = gr.Checkbox(True, label="Show Grid")
|
148 |
+
grid_show_mask = gr.Checkbox(False, label="Show Mask")
|
149 |
+
grid_show_hosp = gr.Checkbox(True, label="Show Hospitals")
|
150 |
+
grid_show_police = gr.Checkbox(True, label="Show Police Stations")
|
151 |
+
|
152 |
+
grid_button = gr.Button("Generate Grid")
|
153 |
+
|
154 |
+
grid_map = gr.Plot(label="Plot")
|
155 |
+
|
156 |
+
grid_region = gr.Radio(
|
157 |
+
label="Zoom to Region",
|
158 |
+
choices=[
|
159 |
+
"None",
|
160 |
+
"Ussangoda",
|
161 |
+
"Indikolapelessa",
|
162 |
+
"Ginigalpelessa",
|
163 |
+
"Yudhaganawa",
|
164 |
+
],
|
165 |
+
)
|
166 |
+
|
167 |
+
grid_button.click(
|
168 |
+
gr_generate_map,
|
169 |
+
inputs=[grid_token, grid_side_len, grid_show_grid, grid_show_mask],
|
170 |
+
outputs=grid_map,
|
171 |
+
)
|
172 |
+
|
173 |
+
grid_region.change(
|
174 |
+
gr_generate_map,
|
175 |
+
inputs=[
|
176 |
+
grid_token,
|
177 |
+
grid_side_len,
|
178 |
+
grid_show_grid,
|
179 |
+
grid_show_mask,
|
180 |
+
grid_show_hosp,
|
181 |
+
grid_show_police,
|
182 |
+
grid_region,
|
183 |
+
],
|
184 |
+
outputs=grid_map,
|
185 |
+
)
|
186 |
+
|
187 |
+
demo.queue(concurrency_count=10).launch(debug=True, share=True)
|
data/hospitals.dbf
ADDED
Binary file (1.67 kB). View file
|
|
data/hospitals.prj
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
|
data/hospitals.shp
ADDED
Binary file (352 Bytes). View file
|
|
data/hospitals.shx
ADDED
Binary file (172 Bytes). View file
|
|
data/police.dbf
ADDED
Binary file (1.5 kB). View file
|
|
data/police.prj
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]
|
data/police.shp
ADDED
Binary file (324 Bytes). View file
|
|
data/police.shx
ADDED
Binary file (164 Bytes). View file
|
|
data/serp.dbf
ADDED
Binary file (2.19 kB). View file
|
|
data/serp.prj
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
PROJCS["WGS_1984_UTM_Zone_44N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",81.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
|
data/serp.shp
ADDED
Binary file (15.7 kB). View file
|
|
data/serp.shx
ADDED
Binary file (284 Bytes). View file
|
|