当前位置:   article > 正文

vue+openlayers 点击地图查询geoserver发布的WMS/WMTS图层的信息[矢量(点、线、面)+栅格]_geoserver的wmts如何查看

geoserver的wmts如何查看

一、基本介绍

网络地图服务 (WMS) 、网络地图切片服务 (WMTS) ,根据用户的请求返回相应的地图(包括PNG,GIF,JPEG等栅格形式或者是SVG和WEB CGM等矢量形式)。

下面是常见的geoserver发布的图层格式,有矢量图层(点、线、面),或栅格图层(具体概念不再赘述)

不论是WMS或者是WMTS,大部分请求回的是图片格式,所以我们没有办法直接获取到当前点击点的信息,但实际工作中,我们经常要查看某一点的具体信息,比如下图,我们想要查看影像图的像元信息 

二、查询

1、矢量图层查询

核心一:借助WFS点与面的相交,获取信息

点/线图层----核心是围绕当前点构造缓冲区与判断是否与图层点相交

围绕点击点构造缓冲区 geom

  1. let polygonGeom = null
  2. let pixel = this.map.getPixelFromCoordinate(mapEvt.coordinate);//获取点击点的x,y坐标
  3. let bufPixel = 10; //缓冲距离
  4. let p1 = [pixel[0] - bufPixel, pixel[1] - bufPixel];
  5. let p2 = [pixel[0] + bufPixel, pixel[1] - bufPixel];
  6. let p3 = [pixel[0] + bufPixel, pixel[1] + bufPixel];
  7. let p4 = [pixel[0] - bufPixel, pixel[1] + bufPixel];
  8. polygonGeom = new Polygon([([
  9. [
  10. this.map.getCoordinateFromPixel(p1),
  11. this.map.getCoordinateFromPixel(p2),
  12. this.map.getCoordinateFromPixel(p3),
  13. this.map.getCoordinateFromPixel(p4),
  14. this.map.getCoordinateFromPixel(p1),
  15. ],
  16. ])]); //构造缓冲区

面图层-----核心就是判断点击点与图层面是否相交

点击点的geom

let pointGeom = new Point(mapEvt.coordinate);

核心二、借助ECQL中的相交函数INTERSECTS

1、先将geom转为wkt格式

  1. //如果是点/线 图层就是
  2. let wkt = new WKT().writeGeometry(polygonGeom) //构造的缓冲区
  3. // 如果是面图层就是
  4. let wkt = new WKT().writeGeometry(pointGeom)

2、利用INTERSECTS 构造cql_filter 

  1. //INTERSECTS用法,第一个字段是数据库表中的空间字段,第二个就是一个wkt字符串
  2. INTERSECTS(the_geom'', wkt)

 核心三、发送wfs请求

下面是封装好的wfs请求函数

  1. /**
  2. 接收一个对象,对象里面的参数支持
  3. url(必传)
  4. layername (必传) bincheng:gis_xjqy
  5. cql(非必传)
  6. propertyname:需要返回的字段,参数是以逗号分割的字符串(默认返回全部)
  7. **/
  8. export function wfsQuery(obj = {}) {
  9. const { url, layername, cql, propertyname,srsName='EPSG:4326' } = obj
  10. return new Promise((resolve, reject) => {
  11. let featureRequest = {
  12. service: "WFS",
  13. version: "1.0.0",
  14. request: "GetFeature",
  15. srsName,
  16. typename: layername,
  17. outputFormat: "application/json",
  18. };
  19. if (cql) featureRequest.CQL_FILTER = cql
  20. if (propertyname) featureRequest.PROPERTYNAME = propertyname
  21. let fetchUrl = ''
  22. fetchUrl = `${url}${url.indexOf("?") > -1 ? '&' : '?'}${formateObjToParamStr(featureRequest)}`
  23. fetch(fetchUrl, {
  24. method: 'GET',
  25. headers: new Headers({
  26. 'Accept': 'application/json;charset=utf-8',
  27. 'Content-Type': 'application/x-www-form-urlencoded'
  28. }),
  29. mode: "cors"
  30. })
  31. .then(function(response) {
  32. return response.json();
  33. }).then(json => {
  34. resolve(json)
  35. }).catch((err) => {
  36. reject(err)
  37. })
  38. })
  39. }
  40. export function formateObjToParamStr(paramObj) {
  41. const params = new URLSearchParams();
  42. for (let attr in paramObj) {
  43. params.append(attr, paramObj[attr]);
  44. }
  45. return params.toString();
  46. }

