如何以编程方式更新 WFS 地理服务器层中的数据

How to programmatically update data in WFS geoserver layer

我正在构建一个应用程序,用户可以在其中检索地理服务器层(商店:postgres)的所有功能并将它们显示在 table 上。为此,我使用 OWSLib (get_feature).

现在我需要添加编辑数据(WFS-T)的功能。据我所知,OWSLib 不提供 add/update 功能。

实现这种功能的方法是什么?

按照建议,我使用 python 请求库来实现 WFS-T 并在图层上更新值:

这是我的部分代码:

      import requests
      url = 'http://localhost:8080/geoserver/wfs'


      xml = """<wfs:Transaction service="WFS" version="1.0.0"
      xmlns:ogc="http://www.opengis.net/ogc"
  xmlns:wfs="http://www.opengis.net/wfs">
  <wfs:Update typeName="geonode:tjk_nhr_shockriskscore">
    <wfs:Property>
      <wfs:Name>Adm2_NAME</wfs:Name>
      <wfs:Value>test_2dsfdsfsdfdsfds</wfs:Value>
    </wfs:Property>
    <ogc:Filter>
      <ogc:FeatureId fid="tjk_nhr_shockriskscore.1"/>
    </ogc:Filter>
  </wfs:Update>
</wfs:Transaction>"""
      headers = {'Content-Type': 'application/xml'} # set what your server accepts
      print requests.post(url, data=xml, headers=headers).text

当我 运行 这个 xml 通过地理服务器演示页面时,它工作正常。层的 属性 得到更新。 当我通过我的 python 脚本执行它时,我得到一个服务异常:

   <?xml version="1.0" ?>
   <ServiceExceptionReport
     version="1.2.0"
     xmlns="http://www.opengis.net/ogc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.opengis.net/ogc http://schemas.opengis.net/wfs/1.0.0/OGC-exception.xsd">
  <ServiceException>
   {http://www.geonode.org/}tjk_nhr_shockriskscore is read-only
  </ServiceException></ServiceExceptionReport>

错误消息(通常)在这里实际上很有用 - 如果层是只读的,您不能 运行 对其进行更新。那么问题就变成了为什么该层是只读的?最可能的原因(特别是如果交易在演示页面中有效)是您的 python 脚本没有通过服务器进行身份验证。从 this page 看来您需要添加:

auth=("admin","geoserver") 

根据您的要求(假设您没有更改默认密码)。