Select to view content in your preferred language

Create Point FC from Mouse Click Event

4591
2
04-26-2013 10:54 AM
by Anonymous User
Not applicable
Original User: wjackson

I'm starting to learn the Python Add-in starting with some simple code. This bit attempts to capture XY of mouse click event and write it to a point feature. The message box displays the XY but nothing else seems to be working. Can anyone show me what'sw wrong?



import arcpy
from arcpy import env
import pythonaddins

class ToolClass2(object):
    """Implementation for ArcGISAddins_addin.tool (Tool)"""
    message = "Your mouse clicked:"
    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):
        message = "Your mouse clicked:" + str(x) + ", " + str(y)
        pythonaddins.MessageBox(message, "My Coordinates")
        
        env.outputCoordinateSystem = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
        
        point = arcpy.Point(x, y)
        pointGeometry = arcpy.PointGeometry(point)
        """ Create a copy of the PointGeometry objects, by using pointGeometry  """
        """  as input to the CopyFeatures tool. """
arcpy.CopyFeatures_management(pointGeometry, "c:/temp/test.gdb/points")
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: Tonyalmeida

I believe you are missing env.workspace and outWorkspace.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000035000000

Along with  arcpy.mapping.MapDocument, arcpy.mapping.ListLayer and arcpy.mapping.ListDataFrames

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Layername")[0]

# Set environment settings
env.workspace = "C:/data"

# Set local variables
outWorkspace = "c:/output/output.gdb"
0 Kudos
TonyAlmeida
Frequent Contributor
Something similar to this.

import arcpy
import pythonaddins
import os
from arcpy import env

class AddXYPoint(object):
    """Implementation for PythonAddOn_addin.AddXYPoint_1 (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 3
    def onMouseDownMap(self, x, y, button, shift):

        fc = "Points"
        workspace = r"C:\Temp\Test.mdb"
        arcpy.env.overwriteOutput = True

        # Start an edit session. Must provide the worksapce.
        edit = arcpy.da.Editor(workspace)

        # Edit session is started without an undo/redo stack for versioned data
        #  (for second argument, use False for unversioned data)
        edit.startEditing(True)

        # Start an edit operation
        edit.startOperation()
        
        mxd = arcpy.mapping.MapDocument("Current")
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        lyr = arcpy.mapping.ListLayers(mxd, "Points")[0]


        message = "Your mouse clicked:" + str(x) + ", " + str(y)
        pythonaddins.MessageBox(message, "My Coordinates")
                
                
        point = arcpy.Point(x, y)
        pointGeometry = arcpy.PointGeometry(point)
        row_value = ((x, y))
        cursor = arcpy.da.InsertCursor(fc, ("SiteNum", "SHAPE@XY"))
        cursor.insertRow(row_value)
        """ Create a copy of the PointGeometry objects, by using pointGeometry  """
        """  as input to the CopyFeatures tool. """
        arcpy.CopyFeatures_management(pointGeometry, "C:\Temp\Test.mdb")

        # Stop the edit operation.
        edit.stopOperation()

        # Stop the edit session and save the changes
        edit.stopEditing(True)


        arcpy.RefreshActiveView()


        pass
    

0 Kudos