New to Python

1495
13
Jump to solution
03-14-2023 12:46 PM
ChrisGlenn
New Contributor II

I am having trouble with a file path or the format of the destination I believe. I am very new to Python and have pieced this together with a lot of questions without a strong understanding of ArcGIS yet.

# Imports
import arcpy

# Workspace
trail_data = r"C:\PythonPro\Final\Trail"
county_data = r"C:\PythonPro\Final\Counties\Oregon_Counties.shp"

# Get a list of unique county names
counties = []
with arcpy.da.SearchCursor(county_data, "County_Name") as cursor:
    for row in cursor:
        if row[0] not in counties:
            counties.append(row[0])

# Print the list of counties and ask the user to choose one
print("Choose a county: ")
for i, county in enumerate(counties):
    print(f"{i+1}. {county}")
selection = int(input("> ")) - 1
selected_county = counties[selection]
print("Measuring...")

# Use the selected county to clip the trail data
clip_output = "C:/data/clipped_trails.shp"
where_clause = f"County_Name = '{selected_county}'"
arcpy.Clip_analysis(trail_data, county_data, clip_output, where_clause)

# Calculate the total distance of bike trails in the clipped data
total_distance = 0
with arcpy.da.SearchCursor(clip_output, "SHAPE@LENGTH") as cursor:
    for row in cursor:
        total_distance += row[0]

# Display the total distance to the user
print(f"The total distance of bike trails in {selected_county} is {total_distance:.2f} meters.")

 

 

The error code I am getting is: RuntimeError: Cannot find field 'County_Name'

 

I have tried to change the source and the configuration of the files on my PC. I suspect that the problem is something simple that I am missing, but I am banging my head against a wall and can't see it clearly. Any help is appreciated. Thanks.

 

Chris

Tags (2)
0 Kudos
13 Replies
by Anonymous User
Not applicable


@Mahdi_Ch 

A set is an unordered collection with no duplicate elements. So basically,  instead of going line by line and assigning the values after checking if they already exist, you can use a "set comprehension" to add all the values to the set function. And then Python set() do the work behind the scene and returns only the unique values for you. 

Nice explanation! Now one line it! 🙂

counties = list(set([row[0] for row in arcpy.da.SearchCursor(fc, "County_Name")]))

 

DavidPike
MVP Frequent Contributor

It's already a set, and sort() produces a list from the set.

by Anonymous User
Not applicable

I'm aware- sorry if that was misguided... I was just showing an all inclusive version of the cursor with list comprehension.

0 Kudos
Mahdi_Ch
New Contributor III

@Anonymous User that was hilarious!  So, waiting for the next person to do that in zero line!  😂

Jokes aside, that's cool. And I love list comprehensions. I used to write pretty long ones but after a while I realized that there are certain problems associated with long list comprehensions.

First, after a certain point it negatively affects the readability and it becomes harder to read and understand the logic.
Second, in case of errors, since a lot is happening inside the list -without making intermediate variables available to us- that makes it very hard to debug. 

I thought it worth mentioning for those who just learned about this concept.

BTW, If you want to go rogue on that,  you can throw some ifs and lambdas there and then watch it burn. 😁

0 Kudos