clip hole in raster

10575
31
Jump to solution
02-08-2016 07:00 AM
RickCheney
Occasional Contributor II

For example, if I have a raster layer of Digital Elevation which represents the United States and I want to keep everything except the state of Texas. I have a vector layer polygon of the state of Texas which I hope can be used to clip or mask or intersect the raster data.

How do I get the raster DEM layer with an area cutout or missing where Texas was?

0 Kudos
31 Replies
RickCheney
Occasional Contributor II

I was only using the US map and the state of Texas as an example, but I will look into Making a Mosaic Layer

0 Kudos
XanderBakker
Esri Esteemed Contributor

I find it strange that the user needs to specify the coordinates as a list of arcpy.Points() in the Extract by Polygon—Help | ArcGIS for Desktop tool. Not very user friendly I would say.

Attached a sample toolbox that you can extract to a folder on you disk and test.

The result will be something like this:

It will use the (first) selected polygon for the erase.

The code used is:

def main():

    import arcpy

    # parameters

    ras = arcpy.GetParameter(0)

    fc = arcpy.GetParameter(1)

    method = "OUTSIDE"

    ras_out = arcpy.GetParameterAsText(2)

    # get first polygon of fc

    polygon = arcpy.da.SearchCursor(fc, ('SHAPE@')).next()[0]

    # create list of points

    points = [pnt for pnt in polygon.getPart(0)]

    # Check out the ArcGIS Spatial Analyst extension license

    arcpy.CheckOutExtension("Spatial")

    # Execute ExtractByPolygon and save result

    result = arcpy.sa.ExtractByPolygon(ras, points, method)

    result.save(ras_out)

if __name__ == '__main__':

    main()

Note; it has't been tested thoroughly, and may crash when using a complex geometry.

RickCheney
Occasional Contributor II

Thanks, it looks like this tool is going to work. I got the tool setup and I tried using it.  I got an error the first time but I will continue testing and maybe tweaking and see if I can get it to work.  Thanks again.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Can you share the error you obtained?

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

and I was done hours ago

RickCheney
Occasional Contributor II

Sorry about the delay in responding.  The error information is below.

Traceback (most recent call last):

  File "E:\holeInRaster\EraseRaster.py", line 24, in <module>

    main()

  File "E:\holeInRaster\EraseRaster.py", line 20, in main

    result = arcpy.sa.ExtractByPolygon(ras, points, method)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 1300, in ExtractByPolygon

    extraction_area)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Utils.py", line 53, in swapper

    result = wrapper(*args, **kwargs)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\sa\Functions.py", line 1295, in Wrapper

    extraction_area)

  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\geoprocessing\_base.py", line 504, in <lambda>

    return lambda *args: val(*gp_fixargs(args, True))

RuntimeError: Object: Error in executing tool

Failed to execute (EraseRaster).

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Rick Cheney , looking at the error, I can see that your paths do not have special characters, so that is not causing the problem. The script fail at the line where the actual work takes place. There are many factors that influence that line. There are environment settings that might impact correct functionality of that line of the tool, coordinate systems of the inputs, and other aspects.

Most likely it has to do with the variable "points"  which holds the list vertices that form the polygon. As you must have noticed from the image I posted I used a very simple polygon (nowhere near the complexity of Texas). Maybe the number of vertices or perhaps an invalid geometry can crash the ExtractByPolygon tool.

Could you post the featureclass of polygon of Texas that you are using? I can mimic some data around Texas and see if I can reproduce the error.

Also can you specify the coordinate system of you data frame, both the inputs and the geoprocessing environment?

Or can you test with a simple polygon (few number of vertices) to see if the tool works? If so it is most likely the geometry that causes the problem.

RickCheney
Occasional Contributor II

I'm going to test with a simple polygon and report back with how that works.

0 Kudos
DarrenWiens2
MVP Honored Contributor

I don't see any mention of Extract by Mask, which seems to me the tool to use, to create an output that you can feed into Set Null (or Con) to remove the unwanted parts of the original raster.

XanderBakker
Esri Esteemed Contributor

The advantage of ExtractByMask is that it allows a featurelayer as input and doesn't require a list of points that forms a polygon. However, in combination with Set Null or Con I would rather rasterize the polygons and use the Con statement to obtain the desired result.

Thinking again... what if you do use the ExtractByMask tool and use you states, but you select Texas and invert the selection (everything selected exact Texas). Wouldn't that generate the yield the desired output?

I'm gonna give it a try and will report back later...