将 EPSG3035 转换为 EPSG3857 墨卡托或 4326 LonLat

conversion EPSG3035 to EPSG3857 Mercator or 4326 LonLat

直接在 JavaScript 中处理 geojson 文件,我找不到任何简单的方法将我的坐标从 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>

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

如果你 运行 console.log(proj4.defs('EPSG:3035')) 你可以看到它 returns 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) 不会 return 任何东西,它定义了具有该名称的投影,以便它可以与该名称一起使用。定义投影后,您可以使用 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>