WFS 服务是OGC服务之一,可以通过 WFS 获取要素信息,ArcGIS Server 跟 GeoServer 都支持。下面且看如何请求WFS。
const crossIds = ['8316','8318'];
const ACC_ROAD_WFS_NAME = 'test:road';
const wfsUrl = `${GEOSERVER_URL}/wfs?service=wfs&version=1.0.0&request=getfeature&typename=${ACC_ROAD_WFS_NAME}&PROPERTYNAME=name,fid,geom&CQL_FILTER=fid in (${crossIds.join(',')})&outputformat=application/json`;
fetch(wfsUrl,{
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(response => response.text())
.then((text) => {
if (typeof text === 'string' && text !== '') {
try {
return JSON.parse(text);
} catch (e) { return null; }
}
return null;
}).then((json) => {
console.log('object data', json);
});
不追究代码写得怎么样,重点在于如何组织 WFS URL
- request: 这里为
getfeature
,其他请求类型,点击官网文档 - typename: 发布的图层名
- PROPERTYNAME: 需要返回的属性,如果不加则默认全部属性返回,多个属性英文逗号隔开,另外,需要geometry,则需加上
geom
(因为数据连的是postgreSQL,默认几何属性为geom) - CQL_FILTER: 查询条件,可参考官网文档
- outputformat: 输出格式,支持shp,json,csv等,详情点击官网文档
支持格式(资料来自官网)
Format | Syntax | Notes |
---|---|---|
GML2 | outputFormat=GML2 | Default option for WFS 1.0.0 |
GML3 | outputFormat=GML3 | Default option for WFS 1.1.0 and 2.0.0 |
Shapefile | outputFormat=shape-zip | ZIP archive will be generated containing the shapefile (see Shapefile output below). |
JSON | outputFormat=application/json | Returns a GeoJSON or a JSON output. Note outputFormat=json is only supported for getFeature (for backward compatibility). |
JSONP | outputFormat=text/javascript | Returns a JSONP in the form: parseResponse(…json…). See WMS vendor parameters to change the callback name. Note that this format is disabled by default (See Global variables affecting WMS). |
CSV | outputFormat=csv | Returns a CSV (comma-separated values) file |
注意:如果请求失败,会返回 XML 的错误说明信息,所以如果要求json,则需要判断结果是否为json。
待研究:如何将编辑后的数据通过WFS塞回数据库