File: prepared.py

package info (click to toggle)
python-shapely 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 18,648; ansic: 6,615; makefile: 88; sh: 62
file content (74 lines) | stat: -rw-r--r-- 2,351 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Support for GEOS prepared geometry operations."""

from pickle import PicklingError

import shapely


class PreparedGeometry:
    """A geometry prepared for efficient comparison to a set of other geometries.

    Examples
    --------
    >>> from shapely.prepared import prep
    >>> from shapely.geometry import Point, Polygon
    >>> triangle = Polygon([(0.0, 0.0), (1.0, 1.0), (1.0, -1.0)])
    >>> p = prep(triangle)
    >>> p.intersects(Point(0.5, 0.5))
    True

    """

    def __init__(self, context):
        """Prepare a geometry for efficient comparison to other geometries."""
        if isinstance(context, PreparedGeometry):
            self.context = context.context
        else:
            shapely.prepare(context)
            self.context = context
        self.prepared = True

    def contains(self, other):
        """Return True if the geometry contains the other, else False."""
        return self.context.contains(other)

    def contains_properly(self, other):
        """Return True if the geometry properly contains the other, else False."""
        return self.context.contains_properly(other)

    def covers(self, other):
        """Return True if the geometry covers the other, else False."""
        return self.context.covers(other)

    def crosses(self, other):
        """Return True if the geometries cross, else False."""
        return self.context.crosses(other)

    def disjoint(self, other):
        """Return True if geometries are disjoint, else False."""
        return self.context.disjoint(other)

    def intersects(self, other):
        """Return True if geometries intersect, else False."""
        return self.context.intersects(other)

    def overlaps(self, other):
        """Return True if geometries overlap, else False."""
        return self.context.overlaps(other)

    def touches(self, other):
        """Return True if geometries touch, else False."""
        return self.context.touches(other)

    def within(self, other):
        """Return True if geometry is within the other, else False."""
        return self.context.within(other)

    def __reduce__(self):
        """Pickling is not supported."""
        raise PicklingError("Prepared geometries cannot be pickled.")


def prep(ob):
    """Create and return a prepared geometric object."""
    return PreparedGeometry(ob)