Select to view content in your preferred language

How to check if a Polygon is inside another Polygon w/o using Select By Location?

6368
5
01-01-2011 08:20 PM
TrungTran
New Contributor
Happy New Year everyone!!

I need your help on this after googling for days.

I am using ArcGIS 9.3 and want to develop a Python code (Python version = 2.6) to recognize if a polygon is inside another polygon. These polygons are created by codes and not in any shapefiles or feature class. Therefore, I don't want to create a new shapefile to just use Select By Location.
I wish there is something like: aPolygon.contains(aPolygon) = True/False

I tried shapely.geometry but ArcToolbox doesn't recognize this line: "from shapely.geometry import Polygon". Shapely can do the topological relationship very simple.

Any helps is appreciated.
0 Kudos
5 Replies
VivekPrasad
Deactivated User
Happy New Year everyone!!

I need your help on this after googling for days.

I am using ArcGIS 9.3 and want to develop a Python code (Python version = 2.6) to recognize if a polygon is inside another polygon. These polygons are created by codes and not in any shapefiles or feature class. Therefore, I don't want to create a new shapefile to just use Select By Location.
I wish there is something like: aPolygon.contains(aPolygon) = True/False

I tried shapely.geometry but ArcToolbox doesn't recognize this line: "from shapely.geometry import Polygon". Shapely can do the topological relationship very simple.

Any helps is appreciated.


Plese check the below forums...they might help you

http://forums.esri.com/thread.asp?c=93&f=993&t=115654

http://forums.esri.com/thread.asp?c=93&f=1729&t=147931
0 Kudos
DanPatterson_Retired
Deactivated User
does the script in arctoolbox recognize a straight import of the shapely module? If not, then it may be an installation issue of shapely. 
This capability has been added to ArcMap 10 for future reference, through the arcpy module
import arcpy
square = [[0,0],[0,1],[1,1],[1,0]]
small_square = [[0.1, 0.1],[0.1, 0.9], [0.9,0.9], [0.9,0.1]]
pnt = arcpy.Point()
array = arcpy.Array()

for a_pnt in square:
  pnt.X = a_pnt[0]; pnt.Y = a_pnt[1]
  array.add(pnt)
poly1 = arcpy.Polygon(array)
array.removeAll()

for a_pnt in small_square:
  pnt.X = a_pnt[0]; pnt.Y = a_pnt[1]
  array.add(pnt)
poly2 = arcpy.Polygon(array)
array.removeAll()

print "poly 1 contains poly2 :", poly1.contains(poly2)
print "poly 2 contains poly1 :", poly2.contains(poly1)
0 Kudos
TrungTran
New Contributor
Thanks Vara and Dan.

Actually, I can do with VBA but just want to do this in Python because I have some other functions in Python as well.

This command "From shapely.geometry import Polygon" was understood in Python Shell. However, when I created a script and ran it through ArcToolbox, the geoprocessing message window kept saying
--------------------
<type 'exceptions Import.Error'>: "No module named shapely.geometry
Fail to execute (test)."
--------------------

Any thoughts?
Thanks.
0 Kudos
ChrisSnyder
Regular Contributor III
In v10 you would probably have to install any modules you wanted to use via v10 in C:\Python26\ArcGIS10.0 folder rather than the default C:\Python26. Note the similar contents of C:\Python26\ArcGIS10.0\Lib and C:\Python26\Lib.
0 Kudos
BruceHarold
Esri Regular Contributor
Hi

Not much help at 9.3 I know, but ArcPy geometry objects have a "contains" method:

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Polygon/000v000000n1000000/

Regards
0 Kudos