Publish PostGIS Layers in GeoServer Automatically using cURL and REST

Disclaimer: The idea came from this blog.

So, what's the difference? I'm using Windows.
 cURL should be installed already.

If you have lots of tables in your database that you wanted to publish in GeoServer, you can use this method. Just prepare a list of tables (separated by new line) you want to publish in a text file format.

Before proceeding in publishing layers, you need to setup a workspace and store in your GeoServer first. Although this can be done using cURL and REST API, but it is more convenient in that way.

After a minute.... :D :D

Create ".bat" file

Use the code below but (of course) change the necessary inputs

for /f %%a in (<list of tables you want to publish>.txt) do  curl -v -u <username>:<password>  -XPOST -H "Content-type: text/xml" -d "<featureType><name>%%a</name></featureType>" http://localhost:8080/geoserver/rest/workspaces/<name of workspace>/datastores/<name of store>/featuretypes
pause


e.g.

for /f %%a in (tables.txt) do  curl -v -u admin:floodmaps  -XPOST -H "Content-type: text/xml" -d "<featureType><name>%%a</name></featureType>" http://localhost:8082/geoserver/rest/workspaces/earthws/datastores/earthds/featuretypes
pause


Here are some few useful commands:

--adding workspace
curl -u admin:floodmaps -v -XPOST -H "Content-type: text/xml" -d "<workspace><name>cite</name></workspace>" http://localhost:8081/geoserver/rest/workspaces

--adding store
curl -v -u admin:floodmaps -XPOST -T ds.xml -H "Content-type: text/xml" http://localhost:8081/geoserver/rest/workspaces/earthws/datastores

--publish layer
curl -v -u admin:floodmaps  -XPOST -H "Content-type: text/xml" -d "<featureType><name>tandag_stats_seniang</name></featureType>" http://localhost:8081/geoserver/rest/workspaces/earthws/datastores/earthds/featuretypes
--change defaultStyle
curl -u admin:floodmaps -v  -XPUT -H "Content-type: text/xml" -d "<layer><defaultStyle><workspace>cite</workspace><name>flood_hazard_level</name></defaultStyle><enabled>true</enabled><advertised>false</advertised></layer>" http://localhost:8081/geoserver/rest/layers/earthws:tandag_stats_5yr

With this simple method, you won't be worrying on how to publish bunch of layers.

You can also automate styling, use this code below and save it as ".bat" file, change the necessary inputs (bold).

for /f %%a in (tables.txt) do  curl -u admin:floodmaps -v  -XPUT -H "Content-type: text/xml" -d "<layer><defaultStyle><workspace>cite</workspace><name>polygon</name></defaultStyle><enabled>true</enabled></layer>" http://localhost:8081/geoserver/rest/layers/earthws:%%a
pause


Sample Files are here.

Popular posts from this blog

Set table width to 100% on PDF export in Datatables

Add/Subtract Single Digit Number in Assembly Language