1

I'm trying to use ArcPy to print a list of addresses of land parcels crossed by a power line. I'm using the following code to accomplish this:

# Loop through features in parcels.shp
for feature in parcel:
    # Search cursor for address field
    cursor = arcpy.da.SearchCursor(parcel, 'SITUSADDR')

    # Loop through cursor
    for row in cursor:
        # Select features crossed by powerline.shp
        matchcount=arcpy.SelectLayerByLocation_management(parcel, 'CROSSED_BY_THE_OUTLINE_OF', power)

        # Loop through matchcount, print
        for i in matchcount:
            print((row[0]))

    # Give feedback if there are no matches
    if matchcount == 0:
        print('No features matched spatial criteria.')

The code is able to print addresses, but isn't filtering them the way I'm expecting it too. Rather than printing several addresses from 'matchcount' it prints the addresses of all parcels and seems to repeat them three times.

How can I edit this code to print only the selected features?

4

You have some issues with your code.

If parcel is referencing a shapefile, for example:

parcel = r'C:\folder\parcels.shp'

Then parcel will be a string and when you do:

for feature in parcel:

You will be iterating over each character in it.

And im pretty sure matchcount will not be a selection or a layer, it will be a result object when you do this:

matchcount=arcpy.SelectLayerByLocation_management(parcel, 'CROSSED_BY_THE_OUTLINE_OF', power)

The selection is made on parcel, if it is a feature layer created with MakeFeatureLayer.

I suggest you try Intersecting parcels and lines then read the output using da.SearchCursor. You can write the output to memory (or in_memory) if you dont want to save the output to disk.

Select By Location for each feature like you try will be slow.

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.