Reputation: 121
I have a GeoPandas dataframe named barrios
and I use barrios.geometry.centroid
and assign it to center
.
So center
is geopandas.geoseries.GeoSeries
with an index and values - POINT (-58.42266 -34.57393)
I need to get these coordinates and save them as a list:
[(point_1_lat, point_1_lon), (point_2_lat, point_2_lon), ...]
I tried:
[center.values.y , center.values.x]
But it returns a list of 2 arrays - [array(lat), array(lng)]
.
How can I get the desired result?
Upvotes: 2
Views: 9878
Reputation: 454
you can use the zip
to loop through multiple variables. This should extract the coordinates to a list.
coord_list = [(x,y) for x,y in zip(gdf['geometry'].x , gdf['geometry'].y)]
alternatively, you can create a GeoDataFrame
with the x and y coordinates.
first, extract the x and y coordinates and put them in new columns.
import geopandas as gpd
url = r"link\to\file"
gdf = gpd.read_file(url)
gdf['x'] = None
gdf['y'] = None
gdf['x'] = gdf.geometry.apply(lambda x: x.x)
gdf['y'] = gdf.geometry.apply(lambda x: x.y)
this returns a GeoDataFrame
with x and y coordinate columns. Now extracting the coordinates into the list.
coordinate_list = [(x,y) for x,y in zip(gdf.x , gdf.y)]
this return the list of coordinate tuples
[(105.27, -5.391),
(107.615, -6.945264),
(107.629, -6.941126700000001),
(107.391, -6.9168726),
(107.6569, -6.9087003),
(107.638, -6.9999),
(107.67, -6.553),
(107.656, -6.8),
...
you'll have a list, and a GeoDataFrame with x and y columns.
Upvotes: 8