2

As a total Novice with Python, I thought 2 weeks would be enough time to learn how to build some tools for our GIS... I was wrong and I'm running up on a deadline.

I am attempting to get a value from a selected feature in geodatabase, and pass that value into a URL link / query after the equals sign.

Opening the link to the image viewer works, if I don't try to pass in the field value.

After 9 days of reading training manuals my eyes are raw. How can I fix this...

import arcpy
import pythonaddins
import webbrowser
from threading import Thread

def GetParameterAsText (str.Unit_No):
"This prints a passed through string into this function"

import str
row.getValue(Unit_No)

def OpenBrowserURL ():
    url = 'http://fcweb/caviewer#unit='
    webbrowser.open(url,new=2)

class CAVButton(object):
"""Implementation for PythonButton_addin.CAVbutton (Button)"""
def __init__(self):
    self.enabled = True
    self.checked = False
def onClick(self):
        t = Thread(target=OpenBrowserURL)
        t.start()
        t.join()
3

You're missing a lot of implementation here, and the code you do have contains syntax and logic errors. Indentation is part of the syntax in Python, so you must be consistent and know when and where to indent. Also you reference variables that have not yet been defined, e.g. str (which is a built-in function, so don't use that name) and row.

You need to implement logic to get the desired attribute from the selected feature in the desired layer, as well as to format the URL with that value. Geoprocessing tools and functions automatically honor selections applied to layers in the map, so using that you could pass the name of the desired layer into a SearchCursor and it will only return the selected rows/features.

You might want to first check how many features are selected by running a Get Count first and checking that exactly 1 feature is selected, if that is the desired behavior.

One slightly tricky part is that you'll need to pass the "unit" value to your OpenBrowserURL function, and to do that you'll need to specify the args or kwargs argument of the Thread constructor.

e.g.:

def GetSelectedUnit():
    """TO DO"""
    raise NotImplementedError

def OpenBrowserURL(unit):
    url = "http://fcweb/caviewer#unit={0}".format(unit)
    webbrowser.open(url,new=2)

class CAVButton(object):    
    def onClick(self):
        unit = GetSelectedUnit()
        t = Thread(target=OpenBrowserURL,args=(unit,))
        t.start()
        t.join()
  • many thanks. Your assistance and suggestions are greatly appreciated and I remain open to any additional guidance you or any other mentor may wish to share. Trying to RUN before I can walk, it is good to have your help. – The Spatialist May 1 '13 at 20:59
1
    import arcpy    
    import pythonaddins  
    import webbrowser       
    import threading    
    class ToolClass2(object):  
        """Implementation for TEST_addin.tool (Tool)"""     
        def __init__(self):
            self.enabled = True   
            self.checked = False   
            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))   
           pythonaddins.MessageBox("Long" + " " + str(x) + '\n'+ "Lat"+ " " + str(y), 'Coordinates', 0)   
           w =lambda: webbrowser.open('http://www.google.com/maps?q='+str(y)+','+str(x)+'', new=2)         
           t = threading.Thread(target=w)   
           t.start()   
           pass

This code works for a python add-in to open up google maps to the same location when clicked in arcmap without crashing. This threading code is more straightforward I think. Good Luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.