Select to view content in your preferred language

Creating multiple points with mouse click

1847
13
08-23-2017 01:56 AM
PaulinaR
New Contributor

Hi,

I am quite a beginner with Python, thus I will be grateful for your help .  I want to create multiple points on my map. I managed to do that but unfortunately my attribute table is not updating (it is always only one row even I added multiple points). Could you take a look at my code at tell me where is the problem? For any help thank you so much!

class Obstacle(object):
    """Implementation for new_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.shape = "circle" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
    def onMouseDown(self, x, y, button, shift):
        pass
    def onMouseDownMap(self, x, y, button, shift):
        #workspace = r"\\\\abc\\home\\xxx\\Documents\\ArcGIS\\Default.gdb"  
        #adding points to the map
        mxd = arcpy.mapping.MapDocument ("CURRENT")
        sr = mxd.activeDataFrame.spatialReference
        pntFc = r"\\abc\home\xxx\Documents\ArcGIS\treepoints"
        pt = arcpy.Point()
        pt.X = float(x)
        pt.Y = float(y)
        ptGeoms = [arcpy.Geometry ("POINT", pt, sr)]
        if not arcpy.Exists (pntFc):
            arcpy.CopyFeatures_management(ptGeoms, pntFc)
        else:
            with arcpy.da.InsertCursor (pntFc, "SHAPE@") as iCurs:
                iCurs.insertRow (ptGeoms)
            arcpy.RefreshActiveView ()

Best,

Paulina

0 Kudos
13 Replies
LukeWebb
Occasional Contributor III

In that case I can only assume your if statement is always triggering too, add a print statement to test this:

if not arcpy.Exists (pntFc):
        arcpy.CopyFeatures_management(ptGeom, pntFc)
        print "Deleting and replacing existing FC"


0 Kudos
PaulinaR
New Contributor

I added the print commend and it was printed

0 Kudos
LukeWebb
Occasional Contributor III

I cant tell you why, but you need to try work out why this script does not find the existing data:

pntFc = r"\\abc\home\xxx\Documents\ArcGIS\treepoints"
if not arcpy.Exists (pntFc):
   print "I cant find the existing data"
0 Kudos
PaulinaR
New Contributor

Yes, I will need to figure it out . Anyway, thank you for your help and time!

0 Kudos