0

I have a script that I am using to create and calculate a new field based on the features spatial location. Basically, I have a geodatabase that contains parcels by county in florida, and I am trying to calculate which wind_zone they are in based on their location within a shapefile that contains wind zone polygons. The script runs fine, but the wind zone description values are not transferring over, instead the field is populated with nothing but NULL values.

The code I have so far:

import arcpy

# Set overwrite option
arcpy.env.overwriteOutput = True

# Define your workspace. This should be your database with parcels feature     class
arcpy.env.workspace = r"C:\Data\HAAS\parc_join\parcels_1.gdb"

# This can be done once for all iterations
arcpy.MakeFeatureLayer_management(r"C:\Data\HAAS\wind_zones\WZ_merge.shp",     "lyr_windZones")

for fc in arcpy.ListFeatureClasses():
    # KEEPING ORIGINAL LAYERS (NOT CREATING ADDITIONAL "JOINED" LAYER)
    # Create FeatureLayers
    arcpy.MakeFeatureLayer_management(fc, "lyr_parcels")
    # Add a Windzone description field
    arcpy.AddField_management("lyr_parcels", "WZ_Desc1", "SHORT")

    # Create a search cursor for the WZs
    rows = arcpy.SearchCursor("lyr_windZones")
        for row in rows:
        # What you'll do is select each WZ one at a time, and then select all the parcels in that WZ and calculate the fields 
        # NOTE: If you are using not using shapefiles, then you'll have to change the FID in the line below to OBJECTID (or similar)
        arcpy.SelectLayerByAttribute_management("lyr_windZones", "NEW_SELECTION", "\"FID\" = " + str(row.getValue("FID")))
        arcpy.SelectLayerByLocation_management("lyr_Parcels", "HAVE_THEIR_CENTER_IN", "lyr_windZones", "", "NEW_SELECTION")
        arcpy.CalculateField_management("lyr_parcels", "WZ_Desc1", "'{0}'".format(str(row.getValue("DESCRIPT"))), "PYTHON_9.3", "")
        print "Finished processing " + fc +str(row.getValue("DESCRIPT"))

print "Script Complete"
  • What's the output? The print line with Finished processing is printing out the row.getValue. Adding the output to the question may help. – Richard Morgan Mar 22 '17 at 19:03
  • A summary of the output in the console is: Finished processing stJohns150 Finished processing saintLucie150 Finished processing sumter150 Finished processing suwanee150 Finished processing taylor150 Finished processing union150 Finished processing volusia150 Finished processing wakulla150 Finished processing walton150 Finished processing washington150 Script Complete Where the word is the name of the county, and the number is the wind category. Optimally, the number would be populated into the corresponding parcel that falls within it. @Richard Morgan – Michael Mar 22 '17 at 19:22
2

A simpler solution would be to use Spatial Join. For example:

import arcpy, os

# List the parcel feature classes
arcpy.env.workspace = r"C:\Data\HAAS\parc_join\parcels_1.gdb"
fcs = arcpy.ListFeatureClasses()

# The wind zones feature class
zones = r"C:\Data\HAAS\wind_zones\WZ_merge.shp"

out_ws = r'C:\path\to\your\geodatabase.gdb\out_feature_dataset'

for fc in fcs:
    # Define the output feature class
    out_feature_class = os.path.join(out_ws, fc + '_spatialjoin')

    # Perform the spatial join--you can specify the overlap type here (default in intersect)
    arcpy.SpatialJoin_analysis(fc, zones, out_feature_class)
  • the operation is intended to iterate through multiple feature classes in a geodatabase. Each feature class is an individual county containing all of the parcels within it. I have tried both a 'TEXT' and 'SHORT', both with the same results. – Michael Mar 22 '17 at 19:25
  • Thank you, I will give this a shot and let you know how it works. Could I make this loop through a geodatabase containing ~75 feature classes? Each fc is an individual county with the parcels within and I'm trying to avoid doing each county manually. – Michael Mar 22 '17 at 19:49
  • I've altered the script to work in a loop – Aaron Mar 22 '17 at 20:00
  • Thank you for your help, but the Join is still populating fields full of NULL values! Could it have anything to do with the fact that I am trying to join a shapefile to a fc in a .gdb? – Michael Mar 24 '17 at 18:36
  • @Michael I would try two things: 1) make sure fc and shp have the same projected coordinate system and if that does not work, try 2) convert shp to file geodatabase fc. – Aaron Mar 24 '17 at 19:12

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.