【问题标题】:conversion EPSG3035 to EPSG3857 Mercator or 4326 LonLat将 EPSG3035 转换为 EPSG3857 墨卡托或 4326 LonLat
【发布时间】:2022-02-11 20:55:19
【问题描述】:

在geojson 文件上直接在JavaScript 中工作,我找不到任何简单的方法将我的坐标从EPSG3035 转换为地理(Lon,Lat)。我的意思很简单:不安装其他软件,如 QGIS 或类似软件。 proj4和ol我都试过了,都失败了。

我不知道。 [OL解决方案在下面的“t.niese”答案中]

let coordinates = [4035675.373507705517113, 2862363.090465864632279]; // Luxembourg
console.log(coordinates);
// a "well-known text" WKT-OGC definition from https://epsg.io/3035
const WKT3035 = `PROJCS["ETRS89 / LAEA Europe",GEOGCS["ETRS89",DATUM["European_Terrestrial_Reference_System_1989",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6258"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4258"]],PROJECTION["Lambert_Azimuthal_Equal_Area"],PARAMETER["latitude_of_center",52],PARAMETER["longitude_of_center",10],PARAMETER["false_easting",4321000],PARAMETER["false_northing",3210000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","3035"]]`;
// trying OpenLayers
try {
  const fromLonLat = ol.proj.getTransform("EPSG:3035", "EPSG:4326");
  coordinates = fromLonLat(coordinates);
  console.log("using ol.proj", coordinates);
} catch (err) {
  console.error(err)
}
try {  // trying proj4
    coordinates = [4035675.373507705517113, 2862363.090465864632279];
    const srcProj = new proj4.Proj(WKT3035);
    proj4(srcProj).inverse(coordinates);
  console.log("using proj4", coordinates);
} catch (err) {
  console.error(err)
}
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.8.1/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.7.5/proj4.js"></script>

【问题讨论】:

标签: javascript coordinate-transformation


【解决方案1】:

你得到的错误是因为 proj4 不知道投影EPSG:3035

如果你运行console.log(proj4.defs('EPSG:3035')),你可以看到它返回undefined

console.log(proj4.defs('EPSG:3035'))
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.8.1/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.7.5/proj4.js"></script>

如果您只使用proj4,您可以使用以下方式定义投影:

proj4.defs("EPSG:3035", /* here goes the definition of that projection */);

使用proj4.defs(name, definition) 不会返回任何内容,它使用该名称定义投影,以便可以使用该名称。定义投影后,您可以使用proj4.defs(name) 来获取它。

如果是OpenLayer,之后还需要注册proj4

proj4.defs("EPSG:3035", /* here goes the definition of that projection */);
ol.proj.proj4.register(proj4);

proj4 的定义应该是+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs(来源https://epsg.io/3035

proj4.defs("EPSG:3035", '+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
ol.proj.proj4.register(proj4);

let coordinates = [4035675.373507705517113, 2862363.090465864632279]; // Luxembourg
console.log(coordinates);
try {
  // trying OpenLayers
  const fromLonLat = ol.proj.getTransform("EPSG:3035", "EPSG:4326");
  console.dir(fromLonLat(coordinates));
} catch (err) {
  console.error(err)
}

try {
  // trying proj4
  console.dir(proj4(proj4.defs('EPSG:3035'), proj4.defs('EPSG:4326'), coordinates));
} catch (err) {
  console.error(err)
}
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.8.1/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.7.5/proj4.js"></script>

【讨论】:

  • 优秀。我错过了 OL 的“注册”步骤。确实有很大的帮助。
  • 但是 proj4 的代码不会改变坐标。它保留源值(在这种情况下已经转换)。
  • @allezl'OM 解决了这个问题。现在 OpenLayer 和 proj4 都在原始坐标上工作,并返回转换后的坐标。
猜你喜欢
  • 1970-01-01
  • 2011-11-26
  • 1970-01-01
  • 2018-07-16
  • 2019-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多