shapely 2.2.0__cp315-cp315-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. shapely/__init__.py +66 -0
  2. shapely/_coverage.py +275 -0
  3. shapely/_enum.py +32 -0
  4. shapely/_geometry.py +1121 -0
  5. shapely/_geometry_helpers.cp315-win32.lib +0 -0
  6. shapely/_geometry_helpers.cp315-win32.pyd +0 -0
  7. shapely/_geos.cp315-win32.lib +0 -0
  8. shapely/_geos.cp315-win32.pyd +0 -0
  9. shapely/_geos.pxd +56 -0
  10. shapely/_pygeos_api.pxd +60 -0
  11. shapely/_ragged_array.py +475 -0
  12. shapely/_version.py +4 -0
  13. shapely/affinity.py +266 -0
  14. shapely/algorithms/__init__.py +1 -0
  15. shapely/algorithms/_oriented_envelope.py +56 -0
  16. shapely/algorithms/cga.py +50 -0
  17. shapely/algorithms/polylabel.py +43 -0
  18. shapely/conftest.py +70 -0
  19. shapely/constructive.py +1614 -0
  20. shapely/coordinates.py +471 -0
  21. shapely/coords.py +119 -0
  22. shapely/creation.py +854 -0
  23. shapely/decorators.py +194 -0
  24. shapely/errors.py +79 -0
  25. shapely/geometry/__init__.py +27 -0
  26. shapely/geometry/base.py +1252 -0
  27. shapely/geometry/collection.py +63 -0
  28. shapely/geometry/geo.py +154 -0
  29. shapely/geometry/linestring.py +216 -0
  30. shapely/geometry/multilinestring.py +100 -0
  31. shapely/geometry/multipoint.py +109 -0
  32. shapely/geometry/multipolygon.py +135 -0
  33. shapely/geometry/point.py +171 -0
  34. shapely/geometry/polygon.py +364 -0
  35. shapely/geos.py +17 -0
  36. shapely/io.py +413 -0
  37. shapely/lib.cp315-win32.lib +0 -0
  38. shapely/lib.cp315-win32.pyd +0 -0
  39. shapely/linear.py +233 -0
  40. shapely/measurement.py +350 -0
  41. shapely/ops.py +717 -0
  42. shapely/plotting.py +222 -0
  43. shapely/predicates.py +1391 -0
  44. shapely/prepared.py +74 -0
  45. shapely/set_operations.py +775 -0
  46. shapely/speedups.py +39 -0
  47. shapely/strtree.py +542 -0
  48. shapely/testing.py +208 -0
  49. shapely/tests/__init__.py +0 -0
  50. shapely/tests/common.py +283 -0
  51. shapely/tests/geometry/__init__.py +0 -0
  52. shapely/tests/geometry/test_collection.py +90 -0
  53. shapely/tests/geometry/test_coords.py +127 -0
  54. shapely/tests/geometry/test_decimal.py +117 -0
  55. shapely/tests/geometry/test_emptiness.py +98 -0
  56. shapely/tests/geometry/test_equality.py +273 -0
  57. shapely/tests/geometry/test_format.py +98 -0
  58. shapely/tests/geometry/test_geometry_base.py +431 -0
  59. shapely/tests/geometry/test_hash.py +28 -0
  60. shapely/tests/geometry/test_linestring.py +232 -0
  61. shapely/tests/geometry/test_multi.py +11 -0
  62. shapely/tests/geometry/test_multilinestring.py +77 -0
  63. shapely/tests/geometry/test_multipoint.py +97 -0
  64. shapely/tests/geometry/test_multipolygon.py +189 -0
  65. shapely/tests/geometry/test_point.py +201 -0
  66. shapely/tests/geometry/test_polygon.py +467 -0
  67. shapely/tests/legacy/__init__.py +0 -0
  68. shapely/tests/legacy/conftest.py +12 -0
  69. shapely/tests/legacy/data/emptypoint_1.8.5.post1.pickle +0 -0
  70. shapely/tests/legacy/data/emptypolygon_1.8.5.post1.pickle +0 -0
  71. shapely/tests/legacy/data/geometrycollection_1.8.5.post1.pickle +0 -0
  72. shapely/tests/legacy/data/linearring_1.8.5.post1.pickle +0 -0
  73. shapely/tests/legacy/data/linestring_1.8.5.post1.pickle +0 -0
  74. shapely/tests/legacy/data/multilinestring_1.8.5.post1.pickle +0 -0
  75. shapely/tests/legacy/data/multipoint_1.8.5.post1.pickle +0 -0
  76. shapely/tests/legacy/data/multipolygon_1.8.5.post1.pickle +0 -0
  77. shapely/tests/legacy/data/point2d_1.8.5.post1.pickle +0 -0
  78. shapely/tests/legacy/data/point3d_1.8.5.post1.pickle +0 -0
  79. shapely/tests/legacy/data/polygon_1.8.5.post1.pickle +0 -0
  80. shapely/tests/legacy/test_affinity.py +308 -0
  81. shapely/tests/legacy/test_box.py +20 -0
  82. shapely/tests/legacy/test_buffer.py +173 -0
  83. shapely/tests/legacy/test_cga.py +51 -0
  84. shapely/tests/legacy/test_clip_by_rect.py +119 -0
  85. shapely/tests/legacy/test_create_inconsistent_dimensionality.py +217 -0
  86. shapely/tests/legacy/test_delaunay.py +32 -0
  87. shapely/tests/legacy/test_empty_polygons.py +22 -0
  88. shapely/tests/legacy/test_equality.py +24 -0
  89. shapely/tests/legacy/test_geointerface.py +118 -0
  90. shapely/tests/legacy/test_invalid_geometries.py +26 -0
  91. shapely/tests/legacy/test_linear_referencing.py +72 -0
  92. shapely/tests/legacy/test_linemerge.py +43 -0
  93. shapely/tests/legacy/test_locale.py +52 -0
  94. shapely/tests/legacy/test_make_valid.py +15 -0
  95. shapely/tests/legacy/test_mapping.py +14 -0
  96. shapely/tests/legacy/test_minimum_clearance.py +30 -0
  97. shapely/tests/legacy/test_ndarrays.py +42 -0
  98. shapely/tests/legacy/test_nearest.py +18 -0
  99. shapely/tests/legacy/test_operations.py +124 -0
  100. shapely/tests/legacy/test_operators.py +60 -0
  101. shapely/tests/legacy/test_orient.py +92 -0
  102. shapely/tests/legacy/test_parallel_offset.py +60 -0
  103. shapely/tests/legacy/test_persist.py +48 -0
  104. shapely/tests/legacy/test_pickle.py +81 -0
  105. shapely/tests/legacy/test_polygonize.py +43 -0
  106. shapely/tests/legacy/test_polylabel.py +85 -0
  107. shapely/tests/legacy/test_predicates.py +95 -0
  108. shapely/tests/legacy/test_prepared.py +65 -0
  109. shapely/tests/legacy/test_products_z.py +13 -0
  110. shapely/tests/legacy/test_shape.py +63 -0
  111. shapely/tests/legacy/test_shared_paths.py +45 -0
  112. shapely/tests/legacy/test_singularity.py +15 -0
  113. shapely/tests/legacy/test_snap.py +24 -0
  114. shapely/tests/legacy/test_split.py +331 -0
  115. shapely/tests/legacy/test_substring.py +220 -0
  116. shapely/tests/legacy/test_svg.py +213 -0
  117. shapely/tests/legacy/test_transform.py +115 -0
  118. shapely/tests/legacy/test_union.py +67 -0
  119. shapely/tests/legacy/test_validation.py +9 -0
  120. shapely/tests/legacy/test_vectorized.py +108 -0
  121. shapely/tests/legacy/test_voronoi_diagram.py +129 -0
  122. shapely/tests/legacy/test_wkb.py +171 -0
  123. shapely/tests/legacy/test_wkt.py +61 -0
  124. shapely/tests/legacy/threading_test.py +44 -0
  125. shapely/tests/test_constructive.py +1869 -0
  126. shapely/tests/test_coordinates.py +526 -0
  127. shapely/tests/test_coverage.py +520 -0
  128. shapely/tests/test_creation.py +771 -0
  129. shapely/tests/test_creation_indices.py +585 -0
  130. shapely/tests/test_decorators.py +138 -0
  131. shapely/tests/test_geometry.py +845 -0
  132. shapely/tests/test_io.py +1347 -0
  133. shapely/tests/test_linear.py +255 -0
  134. shapely/tests/test_measurement.py +368 -0
  135. shapely/tests/test_misc.py +228 -0
  136. shapely/tests/test_plotting.py +121 -0
  137. shapely/tests/test_predicates.py +490 -0
  138. shapely/tests/test_ragged_array.py +667 -0
  139. shapely/tests/test_set_operations.py +503 -0
  140. shapely/tests/test_strtree.py +1950 -0
  141. shapely/tests/test_testing.py +97 -0
  142. shapely/validation.py +55 -0
  143. shapely/vectorized/__init__.py +91 -0
  144. shapely/wkb.py +70 -0
  145. shapely/wkt.py +82 -0
  146. shapely-2.2.0.dist-info/DELVEWHEEL +2 -0
  147. shapely-2.2.0.dist-info/METADATA +202 -0
  148. shapely-2.2.0.dist-info/RECORD +155 -0
  149. shapely-2.2.0.dist-info/WHEEL +4 -0
  150. shapely-2.2.0.dist-info/licenses/LICENSE.txt +29 -0
  151. shapely-2.2.0.dist-info/licenses/LICENSE_GEOS +509 -0
  152. shapely-2.2.0.dist-info/licenses/LICENSE_win32 +26 -0
  153. shapely.libs/geos-6d6d3919885591ad1eac9682b7b8d738.dll +0 -0
  154. shapely.libs/geos_c-4fcefa54f15198a9b38b0234c4d0895f.dll +0 -0
  155. shapely.libs/msvcp140-c0961c272927a1760ae4de73a7d56d5e.dll +0 -0
