15

The Docstring says:

Polygon.contains Returns True if the geometry contains the other, else False

Polygon.within Returns True if geometry is within the other, else False

How are they different?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Sounak
  • 4,803
  • 7
  • 30
  • 48
  • Further to the answer below, also keep in mind that it is sometimes more convenient to check from one or the other geometry depending on what you are currently doing. e.g. if working with a, then you would call one, whereas if you're working with b, then you'd call the inverse. – songololo May 24 '15 at 21:35

2 Answers2

28

They are inverse relationships: A contains B, and B is within A.

   >>> A.contains(B)
   True
   >>> B.within(A)
   True

   +----------------------------------+
   |                                  |
   |         +----------+             |
   |         |          |             |
   |         |          |             |
   |         |          |             |
   |         |          |             |
   |         |          |             |
   |         |    B     |             |
   |         |          |             |
   |         +----------+             |
   |                                  |
   |                                  |
   |   A                              |
   |                                  |
   +----------------------------------+
chepner
  • 497,756
  • 71
  • 530
  • 681
  • what happens if A overlaps B ? why there are two functions? isn't one enough? – Sounak May 23 '15 at 20:25
  • I think neither would be true, since neither shape is wholly within the other. If you mean A and B are essentially the same shape, both would be false. From the manual for [`contains`](http://toblerity.org/shapely/manual.html#object.contains) (emphasis mine): "Returns True if the object’s interior contains the boundary and interior of the other object and *their boundaries do not touch at all.*" – chepner May 23 '15 at 20:25
  • 1
    One would be sufficient--you could define `within(self, obj)` as simply `return obj.contains(self)`--but it may be more readable to use one or the other, depending on the context. – chepner May 23 '15 at 20:29
  • `poly = Polygon(((0, 0), (0, 2), (2, 2), (2, 0)))` `po = Point((1,1))` `print poly.contains(po)` True `print poly.within(po)` False – Sounak May 23 '15 at 20:38
  • something is different here. – Sounak May 23 '15 at 20:39
  • `po.within(poly)` should be true, though. You have to switch the caller and the argument. – chepner May 23 '15 at 21:03
0
a = Polygon([(0, 0), (100, 0), (100, 100), (0, 100)])
b = Polygon([(0, 0), (50, 0), (50, 50), (0, 50)])
print(a.within(b), b.within(a))
print(a.contains(b), b.contains(a))

Output

False True
True False

To the point:

contains is opposite condition of within

   +----------------------------------+
   |                                  |
   |         +----------+             |
   |         |          |             |
   |         |          |             |
   |         |          |             |
   |         |          |             |
   |         |          |             |
   |         |    B     |             |
   |         |          |             |
   |         +----------+             |
   |                                  |
   |                                  |
   |   A                              |
   |                                  |
   +----------------------------------+

In above case:

  • A contains B
  • B within A
USMANHEART
  • 93
  • 1
  • 9