1

According to the shapely documentation:

object.within(other)

Returns True if the object’s boundary and interior intersect only with the interior of the other (not its boundary or exterior)

from shapely.geometry import Polygon

p1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
p2 = Polygon([(0, 0), (1, 0), (1, 1)])
print(p2.within(p1))
# True

But p2 boundary intersect with p1 boundary ((0,0), (1,0)).

1 Answer 1

3

Seems that the documentation is not precise at this point. Shapely is based on GEOS, where the condition for within is defined as T*F**F*** in terms of the DE-9IM model.

This means for your example:

  1. interior of p2 must intersect interior of p1 (--> true)
  2. interior of p2 does not intersect exterior of p1 (--> true)
  3. boundary of p2 does not intersect exterior of p1 (--> true)

All conditions are fulfilled and the within function returns true. There is no condition defined if the boundaries of p1 and p2 must intersect or not. If the boundaries are not allowed to intersect, the relation has to be defined as T*F*FF***.

from shapely.geometry import Polygon

p1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
p2 = Polygon([(0, 0), (1, 0), (1, 1)])

print('within function:')
print(p2.within(p1))
# True

print('within defined manually using DE-9IM:')
print(p2.relate_pattern(p1, 'T*F**F***'))
# True

print('within with additional condition (boundaries do not intersect), defined manually using DE-9IM:')
print(p2.relate_pattern(p1, 'T*F*FF***'))
# False

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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