2

I have more than 200 layers in my PostGIS database and I want to publish those layers in GeoServer using curl. I've imported one layer using the command below but can't figure out how to upload all layers in one step.

curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<featureType><name>testlayer</name></featureType>" http://localhost:8088/geoserver/rest/workspaces/myworkspace/datastores/mystore/featuretypes
5
  • 3
    Use a loop and pass the layer names? Commented Feb 18, 2019 at 10:19
  • I am not familiar with curl, could you describe me how? Commented Feb 18, 2019 at 10:31
  • 1
    I meant, use some script to execute the command in a loop. Something like this stackoverflow.com/a/32791680/5355375 Commented Feb 18, 2019 at 10:43
  • 1
    Are you willing to use any other tool or language, for instance Python? Or are you looking for a pure command line-based approach?
    – wfgeo
    Commented Feb 18, 2019 at 10:51
  • I've tried to use gsonfig tool github.com/boundlessgeo/gsconfig#installing but couldn't install it Commented Feb 18, 2019 at 10:55

2 Answers 2

5

Example of a simple bash script to create Geoserver layers for all tables of a given Postgresql schema.

The workspace and the store should already exist in Geoserver. In this example I use the workspace name = database name; the store name = schema name; postgresql server = geoserver server. That is why my URL is: http://$pghost/geoserver/rest/workspaces/$pgdb/datastores/$pgschema/featuretypes

#!/bin/bash

pghost='server.com'
pgport=5432
pguser='geobox'
pgpass='******'
pgschema='sigpdm_2015'
pgdb='pdm2017'

table_names=`PGPASSWORD=$pgpass psql -U $pguser -h $pghost -p $pgport -d $pgdb -t -c "SELECT * FROM (select table_name as t from information_schema.tables where table_schema = '$pgschema') as tnames"`

while read -a table_name ; do
    curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<featureType><name>$table_name</name><nativeCRS>EPSG:3763</nativeCRS><srs>EPSG:3763</srs><nativeBoundingBox><minx>-33313.0</minx><maxx>-13047.0</maxx><miny>198924.0</miny><maxy>216948.0</maxy><crs>EPSG:3763</crs></nativeBoundingBox></featureType>" http://$pghost/geoserver/rest/workspaces/$pgdb/datastores/$pgschema/featuretypes
done <<< "$table_names"

The <featureType> could be more simpler, but I usually run into problems when layers get new features outside the initial bounding box. So I prefer to create the layers with the larger possible bounding box. I also added the local CRS, instead of a global one.

0

You can use any programming language eg: Java, Phython etc. Where you can run your command in for loop and change parameter 'name' with your layer name in each iteration.

1
  • I wanted to do it in a cmd but I managed to do it like u said, Thanks Commented Feb 18, 2019 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.