Getting XY from mouse click using python

25476
22
02-03-2012 07:42 AM
MichelleHawks
New Contributor
Hi all,
I would like to create a button that captures the location of the next mouse click and displays it in LAT/LONG. I imagine is would have to open a submenu which would be closed when the user is finished getting location information. Is there any way to do this using python?
Tags (2)
0 Kudos
22 Replies
LoganPugh
Occasional Contributor III
In 10.1 I believe Python add-ins will be able to do this, but for 10.0 and earlier you would need to use ArcObjects.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can get the lat/long locations by creating a tool, but as Logan mentioned, you will most likely need ArcObjects to display the lat/long in a menu.  Here is a tool sample that obtains the coordinates from where you click and copies the values to clipboard:

http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=0BF2E736-1422-2418-3416-5AA91...
0 Kudos
IanThomas1
New Contributor III
Hi All,

So 10.1 is now here...

I'm looking for the Python add-ins that will be able to do this neat trick
"Getting XY from mouse click using python":

Specifically I would like to rewire the Fishnet function in ModelBuilder so
that the user can simply click the screen to set both origin_coord & y_axis_coord
interactively rather than having to type all coordinates in....

CreateFishnet_management(out_feature_class, origin_coord, y_axis_coord, cell_width, cell_height, number_rows, number_columns, {corner_coord}, {labels}, {template}, {geometry_type})


Very grateful if anybody can point me in the right direction.

Many thanks,

Ian
0 Kudos
KimOllivier
Occasional Contributor III
The help for addins has exactly this fishnet as an example. I did manage to get it to work, but....
Addins are quite complex and difficult to debug. For each fix you have to recompile the addin, copy the files to the addins directory and restart ArcGIS every time because there is no equivalent to the python 'reload' command.

If you only want to get a couple of points, then do this at the start using the featureset type in the dialog for a normal tool. This is a very flexible input method that can take any feature type (point, line, polygon) to generate a temporary featureclass that is delivered as a parameter to use. You can also add a geoprocessing tool to a menu with a simple icon. These abilities have been available since 9.3 but has not been properly publicised.

Popping up a coordinate in a box can be done in python using the windows message box.

import win32ui,win32con
for i in range(32) :
    ok = win32ui.MessageBox("type"+str(i),"Message Title",i)
    print "Form type",i,"Button",ok
0 Kudos
JohnDye
Occasional Contributor III
Did anyone ever figure this out? The link referenced by Jskinn3 refers to getting an coordinate extent, not the X, Y point of a mouse click.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi John,

The extent is created from two XY locations from two mouse clicks.  If you want the extent, you would click in the lower left corner and the upper right corner for the area of interest.  If you just want a single location you would just specify one mouse click and execute the tool.
0 Kudos
JohnDye
Occasional Contributor III
Hi Jskinn,
Thanks for the quick response. Do you know if there is anyway to do this in Python without requiring the Win32clipboard module. Rather than store the coordinate pair in the clip board, I'm trying to get them into a variable which I can then pass into a Google Map search. I'm essentially trying to rewrite this addin (http://resources.arcgis.com/gallery/file/arcobjects-net-api/details?entryID=48F2BB6C-1422-2418-8822-...) in Python instead of VB
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You could use something like:

import arcpy

input = arcpy.GetParameterAsText(0)

cur = arcpy.SearchCursor(input)

for row in cur:
    geom = row.Shape
    X = geom.centroid.X
    Y = geom.centroid.Y
    XY = str(X) + ", " + str(Y)
    
del row, cur

arcpy.AddMessage(XY)


Here the X,Y coordinates are stored in a variable XY.  For the input parameter, you will want to set the data type as Feature Set.
0 Kudos
JohnDye
Occasional Contributor III
Jake,
Thanks a bunch. That's exactly what I did after I started playing with the code on Friday. After I posted the message, it dawned on me that the Win32 modules were just being used for the clipboard. My Code is exactly the same, except I didnt concatenate the X and Y coordinates, I want them seperate so I can pass them into a URL individually.
0 Kudos