linear.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. """Linear geometry functions."""
  2. from shapely import lib
  3. from shapely.decorators import deprecate_positional, multithreading_enabled
  4. from shapely.errors import UnsupportedGEOSVersionError
  5. __all__ = [
  6. "line_interpolate_point",
  7. "line_locate_point",
  8. "line_merge",
  9. "shared_paths",
  10. "shortest_line",
  11. ]
  12. # Note: future plan is to change this signature over a few releases:
  13. # shapely 2.0:
  14. # line_interpolate_point(line, distance, normalized=False, **kwargs)
  15. # shapely 2.1: shows deprecation warning about positional 'normalized' arg
  16. # same signature as 2.0
  17. # shapely 2.2(?): enforce keyword-only arguments after 'normalized'
  18. # line_interpolate_point(line, distance, *, normalized=False, **kwargs)
  19. @deprecate_positional(["normalized"], category=DeprecationWarning)
  20. @multithreading_enabled
  21. def line_interpolate_point(line, distance, normalized=False, **kwargs):
  22. """Return a point interpolated at given distance on a line.
  23. Parameters
  24. ----------
  25. line : Geometry or array_like
  26. For multilinestrings or geometrycollections, the first geometry is taken
  27. and the rest is ignored. This function raises a TypeError for non-linear
  28. geometries. For empty linear geometries, empty points are returned.
  29. distance : float or array_like
  30. Negative values measure distance from the end of the line. Out-of-range
  31. values will be clipped to the line endings.
  32. normalized : bool, default False
  33. If True, the distance is a fraction of the total
  34. line length instead of the absolute distance.
  35. **kwargs
  36. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  37. Examples
  38. --------
  39. >>> import shapely
  40. >>> from shapely import LineString
  41. >>> line = LineString([(0, 2), (0, 10)])
  42. >>> shapely.line_interpolate_point(line, 2)
  43. <POINT (0 4)>
  44. >>> shapely.line_interpolate_point(line, 100)
  45. <POINT (0 10)>
  46. >>> shapely.line_interpolate_point(line, -2)
  47. <POINT (0 8)>
  48. >>> shapely.line_interpolate_point(line, [0.25, -0.25], normalized=True).tolist()
  49. [<POINT (0 4)>, <POINT (0 8)>]
  50. >>> shapely.line_interpolate_point(LineString(), 1)
  51. <POINT EMPTY>
  52. """
  53. if normalized:
  54. return lib.line_interpolate_point_normalized(line, distance)
  55. else:
  56. return lib.line_interpolate_point(line, distance)
  57. # Note: future plan is to change this signature over a few releases:
  58. # shapely 2.0:
  59. # line_locate_point(line, other, normalized=False, **kwargs)
  60. # shapely 2.1: shows deprecation warning about positional 'normalized' arg
  61. # same signature as 2.0
  62. # shapely 2.2(?): enforce keyword-only arguments after 'normalized'
  63. # line_locate_point(line, other, *, normalized=False, **kwargs)
  64. @deprecate_positional(["normalized"], category=DeprecationWarning)
  65. @multithreading_enabled
  66. def line_locate_point(line, other, normalized=False, **kwargs):
  67. """Return the distance to the line origin of given point.
  68. If given point does not intersect with the line, the point will first be
  69. projected onto the line after which the distance is taken.
  70. Parameters
  71. ----------
  72. line : Geometry or array_like
  73. Line or lines to calculate the distance to.
  74. other : Geometry or array_like
  75. Point or points to calculate the distance from.
  76. normalized : bool, default False
  77. If True, the distance is a fraction of the total line length instead of
  78. the absolute distance.
  79. **kwargs
  80. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  81. Examples
  82. --------
  83. >>> import shapely
  84. >>> from shapely import LineString, Point
  85. >>> line = LineString([(0, 2), (0, 10)])
  86. >>> point = Point(4, 4)
  87. >>> shapely.line_locate_point(line, point)
  88. 2.0
  89. >>> shapely.line_locate_point(line, point, normalized=True)
  90. 0.25
  91. >>> shapely.line_locate_point(line, Point(0, 18))
  92. 8.0
  93. >>> shapely.line_locate_point(LineString(), point)
  94. nan
  95. """
  96. if normalized:
  97. return lib.line_locate_point_normalized(line, other)
  98. else:
  99. return lib.line_locate_point(line, other)
  100. @multithreading_enabled
  101. def line_merge(line, directed=False, **kwargs):
  102. """Return (Multi)LineStrings formed by combining the lines in a MultiLineString.
  103. Lines are joined together at their endpoints in case two lines are
  104. intersecting. Lines are not joined when 3 or more lines are intersecting at
  105. the endpoints. Line elements that cannot be joined are kept as is in the
  106. resulting MultiLineString.
  107. The direction of each merged LineString will be that of the majority of the
  108. LineStrings from which it was derived. Except if ``directed=True`` is
  109. specified, then the operation will not change the order of points within
  110. lines and so only lines which can be joined with no change in direction
  111. are merged.
  112. Parameters
  113. ----------
  114. line : Geometry or array_like
  115. Linear geometry or geometries to merge.
  116. directed : bool, default False
  117. Only combine lines if possible without changing point order.
  118. Requires GEOS >= 3.11.0
  119. **kwargs
  120. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  121. Examples
  122. --------
  123. >>> import shapely
  124. >>> from shapely import MultiLineString
  125. >>> shapely.line_merge(MultiLineString([[(0, 2), (0, 10)], [(0, 10), (5, 10)]]))
  126. <LINESTRING (0 2, 0 10, 5 10)>
  127. >>> shapely.line_merge(MultiLineString([[(0, 2), (0, 10)], [(0, 11), (5, 10)]]))
  128. <MULTILINESTRING ((0 2, 0 10), (0 11, 5 10))>
  129. >>> shapely.line_merge(MultiLineString())
  130. <GEOMETRYCOLLECTION EMPTY>
  131. >>> shapely.line_merge(MultiLineString([[(0, 0), (1, 0)], [(0, 0), (3, 0)]]))
  132. <LINESTRING (1 0, 0 0, 3 0)>
  133. >>> shapely.line_merge(MultiLineString([[(0, 0), (1, 0)], [(0, 0), (3, 0)]]), \
  134. directed=True)
  135. <MULTILINESTRING ((0 0, 1 0), (0 0, 3 0))>
  136. """
  137. if directed:
  138. if lib.geos_version < (3, 11, 0):
  139. raise UnsupportedGEOSVersionError(
  140. "'{}' requires at least GEOS {}.{}.{}.".format(
  141. "line_merge", *(3, 11, 0)
  142. )
  143. )
  144. return lib.line_merge_directed(line, **kwargs)
  145. return lib.line_merge(line, **kwargs)
  146. @multithreading_enabled
  147. def shared_paths(a, b, **kwargs):
  148. """Return the shared paths between a and b.
  149. Both geometries should be linestrings or arrays of linestrings.
  150. A geometrycollection or array of geometrycollections is returned
  151. with two elements in each geometrycollection. The first element is a
  152. multilinestring containing shared paths with the same direction
  153. for both inputs. The second element is a multilinestring containing
  154. shared paths with the opposite direction for the two inputs.
  155. Parameters
  156. ----------
  157. a, b : Geometry or array_like
  158. Linestring or linestrings to compare.
  159. **kwargs
  160. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  161. Examples
  162. --------
  163. >>> import shapely
  164. >>> from shapely import LineString
  165. >>> line1 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  166. >>> line2 = LineString([(1, 0), (2, 0), (2, 1), (1, 1), (1, 0)])
  167. >>> shapely.shared_paths(line1, line2).wkt
  168. 'GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING ((1 0, 1 1)))'
  169. >>> line3 = LineString([(1, 1), (0, 1)])
  170. >>> shapely.shared_paths(line1, line3).wkt
  171. 'GEOMETRYCOLLECTION (MULTILINESTRING ((1 1, 0 1)), MULTILINESTRING EMPTY)'
  172. """
  173. return lib.shared_paths(a, b, **kwargs)
  174. @multithreading_enabled
  175. def shortest_line(a, b, **kwargs):
  176. """Return the shortest line between two geometries.
  177. The resulting line consists of two points, representing the nearest
  178. points between the geometry pair. The line always starts in the first
  179. geometry `a` and ends in the second geometry `b`. The endpoints of the
  180. line will not necessarily be existing vertices of the input geometries
  181. `a` and `b`, but can also be a point along a line segment.
  182. Parameters
  183. ----------
  184. a, b : Geometry or array_like
  185. Geometry or geometries to compare.
  186. **kwargs
  187. See :ref:`NumPy ufunc docs <ufuncs.kwargs>` for other keyword arguments.
  188. See Also
  189. --------
  190. prepare : improve performance by preparing ``a`` (the first argument)
  191. Examples
  192. --------
  193. >>> import shapely
  194. >>> from shapely import LineString
  195. >>> line1 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
  196. >>> line2 = LineString([(0, 3), (3, 0), (5, 3)])
  197. >>> shapely.shortest_line(line1, line2)
  198. <LINESTRING (1 1, 1.5 1.5)>
  199. """
  200. return lib.shortest_line(a, b, **kwargs)