1

I want to create new feature classes based on selections from a list of fields. For now I'm using a user prompt instead of GetParamatersAsText for the selection criteria. I need the script to look for the first field name from the field list and then look for the first value in the value list, make a selection, and then create a new feature class based on the selection with the output name = current field name&""&current selection value. Then the the script should look for the next selection value and apply it to the current field, and so on. When there are no more selection values the script moves on to the next field name and makes a selection based on the first selection value again, and so on until there are no more selection values or field names.

#designate tower generated service location layer
SL = r"J:\workspace\Crown_Geocode\gisdb\layers.gdb\Rt8_15"

#Select route numbers
rtlist = []
while True:
    route = raw_input("Route Number (press enter when finished: ")
    rtlist.append(route)
    if route = "":
        break

#create new feature classes based on route number and day of service
arcpy.MakeFeatureLayer_management("Rt8_15", "Copy_Rt8_15")
fields = ("MonRoute","TueRoute","WedRoute","ThuRoute","FriRoute")
for field in fields, route in routes:
    arcpy.SelectLayerByAttribute_management("Copy_Rt8_15", "NEW_SELECTION", ' field = route ')
    arcpy.CopyFeatures_management("Copy_Rt8_15", ??????)

I should note that I will eventually want to use GetParametersAsText.

2

You should look into building SQL expressions in arcpy. Also, creating your output name is a matter of string manipulation. I always like using .format (...) in such instances. Create your sql query and use Select to export your new features. Using the os module for pathing is also helpful.

import os
import arcpy

outLocation = r"J:\workspace\Crown_Geocode\gisdb\layers.gdb"

#Intermediate scripting here
...

arcpy.MakeFeatureLayer_management("Rt8_15", "Copy_Rt8_15")
fields = ("MonRoute","TueRoute","WedRoute","ThuRoute","FriRoute")
for field in fields:
    for route in routes:
        #Add field delimiters
        delimFld = arcpy.AddFieldDelimiters ("Copy_Rt8_15", field)

        #Get out name
        outName = "{0}_{1}".format (field, route)
        outFc = os.path.join (outLocation, outName)

        #If routes are numbers:
        try:
            sql = "{0} = {1}".format (delimFld, route)
            arcpy.Select_analysis ("Copy_Rt8_15", outFc, sql)

        #routes are strings
        except:
            sql = "{0} = '{1}'".format (delimFld, route)
            arcpy.Select_analysis ("Copy_Rt8_15", outFc,  sql)

        print "Created:", outName

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.