Converting lat long to spatial reference

6974
11
12-13-2016 05:33 PM
NhuMai
by
New Contributor II

I'm trying to project some lat long coordinates into another spatial reference. No errors are thrown, but I printing the result to the console gives "undefined". Anyone see where I'm going wrong?

var lat = window.myMap.extent.getCenter().getLatitude().toFixed(2);
var long = window.myMap.extent.getCenter().getLongitude().toFixed(2);

var latLong = "Latitude : " + lat + " - Longitude : " + long;

var X;
var Y;

var rgpfSixSudSR = new SpatialReference ({
 wkid : 3297
 });
 var geometryService = new GeometryService("http://ourserver/arcgis/rest/services/Utilities/Geometry/GeometryServer");
 var inputPoint = new Point(lat, long, rgpfSixSudSR);
 var prjParams = new ProjectParameters ();
 prjParams.geometries = [inputPoint];
 prjParams.outSR = rgpfSixSudSR;

geometryService.project(prjParams, function (outputpoint) {
 console.log('Conversion completed');
 X = outputpoint[0].x;
 Y = outputpoint[0].y;
});

var center = "X : " + X + " - Y : " + Y;

console.log(latLong);
console.log(center);
0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus

Just to clarify.... longitude latitude comes in different datums, so the numbers themselves need a spatial reference defined (such as GCS WGS84 or GCS NAD83) to begin with.  You need a defined spatial reference before data can be projected to another spatial reference, whether the units are the same or different.

0 Kudos
NhuMai
by
New Contributor II

Okay, I see what you mean. Defining the point with WGS GCS 1984 in the following way still leaves me with undefined however...

var lat = window.myMap.extent.getCenter().getLatitude().toFixed(2);
var long = window.myMap.extent.getCenter().getLongitude().toFixed(2);

var latLong = "Latitude : " + lat + " - Longitude : " + long;

var X;
var Y;

var rgpfSixSudSR = new SpatialReference ({
wkid : 3297
});

var wgsGCSEightFourSR = new SpatialReference ({
wkid : 4326
});

var geometryService = new GeometryService("http://192.168.1.209:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
var inputPoint = new Point(lat, long, wgsGCSEightFourSR);
var prjParams = new ProjectParameters ();
prjParams.geometries = [inputPoint];
prjParams.outSR = rgpfSixSudSR;

geometryService.project(prjParams, function (outputpoint) {
console.log('Conversion completed');
X = outputpoint[0].x;
Y = outputpoint[0].y;

});

var center = "X : " + X + " - Y : " + Y;

console.log(latLong);
console.log(center);

Dan Patterson a écrit:

Just to clarify.... longitude latitude comes in different datums, so the numbers themselves need a spatial reference defined (such as GCS WGS84 or GCS NAD83) to begin with.  You need a defined spatial reference before data can be projected to another spatial reference, whether the units are the same or different.

0 Kudos
KenBuja
MVP Esteemed Contributor

One issue is that some of the values are not going to be ready when you're trying to access them. The extent isn't available until the map is loaded and you're trying to access x and y to create center before they have been returned by the Geometry Service.

I tried updating the code to fix this, but I can't figure out why the the outputPoint is NaN.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Measure Polygon Area and Perimeter</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/css/esri.css">
    <style>
        html, body, #mapDiv {
            height: 100%;
            margin: 0;
            padding: 0;
            width: 100%;
        }
    </style>

    <script src="https://js.arcgis.com/3.18/"></script>
    <script>
        require(["esri/map",
              "esri/geometry/Extent",
              "esri/geometry/Point",
              "esri/SpatialReference",
              "esri/tasks/GeometryService",
              "esri/tasks/ProjectParameters"],
        function (Map, Extent, Point, SpatialReference, GeometryService, ProjectParameters) {

            var inputPoint, latLong;

            var map = new Map("mapDiv", {
                basemap: "topo",
                center: [-149.40684, -17.679],
                zoom: 10
            });

            var geometryService = new GeometryService("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");
            var rgpfSixSudSR = new SpatialReference({
                wkid: 3297
            });

            map.on("load", function () {
                var lat = map.extent.getCenter().getLatitude().toFixed(2);
                var long = map.extent.getCenter().getLongitude().toFixed(2);
                latLong = "Latitude : " + lat + " - Longitude : " + long;

                inputPoint = new Point(lat, long);
                var prjParams = new ProjectParameters();
                prjParams.geometries = [inputPoint];
                prjParams.outSR = rgpfSixSudSR;
                //prjParams.transformation.wkid = 15832;
                geometryService.project(prjParams, function(outputPoints) {
                    console.log('Conversion completed');
                    var X = outputPoints[0].x;
                    var Y = outputPoints[0].y;
                    var center = "X : " + X + " - Y : " + Y;
                    console.log(latLong);
                    console.log(center);

                });
            });
        });
    </script>

