Search code examples
postgresqlrestgeoserver

How create WMS layer in GeoServer by REST API?


In PostgreSQL I have a scheme called geo. Inside that scheme, I have a table with a column which has a geometry data type.

I am new in GeoServer and want to know how to create WMS layer by REST API with data of that remote PostgreSQL database? According to the documentation I need to create workspace and datastore first, right? I'm a little confused. What sequence of actions should be? I will be grateful for any example!

Result of curl request: enter image description here


Solution

  • The REST API works in exactly the same manner as the GUI, so the process is that you can optionally create a new workspace or use an existing one, you then create a store inside a workspace and then create layers from the store. Any layer will automatically become available as a WMS layer.

    1. Create a new PostGIS store, generate a file with the connection details:
          <dataStore>
            <name>nyc</name>
            <connectionParameters>
              <host>localhost</host>
              <port>5432</port>
              <database>nyc</database>
              <user>bob</user>
              <passwd>postgres</passwd>
              <dbtype>postgis</dbtype>
            </connectionParameters>
          </dataStore>
    

    and POST it to the REST endpoint

    curl -v -u admin:geoserver -XPOST -T <file.xml> -H "Content-type: text/xml"
        http://localhost:8080/geoserver/rest/workspaces/<WORKSPACENAME>/datastores
    
    1. Then publish the table as a layer
    curl -v -u admin:geoserver -XPOST -H "Content-type: text/xml" -d "<featureType><name>buildings</name></featureType>" http://localhost:8080/geoserver/rest/workspaces/acme/datastores/nyc/featuretypes