shapely/_geometry.py ADDED
@@ -0,0 +1,1121 @@
1
+ from enum import IntEnum
2
+
3
+ import numpy as np
4
+
5
+ from shapely import _geometry_helpers, lib
6
+ from shapely._enum import ParamEnum
7
+ from shapely.decorators import (
8
+ deprecate_positional,
9
+ requires_geos,
10
+ )
11
+
12
+ __all__ = [
13
+ "GeometryType",
14
+ "force_2d",
15
+ "force_3d",
16
+ "get_coordinate_dimension",
17
+ "get_dimensions",
18
+ "get_exterior_ring",
19
+ "get_geometry",
20
+ "get_interior_ring",
21
+ "get_m",
22
+ "get_num_coordinates",
23
+ "get_num_geometries",
24
+ "get_num_interior_rings",
25
+ "get_num_points",
26
+ "get_parts",
27
+ "get_point",
28
+ "get_precision",
29
+ "get_rings",
30
+ "get_segments",
31
+ "get_srid",
32
+ "get_type_id",
33
+ "get_x",
34
+ "get_y",
35
+ "get_z",
36
+ "set_precision",
37
+ "set_srid",
38
+ ]
39
+
40
+
41
+ class GeometryType(IntEnum):
42
+ """The enumeration of GEOS geometry types."""
43
+
44
+ MISSING = -1
45
+ POINT = 0
46
+ LINESTRING = 1
47
+ LINEARRING = 2
48
+ POLYGON = 3
49
+ MULTIPOINT = 4
50
+ MULTILINESTRING = 5
51
+ MULTIPOLYGON = 6
52
+ GEOMETRYCOLLECTION = 7
53
+
54
+
55
+ # generic
56
+
57
+
58
+ def get_type_id(geometry, **kwargs):
59
+ """Return the type ID of a geometry.
60
+
61
+ Possible values are:
62
+
63
+ - None (missing) is -1
64
+ - POINT is 0
65
+ - LINESTRING is 1
66
+ - LINEARRING is 2
67
+ - POLYGON is 3
68
+ - MULTIPOINT is 4
69
+ - MULTILINESTRING is 5
70
+ - MULTIPOLYGON is 6
71
+ - GEOMETRYCOLLECTION is 7
72
+
73
+ Parameters
74
+ ----------
75
+ geometry : Geometry or array_like
76
+ Geometry or geometries to get the type ID of.
77
+ **kwargs
78
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
79
+
80
+ See Also
81
+ --------
82
+ GeometryType
83
+
84
+ Examples
85
+ --------
86
+ >>> import shapely
87
+ >>> from shapely import LineString, Point
88
+ >>> shapely.get_type_id(LineString([(0, 0), (1, 1), (2, 2), (3, 3)]))
89
+ 1
90
+ >>> shapely.get_type_id([Point(1, 2), Point(2, 3)]).tolist()
91
+ [0, 0]
92
+
93
+ """
94
+ return lib.get_type_id(geometry, **kwargs)
95
+
96
+
97
+ def get_dimensions(geometry, **kwargs):
98
+ """Return the inherent dimensionality of a geometry.
99
+
100
+ The inherent dimension is 0 for points, 1 for linestrings and linearrings,
101
+ and 2 for polygons. For geometrycollections it is the max of the containing
102
+ elements. Empty collections and None values return -1.
103
+
104
+ Parameters
105
+ ----------
106
+ geometry : Geometry or array_like
107
+ Geometry or geometries to get the dimensionality of.
108
+ **kwargs
109
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
110
+
111
+ Examples
112
+ --------
113
+ >>> import shapely
114
+ >>> from shapely import GeometryCollection, Point, Polygon
115
+ >>> point = Point(0, 0)
116
+ >>> shapely.get_dimensions(point)
117
+ 0
118
+ >>> polygon = Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])
119
+ >>> shapely.get_dimensions(polygon)
120
+ 2
121
+ >>> shapely.get_dimensions(GeometryCollection([point, polygon]))
122
+ 2
123
+ >>> shapely.get_dimensions(GeometryCollection([]))
124
+ -1
125
+ >>> shapely.get_dimensions(None)
126
+ -1
127
+
128
+ """
129
+ return lib.get_dimensions(geometry, **kwargs)
130
+
131
+
132
+ def get_coordinate_dimension(geometry, **kwargs):
133
+ """Return the dimensionality of the coordinates in a geometry (2, 3 or 4).
134
+
135
+ The return value can be one of the following:
136
+
137
+ * Return 2 for geometries with XY coordinate types,
138
+ * Return 3 for XYZ or XYM coordinate types
139
+ (distinguished by :meth:`has_z` or :meth:`has_m`),
140
+ * Return 4 for XYZM coordinate types,
141
+ * Return -1 for missing geometries (``None`` values).
142
+
143
+ Note that with GEOS < 3.12, if the first Z coordinate equals ``nan``, this function
144
+ will return ``2``. Geometries with M coordinates are supported with GEOS >= 3.12.
145
+
146
+ Parameters
147
+ ----------
148
+ geometry : Geometry or array_like
149
+ Geometry or geometries to get the coordinate dimension of.
150
+ **kwargs
151
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
152
+
153
+ Examples
154
+ --------
155
+ >>> import shapely
156
+ >>> from shapely import Point
157
+ >>> shapely.get_coordinate_dimension(Point(0, 0))
158
+ 2
159
+ >>> shapely.get_coordinate_dimension(Point(0, 0, 1))
160
+ 3
161
+ >>> shapely.get_coordinate_dimension(None)
162
+ -1
163
+
164
+ """
165
+ return lib.get_coordinate_dimension(geometry, **kwargs)
166
+
167
+
168
+ def get_num_coordinates(geometry, **kwargs):
169
+ """Return the total number of coordinates in a geometry.
170
+
171
+ Returns 0 for not-a-geometry values.
172
+
173
+ Parameters
174
+ ----------
175
+ geometry : Geometry or array_like
176
+ Geometry or geometries to get the number of coordinates of.
177
+ **kwargs
178
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
179
+
180
+ Examples
181
+ --------
182
+ >>> import shapely
183
+ >>> from shapely import GeometryCollection, LineString, Point
184
+ >>> point = Point(0, 0)
185
+ >>> shapely.get_num_coordinates(point)
186
+ 1
187
+ >>> shapely.get_num_coordinates(Point(0, 0, 0))
188
+ 1
189
+ >>> line = LineString([(0, 0), (1, 1)])
190
+ >>> shapely.get_num_coordinates(line)
191
+ 2
192
+ >>> shapely.get_num_coordinates(GeometryCollection([point, line]))
193
+ 3
194
+ >>> shapely.get_num_coordinates(None)
195
+ 0
196
+
197
+ """
198
+ return lib.get_num_coordinates(geometry, **kwargs)
199
+
200
+
201
+ def get_srid(geometry, **kwargs):
202
+ """Return the SRID of a geometry.
203
+
204
+ Returns -1 for not-a-geometry values.
205
+
206
+ Parameters
207
+ ----------
208
+ geometry : Geometry or array_like
209
+ Geometry or geometries to get the SRID of.
210
+ **kwargs
211
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
212
+
213
+ See Also
214
+ --------
215
+ set_srid
216
+
217
+ Examples
218
+ --------
219
+ >>> import shapely
220
+ >>> from shapely import Point
221
+ >>> point = Point(0, 0)
222
+ >>> shapely.get_srid(point)
223
+ 0
224
+ >>> with_srid = shapely.set_srid(point, 4326)
225
+ >>> shapely.get_srid(with_srid)
226
+ 4326
227
+
228
+ """
229
+ return lib.get_srid(geometry, **kwargs)
230
+
231
+
232
+ def set_srid(geometry, srid, **kwargs):
233
+ """Return a geometry with its SRID set.
234
+
235
+ Parameters
236
+ ----------
237
+ geometry : Geometry or array_like
238
+ Geometry or geometries to set the SRID of.
239
+ srid : int
240
+ The SRID to set on the geometry.
241
+ **kwargs
242
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
243
+
244
+ See Also
245
+ --------
246
+ get_srid
247
+
248
+ Examples
249
+ --------
250
+ >>> import shapely
251
+ >>> from shapely import Point
252
+ >>> point = Point(0, 0)
253
+ >>> shapely.get_srid(point)
254
+ 0
255
+ >>> with_srid = shapely.set_srid(point, 4326)
256
+ >>> shapely.get_srid(with_srid)
257
+ 4326
258
+
259
+ """
260
+ return lib.set_srid(geometry, np.intc(srid), **kwargs)
261
+
262
+
263
+ # points
264
+
265
+
266
+ def get_x(point, **kwargs):
267
+ """Return the x-coordinate of a point.
268
+
269
+ Parameters
270
+ ----------
271
+ point : Geometry or array_like
272
+ Non-point geometries will result in NaN being returned.
273
+ **kwargs
274
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
275
+
276
+ See Also
277
+ --------
278
+ get_y, get_z, get_m
279
+
280
+ Examples
281
+ --------
282
+ >>> import shapely
283
+ >>> from shapely import MultiPoint, Point
284
+ >>> shapely.get_x(Point(1, 2))
285
+ 1.0
286
+ >>> shapely.get_x(MultiPoint([(1, 1), (1, 2)]))
287
+ nan
288
+
289
+ """
290
+ return lib.get_x(point, **kwargs)
291
+
292
+
293
+ def get_y(point, **kwargs):
294
+ """Return the y-coordinate of a point.
295
+
296
+ Parameters
297
+ ----------
298
+ point : Geometry or array_like
299
+ Non-point geometries will result in NaN being returned.
300
+ **kwargs
301
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
302
+
303
+ See Also
304
+ --------
305
+ get_x, get_z, get_m
306
+
307
+ Examples
308
+ --------
309
+ >>> import shapely
310
+ >>> from shapely import MultiPoint, Point
311
+ >>> shapely.get_y(Point(1, 2))
312
+ 2.0
313
+ >>> shapely.get_y(MultiPoint([(1, 1), (1, 2)]))
314
+ nan
315
+
316
+ """
317
+ return lib.get_y(point, **kwargs)
318
+
319
+
320
+ def get_z(point, **kwargs):
321
+ """Return the z-coordinate of a point.
322
+
323
+ Parameters
324
+ ----------
325
+ point : Geometry or array_like
326
+ Non-point geometries or geometries without Z dimension will result
327
+ in NaN being returned.
328
+ **kwargs
329
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
330
+
331
+ See Also
332
+ --------
333
+ get_x, get_y, get_m
334
+
335
+ Examples
336
+ --------
337
+ >>> import shapely
338
+ >>> from shapely import MultiPoint, Point
339
+ >>> shapely.get_z(Point(1, 2, 3))
340
+ 3.0
341
+ >>> shapely.get_z(Point(1, 2))
342
+ nan
343
+ >>> shapely.get_z(MultiPoint([(1, 1, 1), (2, 2, 2)]))
344
+ nan
345
+
346
+ """
347
+ return lib.get_z(point, **kwargs)
348
+
349
+
350
+ @requires_geos("3.12.0")
351
+ def get_m(point, **kwargs):
352
+ """Return the m-coordinate of a point.
353
+
354
+ .. versionadded:: 2.1.0
355
+
356
+ Parameters
357
+ ----------
358
+ point : Geometry or array_like
359
+ Non-point geometries or geometries without M dimension will result
360
+ in NaN being returned.
361
+ **kwargs
362
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
363
+
364
+ See Also
365
+ --------
366
+ get_x, get_y, get_z
367
+
368
+ Examples
369
+ --------
370
+ >>> import shapely
371
+ >>> from shapely import Point, from_wkt
372
+ >>> shapely.get_m(from_wkt("POINT ZM (1 2 3 4)"))
373
+ 4.0
374
+ >>> shapely.get_m(from_wkt("POINT M (1 2 4)"))
375
+ 4.0
376
+ >>> shapely.get_m(Point(1, 2, 3))
377
+ nan
378
+ >>> shapely.get_m(from_wkt("MULTIPOINT M ((1 1 1), (2 2 2))"))
379
+ nan
380
+
381
+ """
382
+ return lib.get_m(point, **kwargs)
383
+
384
+
385
+ # linestrings
386
+
387
+
388
+ def get_point(geometry, index, **kwargs):
389
+ """Return the nth point of a linestring or linearring.
390
+
391
+ Parameters
392
+ ----------
393
+ geometry : Geometry or array_like
394
+ Geometry or geometries to get the point of.
395
+ index : int or array_like
396
+ Negative values count from the end of the linestring backwards.
397
+ **kwargs
398
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
399
+
400
+ See Also
401
+ --------
402
+ get_num_points
403
+
404
+ Examples
405
+ --------
406
+ >>> import shapely
407
+ >>> from shapely import LinearRing, LineString, MultiPoint, Point
408
+ >>> line = LineString([(0, 0), (1, 1), (2, 2), (3, 3)])
409
+ >>> shapely.get_point(line, 1)
410
+ <POINT (1 1)>
411
+ >>> shapely.get_point(line, -2)
412
+ <POINT (2 2)>
413
+ >>> shapely.get_point(line, [0, 3]).tolist()
414
+ [<POINT (0 0)>, <POINT (3 3)>]
415
+
416
+ The function works the same for LinearRing input:
417
+
418
+ >>> shapely.get_point(LinearRing([(0, 0), (1, 1), (2, 2), (0, 0)]), 1)
419
+ <POINT (1 1)>
420
+
421
+ For non-linear geometries it returns None:
422
+
423
+ >>> shapely.get_point(MultiPoint([(0, 0), (1, 1), (2, 2), (3, 3)]), 1) is None
424
+ True
425
+ >>> shapely.get_point(Point(1, 1), 0) is None
426
+ True
427
+
428
+ """
429
+ return lib.get_point(geometry, np.intc(index), **kwargs)
430
+
431
+
432
+ def get_num_points(geometry, **kwargs):
433
+ """Return the number of points in a linestring or linearring.
434
+
435
+ Returns 0 for not-a-geometry values. The number of points in geometries
436
+ other than linestring or linearring equals zero.
437
+
438
+ Parameters
439
+ ----------
440
+ geometry : Geometry or array_like
441
+ Geometry or geometries to get the number of points of.
442
+ **kwargs
443
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
444
+
445
+ See Also
446
+ --------
447
+ get_point
448
+ get_num_geometries
449
+
450
+ Examples
451
+ --------
452
+ >>> import shapely
453
+ >>> from shapely import LineString, MultiPoint
454
+ >>> shapely.get_num_points(LineString([(0, 0), (1, 1), (2, 2), (3, 3)]))
455
+ 4
456
+ >>> shapely.get_num_points(MultiPoint([(0, 0), (1, 1), (2, 2), (3, 3)]))
457
+ 0
458
+ >>> shapely.get_num_points(None)
459
+ 0
460
+
461
+ """
462
+ return lib.get_num_points(geometry, **kwargs)
463
+
464
+
465
+ # polygons
466
+
467
+
468
+ def get_exterior_ring(geometry, **kwargs):
469
+ """Return the exterior ring of a polygon.
470
+
471
+ Parameters
472
+ ----------
473
+ geometry : Geometry or array_like
474
+ Geometry or geometries to get the exterior ring of.
475
+ **kwargs
476
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
477
+
478
+ See Also
479
+ --------
480
+ get_interior_ring
481
+
482
+ Examples
483
+ --------
484
+ >>> import shapely
485
+ >>> from shapely import Point, Polygon
486
+ >>> shapely.get_exterior_ring(Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]))
487
+ <LINEARRING (0 0, 0 10, 10 10, 10 0, 0 0)>
488
+ >>> shapely.get_exterior_ring(Point(1, 1)) is None
489
+ True
490
+
491
+ """
492
+ return lib.get_exterior_ring(geometry, **kwargs)
493
+
494
+
495
+ def get_interior_ring(geometry, index, **kwargs):
496
+ """Return the nth interior ring of a polygon.
497
+
498
+ The number of interior rings in non-polygons equals zero.
499
+
500
+ Parameters
501
+ ----------
502
+ geometry : Geometry or array_like
503
+ Geometry or geometries to get the interior ring of.
504
+ index : int or array_like
505
+ Negative values count from the end of the interior rings backwards.
506
+ **kwargs
507
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
508
+
509
+ See Also
510
+ --------
511
+ get_exterior_ring
512
+ get_num_interior_rings
513
+
514
+ Examples
515
+ --------
516
+ >>> import shapely
517
+ >>> from shapely import Point, Polygon
518
+ >>> polygon_with_hole = Polygon(
519
+ ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
520
+ ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
521
+ ... )
522
+ >>> shapely.get_interior_ring(polygon_with_hole, 0)
523
+ <LINEARRING (2 2, 2 4, 4 4, 4 2, 2 2)>
524
+ >>> shapely.get_interior_ring(polygon_with_hole, 1) is None
525
+ True
526
+ >>> polygon = Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])
527
+ >>> shapely.get_interior_ring(polygon, 0) is None
528
+ True
529
+ >>> shapely.get_interior_ring(Point(0, 0), 0) is None
530
+ True
531
+
532
+ """
533
+ return lib.get_interior_ring(geometry, np.intc(index), **kwargs)
534
+
535
+
536
+ def get_num_interior_rings(geometry, **kwargs):
537
+ """Return number of internal rings in a polygon.
538
+
539
+ Returns 0 for not-a-geometry values.
540
+
541
+ Parameters
542
+ ----------
543
+ geometry : Geometry or array_like
544
+ Geometry or geometries to get the number of interior rings of.
545
+ **kwargs
546
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
547
+
548
+ See Also
549
+ --------
550
+ get_exterior_ring
551
+ get_interior_ring
552
+
553
+ Examples
554
+ --------
555
+ >>> import shapely
556
+ >>> from shapely import Point, Polygon
557
+ >>> polygon = Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])
558
+ >>> shapely.get_num_interior_rings(polygon)
559
+ 0
560
+ >>> polygon_with_hole = Polygon(
561
+ ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
562
+ ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
563
+ ... )
564
+ >>> shapely.get_num_interior_rings(polygon_with_hole)
565
+ 1
566
+ >>> shapely.get_num_interior_rings(Point(0, 0))
567
+ 0
568
+ >>> shapely.get_num_interior_rings(None)
569
+ 0
570
+
571
+ """
572
+ return lib.get_num_interior_rings(geometry, **kwargs)
573
+
574
+
575
+ # collections
576
+
577
+
578
+ def get_geometry(geometry, index, **kwargs):
579
+ """Return the nth geometry from a collection of geometries.
580
+
581
+ Parameters
582
+ ----------
583
+ geometry : Geometry or array_like
584
+ Geometry or geometries to get the nth geometry of.
585
+ index : int or array_like
586
+ Negative values count from the end of the collection backwards.
587
+ **kwargs
588
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
589
+
590
+ Notes
591
+ -----
592
+ - simple geometries act as length-1 collections
593
+ - out-of-range values return None
594
+
595
+ See Also
596
+ --------
597
+ get_num_geometries, get_parts
598
+
599
+ Examples
600
+ --------
601
+ >>> import shapely
602
+ >>> from shapely import Point, MultiPoint
603
+ >>> multipoint = MultiPoint([(0, 0), (1, 1), (2, 2), (3, 3)])
604
+ >>> shapely.get_geometry(multipoint, 1)
605
+ <POINT (1 1)>
606
+ >>> shapely.get_geometry(multipoint, -1)
607
+ <POINT (3 3)>
608
+ >>> shapely.get_geometry(multipoint, 5) is None
609
+ True
610
+ >>> shapely.get_geometry(Point(1, 1), 0)
611
+ <POINT (1 1)>
612
+ >>> shapely.get_geometry(Point(1, 1), 1) is None
613
+ True
614
+
615
+ """
616
+ return lib.get_geometry(geometry, np.intc(index), **kwargs)
617
+
618
+
619
+ # Note: future plan is to change this signature over a few releases:
620
+ # shapely 2.0:
621
+ # get_parts(geometry, return_index=False)
622
+ # shapely 2.1: shows deprecation warning about positional 'return_index'
623
+ # same signature as 2.0
624
+ # shapely 2.2(?): enforce keyword-only arguments after 'geometry'
625
+ # get_parts(geometry, *, return_index=False)
626
+
627
+
628
+ @deprecate_positional(["return_index"])
629
+ def get_parts(geometry, return_index=False):
630
+ """Get parts of each GeometryCollection or Multi* geometry object.
631
+
632
+ A copy of each geometry in the GeometryCollection or Multi* geometry object
633
+ is returned.
634
+
635
+ Note: This does not return the individual parts of Multi* geometry objects
636
+ in a GeometryCollection. You may need to call this function multiple times
637
+ to return individual parts of Multi* geometry objects in a
638
+ GeometryCollection.
639
+
640
+ Parameters
641
+ ----------
642
+ geometry : Geometry or array_like
643
+ Geometry or geometries to get the parts of.
644
+ return_index : bool, default False
645
+ If True, will return a tuple of ndarrays of (parts, indexes), where
646
+ indexes are the indexes of the original geometries in the source array.
647
+
648
+ Notes
649
+ -----
650
+
651
+ .. deprecated:: 2.1.0
652
+ A deprecation warning is shown if ``return_index`` is specified as
653
+ a positional argument. This will need to be specified as a keyword
654
+ argument in a future release.
655
+
656
+ Returns
657
+ -------
658
+ ndarray of parts or tuple of (parts, indexes)
659
+
660
+ See Also
661
+ --------
662
+ get_geometry, get_rings
663
+
664
+ Examples
665
+ --------
666
+ >>> import shapely
667
+ >>> from shapely import MultiPoint
668
+ >>> shapely.get_parts(MultiPoint([(0, 1), (2, 3)])).tolist()
669
+ [<POINT (0 1)>, <POINT (2 3)>]
670
+ >>> parts, index = shapely.get_parts([MultiPoint([(0, 1)]), \
671
+ MultiPoint([(4, 5), (6, 7)])], return_index=True)
672
+ >>> parts.tolist()
673
+ [<POINT (0 1)>, <POINT (4 5)>, <POINT (6 7)>]
674
+ >>> index.tolist()
675
+ [0, 1, 1]
676
+
677
+ """
678
+ geometry = np.asarray(geometry, dtype=np.object_)
679
+ geometry = np.atleast_1d(geometry)
680
+
681
+ if geometry.ndim != 1:
682
+ raise ValueError("Array should be one dimensional")
683
+
684
+ if return_index:
685
+ return _geometry_helpers.get_parts(geometry)
686
+
687
+ return _geometry_helpers.get_parts(geometry)[0]
688
+
689
+
690
+ # Note: future plan is to change this signature over a few releases:
691
+ # shapely 2.0:
692
+ # get_rings(geometry, return_index=False)
693
+ # shapely 2.1: shows deprecation warning about positional 'return_index'
694
+ # same signature as 2.0
695
+ # shapely 2.2(?): enforce keyword-only arguments after 'geometry'
696
+ # get_rings(geometry, *, return_index=False)
697
+
698
+
699
+ @deprecate_positional(["return_index"])
700
+ def get_rings(geometry, return_index=False):
701
+ """Get rings of Polygon geometry object.
702
+
703
+ For each Polygon, the first returned ring is always the exterior ring
704
+ and potential subsequent rings are interior rings.
705
+
706
+ If the geometry is not a Polygon, nothing is returned (empty array for
707
+ scalar geometry input or no element in output array for array input).
708
+
709
+ Parameters
710
+ ----------
711
+ geometry : Geometry or array_like
712
+ Geometry or geometries to get the rings of.
713
+ return_index : bool, default False
714
+ If True, will return a tuple of ndarrays of (rings, indexes), where
715
+ indexes are the indexes of the original geometries in the source array.
716
+
717
+ Notes
718
+ -----
719
+
720
+ .. deprecated:: 2.1.0
721
+ A deprecation warning is shown if ``return_index`` is specified as
722
+ a positional argument. This will need to be specified as a keyword
723
+ argument in a future release.
724
+
725
+ Returns
726
+ -------
727
+ ndarray of rings or tuple of (rings, indexes)
728
+
729
+ See Also
730
+ --------
731
+ get_exterior_ring, get_interior_ring, get_parts
732
+
733
+ Examples
734
+ --------
735
+ >>> import shapely
736
+ >>> from shapely import Polygon
737
+ >>> polygon_with_hole = Polygon(
738
+ ... [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)],
739
+ ... holes=[[(2, 2), (2, 4), (4, 4), (4, 2), (2, 2)]]
740
+ ... )
741
+ >>> shapely.get_rings(polygon_with_hole).tolist()
742
+ [<LINEARRING (0 0, 0 10, 10 10, 10 0, 0 0)>,
743
+ <LINEARRING (2 2, 2 4, 4 4, 4 2, 2 2)>]
744
+
745
+ With ``return_index=True``:
746
+
747
+ >>> polygon = Polygon([(0, 0), (2, 0), (2, 2), (0, 2), (0, 0)])
748
+ >>> rings, index = shapely.get_rings(
749
+ ... [polygon, polygon_with_hole],
750
+ ... return_index=True
751
+ ... )
752
+ >>> rings.tolist()
753
+ [<LINEARRING (0 0, 2 0, 2 2, 0 2, 0 0)>,
754
+ <LINEARRING (0 0, 0 10, 10 10, 10 0, 0 0)>,
755
+ <LINEARRING (2 2, 2 4, 4 4, 4 2, 2 2)>]
756
+ >>> index.tolist()
757
+ [0, 1, 1]
758
+
759
+ """
760
+ geometry = np.asarray(geometry, dtype=np.object_)
761
+ geometry = np.atleast_1d(geometry)
762
+
763
+ if geometry.ndim != 1:
764
+ raise ValueError("Array should be one dimensional")
765
+
766
+ if return_index:
767
+ return _geometry_helpers.get_parts(geometry, extract_rings=True)
768
+
769
+ return _geometry_helpers.get_parts(geometry, extract_rings=True)[0]
770
+
771
+
772
+ def get_num_geometries(geometry, **kwargs):
773
+ """Return number of geometries in a collection.
774
+
775
+ Returns 0 for not-a-geometry values. The number of geometries in points,
776
+ linestrings, linearrings and polygons equals one.
777
+
778
+ Parameters
779
+ ----------
780
+ geometry : Geometry or array_like
781
+ Geometry or geometries to get the number of geometries of.
782
+ **kwargs
783
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
784
+
785
+ See Also
786
+ --------
787
+ get_num_points
788
+ get_geometry
789
+
790
+ Examples
791
+ --------
792
+ >>> import shapely
793
+ >>> from shapely import MultiPoint, Point
794
+ >>> shapely.get_num_geometries(MultiPoint([(0, 0), (1, 1), (2, 2), (3, 3)]))
795
+ 4
796
+ >>> shapely.get_num_geometries(Point(1, 1))
797
+ 1
798
+ >>> shapely.get_num_geometries(None)
799
+ 0
800
+
801
+ """
802
+ return lib.get_num_geometries(geometry, **kwargs)
803
+
804
+
805
+ def get_precision(geometry, **kwargs):
806
+ """Get the precision of a geometry.
807
+
808
+ If a precision has not been previously set, it will be 0 (double
809
+ precision). Otherwise, it will return the precision grid size that was
810
+ set on a geometry.
811
+
812
+ Returns NaN for not-a-geometry values.
813
+
814
+ Parameters
815
+ ----------
816
+ geometry : Geometry or array_like
817
+ Geometry or geometries to get the precision of.
818
+ **kwargs
819
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
820
+
821
+ See Also
822
+ --------
823
+ set_precision
824
+
825
+ Examples
826
+ --------
827
+ >>> import shapely
828
+ >>> from shapely import Point
829
+ >>> point = Point(1, 1)
830
+ >>> shapely.get_precision(point)
831
+ 0.0
832
+ >>> geometry = shapely.set_precision(point, 1.0)
833
+ >>> shapely.get_precision(geometry)
834
+ 1.0
835
+ >>> shapely.get_precision(None)
836
+ nan
837
+
838
+ """
839
+ return lib.get_precision(geometry, **kwargs)
840
+
841
+
842
+ class SetPrecisionMode(ParamEnum):
843
+ valid_output = 0
844
+ pointwise = 1
845
+ keep_collapsed = 2
846
+
847
+
848
+ def set_precision(geometry, grid_size, mode="valid_output", **kwargs):
849
+ """Return geometry with the precision set to a precision grid size.
850
+
851
+ By default, geometries use double precision coordinates (grid_size = 0).
852
+
853
+ Coordinates will be rounded if the precision grid specified is less precise
854
+ than the input geometry. Duplicated vertices will be dropped from lines and
855
+ polygons for grid sizes greater than 0. Line and polygon geometries may
856
+ collapse to empty geometries if all vertices are closer together than
857
+ ``grid_size`` or if a polygon becomes significantly narrower than
858
+ ``grid_size``. Spikes or sections in polygons narrower than ``grid_size``
859
+ after rounding the vertices will be removed, which can lead to multipolygons
860
+ or empty geometries. Z values, if present, will not be modified.
861
+
862
+ Notes
863
+ -----
864
+ * subsequent operations will always be performed in the precision of the
865
+ geometry with higher precision (smaller "grid_size"). That same precision
866
+ will be attached to the operation outputs.
867
+ * input geometries should be geometrically valid; unexpected results may
868
+ occur if input geometries are not.
869
+ * the geometry returned will be in
870
+ :ref:`mild canonical form <canonical-form>`, and the order of vertices can
871
+ change and should not be relied upon.
872
+ * returns None if geometry is None.
873
+
874
+ Parameters
875
+ ----------
876
+ geometry : Geometry or array_like
877
+ Geometry or geometries to set the precision of.
878
+ grid_size : float
879
+ Precision grid size. If 0, will use double precision (will not modify
880
+ geometry if precision grid size was not previously set). If this
881
+ value is more precise than input geometry, the input geometry will
882
+ not be modified.
883
+ mode : {'valid_output', 'pointwise', 'keep_collapsed'}, default 'valid_output'
884
+ This parameter determines the way a precision reduction is applied on
885
+ the geometry. There are three modes:
886
+
887
+ 1. `'valid_output'` (default): The output is always valid. Collapsed
888
+ geometry elements (including both polygons and lines) are removed.
889
+ Duplicate vertices are removed.
890
+ 2. `'pointwise'`: Precision reduction is performed pointwise. Output
891
+ geometry may be invalid due to collapse or self-intersection.
892
+ Duplicate vertices are not removed. In GEOS this option is called
893
+ NO_TOPO.
894
+ 3. `'keep_collapsed'`: Like the default mode, except that collapsed
895
+ linear geometry elements are preserved. Collapsed polygonal input
896
+ elements are removed. Duplicate vertices are removed.
897
+ **kwargs
898
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
899
+
900
+ See Also
901
+ --------
902
+ get_precision
903
+
904
+ Examples
905
+ --------
906
+ >>> import shapely
907
+ >>> from shapely import LineString, Point
908
+ >>> shapely.set_precision(Point(0.9, 0.9), 1.0)
909
+ <POINT (1 1)>
910
+ >>> shapely.set_precision(Point(0.9, 0.9, 0.9), 1.0)
911
+ <POINT Z (1 1 0.9)>
912
+ >>> shapely.set_precision(LineString([(0, 0), (0, 0.1), (0, 1), (1, 1)]), 1.0)
913
+ <LINESTRING (0 0, 0 1, 1 1)>
914
+ >>> shapely.set_precision(LineString([(0, 0), (0, 0.1), (0.1, 0.1)]), 1.0, mode="valid_output")
915
+ <LINESTRING EMPTY>
916
+ >>> shapely.set_precision(LineString([(0, 0), (0, 0.1), (0.1, 0.1)]), 1.0, mode="pointwise")
917
+ <LINESTRING (0 0, 0 0, 0 0)>
918
+ >>> shapely.set_precision(LineString([(0, 0), (0, 0.1), (0.1, 0.1)]), 1.0, mode="keep_collapsed")
919
+ <LINESTRING (0 0, 0 0)>
920
+ >>> shapely.set_precision(None, 1.0) is None
921
+ True
922
+
923
+ """ # noqa: E501
924
+ if isinstance(mode, str):
925
+ mode = SetPrecisionMode.get_value(mode)
926
+ elif not np.isscalar(mode):
927
+ raise TypeError("mode only accepts scalar values")
928
+ elif mode not in SetPrecisionMode.values:
929
+ raise ValueError("set_precision function called with illegal mode")
930
+ return lib.set_precision(geometry, grid_size, np.intc(mode), **kwargs)
931
+
932
+
933
+ def force_2d(geometry, **kwargs):
934
+ """Force the dimensionality of a geometry to 2D.
935
+
936
+ Parameters
937
+ ----------
938
+ geometry : Geometry or array_like
939
+ Geometry or geometries to force to 2D.
940
+ **kwargs
941
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
942
+
943
+ Examples
944
+ --------
945
+ >>> import shapely
946
+ >>> from shapely import LineString, Point, Polygon, from_wkt
947
+ >>> shapely.force_2d(Point(0, 0, 1))
948
+ <POINT (0 0)>
949
+ >>> shapely.force_2d(Point(0, 0))
950
+ <POINT (0 0)>
951
+ >>> shapely.force_2d(LineString([(0, 0, 0), (0, 1, 1), (1, 1, 2)]))
952
+ <LINESTRING (0 0, 0 1, 1 1)>
953
+ >>> shapely.force_2d(from_wkt("POLYGON Z EMPTY"))
954
+ <POLYGON EMPTY>
955
+ >>> shapely.force_2d(None) is None
956
+ True
957
+
958
+ """
959
+ return lib.force_2d(geometry, **kwargs)
960
+
961
+
962
+ def force_3d(geometry, z=0.0, **kwargs):
963
+ """Force the dimensionality of a geometry to 3D.
964
+
965
+ 2D geometries will get the provided Z coordinate; Z coordinates of 3D geometries
966
+ are unchanged (unless they are nan).
967
+
968
+ Note that for empty geometries, 3D is only supported for simple geometries
969
+ (non-collections).
970
+
971
+ Parameters
972
+ ----------
973
+ geometry : Geometry or array_like
974
+ Geometry or geometries to force to 3D.
975
+ z : float or array_like, default 0.0
976
+ The Z coordinate value to set on the geometry.
977
+ **kwargs
978
+ See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
979
+
980
+ Examples
981
+ --------
982
+ >>> import shapely
983
+ >>> from shapely import LineString, Point
984
+ >>> shapely.force_3d(Point(0, 0), z=3)
985
+ <POINT Z (0 0 3)>
986
+ >>> shapely.force_3d(Point(0, 0, 0), z=3)
987
+ <POINT Z (0 0 0)>
988
+ >>> shapely.force_3d(LineString([(0, 0), (0, 1), (1, 1)]))
989
+ <LINESTRING Z (0 0 0, 0 1 0, 1 1 0)>
990
+ >>> shapely.force_3d(None) is None
991
+ True
992
+
993
+ """
994
+ if np.isnan(z).any():
995
+ raise ValueError("It is not allowed to set the Z coordinate to NaN.")
996
+ return lib.force_3d(geometry, z, **kwargs)
997
+
998
+
999
+ def get_segments(
1000
+ geometry,
1001
+ *,
1002
+ include_z=False,
1003
+ return_index=False,
1004
+ **kwargs,
1005
+ ):
1006
+ """Get segments of each input linear geometry object.
1007
+
1008
+ Here 'segments' is defined as the individual pairwise coordinates
1009
+ comprising a LineString or LinearRing. Multi* geometry objects are not supported.
1010
+
1011
+ .. versionadded:: 2.2.0
1012
+
1013
+ Parameters
1014
+ ----------
1015
+ geometry : Geometry or array_like
1016
+ A single linear object or collection of linear objects.
1017
+ include_z : bool, default False
1018
+ If True, return LINESTRING Z (3D) geometries.
1019
+ return_index : bool, default False
1020
+ If True, also return the index of each returned geometry as a separate
1021
+ ndarray of integers.
1022
+ **kwargs : dict
1023
+ Keyword arguments to pass into ``shapely.linestrings()``.
1024
+
1025
+ Returns
1026
+ -------
1027
+ ndarray of constituent pairwise segments.
1028
+
1029
+ See Also
1030
+ --------
1031
+ get_parts, get_coordinates, linestrings
1032
+
1033
+ Examples
1034
+ --------
1035
+ >>> from shapely import get_segments
1036
+ >>> from shapely import LineString, LinearRing
1037
+
1038
+ Return the 2 constituent pairwise segments of a 3-coordinate linestring.
1039
+
1040
+ >>> get_segments(LineString(([0, 0], [1, 1], [2, 2]))).tolist()
1041
+ [<LINESTRING (0 0, 1 1)>, <LINESTRING (1 1, 2 2)>]
1042
+
1043
+ Return the 3 constituent pairwise segments of a 4-coordinate linearring.
1044
+
1045
+ >>> get_segments(LinearRing(([0, 0], [1, 1], [2, 2], [0,0]))).tolist()
1046
+ [<LINESTRING (0 0, 1 1)>, <LINESTRING (1 1, 2 2)>, <LINESTRING (2 2, 0 0)>]
1047
+
1048
+ When ``return_index=True``, indexes are returned also:
1049
+
1050
+ >>> segments, index = get_segments(
1051
+ ... [
1052
+ ... LineString(([0, 0], [1, 1], [2, 2])),
1053
+ ... LinearRing(([0, 0], [1, 1], [2, 2], [0,0])),
1054
+ ... ],
1055
+ ... return_index=True,
1056
+ ... )
1057
+ >>> segments.tolist(), index.tolist()
1058
+ ([<LINESTRING (0 0, 1 1)>,
1059
+ <LINESTRING (1 1, 2 2)>,
1060
+ <LINESTRING (0 0, 1 1)>,
1061
+ <LINESTRING (1 1, 2 2)>,
1062
+ <LINESTRING (2 2, 0 0)>],
1063
+ [0, 0, 1, 1, 1])
1064
+
1065
+ By default the third dimension (Z) is ignored.
1066
+
1067
+ >>> get_segments(LineString(([0, 0, 1], [1, 1, 1], [2, 2, 1]))).tolist()
1068
+ [<LINESTRING (0 0, 1 1)>, <LINESTRING (1 1, 2 2)>]
1069
+ >>> get_segments(
1070
+ ... LineString(([0, 0, 1], [1, 1, 1], [2, 2, 1])), include_z=True,
1071
+ ... ).tolist()
1072
+ [<LINESTRING Z (0 0 1, 1 1 1)>, <LINESTRING Z (1 1 1, 2 2 1)>]
1073
+
1074
+ If geometries don't have a Z dimension, these values will be NaN.
1075
+
1076
+ >>> get_segments(LineString(([0, 0], [1, 1], [2, 2])), include_z=True).tolist()
1077
+ [<LINESTRING Z (0 0 NaN, 1 1 NaN)>, <LINESTRING Z (1 1 NaN, 2 2 NaN)>]
1078
+
1079
+ """
1080
+ geometry = np.asarray(geometry, dtype=np.object_)
1081
+ geometry = np.atleast_1d(geometry)
1082
+ if geometry.ndim != 1:
1083
+ raise ValueError("Array should be one dimensional")
1084
+
1085
+ # Ensure valid geometry type
1086
+ allowed_type_values = [
1087
+ GeometryType.LINESTRING.value,
1088
+ GeometryType.LINEARRING.value,
1089
+ GeometryType.MISSING.value,
1090
+ ]
1091
+ valid_geometries = np.isin(get_type_id(geometry), allowed_type_values)
1092
+ if not valid_geometries.all():
1093
+ raise ValueError("Geometry type is not supported")
1094
+
1095
+ # Not currently supported for linestrings
1096
+ include_m = False
1097
+ # Always return index for get_coordinates()
1098
+ xys, idx_coords = lib.get_coordinates(geometry, include_z, include_m, True)
1099
+
1100
+ # Create a mask for which coordinates are NOT the last in their group
1101
+ # Last coordinate in each group has idx_coords[i] != idx_coords[i+1]
1102
+ is_not_last = np.concatenate((idx_coords[:-1] == idx_coords[1:], [False]))
1103
+
1104
+ # Get indices of segment starts (all coords except last in each group)
1105
+ segment_starts = np.where(is_not_last)[0]
1106
+ segment_ends = segment_starts + 1
1107
+
1108
+ # Stack the segment coordinates: each segment is [start_point, end_point]
1109
+ segment_coords = np.stack([xys[segment_starts], xys[segment_ends]], axis=1)
1110
+
1111
+ # 'allow' NaN linestrings
1112
+ allow_nan = np.intc(0)
1113
+ lines = lib.linestrings(segment_coords, allow_nan, **kwargs)
1114
+
1115
+ # Efficiently compute idx_lines from segment_starts
1116
+ if return_index:
1117
+ # Return the index location of the original input geometry
1118
+ idx_lines = idx_coords[segment_starts]
1119
+ return lines, idx_lines
1120
+ else:
1121
+ return lines