Select to view content in your preferred language

Create multiple points with mouse click

3907
13
Jump to solution
07-24-2015 09:55 AM
PacoAlverez
Deactivated User

I would like to be able to click multiple locations and create points where i click and populate each points x, y. i have the current code but only creates one point, so i needs help.

current code.

#import modules  
import arcpy  

arcpy.env.qualifiedFieldNames = False
An = "AnimalSightings" #target point feature class Animal Sightings

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd)[0]  
dfsr = df.spatialReference  
fcsr = arcpy.Describe(An).spatialReference  
if dfsr.name == fcsr.name:  
    """Now do your work"""  

point = arcpy.GetParameterAsText(0)  #click

  
for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):        
    x,y = prow[0]      
del prow  


row_value = [x,y,(x,y)]  
with arcpy.da.InsertCursor(An,('POINT_X', 'POINT_Y','SHAPE@XY')) as cursor:
    cursor.insertRow(row_value)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

I'm not sure if you've got this figured out by now, but here is generally how you can add multiple points from a feature set to a feature class:

import arcpy

point = arcpy.GetParameterAsText(0) # the feature set (or in-memory fc)

pointFC = r'C:/junk/points.shp' # the on-disk fc

insCursor = arcpy.da.InsertCursor(pointFC,'SHAPE@XY') # create insert cursor

with arcpy.da.SearchCursor(point,'SHAPE@XY') as cursor: # loop through feature set
    for row in cursor:
        insCursor.insertRow(row) # insert row

del insCursor # delete insert cursor

Of course, you could just as easily run the Append tool rather than using cursors, but I assume you have a specific reason for going into this detail.

View solution in original post

13 Replies
WesMiller
Deactivated User

You first need to nest every thing under your if statement you'll be able to tell by the indentation

Then move line 19 below line 24 and nest everything 19 - 24 under the for statement. del prow should be aligned with the for statement be careful and watch your indentation it's important in python

0 Kudos
PacoAlverez
Deactivated User

Like so?

#import modules  
import arcpy  

arcpy.env.qualifiedFieldNames = False
An = "AnimalSightings" #target point feature class Animal Sightings

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd)[0]  
dfsr = df.spatialReference  
fcsr = arcpy.Describe(An).spatialReference  
if dfsr.name == fcsr.name:  
    """Now do your work"""  

point = arcpy.GetParameterAsText(0)  #click
pointCount = int(arcpy.GetCount_management(point).getOutput(0))

if  pointCount >= 1:
    for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):        
        x,y = prow[0]      
     

    row_value = [x,y,(x,y)]  
    with arcpy.da.InsertCursor(An,('POINT_X', 'POINT_Y','SHAPE@XY')) as cursor:
        cursor.insertRow(row_value)
del prow 
0 Kudos
WesMiller
Deactivated User

Try to use the code below as a guide.

if sr == sr:
    point = arcpy.GetParameterAsText(0)
    for prow in SC:
        x,y
        row_value =
        with arcpy.da.insertCursor:
            cursor.insertRow()
    delprow
0 Kudos
PacoAlverez
Deactivated User

sorry, i was out for the weekend.

I was not able to get the code to allow me to create multiple points.

i a not sure what if sr ==sr: is doing other then comparing two values.

0 Kudos
WesMiller
Deactivated User

Were you able to get your code formatted correctly?

0 Kudos
CCWeedcontrol
Frequent Contributor

sorry, i was out for the weekend.

I was not able to get the code to allow me to create multiple points.

i a not sure what if sr ==sr: is doing other then comparing two values.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I'm not sure if you've got this figured out by now, but here is generally how you can add multiple points from a feature set to a feature class:

import arcpy

point = arcpy.GetParameterAsText(0) # the feature set (or in-memory fc)

pointFC = r'C:/junk/points.shp' # the on-disk fc

insCursor = arcpy.da.InsertCursor(pointFC,'SHAPE@XY') # create insert cursor

with arcpy.da.SearchCursor(point,'SHAPE@XY') as cursor: # loop through feature set
    for row in cursor:
        insCursor.insertRow(row) # insert row

del insCursor # delete insert cursor

Of course, you could just as easily run the Append tool rather than using cursors, but I assume you have a specific reason for going into this detail.

PacoAlverez
Deactivated User

interesting. so if i wanted to populate the POINT_X and POINT_Y how would i do that.

I am having no luck, i am getting error:

Traceback (most recent call last):

  File "C:\GIS\Python\AddPoint\AddPoint_4.py", line 23, in <module>

    row[0] = centroid.x

NameError: name 'centroid' is not defined

#import modules  
import arcpy  

arcpy.env.qualifiedFieldNames = False
pointFC = "AnimalSightings" #target point feature class Animal Sightings

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd)[0]  
dfsr = df.spatialReference  
fcsr = arcpy.Describe(pointFC).spatialReference  
if dfsr.name == fcsr.name:  
    """Now do your work"""  

point = arcpy.GetParameterAsText(0)  #click
for prow in arcpy.da.SearchCursor(point,'SHAPE@XY'):        
        x,y = prow[0]
del prow        

insCursor = arcpy.da.InsertCursor(pointFC,('POINT_X','POINT_Y','SHAPE@XY')) # create insert cursor  

with arcpy.da.SearchCursor(point,('POINT_X','POINT_Y','SHAPE@XY')) as cursor: # loop through feature set  
    for row in cursor:
        row[0] = centroid.x
        row[1] = centroid.y
        insCursor.insertRow(row) # insert row  
  
del insCursor # delete insert cursor
0 Kudos
DarrenWiens2
MVP Honored Contributor
NameError: name 'centroid' is not defined

^ means there is no variable yet defined as "centroid". This "centroid" thing you are looking for is a property of a PointGeometry. A PointGeometry's centroid is a Point, which has properties 'X' and 'Y'.

Using SHAPE@XY:

This returns an x,y tuple, not a geometry object. You can create a Point from this tuple like so:

point = arcpy.Point(row[2][0],row[2][1])
print point.X
print point.Y

Using SHAPE@:

This returns the geometry itself, so if you are reading points, row[2] will be a point object.

0 Kudos