原文地址:https://www.cnblogs.com/dog2016/p/14960186.html
本文章向大家介紹proj4js 坐標轉換,主要包括proj4js 坐標轉換使用實例、應用技巧、基本知識點總結和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。
一、概述
Proj4js 是一個開源的 JavaScript 庫,用於將點坐標從一個坐標系轉換到另一個坐標系,包括基准轉換。
git代碼庫地址:https://github.com/proj4js/proj4js
另一個坐標系在線查詢和坐標轉換地址:https://epsg.io/
在PostGIS中有一個表 spatial_ref_sys ,可以查詢所有的坐標系信息。
在Geoserver中也有所有坐標系的信息。在ArcGIS中也有。
以下介紹在node中使用proj4,以及在Cesium三維開發中使用。
二、node中使用Proj4
1、引入庫,npm install proj4
2、引用庫,const proj4 = require('proj4')
3、轉換方法,proj4(fromProjection, toProjection, [x, y])
4、坐標系舉例說明
EPSG:4549 CGCS2000 / 3-degree Gauss-Kruger CM 120E
2000大地坐標系,3度分帶,高斯克呂格投影,中央經線120。
如果坐標是加了帶號的,就用4528。具體看需要轉換的數據坐標。加了帶號的X坐標前面兩位就是帶號。
東西向X坐標不加帶號6位數(坐標向東偏移,了500km,因此中央子午線的X坐標就是500000,加了帶號就是365000000),加了帶號8位數。南北方向7位數
EPSG:4528 CGCS2000 / 3-degree Gauss-Kruger zone 40
3度帶中央經線=3乘以帶號
5、代碼,在下面的代碼中實現將點坐標從CGCS2000坐標系EPSG:4549轉換為WGS84坐標系
函數convert111_和convert111結果相同。因為proj4函數可以傳入坐標系的wkt,也可以傳入proj.4。
/* * @Author: 蘋果園dog * @Date: 2021-07-01 11:48:07 * @LastEditTime: 2021-07-01 17:44:56 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: */ const proj4 = require('proj4'); function convert111_(x, y) { const fromProjection = `PROJCS["CGCS2000 / 3-degree Gauss-Kruger CM 120E",GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",120],PARAMETER["scale_factor",1],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","4549"]] `; const toProjection = `GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG", "6326"]], PRIMEM["Greenwich", 0, AUTHORITY["EPSG", "8901"]], UNIT["degree", 0.0174532925199433, AUTHORITY["EPSG", "9122"]], AUTHORITY["EPSG", "4326"]]`; const newCoordinates = proj4(fromProjection, toProjection, [x, y]); return newCoordinates; } function convert111(x, y) { const fromProjection = `+proj=tmerc +lat_0=0 +lon_0=120 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs`; const toProjection = `+proj=longlat +datum=WGS84 +no_defs`; const newCoordinates = proj4(fromProjection, toProjection, [x, y]); return newCoordinates; } module.exports = { convert111: convert111 }
6、坐標轉換測試
三、Cesium中使用Proj4
由於Cesium三維球默認是WGS84的,要想加載一些CGCS2000坐標系的一些矢量文件。
比如geojson,kml等,需要在加載數據的時候動態轉換坐標。
以下介紹加載2000坐標系的Geojson方法。
在下面的代碼中實現將點坐標從CGCS2000坐標系EPSG:4524轉換為WGS84坐標系。
/* * @Author: 蘋果園dog * @Date: 2021-04-12 14:42:01 * @LastEditTime: 2021-04-12 14:46:41 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: */ import { default as proj4 } from "proj4"; Cesium.GeoJsonDataSource.crsNames[ "urn:ogc:def:crs:EPSG::4524" ] = Cesium.GeoJsonDataSource.crsNames["EPSG:4524"] = function(coordinates) { const fromProj = `PROJCS["CGCS2000 / 3-degree Gauss-Kruger zone 36", GEOGCS["China Geodetic Coordinate System 2000", DATUM["China 2000", SPHEROID["CGCS2000", 6378137.0, 298.257222101, AUTHORITY["EPSG","1024"]], AUTHORITY["EPSG","1043"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4490"]], PROJECTION["Transverse_Mercator", AUTHORITY["EPSG","9807"]], PARAMETER["central_meridian", 108.0], PARAMETER["latitude_of_origin", 0.0], PARAMETER["scale_factor", 1.0], PARAMETER["false_easting", 36500000.0], PARAMETER["false_northing", 0.0], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","4524"]]`; const toProj = `GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG", "6326"]], PRIMEM["Greenwich", 0, AUTHORITY["EPSG", "8901"]], UNIT["degree", 0.0174532925199433, AUTHORITY["EPSG", "9122"]], AUTHORITY["EPSG", "4326"]]`; const x = coordinates[0]; const y = coordinates[1]; const newCoordinates = proj4(fromProj, toProj, [x, y]); return Cesium.Cartesian3.fromDegrees(newCoordinates[0], newCoordinates[1], 0); };