7

In an attempt to create a python add-in for ArcMap that will pass x and y coordinates to another application via a mouse click on the map, I have written the following code. I need the output coordinates to be in WGS_1984 and therefore have to project a geometry object. The problem I'm running into is that whenever the script executes the projectAs() method I get the following error. Code at bottom

enter image description here

import arcpy
import pythonaddins
import webbrowser
import os, sys
from arcpy import env
arcpy.env.overwriteOutput = True
class ToolClass2(object):
 """Implementation for StreetView_addin.tool (Tool)"""
 def __init__(self):
 self.enabled = True
 self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
 def onMouseDownMap(self, x, y, button, shift):
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames(mxd)[0]
 sr_df = df.spatialReference
 xValue = x
 yValue = y
 pointList = [[xValue, yValue]]
 point = arcpy.Point()
 for pt in pointList:
 point.X = pt[0]
 point.Y = pt[1]
 pointGeometry = arcpy.PointGeometry(point, sr_df)
 pointGeometry.projectAs("WGS 1984")
 centroid = str(pointGeometry.centroid)
 centList = centroid.split(" ")
 xValue2 = centList[0]
 yValue2 = centList[1]

I have tried adding the optional transformation for the projectAs() method as well without success. What am I missing here?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 4, 2015 at 21:16
9
  • 1
    The problem would appear to be that the point has no spatial reference to start from try pointGeometry=arcpy.PointGeometry(point,df.spatialReference) before .projectAs. The rest of the code looks fine but doesn't appear to do much, I assume you're exporting xValue2 and yValue2 to a file or using them in some other way.. Commented Mar 4, 2015 at 21:31
  • @Michael Miles-Stimson The sr_def object contains the original spatial ref via the 3rd and 4th lines. You have assumed correctly, this is only a portion of what I'm doing, xValue2 and yValue2, are going to be used by another portion of the script once I can get them right. I suppose another way to ask the same thing would be... How do I make the centroid property return the Decimal Degree equivalent in "WGS 1984"? Commented Mar 4, 2015 at 21:49
  • 1
    Your code is mostly right but you're destroying the spatial reference by taking it out to a string and replacing underscores, supply the spatial reference as an object derived from the data frame. Commented Mar 4, 2015 at 21:51
  • That makes sense, and it now works except when I try to return the centroid it is still the original x, and y values. I'll update the code to show where it's at now. Thanks Commented Mar 4, 2015 at 22:00
  • What's the data frame's / input coordinate system? Commented Mar 4, 2015 at 22:13

2 Answers 2

12

@GeoJohn: I don't know if you've solved this, but the result of the pointGeometry.projectAs("WGS 1984") needs to be assigned to a result variable (e.g., projectedPointGeometry = pointGeometry.projectAs("WGS 1984")). The original pointGeometry is unchanged by the projectAs method. The result variable should have the location in decimal degrees. I need to do a similar thing, and was able to figure it out based on your code, so thank you for posting this question.

chrki
2,67517 silver badges23 bronze badges
answered Jan 12, 2016 at 19:32
2
  • Wow! No, I never solved this. What a simple oversight. I sort of gave up on this project since it was just something I was trying to implement for my own use. Thanks for your answer! I can probably finish this now! Commented Jan 13, 2016 at 14:56
  • The code helped me as well! Commented Oct 17, 2017 at 18:28
5

for transforming form Swiss-LV95 to WGS84 I currently use

arcpy.PointGeometry(arcpy.Point(swissX,swissY),arcpy.SpatialReference(2056)).projectAs(arcpy.SpatialReference(4326))

with ArcGIS 10.1

answered Mar 27, 2015 at 15:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.