2

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.

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()

PolyGeo
  • 65,136
  • 29
  • 109
  • 338

2 Answers2

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()
blah238
  • 35,793
  • 7
  • 94
  • 195
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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338