1

I am creating a wildlife survey form that has a field where users enter location. Users will be able to enter location using a map (i.e. draggable pushpin) or by manually entering lat/lng. My clients are also requesting an option for entering UTM coordinates.

If a user does enter UTM coords I would like to convert these before pushing to the database. I've seen a couple websites that have utilities for converting UTM to decimal degrees.

Does anyone know of a javaScript lib that will make these conversion given zone and easting-northing? I also saw this thread

Convert geographic coordinates to UTM zone 29N with Javascript

Does ESRI have a web service that I could pass UTM coords to and get lat/lng back?

3

If you're not happy with using a Geometry Service like Kirk suggested, you could use proj4js client-side.

  • This looks promising. I am converting from UTM Zone 11 EPSG-2153. I don't see this listed under the proj4js lib/defs. Any suggestions? – jotamon May 30 '13 at 20:31
  • I found the definition on this page and it is the string +proj=utm +zone=11 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_def. You can use the user guide here to figure out how to make that work in your app. – Jason Scheirer May 31 '13 at 6:49
1

You could use the GeometryService on sampleserver6, but there's no guarantee Esri won't change or remove it.

Can call it using javascript Project method.

  • interesting but I am looking for a longer term solution – jotamon May 30 '13 at 16:28
  • 1
    @snowgage The geometryservice is part of the default installation for ArcGIS server. If you can find an ArcGIS server site that you can count on, there's a good chance they've published the geometryservice. – Kirk Kuykendall May 30 '13 at 16:35
0

I used this code to convert wgs84 to utm and utm to wgs84. Caution: Accuracy of this code is about one meter.

<script>
function Wgs2Utm( lan1,fi) {

var a=6378137.000;
var b=6356752.314;
var f=(a-b)/a;
var e2=Math.sqrt((Math.pow(a,2)-Math.pow(b,2))/Math.pow(b,2));
var e=Math.sqrt((Math.pow(a,2)-Math.pow(b,2))/Math.pow(a,2));

var zone;
var lan0;
if (lan1>0)
{
    var zone=30+Math.ceil(lan1/6);
    lan0=Math.floor(lan1/6)*6+3;
}
else
{
    var zone=30-Math.floor(Math.abs(lan1)/6);
    lan0=-Math.floor(Math.abs(lan1)/6)*6-3;
}


//-----------------------------------------------

var lan=lan1-lan0;
lan=lan*Math.PI/180;
fi=fi*Math.PI/180;
var N=a/Math.pow(1-Math.pow(e,2)*Math.pow(Math.sin(fi),2),0.5);
var M=a*(1-Math.pow(e,2))/Math.pow((1-(Math.pow(e,2)*Math.pow(Math.sin(fi),2))),(3/2));
var t=Math.tan(fi);
var p=N/M;

//----------------------------------------------
var k0=0.9996;

var term1=Math.pow(lan,2)*p*Math.pow(Math.cos(fi),2)/2;
var term2=Math.pow(lan,4)*Math.pow(Math.cos(fi),4)*(4*Math.pow(p,3)*(1-6*Math.pow(t,2))+Math.pow(p,2)*(1+24*Math.pow(t,2))-4*p*Math.pow(t,2))/24;
var term3=Math.pow(lan,6)*Math.pow(Math.cos(fi),6)*(61-148*Math.pow(t,2)+16*Math.pow(t,4))/720;

var Kutm=k0*(term1+term2+term3);


//----------------------------------------------
term1=Math.pow(lan,2)*p*Math.pow(Math.cos(fi),2)*(p-Math.pow(t,2))/6;
term2=Math.pow(lan,4)*Math.pow(Math.cos(fi),4)*(4*Math.pow(p,3)*(1-6*Math.pow(t,2))+Math.pow(p,2)*(1+8*Math.pow(t,2))-Math.pow(p,2)*Math.pow(t,2)+Math.pow(t,4))/120;
term3=Math.pow(lan,6)*Math.pow(Math.cos(fi),6)*(61-479*Math.pow(t,2)+179*Math.pow(t,4)-Math.pow(t,6))/5040;

var Xutm=500000+k0*lan*N*Math.cos(fi)*(1+term1+term2+term3);

//----------------------------------------------

var A0=1-0.25*Math.pow(e,2)-3/64*Math.pow(e,4)-5/256*Math.pow(e,6);
var A2=3/8*(Math.pow(e,2)+0.25*Math.pow(e,4)+15/128*Math.pow(e,6));
var A4=15/256*(Math.pow(e,4)+0.75*Math.pow(e,6));
var A6=35/3072*Math.pow(e,6);

var sfi=a*(A0*fi-A2*Math.sin(2*fi)+A4*Math.sin(4*fi)-A6*Math.sin(6*fi));

//----------------------------------------------

term1=Math.pow(lan,2)*N*Math.sin(fi)*Math.cos(fi)/2;
term2=Math.pow(lan,4)*N*Math.sin(fi)*Math.pow(Math.cos(fi),3)*(4*Math.pow(p,2)+p-Math.pow(t,2))/24;
term3=Math.pow(lan,6)*N*Math.sin(fi)*Math.pow(Math.cos(fi),5)*(8*Math.pow(p,4)*(11-24*Math.pow(t,2))-28*Math.pow(p,3)*(1-6*Math.pow(t,2))+Math.pow(p,2)*(1-32*Math.pow(t,2))-p*2*Math.pow(t,2)+Math.pow(t,4));
var term4=Math.pow(lan,8)*N*Math.sin(fi)*Math.pow(Math.cos(fi),7)*(1385-3111*Math.pow(t,2)+543*Math.pow(t,4)-Math.pow(t,6));

var Yutm=k0*(sfi+term1+term2+term3+term4);
var sn='N';
if (fi <0)
{
    Yutm = 10000000 + Yutm;
    sn='S';
}
    return Xutm.toString().concat(" ; " + Yutm.toString() + " "+zone.toString()+sn) ;

}

