From d130197525ae6b311297d846b3023e6968ff4174 Mon Sep 17 00:00:00 2001 From: romer8 Date: Sun, 9 Jun 2024 20:14:09 -0600 Subject: [PATCH] one file for the cli --- cli/convert_geom.py | 45 ++++++++++++++++++++++++++++++++++++++++++++- cli/utils.py | 45 --------------------------------------------- 2 files changed, 44 insertions(+), 46 deletions(-) delete mode 100644 cli/utils.py diff --git a/cli/convert_geom.py b/cli/convert_geom.py index 5cdb109..8524c55 100644 --- a/cli/convert_geom.py +++ b/cli/convert_geom.py @@ -1,6 +1,49 @@ import argparse from geo.Geoserver import Geoserver -from .utils import _read_and_transform, _create_and_publish_shp +import geopandas as gpd +import zipfile +import os + + +def _read_and_transform(gpkg_path, layer_name): + gdf = gpd.read_file(gpkg_path, layer=layer_name) + if gdf.crs.to_string() != "EPSG:5070": + gdf = gdf.to_crs(epsg=5070) + return gdf + + +def _create_and_publish_shp( + gdf, + shp_path, + geoserver_host, + geoserver_port, + geoserver_username, + geoserver_password, +): + gdf.to_file(f"{shp_path}.shp", driver="ESRI Shapefile") + shp_files = [f"{shp_path}.{ext}" for ext in ["shp", "shx", "dbf", "prj"]] + + with zipfile.ZipFile(f"{shp_path}.zip", "w") as zipf: + for file in shp_files: + zipf.write(file, arcname=os.path.basename(file)) + + # remove shapefiles + for file in shp_files: + os.remove(file) + + geo = Geoserver( + f"http://{geoserver_host}:{geoserver_port}/geoserver", + username=geoserver_username, + password=geoserver_password, + ) + + geo.create_shp_datastore( + path=f"{shp_path}.zip", + store_name="hydrofabrics", + workspace="nextgen", + ) + # remove zip file + os.remove(f"{shp_path}.zip") def create_workspace( diff --git a/cli/utils.py b/cli/utils.py deleted file mode 100644 index 0fd1182..0000000 --- a/cli/utils.py +++ /dev/null @@ -1,45 +0,0 @@ -import geopandas as gpd -from geo.Geoserver import Geoserver -import zipfile -import os - - -def _read_and_transform(gpkg_path, layer_name): - gdf = gpd.read_file(gpkg_path, layer=layer_name) - if gdf.crs.to_string() != "EPSG:5070": - gdf = gdf.to_crs(epsg=5070) - return gdf - - -def _create_and_publish_shp( - gdf, - shp_path, - geoserver_host, - geoserver_port, - geoserver_username, - geoserver_password, -): - gdf.to_file(f"{shp_path}.shp", driver="ESRI Shapefile") - shp_files = [f"{shp_path}.{ext}" for ext in ["shp", "shx", "dbf", "prj"]] - - with zipfile.ZipFile(f"{shp_path}.zip", "w") as zipf: - for file in shp_files: - zipf.write(file, arcname=os.path.basename(file)) - - # remove shapefiles - for file in shp_files: - os.remove(file) - - geo = Geoserver( - f"http://{geoserver_host}:{geoserver_port}/geoserver", - username=geoserver_username, - password=geoserver_password, - ) - - geo.create_shp_datastore( - path=f"{shp_path}.zip", - store_name="hydrofabrics", - workspace="nextgen", - ) - # remove zip file - os.remove(f"{shp_path}.zip")