I'm writing a Python toolbox and would like to build in a check for raster data within the user-supplied area of interest. This piece of code works most of the time:

if (str(arcpy.GetRasterProperties_management(chsaoi, "ALLNODATA")) == "1"):
    arcpy.AddWarning("No raster coverage in input OID {}.".format(row[1]))

But when the polygon is very large, this check seems to fail. Even when the entire input feature class has raster coverage, the "ALLNODATA" check returns '0'.

Can someone help me come up with a better way to do this?

  • 1
    Do you have a spatial analyst license? You can perform an IsNull using your polygon as a mask (arcpy.env.Mask resources.arcgis.com/en/help/main/10.1/index.html#//…), calculate the statistics and if your minimum and maximum of the IsNull raster obtained by GetRasterProperties both equal 1 then you have all null. but first repair geometries on your input polygons, if your input polygons have self intersections or incorrect ring orientations then your overlay wont work properly. Dec 17 '18 at 0:53

Assuming that you are checking if you have a full raster coverage within the ROI polygon, I reckon your best bet is to use geometries:

img = arcpy.Raster(path_to_img)
raster_coverage = img.extent

if not raster_coverage.contains(ROI_polygon):
    arcpy.AddWarning("No raster coverage in input ROI")

You can perform other checks if you prefer, like ROI.within(raster_extent). you can find a full list here.

However this will not work for areas within the image that have nodata values. For that you may want to polygonize your raster and then perform this check. It may be slightly slower at the beginning, but it is still worth if you have a lot of ROIs to check. Should also be more reliable than playing with pixel values.

  • I only need to know if there is any rater coverage. Full coverage is not necessary.
    – Ian Yau
    Dec 17 '18 at 5:54
  • Then overlaps is your friend if you use this approach. If you scroll down in that page you will see all the methods to define relationship between features, and you are interested in the polygon to polygon case Dec 18 '18 at 2:16

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.