TransWikia.com

Recording selection with and without .lyr file using ArcPy

Geographic Information Systems Asked by ePoQ on June 4, 2021

I tried recording a selection using first arcpy.SelectLayerByAttribute_management and then 'arcpy.CopyFeatures_management' but since my input was a feature class I get an error. So, I converted my input into a .lyr and now my output is empty when I open it in Arcgis.

import arcpy
arcpy.env.workspace = "C:/Temp/PyData"
shpfile = 'building_split.shp'
arcpy.MakeFeatureLayer_management(in_features=shpfile,
                                  out_layer="bui_lyr")
lyr = 'bui_lyr'
arcpy.SelectLayerByAttribute_management(in_layer_or_view=lyr,
                                        selection_type= "SUBSET_SELECTION",
                                        where_clause= '"length" > 25')

arcpy.CopyFeatures_management(in_features=lyr,
                              out_feature_class= 'res_building_25')

Here is my code, I guess there is a simple mistake somewhere I just began writing scripts in Python. Also, how can I perform a selection on my feature class. I guess I am not obliged to go through the creation of a .lyr file.

Edit: I did another try with a simpler code below, the test.shp file remains empty

import arcpy
arcpy.env.workspace = "C:/Temp/PyData"
arcpy.MakeFeatureLayer_management("building_split.shp", "bui_lyr")
arcpy.SelectLayerByAttribute_management("bui_lyr", "SUBSET_SELECTION", "length > 25")
arcpy.CopyFeatures_management("bui_lyr", "C:/Temp/PyData/Test.shp")

2 Answers

There is an issue with your where clause, so nothing gets selected.

First, I'm not sure using SUBSET_SELECTION will work if there isn't an existing selection. Keep the default value (NEW_SELECTION).

Second, field names delimiters should be double quotes (they also can be omitted) for shapefiles. Also, wrapping your clause with triple double quotes is a good practice to ensure inner and outer quotes are matched properly (when you have multiple pairs of embedded quotes):

where_clause = """length > 25"""

See also the arcpy.FieldDelimiters() function if you want to make your query workspace-proof.

If the ultimate goal is to save your selection to a feature class, Select will do the trick in one step. Comment regarding the where clause apply here too.

Correct answer by GISGe on June 4, 2021

The expression you have used in the "Select Layer by Attribute" tool is wrong. Try and use string formatting to create an expression or use the following expression:

exp = """"length" > 25"""

There are a few ways to improve your code.

  1. You can use the "where clause" within the "Make Feature Layer" tool to create a layer based on your condition.

  2. You can use the "Select" tool to directly create a feature class or shapefile from a dataset by using an expression.

    Extracts features from an input feature class or input feature layer, typically using a select or Structured Query Language (SQL) expression and stores them in an output feature class.

Answered by MacroZED on June 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP