一、概述
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); };