I've got the following script to loop through a polygon dataset, but I keep getting a strange error that I can't fathom.
ERROR 000728: Field 10 does not exist within table
Failed to execute (PolygonToRaster).
I don't have a "Field 10" so im not sure why the PolyToRas tool is trying to access it.
Here's the script
# for loop to loop through each "group_extent" polygon in turn and create shadow polygons
with arcpy.da.SearchCursor(group_extent,['SHAPE@', 'GroupRef', 'Species', 'MaxHeight']) as cursor:
groupCount = 0
for fc in cursor:
# variables
shadowRas = "shadowRas"
shadowPnts = "memory\shadowPnts"
shadowPntsJoined = "memory\shadowPntsJoined"
cellsize = fc[3]
# polygon to raster
arcpy.conversion.PolygonToRaster(fc[0], cellsize, shadowRas, "CELL_CENTER", "NONE", cellsize, "DO_NOT_BUILD")
# raster to point
arcpy.conversion.RasterToPoint(shadowRas, shadowPnts, "Value")
arcpy.management.DeleteFeatures(shadowRas)
# join group data back to shadowPnts
arcpy.analysis.SpatialJoin(shadowPnts, group_extent, shadowPntsJoined)
Any ideas what's going on?
Solved! Go to Solution.
Yes.
# for loop to loop through each "group_extent" polygon in turn and create shadow polygons
with arcpy.da.SearchCursor(group_extent,['SHAPE@', 'GroupRef', 'Species', 'MaxHeight']) as cursor:
groupCount = 0
for shp, groupRef, species, maxHeight in cursor:
# variables
memFC = "memory/FC"
shadowRas = "shadowRas"
shadowPnts = "memory\shadowPnts"
shadowPntsJoined = "memory\shadowPntsJoined"
# copy the current feature into a memory feature class
arcpy.management.CreateFeatureclass("memory", "FC", "POLYGON", spatial_reference=shp.spatialReference)
arcpy.management.AddField(memFC, "ValueField", "FLOAT")
with arcpy.da.InsertCursor(memFC, ["SHAPE@", "ValueField"]) as icursor:
icursor.insertRow([shp, maxHeight])
# polygon to raster
arcpy.conversion.PolygonToRaster(memFC, "ValueField", shadowRas, "CELL_CENTER", "NONE", cellsize, "DO_NOT_BUILD")
Another option is to loop through and select by attribute or make feature layer with a where clause
# for loop to loop through each "group_extent" polygon in turn and create shadow polygons
oidname = arcpy.da.Describe(group_extent)["OIDFieldName"]
with arcpy.da.SearchCursor(group_extent, ['OID@']) as cursor:
groupCount = 0
for oid in cursor:
# variables
grp_lyr = arcpy.management.MakeFeatureLayer(group_extent, "temp_group", f"{oidname} = {oid}")[0]
shadowRas = "shadowRas"
shadowPnts = "memory\shadowPnts"
shadowPntsJoined = "memory\shadowPntsJoined"
# polygon to raster
arcpy.conversion.PolygonToRaster(grp_lyr, "MaxHeight", shadowRas, "CELL_CENTER", "NONE", cellsize, "DO_NOT_BUILD")
This is the tool's signature and your call:
arcpy.conversion.PolygonToRaster(in_features, value_field, out_rasterdataset, {cell_assignment}, {priority_field}, {cellsize}, {build_rat})
arcpy.conversion.PolygonToRaster(fc[0], cellsize, shadowRas, "CELL_CENTER", "NONE", cellsize, "DO_NOT_BUILD")
You're using cellsize as value field name. Previously, you assigned the value of MaxHeight to cellsize. In the first feature, the MaxHeight field probably has the value 10, so the tool is trying to access field 10. You should use the actual field name, not the value.
Also, not sure, but I think this only works on feature classes, not singular features?
@JohannesLindner top marks for you! Stupidly I was linking to the value not the field in the value variable of the tool 🤦thank you.
Will test shortly and revert. I'm hoping it does work on a feature by feature basis, as that's the purpose of my script. It's to create a feature class of points, with values and spacings that match the max height of each tree group feature.
@JohannesLindner so im looking at how to call the field name MaxHeight in the conversion gp tool. Am i right in thinking that i need to define a new memory feature class with the feature added to it, and then run on that, and delete at the end of the loop?
I think so, yes.
Create a memory fc, add the field, insert the current feature (shape and maxheight), run the tool, delet the fc.
Depending on your feature count, it might be faster to create the memory fc outside of the loop and truncate instead of delete.
Huh, seems you can't truncate tables in memory, because you're not the data owner...
arcpy.management.DeleteRows(memory_table) works, though.
@JohannesLindner thank you. but do i need an insertcursor to add the searchcursor object to the feature class?
Yes.
# for loop to loop through each "group_extent" polygon in turn and create shadow polygons
with arcpy.da.SearchCursor(group_extent,['SHAPE@', 'GroupRef', 'Species', 'MaxHeight']) as cursor:
groupCount = 0
for shp, groupRef, species, maxHeight in cursor:
# variables
memFC = "memory/FC"
shadowRas = "shadowRas"
shadowPnts = "memory\shadowPnts"
shadowPntsJoined = "memory\shadowPntsJoined"
# copy the current feature into a memory feature class
arcpy.management.CreateFeatureclass("memory", "FC", "POLYGON", spatial_reference=shp.spatialReference)
arcpy.management.AddField(memFC, "ValueField", "FLOAT")
with arcpy.da.InsertCursor(memFC, ["SHAPE@", "ValueField"]) as icursor:
icursor.insertRow([shp, maxHeight])
# polygon to raster
arcpy.conversion.PolygonToRaster(memFC, "ValueField", shadowRas, "CELL_CENTER", "NONE", cellsize, "DO_NOT_BUILD")
Another option is to loop through and select by attribute or make feature layer with a where clause
# for loop to loop through each "group_extent" polygon in turn and create shadow polygons
oidname = arcpy.da.Describe(group_extent)["OIDFieldName"]
with arcpy.da.SearchCursor(group_extent, ['OID@']) as cursor:
groupCount = 0
for oid in cursor:
# variables
grp_lyr = arcpy.management.MakeFeatureLayer(group_extent, "temp_group", f"{oidname} = {oid}")[0]
shadowRas = "shadowRas"
shadowPnts = "memory\shadowPnts"
shadowPntsJoined = "memory\shadowPntsJoined"
# polygon to raster
arcpy.conversion.PolygonToRaster(grp_lyr, "MaxHeight", shadowRas, "CELL_CENTER", "NONE", cellsize, "DO_NOT_BUILD")
That is indeed a far better and cleaner way...