Merge pull request #3 from joshuasundance-swca/restgdf
Browse files- geospatial-data-converter/app.py +48 -6
- requirements.txt +2 -0
geospatial-data-converter/app.py
CHANGED
@@ -1,11 +1,28 @@
|
|
|
|
1 |
import os
|
2 |
|
|
|
3 |
import streamlit as st
|
|
|
|
|
4 |
|
5 |
from utils import read_file, convert, output_format_dict
|
6 |
|
7 |
__version__ = "0.0.3"
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# --- Initialization ---
|
10 |
st.set_page_config(
|
11 |
page_title=f"geospatial-data-converter v{__version__}",
|
@@ -13,7 +30,15 @@ st.set_page_config(
|
|
13 |
)
|
14 |
|
15 |
|
16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
st.file_uploader(
|
18 |
"Choose a geospatial file",
|
19 |
key="uploaded_file",
|
@@ -21,13 +46,30 @@ st.file_uploader(
|
|
21 |
)
|
22 |
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
os.path.basename(st.session_state.uploaded_file.name),
|
27 |
)
|
28 |
|
29 |
st.session_state.gdf = read_file(st.session_state.uploaded_file)
|
30 |
|
|
|
31 |
st.selectbox(
|
32 |
"Select output format",
|
33 |
output_format_dict.keys(),
|
@@ -37,8 +79,8 @@ if st.session_state.uploaded_file is not None:
|
|
37 |
|
38 |
if st.button("Convert"):
|
39 |
file_ext, dl_ext, mimetype = output_format_dict[st.session_state.output_format]
|
40 |
-
output_fn = f"{fn_without_extension}.{file_ext}"
|
41 |
-
dl_fn = f"{fn_without_extension}.{dl_ext}"
|
42 |
|
43 |
st.session_state.converted_data = convert(
|
44 |
gdf=st.session_state.gdf,
|
@@ -55,7 +97,7 @@ if st.session_state.uploaded_file is not None:
|
|
55 |
|
56 |
st.markdown(
|
57 |
"---\n"
|
58 |
-
f"## {fn_without_extension}\n"
|
59 |
f"### CRS: *{st.session_state.gdf.crs}*\n"
|
60 |
f"### Shape: *{st.session_state.gdf.shape}*\n"
|
61 |
"*(geometry omitted for display purposes)*",
|
|
|
1 |
+
import asyncio
|
2 |
import os
|
3 |
|
4 |
+
import geopandas as gpd
|
5 |
import streamlit as st
|
6 |
+
from aiohttp import ClientSession
|
7 |
+
from restgdf import Rest
|
8 |
|
9 |
from utils import read_file, convert, output_format_dict
|
10 |
|
11 |
__version__ = "0.0.3"
|
12 |
|
13 |
+
|
14 |
+
def st_init_null(*variable_names) -> None:
|
15 |
+
for variable_name in variable_names:
|
16 |
+
if variable_name not in st.session_state:
|
17 |
+
st.session_state[variable_name] = None
|
18 |
+
|
19 |
+
|
20 |
+
st_init_null(
|
21 |
+
"fn_without_extension",
|
22 |
+
"gdf",
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
# --- Initialization ---
|
27 |
st.set_page_config(
|
28 |
page_title=f"geospatial-data-converter v{__version__}",
|
|
|
30 |
)
|
31 |
|
32 |
|
33 |
+
# Enter a URL
|
34 |
+
st.text_input(
|
35 |
+
"Enter a URL to an ArcGIS featurelayer",
|
36 |
+
key="arcgis_url",
|
37 |
+
placeholder="https://maps1.vcgov.org/arcgis/rest/services/Beaches/MapServer/6",
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
# Or upload a file
|
42 |
st.file_uploader(
|
43 |
"Choose a geospatial file",
|
44 |
key="uploaded_file",
|
|
|
46 |
)
|
47 |
|
48 |
|
49 |
+
async def get_arcgis_data(url: str) -> tuple[str, gpd.GeoDataFrame]:
|
50 |
+
"""Get data from an ArcGIS featurelayer"""
|
51 |
+
async with ClientSession() as session:
|
52 |
+
rest = await Rest.from_url(url, session=session)
|
53 |
+
name = rest.name
|
54 |
+
gdf = await rest.getgdf()
|
55 |
+
return name, gdf
|
56 |
+
|
57 |
+
|
58 |
+
if st.session_state.arcgis_url:
|
59 |
+
st.session_state.fn_without_extension, gdf = asyncio.run(
|
60 |
+
get_arcgis_data(st.session_state.arcgis_url),
|
61 |
+
)
|
62 |
+
|
63 |
+
st.session_state.gdf = gdf
|
64 |
+
|
65 |
+
elif st.session_state.uploaded_file is not None:
|
66 |
+
st.session_state.fn_without_extension, _ = os.path.splitext(
|
67 |
os.path.basename(st.session_state.uploaded_file.name),
|
68 |
)
|
69 |
|
70 |
st.session_state.gdf = read_file(st.session_state.uploaded_file)
|
71 |
|
72 |
+
if st.session_state.gdf is not None:
|
73 |
st.selectbox(
|
74 |
"Select output format",
|
75 |
output_format_dict.keys(),
|
|
|
79 |
|
80 |
if st.button("Convert"):
|
81 |
file_ext, dl_ext, mimetype = output_format_dict[st.session_state.output_format]
|
82 |
+
output_fn = f"{st.session_state.fn_without_extension}.{file_ext}"
|
83 |
+
dl_fn = f"{st.session_state.fn_without_extension}.{dl_ext}"
|
84 |
|
85 |
st.session_state.converted_data = convert(
|
86 |
gdf=st.session_state.gdf,
|
|
|
97 |
|
98 |
st.markdown(
|
99 |
"---\n"
|
100 |
+
f"## {st.session_state.fn_without_extension}\n"
|
101 |
f"### CRS: *{st.session_state.gdf.crs}*\n"
|
102 |
f"### Shape: *{st.session_state.gdf.shape}*\n"
|
103 |
"*(geometry omitted for display purposes)*",
|
requirements.txt
CHANGED
@@ -1,5 +1,7 @@
|
|
|
|
1 |
beautifulsoup4==4.12.2
|
2 |
geopandas==0.14.0
|
3 |
lxml==4.9.3
|
4 |
pyogrio==0.6.0
|
|
|
5 |
streamlit==1.27.2
|
|
|
1 |
+
aiohttp==3.8.6
|
2 |
beautifulsoup4==4.12.2
|
3 |
geopandas==0.14.0
|
4 |
lxml==4.9.3
|
5 |
pyogrio==0.6.0
|
6 |
+
restgdf==0.0.13
|
7 |
streamlit==1.27.2
|