How can I convert geo coordinates from WGS 84 to UTM using JavaScript?
I tried using proj4js, but it yields these coordinates: 32U 5114272 1633427
, whereas external sources tell us, that 32U 688260 5338516
would be correct.
You probably got confused with Lon/Lat. If you set Lon (x) to 11...
and Lat (y) to 48...
, then everything works as expected.
proj4.defs("EPSG:32632","+proj=utm +zone=32"); // https://epsg.io/32632
const sourceProj = new proj4.Proj('WGS84');
const destProj = new proj4.Proj('EPSG:32632');
function calc() {
const x = parseFloat(document.getElementById('srcX').value)
const y = parseFloat(document.getElementById('srcY').value)
const p = new proj4.Point(x, y);
const r = proj4.transform(sourceProj, destProj, p);
document.getElementById('tgtX').value = r.x
document.getElementById('tgtY').value = r.y
console.log(x, y, p, r, r.x, r.y)
}
calc()
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.7.2/proj4.js"></script>
<pre>
From WGS84
Lon: <input type="number" id="srcX" value="11.532256" onchange="calc()"/>°
Lat: <input type="number" id="srcY" value="48.171974" onchange="calc()"/>°
To UTM 32U
X : <input type="number" id="tgtX" value="" readonly/>
Y : <input type="number" id="tgtY" value="" readonly/>
</pre>