multilinestring.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Collections of linestrings and related utilities."""
  2. import shapely
  3. from shapely.errors import EmptyPartError
  4. from shapely.geometry import linestring
  5. from shapely.geometry.base import BaseMultipartGeometry
  6. __all__ = ["MultiLineString"]
  7. class MultiLineString(BaseMultipartGeometry):
  8. """A collection of one or more LineStrings.
  9. A MultiLineString has non-zero length and zero area.
  10. Parameters
  11. ----------
  12. lines : sequence
  13. A sequence LineStrings, or a sequence of line-like coordinate
  14. sequences or array-likes (see accepted input for LineString).
  15. Attributes
  16. ----------
  17. geoms : sequence
  18. A sequence of LineStrings
  19. Examples
  20. --------
  21. Construct a MultiLineString containing two LineStrings.
  22. >>> from shapely import MultiLineString
  23. >>> lines = MultiLineString([[[0, 0], [1, 2]], [[4, 4], [5, 6]]])
  24. """
  25. __slots__ = []
  26. def __new__(self, lines=None):
  27. """Create a new MultiLineString geometry."""
  28. if not lines:
  29. # allow creation of empty multilinestrings, to support unpickling
  30. # TODO better empty constructor
  31. return shapely.from_wkt("MULTILINESTRING EMPTY")
  32. elif isinstance(lines, MultiLineString):
  33. return lines
  34. lines = getattr(lines, "geoms", lines)
  35. subs = []
  36. for item in lines:
  37. line = linestring.LineString(item)
  38. if line.is_empty:
  39. raise EmptyPartError(
  40. "Can't create MultiLineString with empty component"
  41. )
  42. subs.append(line)
  43. if len(lines) == 0:
  44. return shapely.from_wkt("MULTILINESTRING EMPTY")
  45. return shapely.multilinestrings(subs)
  46. @property
  47. def __geo_interface__(self):
  48. """Return a GeoJSON-like mapping interface for this MultiLineString."""
  49. return {
  50. "type": "MultiLineString",
  51. "coordinates": tuple(tuple(c for c in g.coords) for g in self.geoms),
  52. }
  53. def svg(self, scale_factor=1.0, stroke_color=None, opacity=None):
  54. """Return a group of SVG polyline elements for the LineString geometry.
  55. Parameters
  56. ----------
  57. scale_factor : float
  58. Multiplication factor for the SVG stroke-width. Default is 1.
  59. stroke_color : str, optional
  60. Hex string for stroke color. Default is to use "#66cc99" if
  61. geometry is valid, and "#ff3333" if invalid.
  62. opacity : float
  63. Float number between 0 and 1 for color opacity. Default value is 0.8
  64. """
  65. if self.is_empty:
  66. return "<g />"
  67. if stroke_color is None:
  68. stroke_color = "#66cc99" if self.is_valid else "#ff3333"
  69. return (
  70. "<g>"
  71. + "".join(p.svg(scale_factor, stroke_color, opacity) for p in self.geoms)
  72. + "</g>"
  73. )
  74. shapely.lib.registry[5] = MultiLineString