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)
Solved! Go to Solution.
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")
import arcpy theFC = r"c:\temp\myFeatureclass.shp" g = arcpy.Geometry() to_sr = arcpy.SpatialReference('WGS 1984') geometryList = arcpy.CopyFeatures_management(theFC, g) for geometry in geometryList: projectedGeometry = geometry.projectAs(to_sr,r'NAD_1983_HARN_To_WGS_1984') print projectedGeometry.centroid.X, projectedGeometry.centroid.Y
import arcpy theFC = r"c:\temp\myFeatureclass.shp" g = arcpy.Geometry() to_sr = arcpy.SpatialReference('WGS 1984') geometryList = arcpy.CopyFeatures_management(theFC, g) for geometry in geometryList: projectedGeometry = geometry.projectAs(to_sr,'NAD_1983_To_NAD_1983_CSRS_4 + NAD_1983_CSRS_To_WGS_1984_2') print projectedGeometry.centroid.X, projectedGeometry.centroid.Y
def onMouseDownMap(self, x, y, button, shift): print "x: " + str(x) + " y: " + str(y) ## 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, spref) ptFC = "in_memory\lpoint" if arcpy.Exists(ptFC): arcpy.Delete_management(ptFC) arcpy.CreateFeatureclass_management("in_memory", "lpoint", "POINT", '', "DISABLED", "DISABLED", spref, '') cursor = arcpy.da.InsertCursor(ptFC, "SHAPE@XY") cursor.insertRow((lpoint,)) del cursor to_sr = r'GEOGCS["GCS_WGS_1984",' + \ 'DATUM["D_WGS_1984",' + \ 'SPHEROID["WGS_1984",6378137,298.257223563]],' + \ 'PRIMEM["Greenwich",0],' + \ 'UNIT["Degree",0.017453292519943295]]' flds = ["SHAPE@X", "SHAPE@Y"] scursor = arcpy.da.SearchCursor(ptFC, flds, '', to_sr) prjx = 0.00 prjy = 0.00 for row in scursor: #print "X: " + str(row[0]) + " Y: " + str(row[1]) prjx = row[0] prjy = row[1] del scursor msg = "Latitude: " + str(prjx) + "\n" + "Longitude: " + str(prjy) pythonaddins.MessageBox(msg, 'Report Latitude/Longitude Values', 0)
The fact an input shapefile is being used shouldn't be relevant. As long as you get a pointgeometry object somehow with a defined spatial reference the projectAs should work fine.
Your first solution shouldn't have any issue other than something being wrong with your transformation parameter (r'NAD_1983_HARN_To_WGS_1984'). I get your same error if I use the shapefile and your transformation. The samples I provided were simplified to use shapefiles to eleminate the rest of the code as being an issue.
The fact an input shapefile is being used shouldn't be relevant. As long as you get a pointgeometry object somehow with a defined spatial reference the projectAs should work fine.
Your first solution shouldn't have any issue other than something being wrong with your transformation parameter (r'NAD_1983_HARN_To_WGS_1984'). I get your same error if I use the shapefile and your transformation. The samples I provided were simplified to use shapefiles to eleminate the rest of the code as being an issue.
lyr = arcpy.mapping.ListLayers(mxd)[0] dsc = arcpy.Describe(lyr) spref = dsc.spatialReference lpoint = arcpy.Point() lpoint.X = x lpoint.Y = y ptGeometry = arcpy.PointGeometry(lpoint, spref) to_sr = arcpy.SpatialReference('WGS 1984') ## fails on this line projectedPoint = ptGeometry.projectAs(to_sr, 'NAD_1983_To_NAD_1983_CSRS_4 + NAD_1983_CSRS_To_WGS_1984_2') prjX = projectedPoint.X prjY = projectedPoint.Y print "X:" + str(prjX) + " Y:" + str(prjY)
It makes the code a bit harder to comprehend, but how about using the WKID (well known id) for your projections and transformation definitions? I have notices some "discrepencies" in the naming conventions, and well, computers like numbers, right? I just pull the codes from these documents:
C:\Program Files (x86)\ArcGIS\Desktop10.1\Documentation\geographic_transformations.pdf
C:\Program Files (x86)\ArcGIS\Desktop10.1\Documentation\geographic_coordinate_systems.pdf
C:\Program Files (x86)\ArcGIS\Desktop10.1\Documentation\projected_coordinate_systems.pdf
lyr = arcpy.mapping.ListLayers(mxd)[0] dsc = arcpy.Describe(lyr) spref = dsc.spatialReference lpoint = arcpy.Point() lpoint.X = x lpoint.Y = y ptGeometry = arcpy.PointGeometry(lpoint, spref) to_sr = arcpy.SpatialReference('WGS 1984') projectedPoint = ptGeometry.projectAs(to_sr, 1900) #<--attempted with both integer value and r'1900' prjX = projectedPoint.X prjY = projectedPoint.Y print "X:" + str(prjX) + " Y:" + str(prjY)
projectedPoint = ptGeometry.projectAs(12345, 54321)
Just use the integer (don't format as string):projectedPoint = ptGeometry.projectAs(12345, 54321)
lyr = arcpy.mapping.ListLayers(mxd)[0] dsc = arcpy.Describe(lyr) spref = dsc.spatialReference lpoint = arcpy.Point() lpoint.X = x lpoint.Y = y ptGeometry = arcpy.PointGeometry(lpoint, spref) projectedPoint = ptGeometry.projectAs(7030, 1900) prjX = projectedPoint.X prjY = projectedPoint.Y print "X:" + str(prjX) + " Y:" + str(prjY)