0

Working directly in JavaScript on a geojson file, I could not find any easy way to transform my coordinates from EPSG3035 to geographic (Lon,Lat). By easy I mean: not installing another software like QGIS or similar. I have tried proj4, and ol, but both failed.

I have no clue. [the OL solution is in "t.niese" answer below]

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>
allez l'OM
  • 547
  • 4
  • 13

1 Answers1

1

The errors you get is because proj4 does not know the projection EPSG:3035

If you run console.log(proj4.defs('EPSG:3035')) you can see that it 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>

If you only used proj4 you can just define the projection using:

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

Using proj4.defs(name, definition) does not return anything, it defines the projection with that name so that it can be used with that name. After you defined the projection you can use proj4.defs(name) to get it.

If for OpenLayer you also need to register proj4 after that:

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

The definition for proj4 should be +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 (Source 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>
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 1
    Excellent. I have missed the "register" step for OL. Great help indeed. – allez l'OM Feb 11 '22 at 12:17
  • 1
    However the code for proj4 doesn't change the coordinates. It keeps the source value (which in that case has been already transformed). – allez l'OM Feb 11 '22 at 12:44
  • @allezl'OM fixed that. Now both OpenLayer and proj4 work on the original coordinates and the transformed are returned. – t.niese Feb 11 '22 at 12:55