find the last point of line geopanda

Solutions on MaxInterview for find the last point of line geopanda by the best coders in the world

showing results for - "find the last point of line geopanda"
Cristina
04 Aug 2016
1import geopandas as gpd
2from shapely.geometry import Point, LineString
3line1 = LineString([Point(0,0), Point(-1,-1), Point(2,-3), Point(4,5)])
4line2 = LineString([Point(-2,8), Point(7,4), Point(0,-1), Point(0,2)])
5gdf = gpd.GeoDataFrame(
6    {'City': ['Buenos Aires','Rio de Janeiro'],
7     'Country': ['Argentina', 'Brazil'], 'geometry': [line1, line2]})
8
9gdf['first'] = None
10gdf['last'] = None
11
12for index, row in gdf.iterrows():
13    coords = [(coords) for coords in list(row['geometry'].coords)]
14    first_coord, last_coord = [ coords[i] for i in (0, -1) ]
15    gdf.at[index,'first'] = Point(first_coord)
16    gdf.at[index,'last'] = Point(last_coord)
17    gdf
18