Line Segment

Creates a FeatureCollection of 2-vertex LineString segments from a (Multi)LineString or (Multi)Polygon.

Example

from turfpy.misc import line_segment

poly = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              51.17431640625,
              47.025206001585396
            ],
            [
              45.17578125,
              43.13306116240612
            ],
            [
              54.5361328125,
              41.85319643776675
            ],
            [
              51.17431640625,
              47.025206001585396
            ]
          ]
        ]
      }
}

line_segment(poly)
{"features": [{"bbox": [45.175781, 43.133061, 51.174316, 47.025206], "geometry": {"coordinates": [[45.175781, 43.133061], [51.174316, 47.025206]], "type": "LineString"}, "id": 0, "properties": {}, "type": "Feature"}, {"bbox": [45.175781, 41.853196, 54.536133, 43.133061], "geometry": {"coordinates": [[54.536133, 41.853196], [45.175781, 43.133061]], "type": "LineString"}, "id": 1, "properties": {}, "type": "Feature"}, {"bbox": [51.174316, 41.853196, 54.536133, 47.025206], "geometry": {"coordinates": [[51.174316, 47.025206], [54.536133, 41.853196]], "type": "LineString"}, "id": 2, "properties": {}, "type": "Feature"}], "type": "FeatureCollection"}

Interactive Example

from ipyleaflet import Map, GeoJSON, LayersControl
from turfpy.misc import line_segment

m = Map(center=[44.52337579109473, 50.61581611633301], zoom=6)

poly = {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              51.17431640625,
              47.025206001585396
            ],
            [
              45.17578125,
              43.13306116240612
            ],
            [
              54.5361328125,
              41.85319643776675
            ],
            [
              51.17431640625,
              47.025206001585396
            ]
          ]
        ]
      }
}

polygon = GeoJSON(name="Polygon", data=poly, style={'color':'red'})

m.add_layer(polygon)

segments = GeoJSON(name="segments", data=line_segment(poly))
m.add_layer(segments)

control = LayersControl(position='topright')
m.add_control(control)

m