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?

The result must populate the columns of an established feature class. It can't have a new output like spatial join delivers.


Using the Spatial Join operation instead of a custom ArcPy script you may have more control over which district is chosen (i.e. Intersect, within, completely within, etc.)


An easier way may be to use Spatial Join, but if you want to do this in the same process as you've described above, you would use Select by Location and then CalculateField.

One way to identify the Python code involved is to set up a Model in Model Builder, and exporting the results to Python.

Here's a mockup - be sure to check for syntax errors as I haven't run this.

# create a layer because select by location works on layers
arcpy.MakeFeatureLayer_management(in_features=parcelfile, out_layer='parcellyr')                                           
            'lyr', overlap_type='intersect', select_features='districts')  # look for a different method if needed

fieldtocalculate = # populate with the name of the field in the districts file
calcExpression = # Python format is !fieldname!

arcpy.CalculateField_management("parcellayer", fieldnametopopulate, '!districtfieldname!', "PYTHON_9.3")

The Spatial Join option is something like the below:

arcpy.SpatialJoin_analysis(targetFeatures, joinFeatures, outfc, "#", "#", fieldmappings)

By default this would be a 1:1 join and would keep all of the fields from the districts, not just the District Name, but you can adjust the fields.


Here is the code I came up with. It will capitalize the words in your string output.


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.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
    print "Join Removed"
    print "Temp File is Joined"


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.