Geographic Information Systems Asked by SpindriftSeltzer on May 29, 2021
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:ArcMapApt_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 DocumentsArcGISDefault.gdbApartments_Outside_of_Boundary'
fc1 = r'N:My DocumentsArcGISDefault.gdbApartment_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')
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:ArcMapApt_Parcels'
arcpy.SelectLayerByAttribute_management('Parcels_owner', 'NEW_SELECTION', "[AcctType] = 'APARTMENT'")
arcpy.SelectLayerByAttribute_management('apartment_neighborhood','NEW_SELECTION',"[ID] = '{}'".format(a))
arcpy.SelectLayerByLocation_management('Parcels_owner','INTERSECT','apartment_neighborhood','SUBSET_SELECTION')
arcpy.SelectLayerByLocation_management('Parcels_owner','COMPLETELY_WITHIN','apartment_neighborhood','#','REMOVE_FROM_SELECTION')
arcpy.CopyFeatures_management('Parcels_owner','Apartments_Outside_of_Boundary_final')
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:ArcMapApt_Parcels'
arcpy.SelectLayerByAttribute_management('Parcels_owner', 'NEW_SELECTION', "[AcctType] = 'APARTMENT' AND [ID] = '{}'".format(a))
arcpy.SelectLayerByAttribute_management('apartment_neighborhood','NEW_SELECTION',"[ID] = '{}'".format(a))
arcpy.SelectLayerByLocation_management('Parcels_owner','COMPLETELY_WITHIN','apartment_neighborhood','#','REMOVE_FROM_SELECTION')
arcpy.CopyFeatures_management('Parcels_owner','Apartments_Outside_of_Boundary_final')
Answered by jbalk on May 29, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP