6
from shapely.geometry import Polygon, Point

p = Point(2,2)
poly = Polygon((0,0), (0,5), (5,0), (5,5))

print poly.contains(p)

This prints False, although I'm pretty sure (2,2) is within a square of length 5. Or maybe I just don't know how the contains method really works. p.within(poly) also return False. Am I using the Polygon class correctly or am I just really bad at geometry?

ewcz
  • 12,819
  • 1
  • 25
  • 47
G Bhaskar
  • 93
  • 1
  • 1
  • 4

1 Answers1

9

the polygon should be constructed as

Polygon([(0,0), (5,0), (5,5), (0,5)])

in your case, the sequence (0,0), (0,5), (5,0), (5,5) specifies a "letter Z" rotated 90 degrees clockwise, not a boundary of the square of interest. As it is, it wouldn't even yield a valid polygon, i.e.

>>> P=Polygon([(0,0), (0,5), (5,0), (5,5)])
>>> P.area
0.0
>>> P.is_valid
Self-intersection at or near point 2.5 2.5
False
ewcz
  • 12,819
  • 1
  • 25
  • 47