Problem with geometry methods - Within/Contains

410
4
03-16-2011 07:56 PM
TrungTran
New Contributor
Hello everyone,
I am trying to check if a polygon is within a mask, which is another polygon. To do this, I use the method CONTAINS. However, this method seems to be wrong because it returns FALSE but in fact it should be TRUE.
I am attaching here the mask shapefile and below is the code.

def createPolygonCell(intCellsize, leftX, upperY):
   
    aPoint1 = arcpy.Point()
    aPoint2 = arcpy.Point()
    aPoint3 = arcpy.Point()
    aPoint4 = arcpy.Point()
   
    aPoint1.X = leftX
    aPoint1.Y = upperY

    aPoint2.X = leftX + intCellsize
    aPoint2.Y = upperY

    aPoint3.X = leftX + intCellsize
    aPoint3.Y = upperY - intCellsize

    aPoint4.X = leftX
    aPoint4.Y = upperY - intCellsize

    array= arcpy.Array()
    array.add(aPoint1)
    array.add(aPoint2)
    array.add(aPoint3)
    array.add(aPoint4)
    array.add(aPoint1)

    aPolygon = arcpy.Polygon(array)
    array.removeAll()

    return aPolygon

rows = arcpy.SearchCursor(inMaskShp)  #inMaskShp is the input mask shapefile attached
row = rows.next()
inMask = row.getValue("Shape")   #inMask is the first polygon of the inMaskShp

apoly = createPolygonCell(30, 565845, 3881985)
apoly.within(inMask)   #this returns FALSE although it should be TRUE


I do export inMask and apoly to shapefiles to double check whether apoly is within inMask.
Also, the method CONTAINS returns FALSE as well. And it is not correct.

In addition, interestingly, the four corners of the apoly (which is a little square) are within inMask when I use apoint.within(inMask).

Could anyone please shed a light on this (i.e. how to check a poly within another poly)?
Thanks.
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
without examining the details of your situation in detail, you should be aware that problems have been discovered when using arcpy's geometry objects when no spatial reference is specified.  a protracted discussion is noted in:

http://forums.arcgis.com/threads/9763-Errors-in-arcpy-s-Polygon-class

there is a NIM number which you can track the error on

NIM059845

which indicates that it is resolved as of version 10 SP2 and in 10.1

http://resources.arcgis.com/content/nimbus-bug?bugID=TklNMDU5ODQ1
0 Kudos
TrungTran
New Contributor
Thanks Dan.
Could you please let me know where I can download SP2? I just can't find it myself. Thanks.


without examining the details of your situation in detail, you should be aware that problems have been discovered when using arcpy's geometry objects when no spatial reference is specified.  a protracted discussion is noted in:

http://forums.arcgis.com/threads/9763-Errors-in-arcpy-s-Polygon-class

there is a NIM number which you can track the error on

NIM059845

which indicates that it is resolved as of version 10 SP2 and in 10.2

http://resources.arcgis.com/content/nimbus-bug?bugID=TklNMDU5ODQ1
0 Kudos
DanPatterson_Retired
MVP Emeritus
I don't think it is out yet...so use the workaround in the interim
0 Kudos
TrungTran
New Contributor
OK. Thanks Dan.

I don't think it is out yet...so use the workaround in the interim
0 Kudos