question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Obtaining X and Y values of LineString vertices for Multiple LineStrings

See original GitHub issue

Dear 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:closed
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AlexanderJuestelcommented, Jan 21, 2021

@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 😃

image

0reactions
brendan-wardcommented, Apr 27, 2021

@Aniwax one existing place for chat-style discussions / questions around GeoPandas / Shapely / PyGEOS is the GeoPandas gitter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get the vertices on a LineString either side of a Point
You want the two lines which lie either side of the point ? line1 = LineString([line.coords[0],(point.x, point.y)]) line2 ...
Read more >
Create method to access x, y values of Linestring in GeoSeries
In the GeoSeries class there is a method to access the coordinates of points (gdf.geometry.x, gdf.geometry.y) and in the GeopandasBase class ...
Read more >
shapely.MultiLineString — Shapely 2.0.0 documentation
Construct a MultiLineString containing two LineStrings. ... Get a geometry that represents all points within a distance of this geometry.
Read more >
How to get the endpoint of a LineString in Shapely
To get endpoints of a LineString , you just need to access its boundary property: from shapely.geometry import LineString line ...
Read more >
LineString (org.locationtech.jts:jts-core 1.19.0 API)
A LineString consists of a sequence of two or more vertices, along with all points ... geometry (e.g. a Point) may have a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found