【问题标题】:How to programmatically update data in WFS geoserver layer如何以编程方式更新 WFS 地理服务器层中的数据
【发布时间】:2016-07-13 18:01:23
【问题描述】:

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

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

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

按照建议,我使用 python requests lib 来实现 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

当我通过 geoserver 演示页面运行这个 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>

【问题讨论】:

  • 啊,太好了。没找到那个链接。尽管看起来他们的道路并不顺利,但它有所帮助,对吧?
  • 这些只是具有明确标准的 HTTP 发布请求
  • 是的,当然。但我的意思是我需要创建一个函数来构建请求消息,包括所有要更新的属性。这并不难,但我很惊讶现有的库都没有提供任何东西!
  • 您可以使用 owslib 获取所有属性然后构造 HTTP 请求

标签: python geoserver


【解决方案1】:

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

auth=("admin","geoserver") 

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

【讨论】:

  • 非常感谢!就是这样。这需要很多时间才能发现!
猜你喜欢
  • 1970-01-01
  • 2019-11-23
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多