1

I have two feature classes. The first is a point file, derived from a raster, with the values for slope of the terrain. The second is a shapefile of polygons. What I want to do is spatially join the points to polygons using the ONE_TO_ONE join option, and for each polygon contain an average value of the slope of all the points that are within the polygon.

The problem is when I execute the following code, the GRID_CODE field (slope values from the points shapefile) is empty for the output feature class.

import arcpy
print "\n\n************************\n"
slopes = r'C:\Users\slope_to_pt.shp'
polygons = r'C:\Users\polygons.shp'
outfc = r'C:\Users\slopeToPolygons'
print "Creating Field Map"

#create the field map
myMap = arcpy.FieldMappings()
myMap.addTable(polygons)
myMap.addTable(slopes)


fIndex = myMap.findFieldMapIndex("GRID_CODE")
print "GRID_CODE index is:", fIndex

#create new field map with the "GRID_CODE" field
NewFieldMap = myMap.getFieldMap(fIndex)

#set the merge rule
NewFieldMap.mergeRule = 'Mean'
print "merge rule is:", str(NewFieldMap.mergeRule)
myMap.replaceFieldMap(fIndex,NewFieldMap)
print "performing spatial join..."
arcpy.SpatialJoin_analysis(polygons, slopes, slopeToPolygons, "JOIN_ONE_TO_ONE", "", myMap, "CONTAINS")

Again, when I run this code the GRID_CODE entry for each polygon is null, when it should be the average of all the values of the points that are within the polygon. Whenever I run it with a "JOIN_ONE_TO_MANY" option, it populates the GRID_CODE field; however, I want to perform a ONE_TO_ONE join.

Additional Information: the datatype for the GRID_CODE field is 'double'

Any ideas on why it wouldn't populated the GRID_CODE field for each polygon with the averages from the points?

  • I'm surprised the spatial join execute as slopeToPolygons does not seem to be declared anywhere? – Hornbydd Aug 11 '15 at 19:35
  • I've run pretty much your exact code (swapping in comparable datasets that I have on hand) and I'm getting the averaged value that you're looking for in my equivalent of the GRID_CODE field. As @Hornybydd notes the variable outfc should be called slopeToPolygons or outfc should appear in the spatial join tool, and the file path that outfc points to needs a .shp at the end, but besides that the code seems to be sound. – Grant Humphries Aug 12 '15 at 18:40
  • @Hornbydd: in my actual code there is no discrepancy in terms of the spatial join excecute, I changed the names and filepaths when I posted the code here. – msmi235 Aug 12 '15 at 21:17
  • @msmi235 have you resolved this question? I have successfully field mapped a spatial join with a "SUM" merge rule recently. – forrestchev Mar 29 '16 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.