Select to view content in your preferred language

Can you query a variable in Select_analyis?

807
4
Jump to solution
06-06-2012 02:30 PM
LeeEllenburg
Emerging Contributor
Hello,

I an having troubles querying a variable using the arcpy.Select_analysis() tool.

For example:

import arcpy from arcpy import env   env.workspace = "D:\\Lee\\IrrigationProject"  In = "boombuffer.shp" Out = "D:\\Lee\\IrrigationProject\\IrrigationModel\\BoomBuffers\\Boom" Boom = 1  arcpy.Select_analysis(In, Out + str(Boom) + ".shp" , '"BoomNumber" = 'Boom'')


This is not valid syntax, however I can't seem to figure it out. I would like to include this in a loop that would create a new shapefile from each feature in the input shapefile.

Any help would be greatly appreciated.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
That sounds like you are trying to do a Split

If you need more control than that, this should work. Assumes BoomNumber is a numeric value.

import arcpy from arcpy import env  env.workspace = "D:\\Lee\\IrrigationProject"  In = "boombuffer.shp" Out = "D:\\Lee\\IrrigationProject\\IrrigationModel\\BoomBuffers\\Boom"  boom_list = [] boom = None field_name = "BoomNumber" delim_field_name = arcpy.AddFieldDelimiters(env.workspace,field_name)  # Creates and runs cursor to add all unique values to a list s_curs = arcpy.SearchCursor(In) for s_row in s_curs:     add_val = s_row.getValue(field_name)     if add_val not in boom_list:         boom_list.append(add_val)  # Loops through list of unique boom values and export each one to a new shapefile for boom in boom_list:     arcpy.Select_analysis(In, Out + str(boom) + ".shp", "{0} = {1}".format(delim_field_name,boom)

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Honored Contributor
Something like this should work.
variable = "Boom"
field_name = "BoomNumber"
delim_field_name = arcpy.AddFieldDelimiters(env.workspace,field_name)
arcpy.Select_analysis(In, Out + str(Boom) + ".shp", "{0} = '{1}'".format(delim_field_name,variable)


Or maybe I didn't understand what you are trying to do. Is Boom your attribute or your variable you want to pass?
0 Kudos
LeeEllenburg
Emerging Contributor
Yes, Boom is the variable I would like to pass. It would increase with each iteration ideally, effectively exporting each feature as it's own shapefile.
0 Kudos
MathewCoyle
Honored Contributor
That sounds like you are trying to do a Split

If you need more control than that, this should work. Assumes BoomNumber is a numeric value.

import arcpy from arcpy import env  env.workspace = "D:\\Lee\\IrrigationProject"  In = "boombuffer.shp" Out = "D:\\Lee\\IrrigationProject\\IrrigationModel\\BoomBuffers\\Boom"  boom_list = [] boom = None field_name = "BoomNumber" delim_field_name = arcpy.AddFieldDelimiters(env.workspace,field_name)  # Creates and runs cursor to add all unique values to a list s_curs = arcpy.SearchCursor(In) for s_row in s_curs:     add_val = s_row.getValue(field_name)     if add_val not in boom_list:         boom_list.append(add_val)  # Loops through list of unique boom values and export each one to a new shapefile for boom in boom_list:     arcpy.Select_analysis(In, Out + str(boom) + ".shp", "{0} = {1}".format(delim_field_name,boom)
0 Kudos
LeeEllenburg
Emerging Contributor
Ahhh, yes. Split will work just fine! Thanks.
0 Kudos