2

I'm trying to create a python script that allows a selection attribute based on the name of a field in the attribute table of a layer.

I would like to configure my script such a way that permits the user to select the SOURCE NAME from a drop-down list. Here's the code I have written:

import arcpy
arcpy.env.workspace = 'in_memory'
arcpy.env.overwriteOutput = True 
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
PosteLayer=arcpy.GetParameterAsText(0)
field="NAME_SOURCE"
rows=arcpy.da.SearchCursor(PosteLayer,["NAME_SOURCE"])
list=[]
for row in rows:
   if str(row[O]) not in list:
       liste.append(row[0])
POSTES=liste
POSTES=arcpy.GetParameterAsText(1)

I'm using the Advanced Version Of Arcgis 10.2

  • You need to look into tool validation on a Python Script tool. – PolyGeo May 5 '16 at 12:11
  • @PolyGeo what can i add in my case i never work with the tool validation before – Lahlou Imane May 5 '16 at 12:14
  • Whenever I need to do it I go to the Help to re-learn how. – PolyGeo May 5 '16 at 12:23
  • that's what i have done already before posting this i really need help – Lahlou Imane May 5 '16 at 12:31
  • Here is an example that uses the Python Toolbox. I find it a little easier to use validation in that context than the tbx. gis.stackexchange.com/questions/82473/… – dslamb May 10 '16 at 16:39
0

First: you should make a toolbox with a script set the parameters the name "List of Source Name" the data type "string" the filter "value list as in picture: enter image description here

Second: select Validation the edit button write under the (def updateParameters(self)) the following:

def updateParameters(self):

        #define the path of your feature class
        fc="D:/000/test.shp"
        #define the the name of the field
        col=  ("SOURCENAME")
        self.params[0].filter.list = [str(val) for val in  
                                        sorted(  
                                          set(  
                                            row.getValue(col)  
                                            for row in arcpy.SearchCursor(fc, None, None,col)))]  
        if self.params[0].value not in self.params[0].filter.list:  
          self.params[0].value = self.params[0].filter.list[0]
        return

Third: create a tool script and write the following:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
currentlayer = arcpy.mapping.ListLayers(mxd, "*") [0]
inputLayer = arcpy.MakeFeatureLayer_management(currentlayer)  
TWN = arcpy.GetParameterAsText(0)
query  = "SOURCENAME = '" + TWN + "'"
arcpy.SelectLayerByAttribute_management(currentlayer, "NEW_SELECTION",  query )
count = arcpy.GetCount_management(currentlayer)
arcpy.AddMessage("thes selected are:  "+str(count))

or you can write in the validation script the following:

def updateParameters(self):
    #define the top layer in the mapview
    mxd = arcpy.mapping.MapDocument("CURRENT")
    fc = arcpy.mapping.ListLayers(mxd, "*") [0]

    #define the the name of the field
    col=  ("SOURCENAME")
    self.params[0].filter.list = [str(val) for val in  
                                    sorted(  
                                      set(  
                                        row.getValue(col)  
                                        for row in arcpy.SearchCursor(fc, None, None,col)))]  
    if self.params[0].value not in self.params[0].filter.list:  
      self.params[0].value = self.params[0].filter.list[0]
    return
| improve this answer | |

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.