I am trying to iterate through the features of a layer and verify two different variables in two different fields in the same row. If both variables are true then it will apply a geoprocessing tool with one of the 2 results. The geoprocessing is Feature vertices to Point. I want to run it to create an output with points at the start of the line feature or on both sides of the line feature.
I used already If/elif/else and it didn't work. I added the selection feature to apply the function of the geoprocessing tool to only the features that meet the conditions because without it it will apply it to all the lines in the layer once the condition was True in some of them and not just to the ones that meet the conditions.
I tried this code and it works in separates blocks using notebook in ArcPro but when I put it all together it doesn't work.
What do you think could be change to make it work? or what am I doing wrong here?
import arcpy
from arcpy import env
arcpy.env.workspace = "workspace_gdb"
feature_class = "Line_Feature_Class"
output_vertices_topoint = "output_location"
output_vertices_topoint2 = "output_location"
value_x1 = "W"
value_x2 = "N"
value_x3 = "NW"
value_y1 = "NS"
value_y2 = "WE"
field_name1 = "WindDegrees1"
field_name2 = "WBDirection"
domain_name = "Wind Direction"
domains = arcpy.da.ListDomains(arcpy.env.workspace)
target_domain= None
for domain in domains:
if domain_name == domain.name:
target_domain=domain
break
if target_domain:
# Create a dictionary of coded values.
coded_values = domain.codedValues
arcpy.management.MakeFeatureLayer(feature_class, "temp_layer")
with arcpy.da.SearchCursor("temp_layer", ["SHAPE@", field_name1, field_name2]) as search_cursor:
for row in search_cursor:
object_id = row[0]
coded_value1 = row[1]
coded_value2 = row[2]
if coded_value1 == value_y1 and coded_value2 == value_x3:
# Select the feature
arcpy.management.SelectLayerByAttribute("temp_layer", "NEW_SELECTION", f"{field_name1} = '{value_y1}' AND {field_name2} = '{value_x3}'")
# Perform actions on the selected feature
arcpy.management.FeatureVerticesToPoints("temp_layer", output_vertices_topoint, "START")
else:
print(f"Feature does not meet any condition.")
arcpy.management.SelectLayerByAttribute(feature_class, "CLEAR_SELECTION")
break
with arcpy.da.SearchCursor("temp_layer", ["SHAPE@", field_name1, field_name2]) as search_cursor:
for row in search_cursor:
object_id = row[0]
coded_value1 = row[1]
coded_value2 = row[2]
if coded_value1 == value_y2 and coded_value2 == value_x3:
# Select the feature
arcpy.management.SelectLayerByAttribute("temp_layer", "NEW_SELECTION", f"{field_name1} = '{value_y2}' AND {field_name2} = '{value_x3}'")
# Perform actions on the selected feature
arcpy.management.FeatureVerticesToPoints("temp_layer", output_vertices_topoint2, "START")
else:
print(f"Feature does not meet any condition.")
arcpy.management.SelectLayerByAttribute(feature_class, "CLEAR_SELECTION")
break
with arcpy.da.SearchCursor("temp_layer", ["SHAPE@", field_name1, field_name2]) as search_cursor:
for row in search_cursor:
object_id = row[0]
coded_value1 = row[1]
coded_value2 = row[2]
if coded_value1 == value_y1 and coded_value2 == value_x2:
# Select the feature
arcpy.management.SelectLayerByAttribute("temp_layer", "NEW_SELECTION", f"{field_name1} = '{value_y1}' AND {field_name2} = '{value_x2}'")
# Perform actions on the selected feature
arcpy.management.FeatureVerticesToPoints("temp_layer", output_vertices_topoint2, "START")
else:
print(f"Feature does not meet any condition.")
arcpy.management.SelectLayerByAttribute(feature_class, "CLEAR_SELECTION")
break
with arcpy.da.SearchCursor("temp_layer", ["SHAPE@", field_name1, field_name2]) as search_cursor:
for row in search_cursor:
object_id = row[0]
coded_value1 = row[1]
coded_value2 = row[2]
if coded_value1 == value_y1 and coded_value2 == value_x1:
# Select the feature
arcpy.management.SelectLayerByAttribute("temp_layer", "NEW_SELECTION", f"{field_name1} = '{value_y1}' AND {field_name2} = '{value_x1}'")
# Perform actions on the selected feature
arcpy.management.FeatureVerticesToPoints("temp_layer", output_vertices_topoint, "BOTH_ENDS")
else:
print(f"Feature does not meet any condition.")
arcpy.management.SelectLayerByAttribute(feature_class, "CLEAR_SELECTION")
break
with arcpy.da.SearchCursor("temp_layer", ["SHAPE@", field_name1, field_name2]) as search_cursor:
for row in search_cursor:
object_id = row[0]
coded_value1 = row[1]
coded_value2 = row[2]
if coded_value1 == value_y2 and coded_value2 == value_x2:
# Select the feature
arcpy.management.SelectLayerByAttribute("temp_layer", "NEW_SELECTION", f"{field_name1} = '{value_y2}' AND {field_name2} = '{value_x2}'")
# Perform actions on the selected feature
arcpy.management.FeatureVerticesToPoints("temp_layer", output_vertices_topoint, "BOTH_ENDS")
else:
print(f"Feature does not meet any condition.")
arcpy.management.SelectLayerByAttribute(feature_class, "CLEAR_SELECTION")
break
with arcpy.da.SearchCursor("temp_layer", ["SHAPE@", field_name1, field_name2]) as search_cursor:
for row in search_cursor:
object_id = row[0]
coded_value1 = row[1]
coded_value2 = row[2]
if coded_value1 == value_y2 and coded_value2 == value_x1:
# Select the feature
arcpy.management.SelectLayerByAttribute("temp_layer", "NEW_SELECTION", f"{field_name1} = '{value_y2}' AND {field_name2} = '{value_x1}'")
# Perform actions on the selected feature
arcpy.management.FeatureVerticesToPoints("temp_layer", output_vertices_topoint2, "START")
else:
print(f"Feature does not meet any condition.")
print (row[1], row[2])
arcpy.management.SelectLayerByAttribute(feature_class, "CLEAR_SELECTION")
break