Arcpy.sa.ExtractByPolygon doesn't support Polygon class or multiple polygons

1899
8
Jump to solution
03-23-2016 09:12 AM
FredSpataro
Occasional Contributor III

The help document for ExtractByPolygon (http://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/extract-by-polygon.htm) states:

"A polygon (or polygons) that defines the area of the input raster to be extracted.

Each polygon part is a list of vertices defined by Point classes. Optionally a Polygon class can be used to define a list of polygon parts."

However the tool fails if the parameter is an arcpy.Polygon class. Is a polygon class really supported? It would be much easier than extracting out the points for the polygon part into a new list...

Also, the document seems to indicate that multiple polygons can be used at one time: See the first line of the parameter description and line below clearly shows a list of multiple lists

"form of the object is:

[[point(x1,y1), point(x2,y2), point(xn,yn), ..., point(x1,y1)], [point(x'1,y'1), point(x'2,y'2), point(x'n,y'n), ..., point(x'1,y'1)], ...]"

The tools fails when the list contains multiple lists of points.

Is the documentation wildly incorrect or the tool broken or am I using it correctly?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

I can't get it to work using a multipart Polygon object, but it does work by simply passing the entire flat list of points. In your case, try:

mp = p1+p2

View solution in original post

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

You need to construct the polygon properly.  See the example at the bottom of this link

Polygon—Help | ArcGIS for Desktop

0 Kudos
FredSpataro
Occasional Contributor III

Hi Dan:

Thanks for the response... pretty sure I'm constructing the polygon correctly:

1. If I use the point list of a single polygon in the extract function, they each work fine... I've verified they are closed (last point = first point) and clockwise drawn.  However if I add each point list to a new point list and use that in the extract method, it fails.  My multi-polygon list looks very much like the one that's posted in the help:

p1 = [<Point (340892.76867, 4635242.58032, #, #)>, <Point (340899.5, 4635249.5, #, #)>, <Point (340914.5, 4635237.5, #, #)>, <Point (340899.5, 4635228.5, #, #)>, <Point (340892.967108, 4635229.68185, #, #)>, <Point (340892.76867, 4635242.58032, #, #)>]

works fine

p2 = [<Point (340929.5, 4635231.5, #, #)>, <Point (340921.409873, 4635224.25788, #, #)>, <Point (340919.557786, 4635238.01624, #, #)>, <Point (340922.6005, 4635244.69699, #, #)>, <Point (340932.985417, 4635244.63084, #, #)>, <Point (340929.5, 4635231.5, #, #)>]

works fine

mp = [p1, p2]

fails

2. As for using the arcpy.Polygon class, I'm retrieving the polygons directly from a feature class so I'm pretty sure they are "constructed correctly".  Just to ensure the result of the search cursor in not a proper arcpy.Polygon class, I exported the points to a simple float list and rebuilt the arcpy.Polygon class as described in the help and it still fails.... as either a single or a multiple part. 

<Polygon object at 0x3734d4d0[0x3734d440]>

fails

0 Kudos
XanderBakker
Esri Esteemed Contributor

The polygons 1 and 2 are both with point clockwise, constructed according to what is stated in the Help. I have had problems with the ExtractByPolygon in the past when using multipart polygons, but you are using a list of two single part polygons.

You are probably better off,  when using the Extract by Mask—Help | ArcGIS for Desktop which works with a featurelayer. Just create a feature layer based on the polygons and you are good to go. In my experience Maskis more stable than Polygon.

FredSpataro
Occasional Contributor III

True, ExtractByMask is the fallback, but in my production code we're not reading the points/polygons from feature class --- that was just the test code --- so creating the feature class is an expensive disk io or memory operation ... the ExtractByMask tools also causes the cells alignment to change if the raster analysis environment isn't correctly configured so it tends to lead to errors in the overall process.

It would just be nice to either have the documentation correctly state what works or if the documentation is correct then there's a bug in the tool...

0 Kudos
DarrenWiens2
MVP Honored Contributor

I can't get it to work using a multipart Polygon object, but it does work by simply passing the entire flat list of points. In your case, try:

mp = p1+p2

0 Kudos
FredSpataro
Occasional Contributor III

Winner, Winner, Chicken Dinner!!!

Thanks Darren... that's seems wrong but it does work and gets me moving forward ...

0 Kudos
DarrenWiens2
MVP Honored Contributor

Yes, it does seem wrong. This tool has always caught me off guard.

0 Kudos
FredSpataro
Occasional Contributor III

Adding a note:  The list addition works fine for 2 polygons but if you add a third, it runs but the result is bunk...

p3 = [<Point (340884.5, 4635231.5, #, #)>, <Point (340875.0534, 4635240.9783, #, #)>, <Point (340881.5, 4635249.5, #, #)>, <Point (340890.5, 4635246.5, #, #)>, <Point (340890.5, 4635237.5, #, #)>, <Point (340884.5, 4635231.5, #, #)>]

mp = p1 + p2 + p3

Tools executes to completion but result has a "slices" thru the result rather than discrete chunks of cells.  I've tried a bunch of different combinations and polygons... two always seems to work, three+ no good... oh well ExtractByMask i guess.

0 Kudos