projectAs help

6129
14
Jump to solution
05-13-2013 05:56 AM
JamesCrandall
MVP Alum
I'm attempting to implement some of the functionality found here:

http://support.esri.com/en/knowledgebase/techarticles/detail/40730

I need to acquire the x/y values where mouse click occurs, convert the values into Latitude/Longitude and then report these values.

The plan is to require a layer to be loaded in the map TOC, so there will be a defined spatial reference to go from.  No problem getting these projected x/y values so far but I am not able to project these to WGS1984.  Is this even possible?  Or will I have to use in_memory in some capacity here?

      def onMouseDownMap(self, x, y, button, shift):         print "x: " + str(x) + " y: " + str(y) ## this reports as expected!                          ## get a layer loaded in the TOC to determine the spatial reference                 mxd = arcpy.mapping.MapDocument("CURRENT")         df = arcpy.mapping.ListDataFrames(mxd)[0]         testCount = len(arcpy.mapping.ListLayers(mxd, "", df))         if testCount==0:             msg = "There are no features selected to report"             pythonaddins.MessageBox(msg, 'Report Latitude/Longitude Values', 0)         else:               for lyr in arcpy.mapping.ListLayers(mxd):             dsc = arcpy.Describe(lyr)             spref = dsc.spatialReference              lpoint = arcpy.Point()             lpoint.X = x             lpoint.Y = y             ptGeometry = arcpy.PointGeometry(lpoint)             #ptGeom = arcpy.PointGeometery(arcpy.point(x,y),spref, False, False)              to_sr = arcpy.SpatialReference('WGS 1984')                                  projectedPoint = ptGeometry.projectAs(to_sr, r'NAD_1983_HARN_To_WGS_1984')             prjX = projectedPoint.X             prjY = projectedPoint.Y             print "X:" + str(prjX) + " Y:" + str(prjY) 


The code above fails on the line:

[INDENT]projectedPoint = ptGeometry.projectAs(to_sr, r'NAD_1983_HARN_To_WGS_1984')[/INDENT]



Error msg:
"projectedPoint = ptGeometry.projectAs(to_sr, r'NAD_1983_HARN_To_WGS_1984')
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 761, in projectAs
    return convertArcObjectToPythonObject(self._arc_object.ProjectAs(*gp_fixargs((spatial_reference, transformation_name))))
ValueError: NAD_1983_HARN_To_WGS_1984"
Tags (2)
0 Kudos
14 Replies
ChrisSnyder
Honored Contributor
I think you want GCS_WGS_1984 = 4326 (on page 18). I messed around with it and it seems to work for me like this:

pntObj = arcpy.Point(X= 12345,Y=6789)  inSr = arcpy.SpatialReference(2927) #NAD_1983_HARN_StatePlane_Washington_South_FIPS_4602_Feet outSr = arcpy.SpatialReference(4326) #GCS_WGS_1984 pntGeom = arcpy.PointGeometry(pntObj, inSr) wgs84PntGeom = pntGeom.projectAs(outSr, "NAD_1983_HARN_To_WGS_1984_2")



Which yields:

>>> wgs84PntGeom.JSON
u'{"x":-126.81813166550971,"y":45.173061523748544,"spatialReference":{"wkid":4326}}'
0 Kudos
JamesCrandall
MVP Alum
I think you want GCS_WGS_1984 = 4326 (on page 18). I messed around with it and it seems to work for me like this:

pntObj = arcpy.Point(X= 12345,Y=6789) 
inSr = arcpy.SpatialReference(2927) #NAD_1983_HARN_StatePlane_Washington_South_FIPS_4602_Feet
outSr = arcpy.SpatialReference(4326) #GCS_WGS_1984
pntGeom = arcpy.PointGeometry(pntObj, inSr)
wgs84PntGeom = pntGeom.projectAs(outSr, "NAD_1983_HARN_To_WGS_1984_2")



Which yields:

>>> wgs84PntGeom.JSON
u'{"x":-126.81813166550971,"y":45.173061523748544,"spatialReference":{"wkid":4326}}'


Thanks for this -- once I figured out my inSr WKID, it returns the lat/lon correctly.  I went ahead and made your post the answer, but... 

Can you suggest how to parse out the actual x/y values out of that JSON string? 

Thanks again for your help.
j
0 Kudos
JamesCrandall
MVP Alum


Can you suggest how to parse out the actual x/y values out of that JSON string? 



Pretty simple to do:


      data = json.loads(wgs84PntGeom.JSON)
      wgsX = data["x"]
      wgsY = data["y"]
      print "X:" + str(wgsX) + " Y:" + str(wgsY)

0 Kudos
ChrisSnyder
Honored Contributor
I think this would be the "best" way. Since it's a point you could also use the .centroid property (and some other ones too).

partObj = wgs84PntGeom.getPart(0)
partObj.X
partObj.Y


So, just to clarify - probably best not to parse the JSON... I just put that in there to illustrate it was returning WGS84 coordinates.
0 Kudos
JamesCrandall
MVP Alum
I think this would be the "best" way. Since it's a point you could also use the .centroid property (and some other ones too).

partObj = wgs84PntGeom.getPart(0)
partObj.X
partObj.Y


So, just to clarify - probably best not to parse the JSON... I just put that in there to illustrate it was returning WGS84 coordinates.


Understood.  Although that JSON string is fast enough to print out for the onMouseMoveMap event as the user moves across the view!

Thanks again
0 Kudos