0

I'm trying to calculate the accumulative street length in various jurisdictions in a county. To do this I'm using a search cursor in the city layer and then using the current feature to select streets by location and then sum up the total shape length for that selection and print out the city name and associated shape length. I'm getting an error that ESRI states is due to path name, unsupported type, or data type does not work for this tool. I pasted the path right out of catalog, the city layer is a polygon, the streets are polylines. Here is my code so far:

import arcpy

places = r"C:\Users\jsobryan\Desktop\ArcMap\Scrap.gdb\City_Township_Unincorp"

streets = r"C:\Users\jsobryan\Desktop\ArcMap\Scrap.gdb\Streets"

arcpy.MakeFeatureLayer_management(streets,"lyr") 

with arcpy.da.SearchCursor(places,["DIST_NAME"]) as cursor:
    for row in cursor:
        arcpy.SelectLayerByLocation_management("lyr","HAVE_THEIR_CENTER_IN",row[0],"","NEW_SELECTION")
        sum = arcpy.Statistics_analysis("lyr",r"C:\Users\jsobryan\Desktop\ArcMap\Scrap.gdb\table_1",[["Shape.STLength()","SUM"]])
        print row[0] + ": " + sum

Here is the error:

ERROR 000732: Selecting Features: Dataset CITY OF ANYWHERE_USA does not exist or is not supported  

Thinking maybe I need to do a make feature layer for cities and and use search cursor to select row and then opt to use selected features in select layer by location parms?

  • 2
    It's failing because row[0] == DIST_NAME, an attribute field. SLBL wants geometry to select against. – KHibma Nov 6 '18 at 17:55
  • @KHibma to add to your comment, pre-append the “SHAPE@“ field to the list of fields when you open your cursor (line 9). – Hayden Elza Nov 6 '18 at 18:05
  • Yeah... my comment and yours is like 70% of a solution. I'm not in a position to write out/test code for an answer, thus I just added the starter for a solution as a comment, – KHibma Nov 6 '18 at 18:08
0

Your select_features need to be a layer and not a text string.

In your loop, use that same text value to make a selection from your places layer. Then, select streets with their centers in the places layer. This will cause streets to be selected only if their centers are within pre-selected places. I can't find documentation for this, but just tested and the default behavior is to "use selected features" when a selection exists on your select_features.

import arcpy

places = r"C:\Users\jsobryan\Desktop\ArcMap\Scrap.gdb\City_Township_Unincorp"

streets = r"C:\Users\jsobryan\Desktop\ArcMap\Scrap.gdb\Streets"

arcpy.MakeFeatureLayer_management(streets,"lyr") 

with arcpy.da.SearchCursor(places,["DIST_NAME"]) as cursor:
    for row in cursor:
        arcpy.SelectLayerByAttribute_management (places, "NEW_SELECTION", "DIST_NAME = " + row[0])
        arcpy.SelectLayerByLocation_management("lyr","HAVE_THEIR_CENTER_IN",places,"","NEW_SELECTION")
        sum = arcpy.Statistics_analysis("lyr",r"C:\Users\jsobryan\Desktop\ArcMap\Scrap.gdb\table_1",[["Shape.STLength()","SUM"]])
        print row[0] + ": " + sum

By the way, using "HAVE_THEIR_CENTER_IN" will give you an exaggerated total for the cumulative length of streets in each place (any street may have its center in a given place, but then extend far beyond the place's boundaries on both sides). Look at using Intersect first to get a more accurate total.

  • yep. Thought that might be it. Although I think "have their center in" is more accurate because a street segment can barely intersect a boundary and get selected as well as be in more than one selection. If that happens your break down of sums will be more than the total sum of the feature class. Thanks! – geoJshaun Nov 6 '18 at 21:48
  • @ShaunO I meant use the Intersect tool as opposed to selecting streets where they intersect places. If you apply the Intersect tool first, your streets will be split by the place boundaries. It's very handy for what you're trying to do. – Andy Nov 7 '18 at 13:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.