-1

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.

2

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.)

0

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')                                           
arcpy.SelectLayerByLocation_management(
            '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.

0

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

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)

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.