HowTo:  Capture map coordinates with a mouse click using Python

相关信息
Article ID: 40730
Software:
ArcGIS for Desktop Advanced 10.1
ArcGIS for Desktop Standard 10.1
ArcGIS for Desktop Basic 10.1
Platforms:
Windows XP, Server 2003, Vista, Server 2008, Windows 7, Windows 8, Server 2012

问题描述
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.
已邀请:

EsriSupport

赞同来自:

解决方案

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: 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. 
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: 


其它相关参考
  1. 10.1 Help: Tool Class
  2. 10.1 Help: Creating a Python Add-in Tool
  3. Python Add-in Wizard
  4. ArcGIS Help 10.1 - What is a Python Add-in


创建及修改时间
Created: 12/7/2012

Last Modified: 7/14/2013
原文链接
http://support.esri.com/en/kno ... 40730

要回复问题请先登录注册