proj4js 坐标转换

一、概述

Proj4js 是一个开源的 JavaScript 库,用于将点坐标从一个坐标系转换到另一个坐标系,包括基准转换。

git代码库地址:https://github.com/proj4js/proj4js 

另一个坐标系在线查询和坐标转换地址:EPSG.io: Coordinate Systems Worldwide

在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);
};

复制代码相关:坐标转换接口icon-default.png?t=M3K6http:// https://blog.csdn.net/u014556081/article/details/121242989

Cesium矩阵变换https://blog.csdn.net/u014556081/article/details/118341391

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苹果园dog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值