I am attempting to use Spatial Join to combine tax parcels and zoning information, such that the parcels contain the designation of the underlying zone. However, I only want to take the "esmZONE" attribute from the zoning feature class, without any of the other information. The following script will copy the info from "esmZONE" into "TEST" in the joined feature class, but it also transfers the rest of the attributes from the zoning FC. Can anyone provide some direction as to how I'd remedy this?

import arcpy
import os

base_gdb = r"\\Esm8\gis\python_practice\bellevueBaseData.gdb"
arcpy.env.workspace = base_gdb
target = os.path.join(base_gdb, "inputData", "bellevueParcelsBase")
join = os.path.join(base_gdb, "inputData", "bellevueZoning")
outfc = os.path.join(base_gdb, "outputData", "bellevueTest1")

##good_fields = ["esmZONE"] 
##field_names = [f.name for f in arcpy.Describe(zoning).Fields if not
##               (f.type in ["OID","Geometry"] or
##               f.name in ["Shape_Length","Shape_Area"] or
##               f.name in good_fields)]
##if field_names:
##    arcpy.DeleteField_management(zoning, field_names)

#Create FieldMap and FieldMappings objects
fieldmappings = arcpy.FieldMappings()

zonefield = fieldmappings.findFieldMapIndex("esmZONE")
fieldmap = fieldmappings.getFieldMap(zonefield)

parcel_field = fieldmap.outputField

parcel_field.name = "TEST"
parcel_field.aliasName = "TEST"
fieldmap.outputField = parcel_field

fieldmappings.mergeRule = "Join"
fieldmappings.replaceFieldMap(zonefield, fieldmap)

arcpy.SpatialJoin_analysis(target, join, outfc, "", "KEEP_COMMON", fieldmappings, "WITHIN")

If you're wanting to simply add the EMSZone to the original parcels layer, you can do it without having to do a spatial join... here is a solution similar to an answer I posted here...

import arcpy

# Set overwrite option
arcpy.env.overwriteOutput = True

# Create FeatureLayers
arcpy.MakeFeatureLayer_management(r"\\Esm8\gis\python_practice\bellevueBaseData.gdb\bellevueParcelsBase", "lyr_Parcels")
arcpy.MakeFeatureLayer_management(r"\\Esm8\gis\python_practice\bellevueBaseData.gdb\bellevueZoning", "lyr_Zones")
# If you need to "select only the Zones are of Type=EMS", then use the following line (adjust WHERE CLAUSE as needed) instead of the preceding line...
# arcpy.MakeFeatureLayer_management(r"\\Esm8\gis\python_practice\bellevueBaseData.gdb\bellevueZoning", "lyr_Zones", "\"ZONE_TYPE\" = 'EMS'")

# Add an "EMS Zone" field to the Parcels layer
arcpy.AddField_management("lyr_Parcels", "EMS_ZONE", "TEXT", "", "", "20")

# Create a search cursor for the EMS Zones
rows = arcpy.SearchCursor("lyr_Zones")
for row in rows:
    # What you'll do is select each emsZONE one at a time, and then select all the parcels within that zone and calculate the EMS_ZONE field
    arcpy.SelectLayerByAttribute_management("lyr_Zones", "NEW_SELECTION", "\"OBJECTID\" = " + str(row.getValue("OBJECTID")))
    arcpy.SelectLayerByLocation_management("lyr_Parcels", "HAVE_THEIR_CENTER_IN", "lyr_Zones", "", "NEW_SELECTION")
    arcpy.CalculateField_management("lyr_Parcels", "EMS_ZONE", "'{0}'".format(str(row.getValue("emsZONE"))), "PYTHON_9.3", "")
    print "Finished processing " + str(row.getValue("emsZONE")) 

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.