Select to view content in your preferred language

Using A Geometry Token in the Where Clause

1148
2
Jump to solution
12-01-2017 03:37 PM
AlexanderAudet
Occasional Contributor

So my question is, what if I want to limit my da.SearchCursor results so that it did not select lines of a particular length. 

For example, the following code, is what I tried, but then...

PairedChaeck = #a featureclass
length = #some value
cursor = arcpy.da.SearchCursor('PairedCheck6',['SHAPE@TRUECENTROID', 'SHAPE@LENGTH'], where_clause= 'SHAPE@LENGTH <> length')‍‍‍

I get the following error: A column was specified that does not exist.  Failed to execute (CalculateField).  And refers to line 7 of the following snippet. 

        cursor = arcpy.da.SearchCursor('PairedCheck6',['SHAPE@TRUECENTROID', 'SHAPE@LENGTH'], where_clause= 'SHAPE@LENGTH <> length')
        centroid = None
        
        #collect results
        results = []
        for result in cursor:
            results.append(result)

Is it even possible to reference a geometry token like that in a where clause? I would prefer not to have to make a knew column called length just for that piece of logic. 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

If you are using file geodatabases, and know that is all you will be working with, then Dan's suggestion gets the job done.  One issue that can arise using "Shape_Length" is that the shape length field name can vary by data source and even by geometry data type within a given data source.  For example, shape files are "Shape_Len".

If there is a chance you will be working with data from multiple/different data stores, one approach would be to have the cursor retrieve all the records and then use a list comprehension to filter based on the length property token (SHAPE@LENGTH):

PairedCheck = # path to feature class or layer name
length = # some Value

with arcpy.da.SearchCursor(PairedCheck, ["SHAPE@TRUECENTROID", "SHAPE@LENGTH"]) as cur:
    results = [geom_cent for geom_cent,geom_len in cur if geom_len != length]

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

you have to use  'Shape_Length' since that is the name of the field 

'SHAPE@LENGTH' gets the property (value) not the name

JoshuaBixby
MVP Esteemed Contributor

If you are using file geodatabases, and know that is all you will be working with, then Dan's suggestion gets the job done.  One issue that can arise using "Shape_Length" is that the shape length field name can vary by data source and even by geometry data type within a given data source.  For example, shape files are "Shape_Len".

If there is a chance you will be working with data from multiple/different data stores, one approach would be to have the cursor retrieve all the records and then use a list comprehension to filter based on the length property token (SHAPE@LENGTH):

PairedCheck = # path to feature class or layer name
length = # some Value

with arcpy.da.SearchCursor(PairedCheck, ["SHAPE@TRUECENTROID", "SHAPE@LENGTH"]) as cur:
    results = [geom_cent for geom_cent,geom_len in cur if geom_len != length]