当前位置: 代码迷 >> 综合 >> Cesium 范围Bound获取(多边形wkt、xys、当前范围)
  详细解决方案

Cesium 范围Bound获取(多边形wkt、xys、当前范围)

热度:20   发布时间:2023-12-17 03:37:52.0
  • WKT

  WKT转XY点数组

 /*** 将wkt转换为 X,Y,X,Y结构的数组* @param {string} wkt 多边形的wkt* @returns*/WktToXY(wkt) {const coors = [];const xyzs = wkt.substring(wkt.indexOf("((") + 2, wkt.indexOf("))"));xyzs.split(",").forEach((e) => {const xyz = e.split(" ");coors.push(parseFloat(xyz[0]));coors.push(parseFloat(xyz[1]));});return coors;}

获取范围

 GetWktsBound(wkts) {let xmin = 999;let ymin = 999;let xmax = -999;let ymax = -999;wkts.forEach((wkt) => {let ps = TransformUnit.WktToXY(wkt);for (let i = 0; i < ps.length / 2; i++) {let x = ps[i * 2];let y = ps[i * 2 + 1];if (x < xmin) xmin = x;if (y < ymin) ymin = y;if (x > xmax) xmax = x;if (y > ymax) ymax = y;}});if (xmin == 999 || ymin == 999 || xmax == -999 || ymax == -999) {return null;}let dx = xmax - xmin;let dy = ymax - ymin;let scale = 0.6;let bound = [xmin - dy * scale,ymin - dx * scale,xmax + dx * scale,ymax + dy * scale,];return bound;}
  • XYS
 GetXYSBound(xys) {let xmin = 999;let ymin = 999;let xmax = -999;let ymax = -999;xys.forEach((xy) => {let x = xy[0];let y = xy[1];if (x < xmin) xmin = x;if (y < ymin) ymin = y;if (x > xmax) xmax = x;if (y > ymax) ymax = y;});if (xmin == 999 || ymin == 999 || xmax == -999 || ymax == -999) {return null;}let dx = xmax - xmin;let dy = ymax - ymin;let scale = 0.6;let bound = [xmin - dy * scale,ymin - dx * scale,xmax + dx * scale,ymax + dy * scale,];return bound;}
  • 获取当前视角范围

通过面板的左上角和右下角坐标获取得到

  /*** 获取当前视角范围* @returns 返回  [xmin,xmax,ymin,ymax]*/GetCurrentExtent() {let mapdiv = window.viewer.canvas;var pt1 = new Cesium.Cartesian2(0, 0);var pt2 = new Cesium.Cartesian2(mapdiv.width, mapdiv.height);var pick1 = viewer.scene.globe.pick(viewer.camera.getPickRay(pt1),viewer.scene,);var pick2 = viewer.scene.globe.pick(viewer.camera.getPickRay(pt2),viewer.scene,);//将三维坐标转成地理坐标var geoPt1 = viewer.scene.globe.ellipsoid.cartesianToCartographic(pick1);var geoPt2 = viewer.scene.globe.ellipsoid.cartesianToCartographic(pick2);//地理坐标转换为经纬度坐标var point1 = [(geoPt1.longitude / Math.PI) * 180,(geoPt1.latitude / Math.PI) * 180,];var point2 = [(geoPt2.longitude / Math.PI) * 180,(geoPt2.latitude / Math.PI) * 180,];let xmin = point1[0] > point2[0] ? point2[0] : point1[0];let xmax = point1[0] > point2[0] ? point1[0] : point2[0];let ymin = point1[1] > point2[1] ? point2[1] : point1[1];let ymax = point1[1] > point2[1] ? point1[1] : point2[1];return [xmin, xmax, ymin, ymax];}

  相关解决方案