Skip to content

Latest commit

 

History

History
37 lines (24 loc) · 1.11 KB

How would one get the end points of a polyline?.md

File metadata and controls

37 lines (24 loc) · 1.11 KB

From how would one get the end points of a polyline?

It is easier with Fiona, more "Pythonic", and list slicing:

    import fiona
    with fiona.drivers():
       for line in fiona.open("some_shapefile.shp"):
             # print first and last point of every line
             print line['geometry']['coordinates'][0], line['geometry']['coordinates'][-1]

And with shapely:

    from shapely.geometry import Point
    for line in fiona.open("some_shapefile.shp"):
       print Point(line['geometry']['coordinates'][0]), Point(line['geometry']['coordinates'][-1])

And you can construct you polygon and save it with Fiona

New: using the suggestion of sgillies (boundary) with the shape function of shapely

    
    from shapely.geometry import shape
    for line in fiona.open("some_shapefile.shp"):
         print shape(line['geometry']).boundary[0], shape(line['geometry']).boundary[1]