Obtaining X and Y values of LineString vertices for Multiple LineStrings
See original GitHub issueDear PyGeos Team,
I am currently exploring the functionalities of PyGeos and I am amazed by the performance compared to iterating over Shapely geoemtries.
I am mostly working with GeoDataFrames for a project and use Shapely functions for underlying operations. One of them is to get the X and Y coordinates of the vertices of a list of LineStrings returned as two separate lists, one for X coordinates, one for Y coordinates. I am aware of the get_coordinates
function but it only returns a N, 2 shaped array and the information is lost to which LineString the respective coordinate pair belongs to. Or would iterating over each point of each LineString be an alternative and then using get_x
and get_y
?
Is there another function that does the trick or what would be needed to implement this in pygeos. Maybe I could give it a try.
Currently, I am solving it the following way:
# Extracting X coordinates
gdf['X'] = [[i[0] for i in list(gdf.loc[j].geometry.coords)] for j in range(len(gdf))]
# Extracting Y coordinates
gdf['Y'] = [[i[1] for i in list(gdf.loc[j].geometry.coords)] for j in range(len(gdf))]
The main aim is to improve the performance and reduce computing time for large GeoDataFrames.
Cheers and thanks for your tips
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
@brendan-ward, your method with the DataFrame was a little unhandy since I needed the separate lists of coordinates, so I tried a little bit and came up with the following that yields a 10 times speed up, which is already very good! I am still using list comprehension but that will do the trick for now I think 😃
@Aniwax one existing place for chat-style discussions / questions around GeoPandas / Shapely / PyGEOS is the GeoPandas gitter.