Select to view content in your preferred language

Issue using esri/geometry/project to convert from SR 4326 to SR 26912

2833
3
Jump to solution
04-15-2021 08:40 AM
AlexGilvarry1
Occasional Contributor

I'm trying to to get coordinates by clicking in my map, and then project them into  NAD 83 Zone 12 so I can feed them to our Roads and Highways REST API, but I'm having trouble trying to make the conversion using the geometry projection method.

You can see the entire app on github at: https://github.com/utahdot/Stationing/blob/main/main.js
and there is a live preview at: https://utahdot.github.io/Stationing/

Basically I'm getting the x/y values from the view on a click, trying to make a point geometry from those, and then trying to reproject that point, but it always come up null. I've tested projecting to other coordinate systems(web mercator for example) and that gives me back a result.

I think this is the relevant bit of code

 

 

    view.on("click", function(event){
      const WGS84 = new SpatialReference({ wkid: 3857 });
      const NAD83 = new SpatialReference({ wkid: 26912 });
      console.log(event.mapPoint.x);
      console.log(event.mapPoint.y)
      let point = new Point({
        type: "point",
        x: event.mapPoint.x,
        y: event.mapPoint.y
      })
      projection.load().then(function(){

        // const transformations = projection.getTransformations(WGS84, NAD83);
        // console.log(transformations);

        point = projection.project(point, NAD83);
        console.log(point);
  
      });

    });

 

 

It's commented out but I tried to use the getTransformations() function to see if anything other than the default would work. Thus far it didn't make a difference. Sometimes I read the documentation incorrectly so hopefully there is something I'm doing wrong here and it's not an issue with the projection method.

0 Kudos
1 Solution

Accepted Solutions
AlexGilvarry1
Occasional Contributor

The issue was a silly one. I was trying to use the X/Y when creating the point, but when I used lat/lon from the map click it started working.

View solution in original post

0 Kudos
3 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

What problem are you running into? Are you getting errors? Or the projection is incorrect? The one I can see from the code snippet is that the spatialReference is not being set on the point you are passing into project method. You can directly pass in the event.mapPoint to the projection method or try setting the spatial reference of the point as shown below before you pass it into projection method. 

let point = new Point({
   x: event.mapPoint.x,
   y: event.mapPoint.y,
   spatialReference: WGS84
});

 

JohnGrayson
Esri Regular Contributor


I haven't tested this out, but I wonder if actually using WGS84 (4326) for the source point might help?

view.on("click", function(event){
      console.log(event.mapPoint.longitude,event.mapPoint.latitude);

      const WGS84 = new SpatialReference({ wkid: 4326});
      const NAD83 = new SpatialReference({ wkid: 26912 });

      let point = new Point({
        spatialReference: WGS84,
        x: event.mapPoint.longitude,
        y: event.mapPoint.latitude
      })
      projection.load().then(function(){

        // const transformations = projection.getTransformations(WGS84, NAD83);
        // console.log(transformations);

        point = projection.project(point, NAD83);
        console.log(point);  
      });
    });

 

AlexGilvarry1
Occasional Contributor

The issue was a silly one. I was trying to use the X/Y when creating the point, but when I used lat/lon from the map click it started working.

0 Kudos