Select to view content in your preferred language

Getting XY from mouse click using python

28173
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
TonyAlmeida
MVP Regular Contributor
I have tried to had this as a scipt tool and as an addin but i am not having any look.

Could someone please guide me through to create this xy button please?

Thanks!
0 Kudos
TonyAlmeida
MVP Regular Contributor
Thank you for the replay but when i replace my exsiting code with what you provided my tool icon indicates it's missing.
Any ideas?



import arcpy
import pythonaddins

class XYTool(object):
    """Implementation for xy_addin.XYTool (Tool)"""
   def __init__(self):
        self.enabled = True
        self.cursor=3
    def onMouseDownMap(self, x, y, button, shift):
        
        mxd=arcpy.mapping.MapDocument("current")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        pt=arcpy.PointGeometry(arcpy.Point(x,y))
        #ptfeat=arcpy.management.CopyFeatures(pt,r"in_memory\pt")
        print x,y

0 Kudos
TonyAlmeida
MVP Regular Contributor
I got it to work kind of, i had a syntax error problem. I have fixed it and added the tool button but when i click the button and click on the map nothing happens, nothing is displayed... i am i missing something?

Thanks!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
The coordinates will be printed to your Python window.  Open that window before executing the tool.
0 Kudos
TonyAlmeida
MVP Regular Contributor
I found that out just before i saw your post. Thanks!

Is there a way to popup a message with out having to turn on the python window?
0 Kudos
DeanCarstens
Deactivated User
Hi Tony,

You can do this using the MessageBox in the pythonaddins module.  Pass the coordinates to the message property as text.
0 Kudos
TonyAlmeida
MVP Regular Contributor
I got to pop up the message box, but it has NaN Nan at the end.. what is that?
Would there be a possible way to add the "Long" & "Lat" text to the beginning or the end of the coordinates?

to display like this,

Latitude = 43.5373, Longitude = -114.2578
or
Lat = 43.5373, Long = -114.2578
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You will just need to add pythonaddins.Messagebox to the end of your code.  Ex:

import arcpy
import pythonaddins

class XYTool(object):
    """Implementation for xy_addin.XYTool (Tool)"""
   def __init__(self):
        self.enabled = True
        self.cursor=3
    def onMouseDownMap(self, x, y, button, shift):
        
        mxd=arcpy.mapping.MapDocument("current")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        pt=arcpy.PointGeometry(arcpy.Point(x,y))
        #ptfeat=arcpy.management.CopyFeatures(pt,r"in_memory\pt")
        print x,y
        pythonaddins.MessageBox(str(x) + ", " + str(y), 'Coordinates', 0)
0 Kudos
TonyAlmeida
MVP Regular Contributor
my current code;

import arcpy
import pythonaddins

class XY(object):
    """Implementation for xy_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor=3
    def onMouseDownMap(self, x, y, button, shift):
        
        mxd=arcpy.mapping.MapDocument("current")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        pt=arcpy.PointGeometry(arcpy.Point(x,y))
        #ptfeat=arcpy.management.CopyFeatures(pt,r"in_memory\pt")
        print x,y
        pythonaddins.MessageBox (arcpy.Point(x,y) ,'Location','0')
        pass
0 Kudos
TonyAlmeida
MVP Regular Contributor
Thank you JSkinns3!

I was able to figure it out.

import arcpy
import pythonaddins

class XY(object):
    """Implementation for xy_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor=3
    def onMouseDownMap(self, x, y, button, shift):
        
        mxd=arcpy.mapping.MapDocument("current")
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        pt=arcpy.PointGeometry(arcpy.Point(x,y))
        #ptfeat=arcpy.management.CopyFeatures(pt,r"in_memory\pt")
        print x,y
        pythonaddins.MessageBox("Long" + " " + str(x) + '\n'+ "Lat"+ " " + str(y), 'Coordinates', 0)