Connor Sutton commited on
Commit
c1ea3ba
1 Parent(s): 51f15f6

added topojson conversion option

Browse files
geospatial-data-converter/app.py CHANGED
@@ -68,8 +68,6 @@ elif st.session_state.uploaded_file is not None:
68
  os.path.basename(st.session_state.uploaded_file.name),
69
  )
70
  st.session_state.gdf = read_file(st.session_state.uploaded_file)
71
- # except AttributeError:
72
- # pass # there is a lingering file from the previous instance, but it can be ignored
73
 
74
  if st.session_state.gdf is not None:
75
  st.selectbox(
 
68
  os.path.basename(st.session_state.uploaded_file.name),
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(
geospatial-data-converter/utils.py CHANGED
@@ -1,18 +1,20 @@
1
  import io
2
  import os
3
  import zipfile
4
- from tempfile import TemporaryDirectory
5
- from typing import BinaryIO
6
  import geopandas as gpd
 
7
 
 
 
8
  from kml_tricks import load_ge_data
9
 
10
  output_format_dict = {
11
- "ESRI Shapefile": ("shp", "zip", "application/zip"), # must be zipped
12
- "OpenFileGDB": ("gdb", "zip", "application/zip"), # must be zipped
13
- "GeoJSON": ("geojson", "geojson", "application/geo+json"),
14
  "CSV": ("csv", "csv", "text/csv"),
15
  "KML": ("kml", "kml", "application/vnd.google-earth.kml+xml"),
 
 
 
 
16
  }
17
 
18
 
@@ -34,9 +36,6 @@ def read_file(file: BinaryIO, *args, **kwargs) -> gpd.GeoDataFrame:
34
  elif ext in ("kml", "kmz"):
35
  with TemporaryDirectory() as tmp_dir:
36
  tmp_file_path = os.path.join(tmp_dir, f"{basename}.{ext}")
37
- print(file.name)
38
- print(basename)
39
- print(tmp_file_path)
40
  with open(tmp_file_path, "wb") as tmp_file:
41
  tmp_file.write(file.read())
42
  return load_ge_data(tmp_file_path)
@@ -65,6 +64,9 @@ def convert(gdf: gpd.GeoDataFrame, output_name: str, output_format: str) -> byte
65
  out_path = os.path.join(tmpdir, output_name)
66
  if output_format == "CSV":
67
  gdf.to_csv(out_path)
 
 
 
68
  else:
69
  gdf.to_file(out_path, driver=output_format, engine="pyogrio")
70
 
 
1
  import io
2
  import os
3
  import zipfile
 
 
4
  import geopandas as gpd
5
+ import topojson
6
 
7
+ from tempfile import TemporaryDirectory
8
+ from typing import BinaryIO
9
  from kml_tricks import load_ge_data
10
 
11
  output_format_dict = {
 
 
 
12
  "CSV": ("csv", "csv", "text/csv"),
13
  "KML": ("kml", "kml", "application/vnd.google-earth.kml+xml"),
14
+ "GeoJSON": ("geojson", "geojson", "application/geo+json"),
15
+ "TopoJSON": ("topojson", "topojson", "application/json"),
16
+ "ESRI Shapefile": ("shp", "zip", "application/zip"), # must be zipped
17
+ "OpenFileGDB": ("gdb", "zip", "application/zip"), # must be zipped
18
  }
19
 
20
 
 
36
  elif ext in ("kml", "kmz"):
37
  with TemporaryDirectory() as tmp_dir:
38
  tmp_file_path = os.path.join(tmp_dir, f"{basename}.{ext}")
 
 
 
39
  with open(tmp_file_path, "wb") as tmp_file:
40
  tmp_file.write(file.read())
41
  return load_ge_data(tmp_file_path)
 
64
  out_path = os.path.join(tmpdir, output_name)
65
  if output_format == "CSV":
66
  gdf.to_csv(out_path)
67
+ elif output_format == "TopoJSON":
68
+ topojson_data = topojson.Topology(gdf)
69
+ topojson_data.to_json(out_path)
70
  else:
71
  gdf.to_file(out_path, driver=output_format, engine="pyogrio")
72
 
requirements.txt CHANGED
@@ -5,3 +5,4 @@ lxml==4.9.3
5
  pyogrio==0.7.2
6
  restgdf==0.9.7
7
  streamlit==1.29.0
 
 
5
  pyogrio==0.7.2
6
  restgdf==0.9.7
7
  streamlit==1.29.0
8
+ topojson
tests/test_conversions.py CHANGED
@@ -1,26 +1,19 @@
1
  import os
2
  import pytest
3
- from utils import convert, read_file
4
 
5
  input_exts = ["kml", "kmz", "geojson", "zip"]
6
- output_exts = ["KML", "ESRI Shapefile", "GeoJSON", "CSV", "OpenFileGDB"]
7
- output_format_dict = {
8
- "ESRI Shapefile": "shp",
9
- "GeoJSON": "geojson",
10
- "CSV": "csv",
11
- "KML": "kml",
12
- "OpenFileGDB": "gdb",
13
- }
14
 
15
 
16
  @pytest.mark.parametrize("in_ext", input_exts)
17
  @pytest.mark.parametrize("out_ext", output_exts)
18
- def test_kml_coversion(in_ext: str, out_ext: str) -> None:
19
  test_file = f"test.{in_ext}"
20
  test_file_path = os.path.join(os.getcwd(), "tests", "test_data", test_file)
21
  with open(test_file_path, "rb") as f:
22
  kml = read_file(f)
23
- out_file = f"test.{output_format_dict[out_ext]}"
24
  converted_data = convert(kml, out_file, out_ext)
25
  with open("test.kml", "wb") as f:
26
  f.write(converted_data)
 
1
  import os
2
  import pytest
3
+ from utils import convert, read_file, output_format_dict
4
 
5
  input_exts = ["kml", "kmz", "geojson", "zip"]
6
+ output_exts = output_format_dict.keys()
 
 
 
 
 
 
 
7
 
8
 
9
  @pytest.mark.parametrize("in_ext", input_exts)
10
  @pytest.mark.parametrize("out_ext", output_exts)
11
+ def test_coversion(in_ext: str, out_ext: str) -> None:
12
  test_file = f"test.{in_ext}"
13
  test_file_path = os.path.join(os.getcwd(), "tests", "test_data", test_file)
14
  with open(test_file_path, "rb") as f:
15
  kml = read_file(f)
16
+ out_file = f"test.{output_format_dict[out_ext][0]}"
17
  converted_data = convert(kml, out_file, out_ext)
18
  with open("test.kml", "wb") as f:
19
  f.write(converted_data)