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:
- interior of p2 must intersect interior of p1 (--> true)
- interior of p2 does not intersect exterior of p1 (--> true)
- 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