Create point with mouse click

5771
15
Jump to solution
07-23-2015 03:06 PM
PacoAlverez
New Contributor III

I am attempting to learn python so please bare with me. I would like to create points with a mouse click. I have the following code from another post but it's not working i am getting the following error. at this point i would like to just create one point but after i get past this code i would like to be able to click anywhere and create multiple points. Can some one help me out with this code?

Traceback (most recent call last):

  File "C:\GIS\AddPoint.py", line 17, in <module>

    cursor.insertRow(row)

TypeError: sequence size must match size of the row

Failed to execute (Script).

#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(row)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Won't pretend to understand why this works and my previous suggestion didn't, but this seems to work:

>>> with arcpy.da.InsertCursor(An,['SHAPE@XY']) as cursor:
...    cursor.insertRow([(x,y)])

View solution in original post

15 Replies
DarrenWiens2
MVP Honored Contributor

You need to fill the fields passed in the cursor initiation statement. Because SHAPE@XY is tuple, you must fill that tuple when you call insertRow. See the second code sample here.

with arcpy.da.InsertCursor(An,('SHAPE@XY')) as cursor: 
    cursor.insertRow((x,y)) 

As a side question, how are you planning to capture a click event as text? Is this a Python add-in? It may be possible, I've just never seen this.

0 Kudos
PacoAlverez
New Contributor III

I have made changes to my code that you suggested but i get the same error. i am not sure what you mean by "capture a click event as text?" basically just click on the map where coyotes are spotted and create points for now but eventually add more to the code.  yes at some point it would be made into an pathon add-in. The link you posted for the sample is not working.

  File "C:\GIS\AddPoint.py", line 17, in <module>

    cursor.insertRow((x,y))

TypeError: sequence size must match size of the row

0 Kudos
DarrenWiens2
MVP Honored Contributor

Oh, you've listed 'SHAPE@XY' (itself a tuple), inside another tuple. Try:

cursor.insertRow(((x,y)))

... or remove the parentheses around 'SHAPE@XY'.

0 Kudos
PacoAlverez
New Contributor III

I have tried both suggestions but i get the same error.

0 Kudos
DarrenWiens2
MVP Honored Contributor

Won't pretend to understand why this works and my previous suggestion didn't, but this seems to work:

>>> with arcpy.da.InsertCursor(An,['SHAPE@XY']) as cursor:
...    cursor.insertRow([(x,y)])
FreddieGibson
Occasional Contributor III

Are you trying to create a script tool that allows you to dynamically click on the map to create points as part of the gp tool's UI? If so, have you tried setting your input parameter type to Feature Set?

Using the interactive feature and record input controls

http://desktop.arcgis.com/en/desktop/latest/analyze/executing-tools/using-the-interactive-feature-an...

Otherwise are you needing to implement a tool where you can wire into the OnMouseDown Event?

Creating a Python add-in tool

http://desktop.arcgis.com/en/desktop/latest/guide-books/python-addins/creating-an-add-in-tool.htm

PacoAlverez
New Contributor III

Thanks for the input, very interesting in deed. At this point i have it in a script in a tool box. Yes my input parameter type is set to Feature Set. after getting the code to run i will most likely look into making it a add-in.

0 Kudos
FreddieGibson
Occasional Contributor III

Are you wanting to parse the values from the feature set and insert them into another feature class with the same coordinate system?

PacoAlverez
New Contributor III

No i am not just want to create features on a feature class and at some point get features from another feature class and import those features into the new point.

0 Kudos