openlayers4,geoserver wfs实现空间分析、属性查询
原理
- 工作区启用WFS
- 工作区下发布图层
- 代码
var featureRequest = new ol.format.WFS().writeGetFeature({
srsName: 'EPSG:4326',//坐标系统
featureNS: 'zjk',//命名空间 URI
featurePrefix: 'sf',//工作区名称
featureTypes: '',//查询图层,可以同一个工作区下多个图层,逗号隔开
outputFormat: 'application/json',
filter: ol.format.filter.intersects(//查询过滤条件
'the_geom',//
geometry//
});
//http请求数据
fetch(geoserverUrl + 'wfs', {//geoserver wfs地址如localhost:8080/geoserver/wfs
method: 'POST',
body: new XMLSerializer().serializeToString(featureRequest)
}).then(function (response) {
return response.json();
}).then(function (json) {
//查询结果
})
- 查询过滤条件openlayers api
根据查询的空间关系选择对应filter,传相应参数即可