1

I cannot get the arcpy.env.extent working for Select Layer By Attribute GP tool. I can verify that I get various values for coordinates when I have panned to various areas on the map. Copy Features GP tool does export only those feature that are located within the current visible map extent. According to the Select Layer By Attribute GP tool documentation:

If an Extent environment is specified, or if a definition query is present on the Input Layer or Table View, only the features or rows within the extent or matching the definition query may be selected.

Has anyone succeeded selecting features that are within the extent only? When I run the code below, regardless of the map extent, all features that fulfill the query requirements are selected.

This is the code I execute in the Python window in ArcMap.

fc = "Water distribution network fittings" #layer name in the TOC
field = "ObjectId"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
print df.extent.XMin, df.extent.YMin, df.extent.XMax,df.extent.YMax

arcpy.env.extent = df.extent #also tried arcpy.Extent(XMin, YMin, XMax, YMax)
arcpy.CopyFeatures_management(fc, "in_memory\outputFeatures") #adds new layer correctly
arcpy.SelectLayerByAttribute_management(in_layer_or_view=fc,selection_type="NEW_SELECTION",where_clause = """ObjectId > 160""")
  • Perhaps your feature class needs to have a layer made from it prior to using SelectLayerByAttribute. I'm unclear what using CopyFeatures into the in_memory workspace accomplishes in this code and am not near ArcGIS for Desktop to test at the moment. – PolyGeo Jan 13 '15 at 10:53
  • No problem. fc in the code represent the layer name in the TOC. CopyFeatures creates a copy of the feature class drawn as layer and then adds the output to the display. – Alex Tereshenkov Jan 13 '15 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.