function Utm2Wgs( X,Y,zone,sn) {
    if (sn=='S')
    {
        Y = Y - 10000000;
    }
    X = X - 500000;
    var sa = 6378137.000000;
    var sb = 6356752.314245;

   var e = Math.pow( Math.pow(sa , 2) - Math.pow(sb , 2) , 0.5 ) / sa;
   var e2 = Math.pow( Math.pow( sa , 2 ) - Math.pow( sb , 2 ) , 0.5 ) / sb;
   var e2cuadrada = Math.pow(e2 , 2);
   var c = Math.pow(sa , 2 ) / sb;



   var S = ( ( zone * 6 ) - 183 ); 
   var lat =  Y / ( 6366197.724 * 0.9996 );                         
   var v =  (c * 0.9996)/ Math.pow( 1 + ( e2cuadrada * Math.pow( Math.cos(lat), 2 ))  , 0.5 ) ;
   var a = X / v;
   var a1 = Math.sin( 2 * lat );
   var a2 = a1 * Math.pow( Math.cos(lat), 2);
   var j2 = lat + ( a1 / 2 );
   var j4 = ( ( 3 * j2 ) + a2 ) / 4;
   var j6 = ( ( 5 * j4 ) + ( a2 * Math.pow( Math.cos(lat) , 2) ) ) / 3;
   var alfa = ( 3 / 4 ) * e2cuadrada;
   var beta = ( 5 / 3 ) * Math.pow(alfa , 2);
   var gama = ( 35 / 27 ) * Math.pow(alfa , 3);
   var Bm = 0.9996 * c * ( lat - alfa * j2 + beta * j4 - gama * j6 );
   var b = ( Y - Bm ) / v;
   var Epsi = ( ( e2cuadrada * Math.pow(a , 2)) / 2 ) * Math.pow( Math.cos(lat) , 2);
   var Eps = a * ( 1 - ( Epsi / 3 ) );
   var nab = ( b * ( 1 - Epsi ) ) + lat;
   var senoheps = ( Math.exp(Eps) - Math.exp(-Eps) ) / 2;
   var Delt = Math.atan(senoheps / Math.cos(nab) );
   var TaO = Math.atan(Math.cos(Delt) * Math.tan(nab));
   var longitude = (Delt *(180 / Math.PI ) ) + S;
   var latitude = ( lat + ( 1 + e2cuadrada* Math.pow(Math.cos(lat), 2) - ( 3 / 2 ) * e2cuadrada * Math.sin(lat) * Math.cos(lat) * ( TaO - lat ) ) * ( TaO - lat ) ) * (180 / Math.PI);
    return longitude.toString().concat(" ; " + latitude.toString()) ;
}

</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.