Select to view content in your preferred language

Data in attribute table disappears after spatial join

1298
2
03-30-2012 12:42 PM
MikeMacRae
Frequent Contributor
Hey everyone,

I created the script below. It does this:


  1. Creates a personal geodatabase

  2. spatial joins a point feature class with a point feature class

  3. Places the output spatial join into the new geodatabase

  4. Adds some fields and deletes some fields

  5. does an update cursor to compare 2 fields and and updates one of the new fields


When I run the script through, it retains the attribute for the first row and then all the remaining rows are empty. There is data in both the point feature class and the polygon feature class. These 2 feature classes overlap each other, so there definitely is a join of data. If I comment out everything after the spatial join and just allow the script to create the geodatabase and do the spatial join, the data is there. I've tried to comment out different section to test and it seems that anytime after the spatial join, the output feature classes manintains the first record and the rest are blank. I've tested with 3 different sets of data and nothing seems to work.

This only happens when I run it as a tool. If I run from IDLE, the script works fine.

import arcpy

target_features = r"Z:\test.gdb\point"
join_features = r"Z:\test.gdb\polygon"
output_location = r"Z:\output_folder"

outputmbd = output_location + "\\" + "CompareTable.mdb"
out_feature_class = outputmbd + "\\" + "CompareTable"

arcpy.env.overwriteOutput = True

arcpy.CreatePersonalGDB_management(output_location, "CompareTable.mdb")
arcpy.SpatialJoin_analysis(target_features, join_features, out_feature_class)

arcpy.AddField_management(out_feature_class, "Compare_Fields", "TEXT", "", "", 20)
arcpy.AddField_management(out_feature_class, "POLYGON_SITE", "TEXT", "", "", 10)
arcpy.CalculateField_management(out_feature_class, "POLYGON_SITE", "[SITE]", "VB")
arcpy.AddField_management(out_feature_class, "POINT_SITE", "TEXT", "", "", 10)
arcpy.CalculateField_management(out_feature_class, "POINT_SITE", "[SITE_1]", "VB")

fieldList = arcpy.ListFields(out_feature_class)

fieldNameList = []

for field in fieldList:
    if not field.required and not field.name == "SITE" and not field.name == "SITE_ID" and not field.name == "POINT_SITE" and not field.name == "POLYGON_SITE":
        fieldNameList.append(field.name)    

print fieldNameList
arcpy.DeleteField_management(out_feature_class, fieldNameList)

cur = arcpy.UpdateCursor(out_feature_class)

for row in cur:
    print row.POINT_SITE + "  " + row.POLYGON_SITE
    arcpy.AddMessage(row.POINT_SITE + "  " + row.POLYGON_SITE)

    if row.POLYGON_SITE == row.POINT_SITE:
        row.Compare_Fields = "Same"
    else:
        row.Compare_Fields = "Inclusion"

    cur.updateRow(row.Compare_Fields)
Tags (2)
0 Kudos
2 Replies
PaulKroseman
Frequent Contributor
Are you using SP4?

That could cause this issue according to this thread: http://forums.arcgis.com/threads/54102-Spatial-join-tool-vs.-right-click-spatial-join-producing-diff...
0 Kudos
MikeMacRae
Frequent Contributor
That sounds about right. The behavior is very erratic. Sounds like a bug to me. Thanks for the tip.
0 Kudos