I'm pretty new to ArcPy and Python.

In our database, we have a layer containing parcels (polygons) with a neighborhood code and a polygon layer that contains the neighborhood boundary. Within that parcel layer, we have some errors where the neighborhood that the parcel should be in is not. I'm trying to attempt a way to compare each parcel and see if it falls within the neighborhood boundary that it's assigned. An example would be if this parcel is assigned to neighborhood 1, and falls within the boundary of neighborhood 1. If the neighborhood boundary does not match up, I would like that selected so it can be analyzed further. This is what I have so far as my ArcPy code.

I got a runtime error when it could not find the column, but I'm also unsure if a search cursor is what I need. I tried a join using the standard GIS tool, but it didn't return what I needed. I used column 29 because that is the one that contains the neighborhood code, same for column 3.

# Name: ExtractFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial 
# relationships to another layer, and an attribute query

# Import system modules
import arcpy

# Set the workspace
arcpy.env.workspace = r'C:\ArcMap\Apt_Parcels'

# Select all apartments which overlap the Neighborhood polygon
Apartments_SelectAll = arcpy.SelectLayerByAttribute_management('Parcels_owner', 'NEW_SELECTION', 
                                                          "[AcctType] = 'APARTMENT'")

arcpy.CopyFeatures_management(Apartments_SelectAll, 'Apartments_Outside_of_Boundary')

Apartments_neighborhoods = arcpy.SelectLayerByAttribute_management('apartment_neighborhood', 'NEW_SELECTION', 
                                                          "[Id] = '0'")

arcpy.CopyFeatures_management(Apartments_neighborhoods, 'Apartment_Neighborhoods')

# Within selected features, further select only those apartments that fall outside
# of the neighborhoods they are supposed to be in

fc = r'N:\My Documents\ArcGIS\Default.gdb\Apartments_Outside_of_Boundary'
fc1 = r'N:\My Documents\ArcGIS\Default.gdb\Apartment_Neighborhoods'

with arcpy.da.SearchCursor(fc,['apt_nbhd']) as cursor1:
    for x in cursor1:
        print x[29]

with arcpy.da.SearchCursor(fc1, ['Nbhd']) as cursor2:
    for y in cursor2:
        print y[3]      

arcpy.SelectLayerByAttribute_management(Apartments_Outside_of_Boundary, 'SUBSET_SELECTION', 
                                        'cursor1' == 'cursor2')

# Write the selected features to a new featureclass
arcpy.CopyFeatures_management(Apartments_SelectAll, 'Apartments_Outside_of_Boundary_final')
  • It seems like a spatial join with a one-to-many relationship is the way to go here. It sounds like you tried that. Can you elaborate on what you mean when you say it didn't give the results you wanted? Also, have you added an error handler to your script. Adding an error handler and posting the actual error will help a lot. See the traceback example her desktop.arcgis.com/en/arcmap/10.3/analyze/python/… – GBG Jul 10 at 18:56
  • Like you said, I tried a join, but the results I got were really inconsistent. There were parcels that were not joined that should have been. i.e they had the right neighborhood within the correct boundary. When I tried to validate the join, it gave me a warning saying that I was performing it within an index. I'll look into adding an error handler to my code and post back when I figure out what the error is. – Taylor Kravits Jul 11 at 14:39
  • I got an Syntax error for line 17 which is: Apartments_SelectAll = arcpy.SelectLayerByAttribute_management('Parcels_owner', 'NEW_SELECTION', "[AcctType] = 'APARTMENT'") I'm not sure what I did to cause this error @GBG – Taylor Kravits Jul 16 at 20:23

You need to select the neighborhood, select the parcels that intersect, then remove the parcels that are completely within the neighborhood, leaving you just the overlaps. Try this instead:

import arcpy

#neighborhood id
a = 0

arcpy.env.workspace = r'C:\ArcMap\Apt_Parcels'

arcpy.SelectLayerByAttribute_management('Parcels_owner', 'NEW_SELECTION', "[AcctType] = 'APARTMENT'")

arcpy.SelectLayerByAttribute_management('apartment_neighborhood','NEW_SELECTION',"[ID] = '{}'".format(a))




If the 'Parcels_owner' layer also has the neighborhood id field, you can do this instead:

import arcpy

#neighborhood id
a = 0

arcpy.env.workspace = r'C:\ArcMap\Apt_Parcels'

arcpy.SelectLayerByAttribute_management('Parcels_owner', 'NEW_SELECTION', "[AcctType] = 'APARTMENT' AND [ID] = '{}'".format(a))

arcpy.SelectLayerByAttribute_management('apartment_neighborhood','NEW_SELECTION',"[ID] = '{}'".format(a))


  • I get "ERROR 000622: Failed to execute (Select Layer By Location). Parameters are not valid. ERROR 000628: Cannot set input into parameter search_distance." It seems that arcpy doesn't like the 'remove from selection' in the select layer by location line of code. I'll play with this a bit and see what I get – Taylor Kravits Jul 11 at 14:34
  • I forgot the placeholder for search distance. Try it with my edits @TaylorKravits – jbalk Jul 11 at 21:26
  • Hmmm, I got a Warning 000117: Empty output generated... I'll look into this some more, but appreciate the help! – Taylor Kravits Jul 11 at 22:28

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.