multiple definition query for multiple layers

262
1
03-17-2024 04:47 AM
marklee1869
New Contributor

I need to write python code to apply definition query for all layers at once, i already tried some code which i insert it below but it makes a new layers 

# Set the workspace (change to your desired geodatabase or folder)
arcpy.env.workspace = r"D:\abo-ammar\geoParcel.gdb"

# List of feature classes (change to your desired feature class names)
feature_classes = ["parcels_corner", "parcels_dimension", "LINE_AMLAK", "Parcels"]

# Define the common query expression (change to your desired query)
query_expression = "parcel_no = 2"

# Loop through each feature class and apply the definition query
for feature_class in feature_classes:
# Get the existing layer
layer = arcpy.management.MakeFeatureLayer(feature_class, f"{feature_class}_Layer", query_expression)
print(f"Definition query applied to {feature_class}.")

# Print a success message
print("Definition queries applied to all specified feature classes.")

 

i want to know how to apply the same code but over the current layers not with generation of a copy of each layer

Tags (1)
1 Reply
Amir-Sarrafzadeh-Arasi
Occasional Contributor

Dear marklee,

I hope you are doing well,

 

you just need to delete the "_layer" from your second element in the arcpy.management.MakeFeatureLayer function.

# Set the workspace (change to your desired geodatabase or folder)
arcpy.env.workspace = r"D:\abo-ammar\geoParcel.gdb"


# List of feature classes (change to your desired feature class names)
feature_classes = ["parcels_corner", "parcels_dimension", "LINE_AMLAK", "Parcels"]


# Define the common query expression (change to your desired query)
query_expression = "parcel_no = 2"


# Loop through each feature class and apply the definition query
for feature_class in feature_classes:
# Get the existing layer
layer = arcpy.management.MakeFeatureLayer(feature_class, f"{feature_class}", query_expression)
print(f"Definition query applied to {feature_class}.")


# Print a success message
print("Definition queries applied to all specified feature classes.")

Good luck

Amir Sarrafzadeh Arasi
0 Kudos