通过JS前端调用Geoserver的wfs接口新增修改删除空间表feature

admin通过JS前端调用Geoserver的wfs接口新增修改删除空间表feature已关闭评论条评论 3,321 次浏览

借助github的js插件 geojson-to-wfs-t-2

安装方式 npm install geojson-to-wfs-t-2

使用方式

import wfs from 'geojson-to-wfs-t-2';

const nullIsland = {
  type: 'Feature',
  properties: {place_name: 'null island'},
  geometry: {
    type: 'Point',
    coordinates: [0, 0]
  }
  id: 'feature_id'
}
const params = {geometry_name: 'geom', layer: 'my_lyr', ns: 'my_namespace'};

// create a stringified transaction inserting null island
wfs.Transaction(
  wfs.Insert(nullIsland, params),
  {
    nsAssignments: {
      my_namespace: 'https://example.com/namespace_defn.xsd'
    }
  }
);

// create a stringified transaction updating null island's name
wfs.Transaction(
  wfs.Update({properties: {place_name: 'not Atlantis'}, id: nullIsland.id }),
  {nsAssignments: ...}
)
// same deal, but deleting it
wfs.Transaction(
  wfs.Delete({id: nullIsland.id}, params),
  {nsAssignments: ...}
)

一.wfs-t删除功能

通过wfs-t插件 将json 转成xml


转换的xml如下

<wfs:Transaction
    xmlns:fes=”http://www.opengis.net/fes/2.0″
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
    xmlns:gml=”http://www.opengis.net/gml/3.2″
    xmlns:wfs=”http://www.opengis.net/wfs/2.0″ xsi:schemaLocation=”http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd” service=”WFS” version=”2.0.0″>
    <wfs:Delete typeName=”gtp”>
        <fes:Filter>
            <fes:ResourceId rid=”gtp.72″/>
        </fes:Filter>
    </wfs:Delete>
</wfs:Transaction>

使用ajax、fetch、axios、postman请求接口

记得加入鉴权

将转换的xml代入参数 调接口成功

可以数据库中看到数据已被成功删除

二.wfs-t修改功能 (测试不可修改坐标,仅可修改属性,可批量修改)

转换的xml如下

<wfs:Transaction
xmlns:fes=”http://www.opengis.net/fes/2.0″
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:gml=”http://www.opengis.net/gml/3.2″
xmlns:wfs=”http://www.opengis.net/wfs/2.0″ xsi:schemaLocation=”http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd” service=”WFS” version=”2.0.0″>
<wfs:Update typeName=”gtp”>
<wfs:Property>
<wfs:ValueReference>offset_x</wfs:ValueReference>
<wfs:Value>111</wfs:Value>
</wfs:Property>
<wfs:Property>
<wfs:ValueReference>gtp_id</wfs:ValueReference>
<wfs:Value>dddd</wfs:Value>
</wfs:Property>
<fes:Filter>
<fes:ResourceId rid=”gtp.67″/>
</fes:Filter>
</wfs:Update>
</wfs:Transaction>

xml 放入参数 请求接口

三.wfs-t新增功能

转换的xml如下

<wfs:Transaction
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:gml=”http://www.opengis.net/gml/3.2″
xmlns:wfs=”http://www.opengis.net/wfs/2.0″ xsi:schemaLocation=”http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd” service=”WFS” version=”2.0.0″>
<wfs:Insert>
<gtp gml:id=”gtp.”>
<geom>
<gml:Point>
<gml:pos>100 20 30</gml:pos>
</gml:Point>
</geom>
<offset_x>222</offset_x>
<gtp_id>3333xxx</gtp_id>
</gtp>
</wfs:Insert>
</wfs:Transaction>

四.wfs-t替换功能 (修改单个点属性和坐标)

转换的xml如下

<wfs:Transaction
xmlns:fes=”http://www.opengis.net/fes/2.0″
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xmlns:gml=”http://www.opengis.net/gml/3.2″
xmlns:wfs=”http://www.opengis.net/wfs/2.0″ xsi:schemaLocation=”http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd” service=”WFS” version=”2.0.0″>
<wfs:Replace>
<gtp gml:id=”gtp.67″>
<geom>
<gml:Point>
<gml:pos>120 40 60</gml:pos>
</gml:Point>
</geom>
<offset_x>1114551</offset_x>
<project_id>665541</project_id>
<gtp_id>iamxiyou</gtp_id>
</gtp>
<fes:Filter>
<fes:ResourceId rid=”gtp.67″/>
</fes:Filter>
</wfs:Replace>
</wfs:Transaction>


分类目录