I have a .json
file of points to put on a Leaflet map, and I have used the proj4js
to convert them from the EPSG 3946 projection into the WGS84 EPSG 4326 one. All worked, but the points are not located where they should be: they are about 8 degrees latitude more towards south and a little bit more towards east. The same points look fine on QGIS, they place where they should be.
This is the .js
script:
var pTransfo;
$.getJSON('transfo.json',function(data){
proj4.defs("EPSG:3946", "+proj=lcc +lat_1=43 +lat_2=49 +lat_0=46.5 +lon_0=6 +x_0=1700000 +y_0=6200000 +datum=RGF93 +units=m +no_defs +towgs84=0,0,0");
var fromProj = proj4('EPSG:3946');
var toProj = proj4('EPSG:4326');
function convertCoordinates(coordinates) {
var convertedCoords = proj4(fromProj, toProj, [coordinates[0], coordinates[1]]);
console.log('Coordonnées originales :', coordinates);
console.log('Coordonnées converties :', convertedCoords);
return [convertedCoords[0], convertedCoords[1]];
}
data.features.forEach(function(feature) {
if (feature.geometry.type === "Point") {
feature.geometry.coordinates = convertCoordinates(feature.geometry.coordinates);
}
});
And I have already added this line in the html: <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.6.0/proj4-src.js"></script>
after the jquery and leaflet line.
Why this delay of location?
2
Answers
everybody! Because I had to finish this project very fast, I actually used the new columns I have created, filled with WGS84 data converted from the 3946 ones. All worked fine with WGS84 data. And Thomas, I thought of that too and I have tested the 3946 data. Nothing was out of order. Only after using the proj4js...
The issue is most likely the order of your EPSG:3946 coordinates. Since I don’t have your data to test, I can only guess though. However, I wrote an example using
1124390.4605155424, 5490541.680680278
(x, y) for Le Conquet near Brest in Bretagne.The conversation output is
-4.773869999999995, 48.36004666665183
(lng, lat). If I pass the EPSG:3946 coordinate in its reversed order (y, x), then I also get a coordinate that is way off.In addition, I am using the projection definition suggested by Jaromanda X in his comment.
Here is a working example (with loading the projection definition for EPSG:3946 from spatialreference.org on the fly):
DEMO