Cesium
出现Unknown crs name: urn:ogc:def:crs:EPSG:xxxx
1. 问题描述
- 问题描述,
Cesium
支持的默认坐标系是WGS 84
也就是EPSG 4326
, 国内一般的CGCS2000
也就是EPSG 4490
需要转换, 因此就会出现Unknown crs name: urn:ogc:def:crs:EPSG:xxxx
的问题 - 如何确认文件是哪个类型的, 可以打开提供的
geojson
或者json
文件, 查找"name": "urn:ogc:def:crs:EPSG::4490"
相似的字段名称
2. 问题解决
2.1 安装proj4
依赖模块
yarn add proj4
2.2 打开网址epsg(可能需要科学上网)
搜索需要转换的EPSG
格式,这里以EPSG:4490
为例子, 注意直接搜索后面的数字即可, 比如4490
下滑到最底端, 选择GeoServer
复制后面的文本, 从4490=
之后开始复制
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"]]
到这里EPSG 4490
的准备完成, WGS 84
的也需要按照上面一样操作, 下面的字符串就是WGS84
的GeoServer
, 直接复制即可
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"]]
2.3 代码部分
主要使用了Cesium
中Cesium.GeoJsonDataSource.crsNamesAPI
注意: 坐标转换方法必须要放在加载 geoJson 文件之前
JS
代码
import { default as proj4 } from 'proj4'
export default class MapClass {
viewer = null
Cesium = window.Cesium
constructor(container) {
this.init(container)
}
init() {
this.tranformCoordinate()
// 加载geoJson文件
}
...
...
// 坐标系转换方法
tranformCoordinate() {
Cesium.GeoJsonDataSource.crsNames['urn:ogc:def:crs:EPSG::4490'] =
Cesium.GeoJsonDataSource.crsNames['EPSG:4490'] = coordinates => {
const fromProjection = `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"]]`
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 x = coordinates[0]
const y = coordinates[1]
const newCoordinates = proj4(fromProjection, toProjection, [x, y])
return Cesium.Cartesian3.fromDegrees(
newCoordinates[0],
newCoordinates[1],
0
)
}
}
}