Select_analysis

1462
5
05-23-2012 08:22 AM
ScottBlankenbeckler
Occasional Contributor
Not sure if this is a bug or as intended.

# Set local variables
in_features = "working.gdb\linefile"
out_feature_class = "C:/output/linefile_zeros.shp"
where_clause = '"\"Shape_Length\" = 0''

# Execute Select
arcpy.Select_analysis(in_features, out_feature_class, where_clause)


What I expected -
in the shapefile linefile_zeros would be all the line features with a zero line length.

What I get -
The original file "linefile" now contains all of the zero length lines and linefile_zero now contains all of the features (zero's and greater)

Is this working as intended?
Tags (2)
0 Kudos
5 Replies
MathewCoyle
Frequent Contributor
You have a few issues, not sure if they are related to the problem you are having or some mistake in copying your code. Your where_clause has a syntax error and your in_features variable should be raw stringed or you should be setting your workspace to the FGDB.

Something like this
import arcpy
# Set local variables
arcpy.env.workspace = ws = r"..\working.gdb"
in_features = "linefile"
out_feature_class = "C:/output/linefile_zeros.shp"
query_field = "Shape_Length"
field_delim = arcpy.AddFieldDelimiters(ws,query_field)
where_clause = "{0} = 0".format(field_delim)

# Execute Select
arcpy.Select_analysis(in_features, out_feature_class, where_clause)
0 Kudos
ScottBlankenbeckler
Occasional Contributor
You have a few issues, not sure if they are related to the problem you are having or some mistake in copying your code. Your where_clause has a syntax error and your in_features variable should be raw stringed or you should be setting your workspace to the FGDB.

Something like this


Actually my issues have nothing to do with what you stated as it reads my in_features correctly and my where_clause works fine as well.  If you read my issues it has to do with the output and output only.

The results of my code is the Input file becomes what is expected in the Output and the Output becomes a copy of the Original Input.  This and only this is the issue I am trying to get resolved.
If my where_clause was wrong the output file would give me different results but the Input file now has the results that I expected in the Output (as per my where_clause).  Also if my input was wrong I would get an error as there would be nothing to read.

Example:
Input (linefile)
Name, Shape_length
A, 1.508
B, 0
C, 2.134

Ouput
(linefile_zeros.shp)
Name, Shape_length
A, 1.508
B, 0
C, 2.134

(linefile)
Name, Shape_length
B, 0
0 Kudos
MathewCoyle
Frequent Contributor
I was just trying to work from the ground up to see what the issue might be. It is difficult to troubleshoot without the actual code you are executing. Have you tried creating a feature layer of your input feature class and running the select tool on that layer instead? Also, what version of ArcGIS are you using?
0 Kudos
ScottBlankenbeckler
Occasional Contributor
If it makes it easier for you to understand this is the line of code as it rest in my script

    # Process: Select
    arcpy.Select_analysis(IN_line, OUT_zero, "\"Shape_Length\" = 0")


I just thought I would be nice and put what formats of input and output were expected. This is important when building the where clause as the format of the field would be different based on the format of the input storage.

I am running ArcMap 10.1 pre-release. I am posting here because I have never used this function in a script and was not sure if this was the typical output for this function.  I was hoping someone using 10.0 or earlier would say "Yes it has always been that way.", or "No it shouldn't.", or even "I works correctly in my code."  At that point I would know how to proceed, either with a bug report of change my code so it uses the data as outputted.
0 Kudos
MathewCoyle
Frequent Contributor
It is definitely not how the tool works in 10.0. I'd suspect some kind of bug.
0 Kudos