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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
"""Methods for measuring (between) geometries."""
import warnings
import numpy as np
from shapely import lib
from shapely.decorators import multithreading_enabled
__all__ = [
"area",
"bounds",
"distance",
"frechet_distance",
"hausdorff_distance",
"length",
"minimum_bounding_radius",
"minimum_clearance",
"total_bounds",
]
@multithreading_enabled
def area(geometry, **kwargs):
"""Compute the area of a (multi)polygon.
Parameters
----------
geometry : Geometry or array_like
Geometry or geometries for which to compute the area.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import MultiPolygon, Polygon
>>> polygon = Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])
>>> shapely.area(polygon)
100.0
>>> polygon2 = Polygon([(10, 10), (10, 20), (20, 20), (20, 10), (10, 10)])
>>> shapely.area(MultiPolygon([polygon, polygon2]))
200.0
>>> shapely.area(Polygon())
0.0
>>> shapely.area(None)
nan
"""
return lib.area(geometry, **kwargs)
@multithreading_enabled
def distance(a, b, **kwargs):
"""Compute the Cartesian distance between two geometries.
Parameters
----------
a, b : Geometry or array_like
Geometry or geometries to compute the distance between.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import LineString, Point, Polygon
>>> point = Point(0, 0)
>>> shapely.distance(Point(10, 0), point)
10.0
>>> shapely.distance(LineString([(1, 1), (1, -1)]), point)
1.0
>>> shapely.distance(Polygon([(3, 0), (5, 0), (5, 5), (3, 5), (3, 0)]), point)
3.0
>>> shapely.distance(Point(), point)
nan
>>> shapely.distance(None, point)
nan
"""
return lib.distance(a, b, **kwargs)
@multithreading_enabled
def bounds(geometry, **kwargs):
"""Compute the bounds (extent) of a geometry.
For each geometry these 4 numbers are returned: min x, min y, max x, max y.
Parameters
----------
geometry : Geometry or array_like
Geometry or geometries for which to compute the bounds.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import LineString, Point, Polygon
>>> shapely.bounds(Point(2, 3)).tolist()
[2.0, 3.0, 2.0, 3.0]
>>> shapely.bounds(LineString([(0, 0), (0, 2), (3, 2)])).tolist()
[0.0, 0.0, 3.0, 2.0]
>>> shapely.bounds(Polygon()).tolist()
[nan, nan, nan, nan]
>>> shapely.bounds(None).tolist()
[nan, nan, nan, nan]
"""
return lib.bounds(geometry, **kwargs)
def total_bounds(geometry, **kwargs):
"""Compute the total bounds (extent) of the geometry.
Parameters
----------
geometry : Geometry or array_like
Geometry or geometries for which to compute the total bounds.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Returns
-------
numpy ndarray of [xmin, ymin, xmax, ymax]
Examples
--------
>>> import shapely
>>> from shapely import LineString, Point, Polygon
>>> shapely.total_bounds(Point(2, 3)).tolist()
[2.0, 3.0, 2.0, 3.0]
>>> shapely.total_bounds([Point(2, 3), Point(4, 5)]).tolist()
[2.0, 3.0, 4.0, 5.0]
>>> shapely.total_bounds([
... LineString([(0, 1), (0, 2), (3, 2)]),
... LineString([(4, 4), (4, 6), (6, 7)])
... ]).tolist()
[0.0, 1.0, 6.0, 7.0]
>>> shapely.total_bounds(Polygon()).tolist()
[nan, nan, nan, nan]
>>> shapely.total_bounds([Polygon(), Point(2, 3)]).tolist()
[2.0, 3.0, 2.0, 3.0]
>>> shapely.total_bounds(None).tolist()
[nan, nan, nan, nan]
"""
b = bounds(geometry, **kwargs)
if b.ndim == 1:
return b
with warnings.catch_warnings():
# ignore 'All-NaN slice encountered' warnings
warnings.simplefilter("ignore", RuntimeWarning)
return np.array(
[
np.nanmin(b[..., 0]),
np.nanmin(b[..., 1]),
np.nanmax(b[..., 2]),
np.nanmax(b[..., 3]),
]
)
@multithreading_enabled
def length(geometry, **kwargs):
"""Compute the length of a (multi)linestring or polygon perimeter.
Parameters
----------
geometry : Geometry or array_like
Geometry or geometries for which to compute the length.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import LineString, MultiLineString, Polygon
>>> shapely.length(LineString([(0, 0), (0, 2), (3, 2)]))
5.0
>>> shapely.length(MultiLineString([
... LineString([(0, 0), (1, 0)]),
... LineString([(1, 0), (2, 0)])
... ]))
2.0
>>> shapely.length(Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]))
40.0
>>> shapely.length(LineString())
0.0
>>> shapely.length(None)
nan
"""
return lib.length(geometry, **kwargs)
@multithreading_enabled
def hausdorff_distance(a, b, densify=None, **kwargs):
"""Compute the discrete Hausdorff distance between two geometries.
The Hausdorff distance is a measure of similarity: it is the greatest
distance between any point in A and the closest point in B. The discrete
distance is an approximation of this metric: only vertices are considered.
The parameter 'densify' makes this approximation less coarse by splitting
the line segments between vertices before computing the distance.
Parameters
----------
a, b : Geometry or array_like
Geometry or geometries to compute the distance between.
densify : float or array_like, optional
The value of densify is required to be between 0 and 1.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import LineString
>>> line1 = LineString([(130, 0), (0, 0), (0, 150)])
>>> line2 = LineString([(10, 10), (10, 150), (130, 10)])
>>> shapely.hausdorff_distance(line1, line2)
14.142135623730951
>>> shapely.hausdorff_distance(line1, line2, densify=0.5)
70.0
>>> shapely.hausdorff_distance(line1, LineString())
nan
>>> shapely.hausdorff_distance(line1, None)
nan
"""
if densify is None:
return lib.hausdorff_distance(a, b, **kwargs)
else:
return lib.hausdorff_distance_densify(a, b, densify, **kwargs)
@multithreading_enabled
def frechet_distance(a, b, densify=None, **kwargs):
"""Compute the discrete Fréchet distance between two geometries.
The Fréchet distance is a measure of similarity: it is the greatest
distance between any point in A and the closest point in B. The discrete
distance is an approximation of this metric: only vertices are considered.
The parameter 'densify' makes this approximation less coarse by splitting
the line segments between vertices before computing the distance.
Fréchet distance sweep continuously along their respective curves
and the direction of curves is significant. This makes it a better measure
of similarity than Hausdorff distance for curve or surface matching.
Parameters
----------
a, b : Geometry or array_like
Geometry or geometries to compute the distance between.
densify : float or array_like, optional
The value of densify is required to be between 0 and 1.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import LineString
>>> line1 = LineString([(0, 0), (100, 0)])
>>> line2 = LineString([(0, 0), (50, 50), (100, 0)])
>>> shapely.frechet_distance(line1, line2)
70.71067811865476
>>> shapely.frechet_distance(line1, line2, densify=0.5)
50.0
>>> shapely.frechet_distance(line1, LineString())
nan
>>> shapely.frechet_distance(line1, None)
nan
"""
if densify is None:
return lib.frechet_distance(a, b, **kwargs)
return lib.frechet_distance_densify(a, b, densify, **kwargs)
@multithreading_enabled
def minimum_clearance(geometry, **kwargs):
"""Compute the Minimum Clearance distance.
A geometry's "minimum clearance" is the smallest distance by which
a vertex of the geometry could be moved to produce an invalid geometry.
If no minimum clearance exists for a geometry (for example, a single
point, or an empty geometry), infinity is returned.
Parameters
----------
geometry : Geometry or array_like
Geometry or geometries for which to compute the minimum clearance.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import Polygon
>>> polygon = Polygon([(0, 0), (0, 10), (5, 6), (10, 10), (10, 0), (5, 4), (0, 0)])
>>> shapely.minimum_clearance(polygon)
2.0
>>> shapely.minimum_clearance(Polygon())
inf
>>> shapely.minimum_clearance(None)
nan
See Also
--------
minimum_clearance_line
"""
return lib.minimum_clearance(geometry, **kwargs)
@multithreading_enabled
def minimum_bounding_radius(geometry, **kwargs):
"""Compute the radius of the minimum bounding circle of an input geometry.
Parameters
----------
geometry : Geometry or array_like
Geometry or geometries for which to compute the minimum bounding radius.
**kwargs
See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
Examples
--------
>>> import shapely
>>> from shapely import GeometryCollection, LineString, MultiPoint, Point, Polygon
>>> shapely.minimum_bounding_radius(
... Polygon([(0, 5), (5, 10), (10, 5), (5, 0), (0, 5)])
... )
5.0
>>> shapely.minimum_bounding_radius(LineString([(1, 1), (1, 10)]))
4.5
>>> shapely.minimum_bounding_radius(MultiPoint([(2, 2), (4, 2)]))
1.0
>>> shapely.minimum_bounding_radius(Point(0, 1))
0.0
>>> shapely.minimum_bounding_radius(GeometryCollection())
0.0
See Also
--------
minimum_bounding_circle
"""
return lib.minimum_bounding_radius(geometry, **kwargs)
|