3

I'm trying to use Proj4js to perform some coordinate conversions but there is very little information out there on how to use it.

What I want to be able to do is convert a latitude and longitude to a UTM coordinate but I don't know what the zone is. This should be easy since the longitude dictates the zone and if you know that the zone comes out by default.

I've tried to do this in Proj4js but I get an error saying

"Uncaught TypeError: undefined is not a function"

My code loos like this:

 proj4Arr = [-105.2098, 39.7458];
 var source = ('+proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees +no_defs');
 var dest = ("+proj=utm +ellps=GRS80 +datum=nad83 +units=m +no_defs");

 console.log(proj4(source, dest, proj4Arr));

I just don't know if it's possible to do this without the zone included. I could calculate the zone first and then put it in the string if I need to but I'm trying to keep it simple.

And if anyone knows any good purely js resources on examples (not necessarily using OpenLayers 3) I'd be grateful for that as well.

Thanks!

mudrock
  • 75
  • 1
  • 9

2 Answers2

7

This year is 2021, it is possible to post a runnable code that not only answers the question, but also demonstrates all the steps that lead to the coordinate transformation results.

  function utmzone_from_lon(lon_deg) {
    //get utm-zone from longitude degrees
    return parseInt(((lon_deg+180)/6)%60)+1;
  }
  
  function proj4_setdef(lon_deg) {
    //get UTM projection definition from longitude
    const utm_zone = utmzone_from_lon(lon_deg);
    const zdef = `+proj=utm +zone=${utm_zone} +datum=WGS84 +units=m +no_defs`;
    return zdef;
  }
  
  // computation test
  let lon_input = 95.99;
  let lat_input = 15.15;
  console.log("Input (long,lat):", lon_input, lat_input);
  const azone = utmzone_from_lon(lon_input);
  console.log(`UTM zone from longitude: ${azone}`);
  console.log("AUTO projection definition:", proj4_setdef(lon_input));
  
  // define proj4_defs for easy uses
  // "EPSG:4326" for long/lat degrees, no projection
  // "EPSG:AUTO" for UTM 'auto zone' projection
  proj4.defs([
  [
    "EPSG:4326",
    "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees"
  ],
  ["EPSG:AUTO", proj4_setdef(lon_input)]]);
 
  // usage:
  // conversion from (long/lat) to UTM (E/N)
  const en_m = proj4("EPSG:4326", "EPSG:AUTO", [lon_input, lat_input]);
  const e4digits = en_m[0].toFixed(4); //easting
  const n4digits = en_m[1].toFixed(4); //northing
  console.log(`Zone ${azone}, (E,N) m: ${e4digits}, ${n4digits}`);
  
  // inversion from (E,N) to (long,lat)
  const lonlat_chk = proj4("EPSG:AUTO", "EPSG:4326", en_m);
  console.log("Inverse check:", lonlat_chk);
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.6.2/proj4.js"></script>
swatchai
  • 17,400
  • 3
  • 39
  • 58
2

I think you will have to specify the zone info for projections for proj4 projections in order to use the library. Without zone it is not a valid proj4 projection.

Here is the question about how to identify the zone from lon/lat.

And here are two examples: [1], [2]

You can check out the js code by yourself.

Community
  • 1
  • 1
mfdev
  • 196
  • 2
  • 7
  • 1
    I realized that this was in fact the issue but I voted you answer as correct. I used this code to first get the zone: `var zone = 1 + Math.floor((lngd+180)/6);` – mudrock Apr 20 '15 at 18:49