3

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

Kirk Kuykendall
25.8k9 gold badges68 silver badges155 bronze badges
asked May 5, 2016 at 12:01
5
  • You need to look into tool validation on a Python Script tool. Commented May 5, 2016 at 12:11
  • @PolyGeo what can i add in my case i never work with the tool validation before Commented May 5, 2016 at 12:14
  • Whenever I need to do it I go to the Help to re-learn how. Commented May 5, 2016 at 12:23
  • that's what i have done already before posting this i really need help Commented May 5, 2016 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/… Commented May 10, 2016 at 16:39

1 Answer 1

1

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
answered Jul 24, 2019 at 20:46
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.