Like a blind squirrel accidentally finding a nut, I managed to answer this question without understanding the main interesting point: collecting a click event and populating a script parameter with its coordinates.
Here's the final, working script in question:
#import modules import arcpy point = arcpy.GetParameterAsText(0) #click An = "AnimalPoints" #target point feature class Animal Sightings for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'): x,y = prow[0] del prow point1 = arcpy.Point(x, y) ptGeometry = arcpy.PointGeometry(point1) with arcpy.da.InsertCursor(An,['SHAPE@XY']) as cursor: cursor.insertRow([(x,y)])
Can someone please explain in what context line 4 ("point = ...") could possibly be populated with coordinates from a click event on the map? My understanding is that this is run from a regular old script tool (parameter type = record set), in a regular old toolbox, not a Python Add-In.
Solved! Go to Solution.
I think for what you're wanting you'll want to use a python addin so you can actually work with the OnMouseDown event and get a single xy coordinate when the user clicks on the map.
Your point variable is actually referencing a FeatureSet object, which could contain multiple points. A call to arcpy.GetParameter would return the actual FeatureSet object, whereas your call to GetParameterAsText would return a path to the FeatureSet in memory (i.e. something similar to in_memory\{9F58A122-7DA8-42B0-A3DF-AD02622C1E6C}).
The context of this line would be the same as if you were point to a feature class. This class would provide access to a table that you would need to parse and add its values to your table.
I think for what you're wanting you'll want to use a python addin so you can actually work with the OnMouseDown event and get a single xy coordinate when the user clicks on the map.
Your point variable is actually referencing a FeatureSet object, which could contain multiple points. A call to arcpy.GetParameter would return the actual FeatureSet object, whereas your call to GetParameterAsText would return a path to the FeatureSet in memory (i.e. something similar to in_memory\{9F58A122-7DA8-42B0-A3DF-AD02622C1E6C}).
The context of this line would be the same as if you were point to a feature class. This class would provide access to a table that you would need to parse and add its values to your table.
Thanks, Freddie. Perhaps I misread the original question here, which indicated that the working script generated a new point feature based on the click coordinates, not used a previously existing point feature from an existing feature class.
My understanding was also that this would have to be done using a Python Add-In, but that didn't seem to be how he was using it.
The code does generate records based on the user's click coordinates, but as a result of applying this approach from a script tool the points are first collected from the tool's UI prior to being communicated to the code behind the tool as a FeatureSet.
The Add-Ins framework will be a little different because you'd have access to the MouseEvents, which would only pass a single point one at a time instead of using a FeatureSet.
My only suggestion would be to utilize the framework that provides the experience you're wanting. For example, say that you want to create a buffer based on the locations where a user clicks on the map. Option A would be to leverage the Script Tool/ModelBuilder approach. This would mean someone would open the GP tool, click everywhere on the map they want a point, and then click ok to begin processing. Option B would be to implement a Tool where you could wire into the map's OnMouseDown event. In this case every individual click would block the UI thread and generate a buffer prior to allowing the user to click the next point.
Thanks again, Freddie.
My major problem was an incomplete understanding of what a Feature Set is, and how to use it. In particular, I did not realize, until now, that the crucial step I was missing in setting up my Feature Set parameter was setting its schema.
Anyhow, now that I've set the schema, I'm off to the races.