Cesium坐标转换相关代码
//Cesium坐标转换相关 苹果园dog add export var cesiumCoordUtil = { degreeToCartesian3: function (lon, lat, alt) { //经纬度转换为世界坐标 var ellipsoid = viewer.scene.globe.ellipsoid; var cartographic = Cesium.Cartographic.fromDegrees(lon, lat, alt); var cartesian3 = ellipsoid.cartographicToCartesian(cartographic); return cartesian3; }, cartesian3ToCartesian2: function (scene, catesian3) { //世界坐标转屏幕坐标 return Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, catesian3); }, cartesian2ToCartesian3: function (x, y) { //屏幕转世界坐标 var pick1 = new Cesium.Cartesian2(x, y); var ray = viewer.camera.getPickRay(pick1); var cartesian = viewer.scene.globe.pick(ray, viewer.scene); return cartesian; }, cartesian3ToDegree: function (x, y, z) { //世界坐标转换为经纬度 var ellipsoid = viewer.scene.globe.ellipsoid; var cartesian3 = new Cesium.Cartesian3(x, y, z); var cartographic = ellipsoid.cartesianToCartographic(cartesian3); var lat = Cesium.Math.toDegrees(cartographic.latitude); var lng = Cesium.Math.toDegrees(cartographic.longitude); var alt = cartographic.height; return { lon: lng, lat: lat, height: alt }; }, cartesian3ToDegree2: function (cartesian3) { //世界坐标转换为经纬度 var ellipsoid = viewer.scene.globe.ellipsoid; var cartographic = ellipsoid.cartesianToCartographic(cartesian3); var lat = Cesium.Math.toDegrees(cartographic.latitude); var lng = Cesium.Math.toDegrees(cartographic.longitude); var alt = cartographic.height; return { lon: lng, lat: lat, height: alt }; }, /** * wgs84转WebMercator * @param lon 经度 * @param lat 纬度 * @constructor */ wgs842WebMercator: function (lon, lat) { let x = (lon * 20037508.34) / 180; //纬线方向 let y = Math.log(Math.tan(((90 + lat) * Math.PI) / 360)) / (Math.PI / 180); //经线方向 y = (y * 20037508.34) / 180; return { x: x, y: y }; }, /** *WebMercator转WGS84 * @param x//纬线方向 * @param y//经线方向 * @returns {{lon: number, lat: number}} * @constructor */ webMercator2WGS84: function (x, y) { let jd = (x / 20037508.34) * 180; let wd = (y / 20037508.34) * 180; wd = (180 / Math.PI) * (2 * Math.atan(Math.exp((wd * Math.PI) / 180)) - Math.PI / 2); return { lon: jd, lat: wd }; }, getCurExtent: function () { var extent = {}; var scene = viewer.scene; var ellipsoid = scene.globe.ellipsoid; var canvas = scene.canvas; var pt1 = new Cesium.Cartesian2(0, 0); var pt2 = new Cesium.Cartesian2(canvas.width, canvas.height); extent.point1 = pt1; extent.point2 = pt2; return extent; }, getUtmDaihao: function (lon) { return parseInt((lon + 180) / 6) + 1; }, getCentral_MeridianFromDaihaoGauss6: function (daiHao) { var central_Meridian = 0; central_Meridian = daiHao * 6 - 183; return central_Meridian; }, wgs84toUtm: function (longitude, latitude) { var a = 6378137.0; var b = 6356752.314245179; var latitudeOrige = 0.0; var false_Easting = 500000.0; var false_Northing = 0.0; var scale_Factor = 0.9996; var daih = parseInt((longitude + 180) / 6) + 1; var m_Central_Meridian = daih * 6 - 183; var longitude1, latitude1, longitude0, X0, Y0, xval, yval; var e2, ee, NN, T, C, A, M; longitude0 = m_Central_Meridian; longitude0 = (longitude0 * Math.acos(-1.0)) / 180.0; //iPI; longitude1 = (longitude * Math.acos(-1.0)) / 180.0; //iPI; latitude1 = (latitude * Math.acos(-1.0)) / 180.0; //iPI; //e2=2*f-f*f; e2 = (Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2); ee = e2 * (1.0 - e2); NN = a / Math.sqrt(1.0 - e2 * Math.sin(latitude1) * Math.sin(latitude1)); T = Math.tan(latitude1) * Math.tan(latitude1); C = ee * Math.cos(latitude1) * Math.cos(latitude1); A = (longitude1 - longitude0) * Math.cos(latitude1); M = a * ((1 - e2 / 8 - (3 * e2 * e2) / 64 - (7 * e2 * e2 * e2) / 256) * latitude1 - ((3 * e2) / 8 + (3 * e2 * e2) / 32 + (45 * e2 * e2 * e2) / 1024) * Math.sin(2 * latitude1) + ((15 * e2 * e2) / 256 + (45 * e2 * e2 * e2) / 1024) * Math.sin(4 * latitude1) - ((35 * e2 * e2 * e2) / 3072) * Math.sin(6 * latitude1)); var latitude_Orige_Hd = (latitudeOrige * Math.acos(-1.0)) / 180.0; var M0 = a * ((1 - e2 / 4 - (3 * e2 * e2) / 64 - (3 * e2 * e2 * e2) / 256) * latitude_Orige_Hd - ((3 * e2) / 8 + (3 * e2 * e2) / 32 + (45 * e2 * e2 * e2) / 1024) * Math.sin(2 * latitude_Orige_Hd) + ((15 * e2 * e2) / 256 + (45 * e2 * e2 * e2) / 1024) * Math.sin(4 * latitude_Orige_Hd) - ((35 * e2 * e2 * e2) / 3072) * Math.sin(6 * latitude_Orige_Hd)); xval = NN * (A + ((1 - T + C) * A * A * A) / 6 + ((5 - 18 * T + T * T + 72 * C - 58 * ee) * A * A * A * A * A) / 120); yval = M - M0 + NN * Math.tan(latitude1) * ((A * A) / 2 + ((5 - T + 9 * C + 4 * C * C) * A * A * A * A) / 24 + ((61 - 58 * T + T * T + 300 * C - 330 * ee) * A * A * A * A * A * A) / 720); X0 = false_Easting; Y0 = false_Northing; xval *= scale_Factor; yval *= scale_Factor; var X = xval + X0; var Y = yval + Y0; return { x: X, y: Y }; }, utm2wgs84: function (X, Y, m_Central_Meridian) { var a = 6378137.0; var b = 6356752.314245179; var latitudeOrige = 0.0; var false_Easting = 500000.0; var false_Northing = 0.0; var scale_Factor = 0.9996; var longitude1, latitude1, X0, Y0, xval, yval; var e1, e2, ee, NN, T, C, M, D, R, u, fai; X0 = false_Easting; Y0 = false_Northing; xval = X - X0; yval = Y - Y0; xval /= scale_Factor; yval /= scale_Factor; e2 = (Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2); e1 = (1.0 - Math.sqrt(1 - e2)) / (1.0 + Math.sqrt(1 - e2)); ee = e2 / (1 - e2); var latitude_Orige_Hd = (latitudeOrige * Math.acos(-1.0)) / 180.0; var M0 = a * ((1 - e2 / 4 - (3 * e2 * e2) / 128 - (5 * e2 * e2 * e2) / 256) * latitude_Orige_Hd - ((3 * e2) / 8 + (3 * e2 * e2) / 32 + (45 * e2 * e2 * e2) / 1024) * Math.sin(2 * latitude_Orige_Hd) + ((15 * e2 * e2) / 256 + (45 * e2 * e2 * e2) / 1024) * Math.sin(4 * latitude_Orige_Hd) - ((35 * e2 * e2 * e1) / 3072) * Math.sin(6 * latitude_Orige_Hd)); M = yval + M0 / scale_Factor; u = M / (a * (1 - e2 / 4 - (3 * e2 * e2) / 64 - (5 * e2 * e2 * e2) / 256)); fai = u + ((3 * e1) / 2 - (27 * e1 * e1 * e1) / 32) * Math.sin(2 * u) + ((21 * e1 * e1) / 16 - (55 * e1 * e1 * e1 * e1) / 32) * Math.sin(3 * u) + ((151 * e1 * e1 * e1) / 96) * Math.sin(6 * u) + ((1097 * e1 * e1 * e1 * e1) / 512) * Math.sin(8 * u); C = ee * Math.cos(fai) * Math.cos(fai); T = Math.tan(fai) * Math.tan(fai); NN = a / Math.sqrt(1.0 - e2 * Math.sin(fai) * Math.sin(fai)); R = (a * (1 - e2)) / Math.sqrt( (1 - e2 * Math.sin(fai) * Math.sin(fai)) * (1 - e2 * Math.sin(fai) * Math.sin(fai)) * (1 - e2 * Math.sin(fai) * Math.sin(fai)) ); D = xval / NN; longitude1 = (D - ((1 + 2 * T + C) * D * D * D) / 6 + ((5 - 2 * C + 28 * T - 3 * C * C + 8 * ee + 24 * T * T) * D * D * D * D * D) / 120) / Math.cos(fai); latitude1 = fai - ((NN * Math.tan(fai)) / R) * ((D * D) / 2 - ((5 + 3 * T + 10 * C - 4 * C * C - 9 * ee) * D * D * D * D) / 24 + ((61 + 90 * T + 298 * C + 45 * T * T - 256 * ee - 3 * C * C) * D * D * D * D * D * D) / 720); var L = m_Central_Meridian + (longitude1 / Math.acos(-1.0)) * 180.0; //iPI; var B = (latitude1 / Math.acos(-1.0)) * 180.0; //iPI; return { x: L, y: B }; }, getDistance: function (point1, point2) { if (!point1.z) { point1.z = 10; } if (!point2.z) { point2.z = 10; } var cartesian1, cartesian2; if (!point1 || !point2) { return 0; } if (point1.x >= -180 && point1.x <= 180) { cartesian1 = this.degreeToCartesian3(point1.x, point1.y, point1.z); } else { cartesian1 = Cesium.Cartesian3(point1.x, point1.y, point1.z); } if (point2.x >= -180 && point2.x <= 180) { cartesian2 = this.degreeToCartesian3(point2.x, point2.y, point2.z); } else { cartesian2 = Cesium.Cartesian3(point2.x, point2.y, point2.z); } var d = Cesium.Cartesian3.distance(cartesian1, cartesian2); return d; }, createSquare: function (centreX, centreY, centreZ, width, height) { var cartesian; if (!centreZ) { centreZ = 10; } if (centreX >= -180 && centreX <= 180) { cartesian = this.degreeToCartesian3(centreX, centreY, centreZ); } else { cartesian = Cesium.Cartesian3(centreX, centreY, centreZ); } var minX, minY, maxX, maxY; minX = cartesian.x - width; minY = cartesian.y - height; maxX = cartesian.x + width; maxY = cartesian.y + height; var wgs84point1 = this.cartesian3ToDegree(minX, minY, cartesian.z); var wgs84point2 = this.cartesian3ToDegree(maxX, maxY, cartesian.z); return { minX: wgs84point1.lon, minY: wgs84point1.lat, maxX: wgs84point2.lon, maxY: wgs84point2.lat }; }, getDistance2: function (x1, y1, z1, x2, y2, z2) { if (x1 >= -180 && x1 <= 180) { var utm1 = this.wgs84toUtm(x1, y1); x1 = utm1.x; y1 = utm1.y; } if (x2 >= -180 && x2 <= 180) { var utm1 = this.wgs84toUtm(x2, y2); x2 = utm1.x; y2 = utm1.y; } var d = 0; if (z1 && z2) { d = Math.sqrt( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2) ); } else { d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } return d; }, createSquare2: function (centreX, centreY, centreZ, width, height) { var utm; if (centreX >= -180 && centreX <= 180) { utm = this.wgs84toUtm(centreX, centreY); } else { utm = { x: centreX, y: centreY }; } var minX, minY, maxX, maxY; minX = utm.x - width / 2; minY = utm.y - height / 2; maxX = utm.x + width / 2; maxY = utm.y + height / 2; var centLon = this.getCentral_MeridianFromDaihaoGauss6( this.getUtmDaihao(centreX) ); var wgs84point1 = this.utm2wgs84(minX, minY, centLon); var wgs84point2 = this.utm2wgs84(maxX, maxY, centLon); return { minX: wgs84point1.x, minY: wgs84point1.y, maxX: wgs84point2.x, maxY: wgs84point2.y, minXutm: minX, minYutm: minY, maxXutm: maxX, maxYutm: maxY }; }, createSquare2With2Points: function ( minX, minY, maxX, maxY, width, height, centLon ) { var utmMin; if (minX > -180 && minX < 180) { utmMin = this.wgs84toUtm(minX, minY); } else { utmMin = { x: minX, y: minY }; } var utmMax; if (maxX > -180 && maxX < 180) { utmMax = this.wgs84toUtm(maxX, maxY); } else { utmMax = { x: maxX, y: maxY }; } if (minX > -180 && minX < 180) { return { minX: minX, minY: minY, maxX: maxX, maxY: maxY, minXutm: utmMin.x, minYutm: utmMin.y, maxXutm: utmMax.x, maxYutm: utmMax.y }; } else { //var centLon=this.getCentral_MeridianFromDaihaoGauss6(this.getUtmDaihao(minX)); var wgs84point1 = this.utm2wgs84(utmMin.x, utmMin.y, centLon); var wgs84point2 = this.utm2wgs84(utmMax.x, utmMax.y, centLon); return { minX: wgs84point1.x, minY: wgs84point1.y, maxX: wgs84point2.x, maxY: wgs84point2.y, minXutm: utmMin.x, minYutm: utmMin.y, maxXutm: utmMax.x, maxYutm: utmMax.y }; } }, createSquare2With2PointsNoutm: function ( minX, minY, maxX, maxY, width, height, centLon ) { return { minX: minX, minY: minY, maxX: maxX, maxY: maxY }; }, isPointInExtent: function (extent, point) { return isPointInPolygon( 4, [extent.xmin, extent.xmin, extent.xmax, extent.xmax], [extent.ymin, extent.ymax, extent.ymax, extent.ymin], point.x, point.y ); }, isPointInPolygon: function (nvert, vertx, verty, testx, testy) { var i, j, c = 0; for (i = 0, j = nvert - 1; i < nvert; j = i++) { if ( verty[i] >= testy != verty[j] > testy && testx <= ((vertx[j] - vertx[i]) * (testy - verty[i])) / (verty[j] - verty[i]) + vertx[i] ) { c = 1 + c; } } if (c % 2 == 0) { return false; } else { return true; } }, isPolygonsIntersect: function (polygons, polygon) { for (var i = 0; i < polygons.length; i++) { var polygonc = polygons[i]; var xArray = polygonc.xArray; var yArray = polygonc.yArray; var xArrayplane = polygonc.xArrayplane; var yArrayplane = polygonc.yArrayplane; if ( this.isPointInPolygon( xArray.length, xArray, yArray, polygon.minX, polygon.minY ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArray, yArray, polygon.minX, polygon.maxY ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArray, yArray, polygon.maxX, polygon.maxY ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArray, yArray, polygon.maxX, polygon.minY ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArrayplane, yArrayplane, polygon.minXutm, polygon.minYutm ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArrayplane, yArrayplane, polygon.minXutm, polygon.maxYutm ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArrayplane, yArrayplane, polygon.maxXutm, polygon.maxYutm ) ) { return true; } if ( this.isPointInPolygon( xArray.length, xArrayplane, yArrayplane, polygon.maxXutm, polygon.minYutm ) ) { return true; } } return false; }, getCenterPos: function (coord) { let coordArray = coord.split(","); let coord2 = []; let jd = 0.0; let wd = 0.0; let sumjd = 0.0; let sumwd = 0.0; for (let j = 0; j < coordArray.length; j++) { if (j % 2 === 0) { sumjd += parseFloat(coordArray[j]); } else { sumwd += parseFloat(coordArray[j]); } coord2.push(parseFloat(coordArray[j])); } jd = (sumjd / coordArray.length) * 2; wd = (sumwd / coordArray.length) * 2; return { jd: jd, wd: wd, cesiumPolygon: coord2 }; }, /** * 定比分点公式 */ dingbifendian: function (yi, er, lamda) { return (yi + lamda * er) / (1 + lamda); }, }; export default cesiumCoordUtil
以上代码不保证全对。