Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm new to python and arcpy but I've been trying to learn how to run geoprocessing tools using arcpy. I have (for this example) 2 shapefiles, a and b, which are both polygons. From plotting them in arcMap, I know that b is found entirely within a.

I would like to write a script that will take these two shapefiles and give me a True/False response about whether or not one polygon contains the other. I was thinking of using the arcpy.Geometry.contains() function.

I have the following:

>>> a_shp="C:\\Data\\shapefile_a.shp"
>>> b_shp="C:\\Data\\shapefile_b.shp"

>>> #read in as geometry
>>> a=arcpy.CopyFeatures_management(a_shp, arcpy.Geometry())
>>> b=arcpy.CopyFeatures_management(b_shp, arcpy.Geometry())

>>> arcpy.Geometry.contains(a[0],b[0])
>>> arcpy.Geometry.contains(b[0],a[0]) #maybe I got the order wrong? Trying the other order

>>> #Maybe I'm getting the syntax wrong, trying something else
>>> a[0].contains(b[0])
>>> b[0].contains(a[0])

I get no error messages but clearly something isn't working properly. Am I doing this right?

share|improve this question

migrated from Oct 10 '11 at 10:52

This question came from our site for professional and enthusiast programmers.

This strategy should work as well as Jason Scheirer's method. I have used geometry object outputs before. Did you get the correct output from Jason's method? The correct syntax is a[0].contains(b[0]) or conversely b[0].within(a[0]). I am not certain though that you can reference by subscripting on a geometry list. – blord-castillo Oct 10 '11 at 11:08
up vote 6 down vote accepted

a and b will be result objects in this case. You'll need to open two search cursors and pull the shape field off of each to use the contains operator. Something like this:

for row1 in arcpy.SearchCursor(shapefile1):
    for row2 in arcpy.SearchCursor(shapefile2):
        if row1.shape.contains(row2.shape):
            return True
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.