一直以来使用ol 就避免不了使用geoserver,然而在一次使用wfs 进行geometry过滤是遇到了许多奇怪的问题,而因经验不足,找寻不到解决方法,甚是苦恼;在多方搜寻和理解情况下找到了一些解决方法,故再次记录,便于今后查询
找寻对应的属性值
一、命名空间,工作区名称
工作区间> 对应的应用点开
二、开启wfs
三、需要查询的图层
四、配置测试方法
有了上面查询的geoserver 相关属性,下面就可以开始准备代码了
查询的代码
import geoWFS from 'ol/format/WFS';
import {
intersects as geoIntersects
} from 'ol/format/filter';
import Point from 'ol/geom/Point';
/**
* 根据geoserver wfs进行分析
* @param {*} computeGeometry 需要分析的geometry
*/
spanceAnalysisInGeoserverWfs(computeGeometry) {
const newgeo = new Point([98.9427776, 31.84491791]) // computeGeometry ;
let featureRequest = new geoWFS().writeGetFeature({
//坐标系统
srsName: 'EPSG:4326',
//命名空间
featureNS: 'http://app',
//工作区名称
featurePrefix: 'app',
//查询图层,这里多个用逗号隔开,如'sjhb_glfwm','sjhb2_glfwm'
featureTypes: ['sjhb_glfwm'],
outputFormat: 'application/json',
filter: geoIntersects('the_geom', newgeo, 'EPSG:4326')
});
//WFS服务地址
fetch('http://192.168.10.88:8080/geoserver/wfs', {
method: 'POST',
body: new XMLSerializer().serializeToString(featureRequest),
}).then(function (response) {
return response.json();
}).then(function (json) {
console.log(json.features);
// let feasarr =[];
// for(let i=0;i<json.totalFeatures;i++){
// let features=new ol.format.GeoJSON().readFeature(JSON.stringify(json.features[i]));
// feasarr.push(features);
// }
// VectorLayer.addFeatures(feasarr);
// map.removeInteraction(draw);
// console.log(VectorLayer.getFeatures())
}).catch(err => {
console.log(err)
})
}
异常一:
<ows:Exception exceptionCode="InvalidParameterValue">
<ows:ExceptionText>Illegal property name: the_geom for feature type app:sjhb_glfwm</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
异常二:
<ows:Exception exceptionCode="InvalidParameterValue">
<ows:ExceptionText>org.geotools.referencing.operation.projection.PointOutsideEnvelopeException: 98.9427776 outside of (-90.0,90.0)
Parsing failed for Point: org.geoserver.wfs.WFSException: org.geotools.referencing.operation.projection.PointOutsideEnvelopeException: 98.9427776 outside of (-90.0,90.0)
org.geotools.referencing.operation.projection.PointOutsideEnvelopeException: 98.9427776 outside of (-90.0,90.0)
98.9427776 outside of (-90.0,90.0)</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
当一运行,就会遇到如上两个错误,第二个错误一看大概知道是因为EPSG不匹配,于是去geoserver查看图层对应的EPSG
打开geoserver 找到图层
于是将上面的代码中的
filter: geoIntersects('the_geom', newgeo, 'EPSG:4326')
修改为
filter: geoIntersects('the_geom', newgeo, 'EPSG:4490')
【注意】这里的EPSG:4490需要自定义否则会报错,这里就不赘述了,需要了解的可以参考openlayers自定义投影坐标系(EPSG:4542),GeoJSON读取geometry和坐标转换
五、geometry_name 异常
现在提示的是异常一了,这又是咋回事???
多方查证基本确定是Intersects 中的geometryName 错误引起的
看ol官网
geometryName string Geometry name to use.
geometry name 这个是什么?我原本以为是对应的图层名称,于是将代码
filter: geoIntersects('the_geom', newgeo, 'EPSG:4490')
修改为
filter: geoIntersects('sjhb_glfwm', newgeo, 'EPSG:4490')
提示
<ows:Exception exceptionCode="InvalidParameterValue">
<ows:ExceptionText>Illegal property name: sjhb_glfwm for feature type app:sjhb_glfwm</ows:ExceptionText>
</ows:Exception>
</ows:ExceptionReport>
看样子不是哦,于是不停的找寻这个geometry name在哪里?
痛苦的是你知道问题,但是不知道如何描述,网络搜索完全无果!!!
六、初见端倪
在经过几天的纠结后,没有找到结果,甚是郁闷,今天在一个哥们的博客看见了希望
但是我要到哪里去找这个wfs 服务呢,GeoJson 下的geometry_name又在哪里呢?
于是我跑到安装geoserver 的目录下翻找了半天无果!!!
想来想去最后在这个篇知乎 04-GeoServer WFS服务发布及参数设置中突然明白了,原来他一致在身边
看这个图。。。
于是赶紧到geoserver中找寻。。。。
原来真在这里。
于是一点击。。。。
打开页面后一大片的坐标点,在最后找到了想要的结果
于是将代码
filter: geoIntersects('sjhb_glfwm', newgeo, 'EPSG:4490')
修改为
filter: geoIntersects('wkb_geometry', newgeo, 'EPSG:4490')
再一运行,哇塞!
终于成功了。
最后修改的完整代码
import geoWFS from 'ol/format/WFS';
import {
intersects as geoIntersects
} from 'ol/format/filter';
import Point from 'ol/geom/Point';
/**
* 根据geoserver wfs进行分析
* @param {*} computeGeometry 需要分析的geometry
*/
spanceAnalysisInGeoserverWfs(computeGeometry) {
const newgeo = new Point([98.9427776, 31.84491791]) // computeGeometry ;
let featureRequest = new geoWFS().writeGetFeature({
//坐标系统
srsName: 'EPSG:4326',
//命名空间
featureNS: 'http://app',
//工作区名称
featurePrefix: 'app',
//查询图层,这里多个用逗号隔开,如'sjhb_glfwm','sjhb2_glfwm'
featureTypes: ['sjhb_glfwm'],
outputFormat: 'application/json',
filter: geoIntersects('wkb_geometry', newgeo, 'EPSG:4490')
});
//WFS服务地址
fetch('http://192.168.10.88:8080/geoserver/wfs', {
method: 'POST',
body: new XMLSerializer().serializeToString(featureRequest),
}).then(function (response) {
return response.json();
}).then(function (json) {
console.log(json.features);
}).catch(err => {
console.log(err)
})
}
好了这次wfs 坑已填满,虽然最后intersects并不太理想,但是能查询到就很不错了
这个也许对于熟悉的人来说很简单的问题,但如我这种菜鸟还是蛮大难度的,这次记录到此为止谢谢阅读。