Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 582
Nurzhan Nogerbek Avatar asked Dec 18 '25 16:12

Nurzhan Nogerbek


2 Answers

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
like image 182
Ian Turton Avatar answered Dec 20 '25 07:12

Ian Turton


optional. Publishing a table from an existing PostGIS store. on php curl.

function curl_post($url,$params) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD,"user:password");//--> on geoserver.
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Receive server response ...


$response = curl_exec($ch);

curl_close ($ch);
return $response;
}

 //- table_name      : xxx 
 //- work_space_name : your_workspace 
 //- store_name    : dbtest


$layer_name = "xxx";

$params ="
    <featureType>
      <name>".$layer_name."</name>
      
      <nativeName>".$layer_name."</nativeName>
      
      <title>".$layer_name."</title>
      
      <keywords>
        <string>".$layer_name."</string>
        <string>features</string>
      </keywords>
      
      <srs>EPSG:3857</srs>
      
      <nativeBoundingBox>
        <minx>1.0836244E7</minx>
        <maxx>1.1759454E7</maxx>
        <miny>625842.375</miny>
        <maxy>2328151.75</maxy>
      </nativeBoundingBox>
      
      <latLonBoundingBox>
        <minx>97.34363607648459</minx>
        <maxx>105.63697261100442</maxx>
        <miny>5.613037739416236</miny>
        <maxy>20.464604971116074</maxy>
        
      </latLonBoundingBox>
      
      <projectionPolicy>FORCE_DECLARED</projectionPolicy>
      <enabled>true</enabled>
      <metadata>
        <entry key=\"cachingEnabled\">false</entry>
      </metadata>
      <maxFeatures>0</maxFeatures>
      <numDecimals>0</numDecimals>
    </featureType>
";

$str_url = "http://xxx.co.uk/geoserver/rest/workspaces/**your_workspace**/datastores/**your_data_store**/featuretypes";

$rs = curl_post($str_url, $params);
like image 21
Ruthe Avatar answered Dec 20 '25 08:12

Ruthe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!