I am new in ArcPy.

As you can see in the image below, I have:

  1. One shapefile with 9 polygons (each polygon illustrates a community)
  2. 1200 layers. Each shapefile illustrates a GPS track, so they are point layers. I know there are 1200 layer points because I used the "arcpy.mapping.ListLayers" to figure it out.

For each polygon, I want ArcPy to tell me all the layer names that fall "inside", or "are completely contained" by each polygon.

enter image description here

  • So for each of your 1200 shapefiles you'd like to know which have all their points within polygons. If one or more of their points fall outside of a polygon it's not counted, correct? Dec 17 '19 at 18:06
  • Yes, it´s correct. Is it possible to do it in arcpy?.
    – Oskr
    Dec 17 '19 at 19:36
  • Welcome to GIS SE! We're a little different from other sites. We're a Q&A site, not a discussion forum. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. Please check out our short tour for more about how the site works. Thanks.
    – PolyGeo
    Dec 17 '19 at 23:27

I would iterate through your layers and perform a select by location, selecting for points intersecting your polygons. If all your points are selected the layer is completely contained in your polygons.

First make sure no features are selected.

In your python window:

polygons = "polygonlyr" ##name of your polygon layer
mxd = arcpy.mapping.MapDocument ("CURRENT")
lyrs = arcpy.mapping.ListLayers (mxd)

for lyr in lyrs:
    if lyr.name == polygons: continue ##skip polygon layer
    inCount = arcpy.GetCount_management (lyr) [0] ##count features
    arcpy.SelectLayerByLocation_management (lyr, "", polygons)
    if arcpy.GetCount_management (lyr) [0] == inCount: ##count selected features
        print "{} is completely within {}".format (lyr.name, polygons)

I'd also suggest pausing your display to improve efficiency.


Populate your polygon's attribute tables with the name of the shapefile. Then use a spatial join to add those names to the attribute table of the points.

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