Arcpy select by location

1040
4
02-27-2018 09:12 AM
MikeJones7
New Contributor III

I'm running arcpy and trying to populate my parcel layer with the district name it falls in.  I can manually populate the column by using 'select by location' and then copying the district name from the district layer and calculating the selected values in the parcels.  Is there a way to automate this in arcpy?  

0 Kudos
4 Replies
MicahBabinski
Occasional Contributor III

Hey Mike,

Have a look at Spatial Join:

Spatial Join—Help | ArcGIS Desktop 

It should allow you to define the parameters for doing exactly what you are looking for. The caveat is that this will create a new feature class so you'd need to overwrite your parcel dataset with the result or create an in_memory feature class, join it your original parcels dataset, and update your original district attribute using a field calculator function.

Hope this helps.

Micah

0 Kudos
MikeJones7
New Contributor III

Good thought, but I need to keep my existing feature classes.  I can't create new outputs.  

0 Kudos
MicahBabinski
Occasional Contributor III

In that case I would recommend creating your output in the in_memory workspace. You should then be able to join that result to your original parcel layer using a unique parcel ID, then update your District field using calculate field:

Calculate Field—Data Management toolbox | ArcGIS Desktop 

Micah

0 Kudos
MikeJones7
New Contributor III

Here's what I came up with:

arcpy.MakeFeatureLayer_management(Parcels,"ParcelLayer")

def spJoin(dataName,DistrictColumn,ParcelColumn):
arcpy.SpatialJoin_analysis(target_features = Parcels, join_features = dataName,out_feature_class = "TempJoin")
print dataName + " joined with a spatial join"
arcpy.MakeFeatureLayer_management("TempJoin","TempJoinLayer")
arcpy.AddJoin_management(in_layer_or_view="ParcelLayer", in_field="PARCEL_ID", join_table="TempJoinLayer", join_field="PARCEL_ID", join_type="KEEP_ALL")
print "Join on table"
expression = '!TempJoin.{0}!.title()'.format(DistrictColumn)
print expression
arcpy.CalculateField_management("ParcelLayer","TaxParcel."+ParcelColumn, expression, expression_type="PYTHON_9.3", code_block="")
print "Calcualted Field for "+ dataName
arcpy.RemoveJoin_management("ParcelLayer")
print "Join Removed"
arcpy.DeleteFeatures_management("TempJoin")
print "Temp File is Joined"

#spJoin(DATANAME,COLUMN YOU WANT TO MOVE, COLUMN IN THE DESTINATION)

0 Kudos