使用

  1. wfsQuery({
  2. layername:'test:cjqy',
  3. url:`http://localhost:8010/geoserver/wfs`,
  4. cql:`INTERSECTS(the_geom'',${wkt})`
  5. }).then(res => {
  6. console.log(res) //查询结果
  7. })

2、栅格图层查询

栅格图层查像元信息,主要借助的是getFeatureInfoUrl(该方法针对wms图层,如果是wmts/gwc(瓦片)图层没有该方法,我之前是借助wms图层实现的)

调用代码

  1. var viewResolution = this.map.getView().getResolution();
  2. let url = this.layer.getSource().getFeatureInfoUrl(e.coordinate, viewResolution, this.projection || "EPSG:4326", {
  3. INFO_FORMAT: "application/json",
  4. })
  5. if(url){
  6. fetch(url).then(function (res) {
  7. return res.text(); //返回Promise
  8. }).then((data) => {
  9. let objData = JSON.parse(data);
  10. // let info = objData.features[0].properties.GRAY_INDEX;
  11. console.log(objData)
  12. });
  13. }

三、查询示例(封装矢量/栅格--查询)

  1. //wfsQuery函数,上面已经有了,这里不再重复写了
  2. /*
  3. coordinate:坐标点
  4. wfsUrl:wfs地址
  5. srs: 投影坐标系
  6. type类型,接收(Point/PolyLine/Polygon/tiff)
  7. shape(string|null): 空间字段,如果是矢量图层,则需要传,默认the_geom, 栅格图层传null即可
  8. layername: 图层
  9. */
  10. queryclickinfo({ coordinate, type, wfsUrl, shape, layername, srs }) {
  11. return new Promise((resolve, reject) => {
  12. {
  13. if (type == "tiff") {
  14. var viewResolution = this.map.getView().getResolution();
  15. let url = this.layer
  16. .getSource()
  17. .getFeatureInfoUrl(
  18. e.coordinate,
  19. viewResolution,
  20. srs || "EPSG:4326",
  21. {
  22. INFO_FORMAT: "application/json",
  23. }
  24. );
  25. if (url) {
  26. fetch(url)
  27. .then(function (res) {
  28. return res.text(); //返回Promise
  29. })
  30. .then((data) => {
  31. let objData = JSON.parse(data);
  32. // let info = objData.features[0].properties.GRAY_INDEX;
  33. resolve(objData);
  34. })
  35. .catch((err) => reject(err));
  36. }
  37. }
  38. let geom, wkt;
  39. if (type == "Point" || type == "PolyLine") {
  40. let pixel = this.map.getPixelFromCoordinate(coordinate); //获取点击点的x,y坐标
  41. let bufPixel = 10; //缓冲距离
  42. let p1 = [pixel[0] - bufPixel, pixel[1] - bufPixel];
  43. let p2 = [pixel[0] + bufPixel, pixel[1] - bufPixel];
  44. let p3 = [pixel[0] + bufPixel, pixel[1] + bufPixel];
  45. let p4 = [pixel[0] - bufPixel, pixel[1] + bufPixel];
  46. geom = new Polygon([
  47. [
  48. [
  49. this.map.getCoordinateFromPixel(p1),
  50. this.map.getCoordinateFromPixel(p2),
  51. this.map.getCoordinateFromPixel(p3),
  52. this.map.getCoordinateFromPixel(p4),
  53. this.map.getCoordinateFromPixel(p1),
  54. ],
  55. ],
  56. ]); //构造缓冲区
  57. }
  58. if (type == "Polygon") {
  59. geom = new Point(coordinate);
  60. }
  61. if (geom) wkt = new WKT().writeGeometry(geom);
  62. if (wkt) {
  63. this.wfsQuery({
  64. layername,
  65. url: wfsUrl,
  66. cql: `INTERSECTS(${shape},${wkt})`,
  67. })
  68. .then((res) => {
  69. resolve(res);
  70. })
  71. .catch((err) => reject(err));
  72. }
  73. }
  74. });
  75. }

返回结果如下 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/492632
推荐阅读
  

闽ICP备14008679号