Geometry cannot be converted to spatial reference of the map

10096
9
Jump to solution
08-25-2014 10:57 AM
JohnChurchill
New Contributor III

Please help me if you can,

I'm getting the following message in the console ...

Map: Geometry (wkid: 102685) cannot be converted to spatial reference of the map (wkid: 102100)

 

Similar to this thread

https://community.esri.com/thread/66936

 

I also found this one which appears related

Map options: issues with non-mercator spatial reference 

 

I tried using the suggestion by Adam Skoog in that first thread (66936) but when I get to what shows up as line # 07. In his code, I get an error as follows:

Uncaught TypeError: Cannot read property 'spatialReference' of undefined

 

Which seems odd since all of this is inside a require block that includes Map, ProjectParameters, SpatialReference, Extent, Geometry, GeometryService etc. etc.

 

My code looks like this

 

    esriConfig.defaults.io.proxyUrl = "/proxy";

    esriConfig.defaults.io.alwaysUseProxy = false;

    esriConfig.defaults.geometryService = new GeometryService("http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

 

// I have xMin, xMax, yMin, yMax sent in to this php file and it seems I need the Number() fx to get real numbers

// This prints the following to the console ...

// -8819410.666559163 4792613.494165327 -8813596.67509507 4794481.424044095 pmeasure.js:50

// -8816503.670827117 4793547.459104711 pmeasure.js:51

// AND IF THE BLOCK STARTING WITH "var defer = " IS UNCOMMENTED, I GET THIS

// Uncaught TypeError: Cannot read property 'spatialReference' of undefined

 

 

    var avgX = ((Number(xMin) + Number(xMax)) / 2);

    var avgY = ((Number(yMin) + Number(yMax)) / 2);

 

 

    console.log(xMin, yMin, xMax, yMax);

    console.log(avgX, avgY);

    var passedExtent = new esri.geometry.Extent(Number(xMin), Number(yMin), Number(xMax), Number(yMax), new esri.SpatialReference({wkid:102685}) );

    var projectParams = new esri.tasks.ProjectParameters();

    projectParams.geometries = passedExtent;

    projectParams.outSR = new esri.SpatialReference({wkid:102100}); // map.spatialReference;  ummmmm map not defined yet

    projectParams.spatialReference = new esri.SpatialReference({wkid:2248}); // map.spatialReference;

   

    // var defer = esri.config.defaults.geometryService.project(projectParams);

    // dojo.when(defer, function (projectedGeometry) {

    //   if (projectedGeometry.length > 0) {

    //     map.setExtent(projectedGeometry[0]);

    //   }

    // });

 

    map = new Map("map", {

      basemap: "streets",

      center: [avgX, avgY], // center: [-79.23, 39.51],

      extent: passedExtent,

      zoom: 12

    });

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

John,

   Those are definitely Web Mercator already.

View solution in original post

9 Replies
RobertScheitlin__GISP
MVP Emeritus

John,

   Maybe I am mis-reading this but projectParams does not have a spatialReference property.

JohnChurchill
New Contributor III

The line that gives the error is this line.

var defer = esri.config.defaults.geometryService.project(projectParams);

I did add a line

projectParams.spatialReference = new esri.SpatialReference({wkid:2248});

but if I comment that out I still get that error in red. In fact that is why I added it (in a weak attempt to fix the error).

Uncaught TypeError: Cannot read property 'spatialReference' of undefined

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   projectParams.geometries = passedExtent; is suppose to be an array of geometries like:

projectParams.geometries = [passedExtent];

0 Kudos
JohnChurchill
New Contributor III

Ok - Thanks for your help -  if I send it this ...

spatRef = new esri.SpatialReference({wkid:2248});

var passedExtent = new esri.geometry.Extent({"xmin": Number(xMin), "ymin": Number(yMin), "xmax": Number(xMax), "ymax": Number(yMax), "spatialReference":spatRef});`

and then do this ...

    var projectParams = new ProjectParameters();

    projectParams.geometries = [passedExtent];

    projectParams.outSR = map.spatialReference;

Now I'm getting this error instead ...

Uncaught TypeError: Cannot read property 'wkid' of undefined

which is actually where I was earlier ... and I still get this ...

Map: Geometry (wkid: 102685) cannot be converted to spatial reference of the map (wkid: 102100)

I noted too that the brackets don't matter, I get the exact same result either way with

projectParams.geometries = [passedExtent];

or

projectParams.geometries = passedExtent;

Am I formatting the "Extent" incorrectly ? It seems to me I'm formatting it just like in this guide Setting and using extents in a map | Guide | ArcGIS API for JavaScript

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   Can you give me some example of the extent numbers you are trying to pass (and describe where that extent should be in the world) and its wkid 102685 or 2248. I need to trow together a sample to see what going on.

0 Kudos
MelitaKennedy
Esri Notable Contributor

I don't know what the problem is, but I wanted to double-check the spatial references (coordinate systems) that you're using. wkid:102685 and wkid:2248 are both NAD 1983 (US feet) State Plane Maryland. You should be able to use either wkid unless you're using a very old geometry service, and we changed the wkid from 102685 to 2248 a while ago. Are you positive that the values/extent that you're passing are in this coordinate system, or in Web Mercator? Note: I may have the map vs source/layer coordinate systems backwards. If they were actually Web Mercator or lat/lon, that can cause errors like "can't convert".


Another possibility is that it's complaining about the two different GCS: NAD83 and WGS84, but then there should be some way to set a geographic/datum transformation.

Melita

JohnChurchill
New Contributor III

Thanks for your response - Yes I'm using MD State Plane. Here is a link to the service I'm trying to use.

P_and_Z/Parcels_and_Zoning (MapServer)

That is good information because I wasn't sure which to use or why it was on the list twice (now I know). It is probably best to use 2248 then if it is more up to date.

It is possible (maybe probable now that I look at the extents of my service) that these coordinates are Web Mercator and not wkid: 2248

so perhaps I shouldn't be trying to project these anyway but I didn't have any luck earlier trying to set the map extent e.g.

but maybe I should go back and work from this instead.

    map = new Map("map", {

      basemap: "streets",

      center: [avgX, avgY], // center: [-79.23, 39.51],

      extent: passedExtent,

      zoom: 12

    });

the xMin, xMax, yMin, and yMax I get in console.log look like this

-8819410.666559163 4792613.494165327 -8813596.67509507 4794481.424044095 pmeasure.js:50

-8816503.670827117 4793547.459104711 (avgX and avgY)
which is not in the range I would expect.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

John,

   Those are definitely Web Mercator already.

JohnChurchill
New Contributor III

Well I'm happy to say that I got this to work the way I wanted to. I gave Robert credit for the correct answer since he was diligent in helping me out, though Melita got me on the right track. Thanks to Robert and Melita's guidance, I confirmed that the coordinates I was getting were Web Mercator already so I did not need to attempt to project them. I assumed my map was in Web Mercator and that the streets basemap was in Web Mercator but it did occur to me that when I created the map variable, I was setting the center to [-79.2, 39.5] which I know are geographic coordinates wkid:4326. I'm not sure how or why the map is using 4326.

I also learned from reading about this that I don't need the entire extent (this is apparently an old way of doing this), I just needed the center and zoom level which are available through the getCenter and getLevel functions. I'm using v3.10 of the arcgis javascript API btw.

I used webMercatorUtils to convert my center coordinates to lat/longs, and got the zoom level, and successfully got the map to load the php file with the extent from the original map html file.

(function(){require(["esri/geometry/webMercatorUtils"],function(w){var c=w.webMercatorToGeographic(map.extent.getCenter());console.log([parseFloat(c.x.toFixed(3)), parseFloat(c.y.toFixed(3))]);});})();

I found this link where I got the above code snippet to be very helpful ...

BearPanther.com | Esri JavaScript map center, zoom level and extent

My testing code (in the html file) looked like this

          var center=webMercatorUtils.webMercatorToGeographic(map.extent.getCenter());

          passedX = parseFloat(center.x.toFixed(5));

          passedY = parseFloat(center.y.toFixed(5));

          zoomLevel = map.getLevel();

          console.log(passedX + ", " + passedY);

          console.log("Zoom: " + zoomLevel);

0 Kudos