gicait / geoserver-rest

Python library for management of geospatial data in GeoServer.
https://geoserver-rest.readthedocs.io
MIT License
191 stars 77 forks source link

manual operation bottleneck in uploading .shp files into the database #156

Open tommasosansone91 opened 1 month ago

tommasosansone91 commented 1 month ago

as far as I can understand, geoserver-rest gives us a simple way to upload raster files in geoserver :

geo.create_coveragestore(
    layer_name='my-raster1', 
    path=r'data/raster/raster1.tif', 
    workspace='demo')

with this command, we indicate the file path of the raster we want to load, then we decide its layer name and the target workspace, and the file is uploaded in that workspace, with that layer name.

but when it comes to upload shape files in geoserver, it is more difficult:

we need 2 commands, one to create and one to publish the shape file

geo.create_featurestore(
    store_name='postgis', 
    workspace='demo', 
    db='postgres', 
    host='localhost', 
    pg_user='postgres', 
    pg_password='password')

geo.publish_featurestore(
    workspace='demo', 
    store_name='postgis', 
    pg_table='jamoat-db')

but before of that, we must manually load the .shp file into the database by using the wizard tool of postgis in postgres UI client (pgadmin).

this is a bottleneck, because it means that this process cannot be entirely automatized via python.

Would it be possible to extend the geoserver-rest library so that this step (manually load the .shp file into the database) is also managed via python?

I am willing to give my contribute to extend geoserver-rest and go past this issue.

iboates commented 1 month ago

If I am understanding correctly, you want to start with a shapefile, then load it into a PostGIS database, and then publish the table in the database as a layer?

Loading a shapefile into a PostGIS database requires that you use a database connector, e.g. sqlalchemy. Geoserver-rest is an API client for interaction with Geoserver, so in my opinion, doing database manipulations is not within the scope of the library.

If you want to just upload and publish a shapefile directly, you should be able to do it with .create_shp_datastore

If you absolutely want to go from shapefile to PostGIS table first, then I would recommend using geopandas to load the shapfile into a GeoDataFrame, then loading it into PostGIS and using .publish_featurestore as normal. This guide explains how to open and load a shapefile with geopandas: https://hatarilabs.com/ih-en/how-to-upload-shapefiles-to-postgis-with-python-geopandas-and-sqlalchemy-tutorial