Select to view content in your preferred language

Deleting very large number of polygons

2791
4
Jump to solution
09-28-2015 01:35 AM
BenLeslie1
Frequent Contributor
BlackPixPoly = "BlackPixPoly.shp"
arcpy.RasterToPolygon_conversion(BlackPixRast, BlackPixPoly, "SIMPLIFY")
#delete black pixel polygons to leave non-black pixels
arcpy.MakeFeatureLayer_management(BlackPixPoly, "BlackPixPoly_lyr")
arcpy.SelectLayerByAttribute_management("BlackPixPoly_lyr", "NEW_SELECTION", "\"GRIDCODE\" = 0")
arcpy.DeleteFeatures_management("BlackPixPoly_lyr")

Here is an extract of my script that converts a raster to polygons and then deletes the polygons that represent black pixels.  The script has been used many times successfully but has hit a problem with a particular file.  I get:

ERROR 999999

Failed to execute (DeleteFeatures)

For this particular file it is attempting to delete almost 1.2 million polygons - my guess is that this is the cause of the problem.

I tried DeleteRows (is there a difference?) but got the same problem.

I tried adding a line to RepairGeometry prior to deletion but get the same problem.

Is there something I can do to help it get through this task?

0 Kudos
1 Solution

Accepted Solutions
Luis_ÁngelPascual_Camino
Occasional Contributor

Hi Ben.

I'm thinking that if you already have a raster, first clean black pixel and after convert to polygon if you need it.

You can use the SetNull function to operate with pixel values, and obtain a new raster keeping only the pixel you want to. Into the sentence, the SetNull function will give the 'NoData' value to the pixel that will match the expression. The rest will remain with the value you want, for example 1.

It will be something like (Assuming that the black pixels have a 0 value):

# Check out the ArcGIS Spatial Analyst extension license

arcpy.CheckOutExtension("Spatial")

# Execute SetNull

outSetNull = SetNull(BlackPixRast, 1, "VALUE == 0")

# Save the output

outSetNull.save("C:/TEMP/newRaster")

# Convert To Polygon

arcpy.RasterToPolygon_conversion(newRaster, BlackPixPoly, "SIMPLIFY")

Good luck!

Luis Pascual

View solution in original post

0 Kudos
4 Replies
Luis_ÁngelPascual_Camino
Occasional Contributor

Hi Ben.

I'm thinking that if you already have a raster, first clean black pixel and after convert to polygon if you need it.

You can use the SetNull function to operate with pixel values, and obtain a new raster keeping only the pixel you want to. Into the sentence, the SetNull function will give the 'NoData' value to the pixel that will match the expression. The rest will remain with the value you want, for example 1.

It will be something like (Assuming that the black pixels have a 0 value):

# Check out the ArcGIS Spatial Analyst extension license

arcpy.CheckOutExtension("Spatial")

# Execute SetNull

outSetNull = SetNull(BlackPixRast, 1, "VALUE == 0")

# Save the output

outSetNull.save("C:/TEMP/newRaster")

# Convert To Polygon

arcpy.RasterToPolygon_conversion(newRaster, BlackPixPoly, "SIMPLIFY")

Good luck!

Luis Pascual

0 Kudos
BenLeslie1
Frequent Contributor

That works very nicely. Thank you.

For anyone else referencing this - the correct syntax for the sql statement is a single equals sign:

outSetNull = SetNull(BlackPixRast, 1, "VALUE = 0")

NeilAyres
MVP Alum

I investigated the speed of deleting a large number of features recently.

I found that both DeleteFeatures & a da.UpdateCursor / deleteRow managed about 50 records per second. So, even if you get it to work it will take a long time.

Rather go Luis' route and not create the features you don't want in the first place.

NeilAyres
MVP Alum

Or, of course, select what you want then save to another dataset.