[GIS] Update programmatically BBOX of WMS Geoserver layer

bboxgeoservergeoserver-rest-api

I create programmatically a layer in PostGIS and Geoserver. I give the option to the user to choose the geometry and the attributes and then I create the table in PostGIS and add it in an existing store in Geoserver.

The layer, as initialized, it gets a global Bounding Box.
Then the user is able to add specific data. What I want is to be able to recalculate the Bounding Box depending on the data, so when I show the map on Leaflet to show only the selected area.
I need to do that programmatically using the Rest API.

I haven't found anything like this for single layers (not layergroups) and also for WMS layers.

Does anyone has any experience on it?

Best Answer

You need to PUT an updated FeatureType document with the new bounding box set in it.

So your workflow is something like:

 curl -v -u admin:geoserver -H 'Accept: text/xml' \
   -XGET http://localhost:8080/geoserver/rest/workspaces/topp/datastores/states_shapefile/featuretypes/states.xml

Then edit the Bounding box to your new value and delete the lat/lon bounding box and PUT it back:

 curl -v -u admin:geoserver -H 'Accept: text/xml' -H 'Content-type: text/xml' \
   -XPUT http://localhost:8080/geoserver/rest/workspaces/topp/datastores/states_shapefile/featuretypes/states.xml \
   -d @states.xml 

GeoServer will automatically recalculate the lat/lon bounding box for you. There is also a recalculate parameter you can use to force the recalculation of the bounding boxes if you prefer but this may be much slower if you have a large table.