File: __init__.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 (91 lines) | stat: -rw-r--r-- 2,812 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Provides multi-point element-wise operations such as ``contains``."""

import warnings

import numpy as np

import shapely
from shapely.prepared import PreparedGeometry


def _construct_points(x, y):
    x, y = np.asanyarray(x), np.asanyarray(y)
    if x.shape != y.shape:
        raise ValueError("X and Y shapes must be equivalent.")

    if x.dtype != np.float64:
        x = x.astype(np.float64)
    if y.dtype != np.float64:
        y = y.astype(np.float64)

    return shapely.points(x, y)


def contains(geometry, x, y):
    """Check whether multiple points are contained by a single geometry.

    Vectorized (element-wise) version of `contains`.

    Parameters
    ----------
    geometry : PreparedGeometry or subclass of BaseGeometry
        The geometry which is to be checked to see whether each point is
        contained within. The geometry will be "prepared" if it is not already
        a PreparedGeometry instance.
    x : array
        The x coordinates of the points to check.
    y : array
        The y coordinates of the points to check.

    Returns
    -------
    Mask of points contained by the given `geometry`.

    """
    warnings.warn(
        "The 'shapely.vectorized.contains' function is deprecated and will be "
        "removed a future version. Use 'shapely.contains_xy' instead (available "
        "since shapely 2.0.0).",
        DeprecationWarning,
        stacklevel=2,
    )
    if isinstance(geometry, PreparedGeometry):
        geometry = geometry.context
    shapely.prepare(geometry)
    return shapely.contains_xy(geometry, x, y)


def touches(geometry, x, y):
    """Check whether multiple points touch the exterior of a single geometry.

    Vectorized (element-wise) version of `touches`.

    Parameters
    ----------
    geometry : PreparedGeometry or subclass of BaseGeometry
        The geometry which is to be checked to see whether each point is
        contained within. The geometry will be "prepared" if it is not already
        a PreparedGeometry instance.
    x : array
        The x coordinates of the points to check.
    y : array
        The y coordinates of the points to check.

    Returns
    -------
    Mask of points which touch the exterior of the given `geometry`.

    """
    warnings.warn(
        "The 'shapely.vectorized.touches' function is deprecated and will be "
        "removed a future version. Use 'shapely.intersects_xy(geometry.boundary, x, y)'"
        " instead (available since shapely 2.0.0).",
        DeprecationWarning,
        stacklevel=2,
    )
    if isinstance(geometry, PreparedGeometry):
        geometry = geometry.context
    # Touches(geom, point) == Intersects(Boundary(geom), point)
    boundary = geometry.boundary
    shapely.prepare(boundary)
    return shapely.intersects_xy(boundary, x, y)