</head>
<body>
    <div id="mapDiv"></div>
</body>
</html>

If I uncomment out line 51 (adding the transformation information as Dan_Patterson‌ suggested), the service never returns a result.

0 Kudos
NhuMai
by
New Contributor II

In this case, the script in question doesn't run until map is loaded (there's a handler with map creation). The lat long prints out fine, so I'm pretty sure the inputPoint is defined. How does the line for transformation work? I'd tried defining the geographic datum directly on the input point (see my response to Dan Patterson), but still getting undefined.

0 Kudos
NhuMai
by
New Contributor II

I tried your line 51 and defined transformForward just before it, but it's still returning 'undefined':

prjParams.transformForward = true;

prjParams.transformation = 15832;

0 Kudos
KenBuja
MVP Esteemed Contributor

Thanks for pointing out the the transformForward property...I overlooked that. If you look at the transformation property, you'll see it has an additional property, wkid. However, I'm still not getting a response from the Geometry Service in my code when using the transformation.

0 Kudos
NhuMai
by
New Contributor II

"Not getting a response from the Geometry Service" means that "Conversion completed" isn't printing out for you either?

In my case, I get "Conversion completed", but X and Y print out "undefined."

0 Kudos
KenBuja
MVP Esteemed Contributor

Is this using the same code you have in your response to Dan? If so, you need to put the console.log statements inside the project callback function.

geometryService.project(prjParams, function (outputpoint) {
  console.log('Conversion completed');
  X = outputpoint[0].x;
  Y = outputpoint[0].y;
  var center = "X : " + X + " - Y : " + Y;
  console.log(latLong);
  console.log(center);
});

 

0 Kudos
NhuMai
by
New Contributor II

Okay, makes sense. So I did that, but I still get the same print statements (just a bit later than when they are outside the function) :

Conversion completed
Latitude : *correct latitude* - Longitude : *correct longitude*
X : undefined - Y : undefined

Printing outputpoint gives me this:

Array[1]
0:Object
spatialReference:Object

wkid:3297
type:"point"
x:"NaN"
y:"NaN"
__proto__:Object
length:1
__proto__:Array[0]

 

Getting why X and Y are undefined, but still not seeing why the projection didn't work.

**UPDATE**

I saw this info from another thread :

I had a similar problem, and found a soltuion that might help.    I needed to project a mapPoint from 102100 to other systems using the GeometryService.Project, however this would consistently return a mapPoint with NaN coordinates.   The solution was to first check that the map point is in 102100, if so pass this to the WebMercatorUtil.webMercatorToGeographic to get a latlong mappoint.   Then use the geometry service to project this latlong mapPoint into whatever other coordsystem I needed.   BTW, I'm working with the API for Flex.

-Greg Nichols, TAIC

I was previously under the impression that my coordinates were GCS, but I may be wrong.

How to check what is the CS here? --> var lat = window.myMap.extent.getCenter().getLatitude().toFixed(2);

With that being said, I tried the following code :

var lat = window.myMap.extent.getCenter().getLatitude().toFixed(2);
var long = window.myMap.extent.getCenter().getLongitude().toFixed(2);

var latLong = "Latitude : " + lat + " - Longitude : " + long;

var X;
var Y;

var rgpfSixSudSR = new SpatialReference ({
wkid : 3297
});

var webMercatorSR = new SpatialReference ({
wkid : 102100
});

var geometryService = new GeometryService("http://ourserver/arcgis/rest/services/Utilities/Geometry/GeometryServer");
var mercatorPoint = new Point(lat, long, webMercatorSR);
var inputPoint = webMercatorUtils.webMercatorToGeographic(mercatorPoint);
console.log(inputPoint);


var prjParams = new ProjectParameters ();
prjParams.geometries = [inputPoint];
prjParams.outSR = rgpfSixSudSR;

geometryService.project(prjParams, function (outputpoint) {
    console.log('Conversion completed');
    X = outputpoint[0].x;
    Y = outputpoint[0].y;
    console.log(latLong);
    console.log(center);

});

It prints out this :

Object

spatialReference: Object

type: "point"

x: -0.00015154578843096328

y: -0.0013529526492823366

__proto__: Object
Conversion completed
Latitude : -16.87 - Longitude : -150.61
X : undefined - Y : undefined

Note that I left the real "lat long" values, because I'm having doubts as to what is the correct wkid. Not sure now if it's 102100 or 4326...How can I check that?

0 Kudos