337 lines
14 KiB
Markdown
337 lines
14 KiB
Markdown
---
|
||
title: WFS简述
|
||
date: 2020-03-28
|
||
author: ac
|
||
tags:
|
||
- WFS
|
||
categories:
|
||
---
|
||
|
||
## 1.WFS(Web Feature Service)
|
||
|
||
[Web Feature Service](http://www.opengeospatial.org/standards/wfs) (WFS),支持对地理要素的插入、更新、删除、检索和发现服务。该服务根据HTTP客户请求返回GML(Geography Markup Language、地理标识语言)数据;也可以通过OGC Filter构造查询条件,并支持基于空间几何关系的查询和基于属性的查询以及基于空间关系和属性域的共同查询。
|
||
|
||
WFS标准定义了一些操作,这些操作允许用户在分布式的环境下通过HTTP对空间数据进行查询、编辑等操作。
|
||
|
||
WFS服务要求服务的接口必须由XML描述,另外数据交互必须由GML进行,数据过滤采用CQL语言。
|
||
|
||
https://docs.geoserver.org/latest/en/user/filter/index.html#filtering
|
||
https://docs.geoserver.org/latest/en/user/filter/ecql_reference.html
|
||
|
||
`GeoServer`对WFS服务的提供了1.0.0,1.1.0和2.0.0三个版本的支持。
|
||
|
||
**版本的差异**:
|
||
|
||
1. WFS 2.0.0请求以默认格式返回GML 3.2,
|
||
|
||
WFS 1.1.0请求以默认格式返回GML 3,
|
||
|
||
WFS 1.0.0请求以默认格式返回GML 2。
|
||
|
||
2. GeoServer 对于每种支持的GML格式,可以选择不同的SRS格式。
|
||
|
||
3. WFS 1.1.0和2.0.0支持动态重新投影数据,这支持在本机SRS以外的SRS中返回数据。
|
||
|
||
4. WFS 2.0.0引入了新版本的过滤器编码规范,增加了对时间过滤器的支持。
|
||
|
||
5. WFS 2.0.0支持通过GetFeature请求进行联接。
|
||
|
||
6. WFS 2.0.0增加了通过startIndex和count参数分页GetFeature请求的结果的功能。GeoServer现在在WFS 1.0.0和1.1.0中支持此功能。
|
||
|
||
7. WFS 2.0.0支持存储的查询,这是存储在服务器上的常规WFS查询,因此可以通过将适当的标识符与WFS请求一起传递来调用它们。
|
||
|
||
8. WFS 2.0.0支持SOAP(简单对象访问协议)作为OGC接口的替代方法。
|
||
|
||
这里需要特别注意上面的第二点:
|
||
|
||
SRS Style:对返回数据的轴顺序有影响,对几何字段的坐标也会有影响。
|
||
EPSG Code:以格式EPSG:XXXX(例如EPSG:4326)返回典型的EPSG号。以经度/纬度(x / y)的顺序格式化地理坐标。
|
||
|
||
> - WFS 1.0.0 ==> GML 2 ==> OGC HTTP URL ("http://www.opengis.net/gml/srs/epsg.xml#4326")
|
||
>
|
||
> 以经度/纬度(x / y)的顺序格式化地理坐标
|
||
>
|
||
> - WFS 1.1.0 ==> GML 3 ==> OGC Experimental URN(urn:x-ogc:def:crs:EPSG:4326)
|
||
>
|
||
> 以地理和制图系统的传统轴顺序(即纬度/经度(y / x))格式化数据。
|
||
>
|
||
> - WFS 1.1.1,WFS 2.0.0 ==> GML 3.2 ==> OGC URN (urn:ogc:def:crs:EPSG::4326)
|
||
|
||
|
||
|
||
**在GeoServer中,将WFS服务划分为三个级别**:
|
||
|
||
1. Basic WFS:就是最常被提及的WFS,支持GetCapabilities、 DescribeFeatureType、GetFeature操作,在功能上意味着是一个只读的数据服务;
|
||
3. Transaction WFS:也有称为WFS-T,必须在Basic WFS基础上加上Transaction操作以支持编辑数据(包含创建、删除、更新等操作);
|
||
3. Complete WFS: 支持包括对事务级别操作的LockFeature接口。LockFeature操作通过处理一个或多个功能类型实例上的锁定请求来帮助解决相关资源之间的链接。
|
||
|
||
在服务的"发布"tab页中的WFS设置中可以设置:
|
||
|
||
<img src="./images/services_WFS_servicelevel.png" style="zoom: 80%;" />
|
||
|
||
## 2 WFS Opration
|
||
|
||
下图是WFS服务提供的接口UML图:
|
||
|
||

|
||
|
||
|
||
|
||
| 接口 | 操作类型 | 说明 |
|
||
| --------------------- | --------------- | -------------------------------------------------------- |
|
||
| GetCapabilities | discovery | 获取该WFS服务支持的操作和服务的元数据文档 |
|
||
| DescribeFeatureType | discovery | 获取WFS服务支持的功能类型的描述(图层属性字段等) |
|
||
| GetPropertyValue | query | 根据要素属性值或部分属性值进行查询,返回符合表达式的要素 |
|
||
| GetFeature | query | 从数据源中,获取指定图层符合条件的数据要素集 |
|
||
| GetFeatureWithLock | query & locking | 返回并锁定指定图层的符合要求的要素,默认锁300s |
|
||
| LockFeature | locking | 对指定的要素进行锁定,以保证一致性 |
|
||
| Transaction | transaction | 对要素服务中的数据要素进行增删改的操作 |
|
||
| CreateStoredQuery | stored query | 在服务端创建一个存储查询表达式 |
|
||
| DropStoredQuery | stored query | 在服务端删除一个存储查询表达式 |
|
||
| ListStoredQueries | stored query | 获取服务器上的存储查询变量列表 |
|
||
| DescribeStoredQueries | stored query | 获取服务器上的每个存储查询表达式的详细元数据。 |
|
||
|
||
### 2.1 GetFeature
|
||
|
||
> getFeature操作有可以使用CQL filters(cql_filter参数)来指定ECQL(Extended Common Query Language)格式的过滤器,通过过滤器筛选符合条件的要素集。这里只是简单描述getFeature的请求流程。
|
||
|
||
GetFeature操作返回的是选择的数据源信息和要素集。所以请求是针对特定的图层名称来执行GetFeature请求(namespace:featuretype)的。
|
||
|
||
**请求示例**
|
||
|
||
`http://localhost:8080/geoserver/wfs?request=GetFeature&version=1.1.0&typeName=topp:states&outputFormat=GML2&FILTER=%3CFilter%20xmlns=%22http://www.opengis.net/ogc%22%3E%3CFeatureId%20fid=%22states.23%22/%3E%3C/Filter%3E`
|
||
|
||

|
||
|
||
| 参数 | 是否必须 | 说明 |
|
||
| ------------------ | -------- | ------------------------------------------------------- |
|
||
| version | 是 | 服务版本,默认1.1.0 |
|
||
| service | 是 | WFS |
|
||
| request=GetFeature | 是 | 请求接口 |
|
||
| typename | 是 | 指定操作的图层名称(命名空间+图层名) |
|
||
| outputformat | | 输出的文本格式(GML2、GML3、JSON、CSV、JSONP、Shapefile) |
|
||
| bbox | | 边界 |
|
||
| filter | | 查询条件,xml文本(CQL/ECQL) |
|
||
| sortby | | 排序,1.1.0及以上版本才有效 |
|
||
| maxfeatures | | 返回要素集的最大记录数 |
|
||
| propertyname | | |
|
||
| srsname | | 返回要素的空间参考,如:EPSG:4326 |
|
||
| faetureid | | 要素id |
|
||
|
||
**请求流程:**
|
||
当一个客户端想要访问WFS服务时,一般会涉及到以下的流程:
|
||
|
||
1. 通过操作获取WFS服务支持的操作和要素类(FeatureType,可以理解为WFS中的数据集)
|
||
2. (可能)通过操作获取WFS服务支持的要素类的定义。
|
||
3. 客户端发送某个操作的请求。
|
||
4. WFS服务处理请求。
|
||
5. WFS服务返回处理的结果和状态。
|
||
|
||
上面几个步骤中所提到的“操作”包括:
|
||
|
||
1. GetCapabilities(获取服务中的要素类及支持的操作)
|
||
2. DescribeFeatureType(描述要素类的信息)
|
||
3. GetFeature(获取要素)或 GetGmlObject(通过XLink获取GML对象)
|
||
4. Transaction(创建、更新、删除数据的事务操作)
|
||
5. LockFeature(在事务过程中锁定要素)
|
||
|
||
示例:
|
||
|
||
根据要素表的数据做属性查询:
|
||
|
||
`http://127.0.0.1:8080/geoserver/workspace/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=rural_100000:town&maxFeatures=999&outputFormat=application/json&FILTER=(%3CFilter%20xmlns=%22http://www.opengis.net/ogc%22%3E%3CPropertyIsEqualTo%3E%3CPropertyName%3Eowner_coun%3C/PropertyName%3E%3CLiteral%3E441521%3C/Literal%3E%3C/PropertyIsEqualTo%3E%3C/Filter%3E)`
|
||
|
||
获取某个bbox范围内的要素shp数据:
|
||
|
||
`http://127.0.0.1:8080/geoserver/workspace/ows?service=WFS&version=1.1.0&request=GetFeature&typename=rural_330000:risk_census_house_yhpc_upload_330100&outputFormat=application/json&srsname=EPSG:4490&bbox=120.03720402718305,30.060657262809276,120.05367636679888,30.071550607674123,EPSG:4490`
|
||
|
||
### 2.2 GetCapabilities
|
||
|
||
**操作示例**
|
||
KVP格式:http://localhost:8080/geoserver/wfs?service=wfs&version=1.1.0&request=GetCapabilities
|
||
XML格式的POST等效请求:
|
||
|
||
```xml
|
||
<?xml version="1.0" ?>
|
||
<GetCapabilities
|
||
service="WFS"
|
||
xmlns="http://www.opengis.net/wfs"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://www.opengis.net/wfs
|
||
http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"/>
|
||
```
|
||
|
||
**参数列表**
|
||
|
||
| param | o/m |
|
||
| ------- | ------------- |
|
||
| service | must(值为WFS) |
|
||
| version | must |
|
||
| request | must |
|
||
|
||
> 注意:version必须完成的版本号
|
||
|
||
**响应示例**
|
||
|
||

|
||
|
||
|
||
|
||
### 2.3 **DescribeFeatureType:**
|
||
|
||
DescribeFeatureType:返回所指定的WFS服务支持的要素类型的描述(图层属性字段等)
|
||
|
||
**操作示例**
|
||
KVP格式:http://localhost:8080/geoserver/wfs?service=wfs&version=2.0.0&request=DescribeFeatureType&typeNames=topp:states
|
||
|
||
XML格式等效的POST请求:
|
||
|
||
```xml
|
||
<?xml version="1.0" ?>
|
||
<DescribeFeatureType
|
||
service="WFS"
|
||
version="2.0.2"
|
||
xmlns="http://www.opengis.net/wfs/2.0"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd">
|
||
<TypeName>topp:states</TypeName>
|
||
</DescribeFeatureType>
|
||
```
|
||
|
||
**参数列表**
|
||
|
||
| param | o/m |
|
||
| ------------- | ------ |
|
||
| service | must |
|
||
| version | must |
|
||
| request | must |
|
||
| typeNames | must |
|
||
| exceptions | option |
|
||
| outputFormate | option |
|
||
|
||
> typeNames指定所要描述的要素服务(typeName对于WFS 1.1.0和更早版本);
|
||
> 报告异常的格式-默认值为 application/vnd.ogc.se_xml
|
||
|
||
**响应示例**
|
||
|
||

|
||
|
||
|
||
|
||
### 2.4 Transaction
|
||
|
||
在OWS规范中知道,Web服务体系规范的HTTP接口参数有XML和KVP两种形式,但WFS要求服务的`Transaction`接口必须由XML描述,另外空间数据交互必须由GML进行,数据过滤采用CQL语言。
|
||
|
||
每个事务将由零个或多个插入、更新和删除元素组成,每个事务元素按顺序执行。
|
||
|
||
> 每个GeoServer事务都是原子的,这意味着如果任何元素失败,则事务将被放弃,并且数据将保持不变。
|
||
> 支持事务的 WFS服务器有时也称为WFS-T服务器。
|
||
|
||
1. Insert
|
||
|
||
```xml
|
||
<?xml version="1.0"?>
|
||
<wfs:Transaction
|
||
version="1.1.0"
|
||
service="WFS"
|
||
xmlns="http://www.someserver.com/myns"
|
||
xmlns:gml="http://www.opengis.net/gml"
|
||
xmlns:ogc="http://www.opengis.net/ogc"
|
||
xmlns:test="http://localhost:8080/test"
|
||
xmlns:wfs="http://www.opengis.net/wfs"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://www.someserver.com/myns
|
||
http://www.someserver.com/wfs/cwwfs.cgi?
|
||
http://www.opengis.net/wfs ../wfs/1.1.0/WFS.xsd">
|
||
<wfs:Insert inputFormat="text/xml;subtype=2.1.2" idgen="UseExisting">
|
||
<test:tiger_roads gml:id="10002">
|
||
<cfcc>A94</cfcc>
|
||
<name>测试</name>
|
||
<geom>
|
||
<MultiLineString srsName="EPSG:3857">
|
||
<lineStringMember>
|
||
<LineString>
|
||
<coordinates>-8237795.685217111,4980323.905513588 -8238827.585098961,4978584.963120101</coordinates>
|
||
</LineString>
|
||
</lineStringMember>
|
||
</MultiLineString>
|
||
</geom>
|
||
</test:tiger_roads>
|
||
</wfs:Insert>
|
||
</wfs:Transaction>
|
||
```
|
||
|
||
2. update
|
||
|
||
```xml
|
||
<?xml version="1.0"?>
|
||
<wfs:Transaction
|
||
version="1.1.0"
|
||
service="WFS"
|
||
xmlns="http://www.someserver.com/myns"
|
||
xmlns:gml="http://www.opengis.net/gml"
|
||
xmlns:ogc="http://www.opengis.net/ogc"
|
||
xmlns:test="http://localhost:8080/test"
|
||
xmlns:wfs="http://www.opengis.net/wfs"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://www.someserver.com/myns
|
||
http://www.someserver.com/wfs/cwwfs.cgi?
|
||
http://www.opengis.net/wfs ../wfs/1.1.0/WFS.xsd">
|
||
<wfs:Update typeName="tiger_roads">
|
||
<wfs:Property>
|
||
<wfs:Name>name</wfs:Name>
|
||
<wfs:Value>测试Insert312</wfs:Value>
|
||
</wfs:Property>
|
||
<ogc:Filter>
|
||
<ogc:GmlObjectId gml:id="tiger_roads.8338"/>
|
||
</ogc:Filter>
|
||
</wfs:Update>
|
||
</wfs:Transaction>
|
||
```
|
||
|
||
3. delete
|
||
|
||
```xml
|
||
<?xml version="1.0" ?>
|
||
<wfs:Transaction
|
||
version="1.1.0"
|
||
service="WFS"
|
||
xmlns="http://www.someserver.com/myns"
|
||
xmlns:ogc="http://www.opengis.net/ogc"
|
||
xmlns:wfs="http://www.opengis.net/wfs"
|
||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||
xsi:schemaLocation="http://www.opengis.net/wfs ../wfs/1.1.0/WFS.xsd">
|
||
<wfs:Delete typeName="tiger_roads">
|
||
<ogc:Filter>
|
||
<ogc:GmlObjectId gml:id="tiger_roads.8337"/>
|
||
</ogc:Filter>
|
||
</wfs:Delete>
|
||
</wfs:Transaction>
|
||
```
|
||
|
||
|
||
## 参考文章
|
||
|
||
[1] WFS Settings https://docs.geoserver.org/latest/en/user/services/wfs/webadmin.html#features
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
微服务
|
||
|
||
spring boot + Nacos Spring Cloud + redis + feign
|
||
|
||
MyBatis(ORM)
|
||
|
||
clickhouse(业务库) + pg(要素库)
|
||
|
||
|
||
|
||
ArcGIS
|
||
|
||
|
||
|