1

I'm back with a script I've previously posted about that I'm constantly improving.

Below is a working script that is connected to a database with 52 feature classes on a server that, when having user specified inputs for a parish name and output folder, extracts features that were selected by location from that database.

What I'm trying to add is the optional parameter of being able to select which feature classes/layers to extract from, instead of extracting all 52. I figured I would create a multivalue list input and have it loop through that by adding an if statement for the optional list paramater but how can I get a list of the feature classes from the database so that the user can select from those and add them to a list as a single list parameter?

import arcpy
from arcpy import env

env.workspace = r'\\server\database.sde'

stateFeature = r'\\server\state.shp'
parish = arcpy.GetParameterAsText(0)
output = arcpy.GetParameterAsText(1)
nameField = "PARISH"


arcpy.CreateFileGDB_management(output, parish)

arcpy.Delete_management('SelectionStateLayer')
arcpy.Delete_management('lyr')

arcpy.MakeFeatureLayer_management(stateFeature, 'SelectionStateLayer', '"' +     
str(nameField) + '" = ' + "'" + str(parish) + "'")


for fc in arcpy.ListFeatureClasses():

    if arcpy.Exists('lyr'): 
        arcpy.Delete_management('lyr')

    arcpy.MakeFeatureLayer_management(fc, 'lyr') 
    arcpy.SelectLayerByLocation_management('lyr', "WITHIN", 'SelectionStateLayer') 

    outname = fc.replace(".","_") 
    outpath = output + r'/' + parish + '.gdb' + r'\{0}'.format(outname)

    arcpy.CopyFeatures_management('lyr', outpath)

arcpy.Delete_management('lyr')



arcpy.env.workspace = output + r'/' + parish + '.gdb'

for fc in arcpy.ListFeatureClasses():

    if int(arcpy.GetCount_management(fc).getOutput(0)) == 0:
        arcpy.Delete_management(fc)
        arcpy.AddMessage('Deleted "{}" because it contains no features!'.format(fc))
2

This kind of task is best approached by using the tool validator; you can set value of a parameter by reading another parameter's value (and listing feature classes from the workplace user specified).

def updateParameters(self):
  if self.params[0].value:
    self.params[1].value = #ListFeatureClasses code
  else:

If you want to populate a parameter with values based on the static value of another parameter:

def updateParameters(self):    
      self.params[1].value = #ListFeatureClasses code
  • Is there anyway I can have it update a workspace that is already defined in the script by default to pull the feature classes? This script is specific to this database and I'm trying to make it as user-friendly for my co-workers without having to go into connecting to a relative server path. – TacoB0t May 14 '15 at 15:25
  • 1
    Updated my answer. You just need to set values for the parameter directly. Look into the help page, there are great samples there. – Alex Tereshenkov May 14 '15 at 15:43

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.