File: prepared.py

package info (click to toggle)
python-shapely 1.6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,368 kB
  • sloc: python: 9,915; makefile: 102; sh: 31
file content (93 lines) | stat: -rw-r--r-- 2,898 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Support for GEOS prepared geometry operations.
"""

from shapely.geos import lgeos
from shapely.impl import DefaultImplementation, delegated
from pickle import PicklingError


class PreparedGeometry(object):
    """
    A geometry prepared for efficient comparison to a set of other geometries.
    
    Example:
      
      >>> 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
    """
   
    impl = DefaultImplementation
    
    def __init__(self, context):
        self.context = context
        self.__geom__ = lgeos.GEOSPrepare(self.context._geom)
    
    def __del__(self):
        if self.__geom__ is not None:
            try:
                lgeos.GEOSPreparedGeom_destroy(self.__geom__)
            except AttributeError:
                pass # lgeos might be empty on shutdown
        self.__geom__ = None
        self.context = None
    
    @property
    def _geom(self):
        return self.__geom__

    @delegated
    def contains(self, other):
        """Returns True if the geometry contains the other, else False"""
        return bool(self.impl['prepared_contains'](self, other))

    @delegated
    def contains_properly(self, other):
        """Returns True if the geometry properly contains the other, else False"""
        return bool(self.impl['prepared_contains_properly'](self, other))

    @delegated
    def covers(self, other):
        """Returns True if the geometry covers the other, else False"""
        return bool(self.impl['prepared_covers'](self, other))

    @delegated
    def crosses(self, other):
        """Returns True if the geometries cross, else False"""
        return bool(self.impl['prepared_crosses'](self, other))

    @delegated
    def disjoint(self, other):
        """Returns True if geometries are disjoint, else False"""
        return bool(self.impl['prepared_disjoint'](self, other))

    @delegated
    def intersects(self, other):
        """Returns True if geometries intersect, else False"""
        return bool(self.impl['prepared_intersects'](self, other))

    @delegated
    def overlaps(self, other):
        """Returns True if geometries overlap, else False"""
        return bool(self.impl['prepared_overlaps'](self, other))

    @delegated
    def touches(self, other):
        """Returns True if geometries touch, else False"""
        return bool(self.impl['prepared_touches'](self, other))

    @delegated
    def within(self, other):
        """Returns True if geometry is within the other, else False"""
        return bool(self.impl['prepared_within'](self, other))

    def __reduce__(self):
        raise PicklingError("Prepared geometries cannot be pickled.")

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