如何通过REST API在GeoServer中创建WMS图层?

3
PostgreSQL 中,我有一个名为 geo 的模式。在该模式中,我有一个具有 geometry 数据类型的列的表。
我是 GeoServer 的新手,想知道如何使用该远程 PostgreSQL 数据库的数据通过 REST API 创建 WMS 图层?根据 文档,我需要先创建工作区和数据存储,是吗?我有点困惑。应该采取什么样的行动顺序?我将非常感激任何示例!
curl 请求的结果: enter image description here
2个回答

4
REST API与GUI的工作方式完全相同,因此您可以选择创建一个新的工作区或使用现有工作区,然后在工作区内创建一个存储库,并从中创建图层。任何图层都将自动作为WMS图层可用。
请参考以下步骤创建新的数据库存储库并生成连接详细信息文件:
1. 创建一个新的PostGIS存储库, 生成连接详细信息文件: 添加一个PostGIS数据库存储库
      <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>

并将其POST到REST端点

curl -v -u admin:geoserver -XPOST -T <file.xml> -H "Content-type: text/xml"
    http://localhost:8080/geoserver/rest/workspaces/<WORKSPACENAME>/datastores
  1. 然后将该表作为图层发布
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

抱歉回复晚了。就像我之前说过的那样,我已经创建了workspace(georeports)和datastore(subscribers)。我尝试测试您第二个curl命令,但不幸的是它出现了错误。服务器返回400 Bad Request并返回这样的消息:Trying to create new feature type inside the store, but no attributes were specified。看起来我漏掉了一些属性。你有什么想法吗?另外,请再次检查我的POST请求,谢谢。我添加了包含信息的图片。 - Nurzhan Nogerbek
据我理解,您在<featureType><name>buildings</name></featureType>中设置的“建筑物”是PostgreSQL数据库中表的名称,对吗?我已经更改了名称。在我的情况下,它称为“layers”。在这种情况下,curl请求返回“500内部服务器错误”,并显示以下消息:“存储库中已存在名为'layers'的资源:'subscribers'”。您有任何想法吗? - Nurzhan Nogerbek
我还注意到在GUI中,当我尝试创建图层时,它会要求设置属性,如srsnativeBoundingBoxlatLonBoundingBox - Nurzhan Nogerbek
我的下一个 GET 请求 http://localhost:8080/geoserver/rest/layers 返回所有图层的列表。我在列表中找到了我的图层。然后,我使用 DELETE 请求删除了我的图层。之后,我再次检查了图层列表。我的图层已经从列表中消失了。然后我再次发起 curl 请求,但是它报出了和上一条评论相同的错误:Resource named 'layers' already exists in store: 'subscribers'。这太奇怪了。 - Nurzhan Nogerbek
好了,终于我找到了我需要进行两个DELETE请求来正确删除图层的方法。之后你就可以毫无问题地执行第二个curl了。 - Nurzhan Nogerbek
显示剩余2条评论

1
从现有的PostGIS存储库中发布表格。使用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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接