HOW TO

Capture map coordinates with a mouse click using Python

Last Published: April 25, 2020

Summary

Prior to ArcGIS 10.1, Python could not be used to interact with the ArcGIS interface. Now Python Add-ins give users the ability to create Python written tools that respond to a mouse click.

The instructions provided describe how to set up Python Add-in code to capture coordinates with a mouse click.

Procedure


Note:
Please note, using this feature requires familiarization with the use of the Python Add-ins Wizard. Refer to the Python Add-in Guide Book found in Related Information for information on making add-ins. Click the 'ArcGIS Help 10.1 - What is a Python add-in?' link to get started.

  1. Install the Python Add-in Wizard and start a project. Go through the wizard to create a new toolbar and a subsequent new tool.
  2. Open the Python script that is generated for the add-in and find the tool class that was added in Step 1.
  3. The function 'onMouseDownMap' passes the map's coordinates to the add-in. Delete all the other functions (def).

    The code should look similar to this:
    Code:
    import arcpy
    import pythonaddins

    class MC(object):
    """Implementation for MouseClickSample_addin.MCtool (Tool)"""
    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):
    pass

  4. Use the x and y variables to pass the coordinate information into the tool.

    The following code example shows a message box with the coordinates.

    Code:
    import arcpy
    import pythonaddins

    class MC(object):
    """Implementation for MouseClickSample_addin.MCtool (Tool)"""
    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")


    The results:

    [O-Image] SampleMouseClick

Article ID:000011690

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic