Convert Coordinates in Leaflet.js: WGS84 to NAD 1983 HARN StatePlane New Mexico Central FIPS 3002 Feet

10 Dec

I wish I could do everything in WGS84, but nobody wants to change their projection – even if they have no technical reason for keeping the one they currently use – see this presentation. I have shown how from Leaflet.js I can use the ESRI REST API to add geometry to a feature class. The problem is, I can only do it using WGS84 right now.

How can I convert WKID: 4326 to WKID: 2903?

I could use Proj4Leaflet. I took a quick look but came up with an idea that I know I can implement right now. I will use the ESRI REST API. Here is how it works.

I created a service in WGS84(WKID:4326). The service allows me to use

map.on(“click”,function(e){

       put e.latlng in featureclass

});

to add a point to my feature class. Now I have a point in WGS84 and it gave me back the objectid when it succeeded. I then use the objectid to query the featureclass and set the outSR to the projection I need (2903).

var queryparams = “objectIds=”+id+”&outSR=2903&f=json”;

var queryurl=urlToService+”/query”;
var http;
http=new XMLHttpRequest();
http.open(“POST”, queryurl, true);
http.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
var k = JSON.parse(http.responseText);
alert(k.features[0].geometry.x+”,”+k.features[0].geometry.y);
}}

http.send(queryparams);

I know it takes a few AJAX calls, but now I can get any projection coordinates I want, all while starting with WGS84. Having only started testing, it appears to work fast enough for me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: