0

I need a faster way to extract geometry xy of a large dataset which is in a geodataframe format. I have developed a lambda function as below to do that but I need a faster way. The following is a simple data with 3 MultiLineString lines to show the way I have coded. This code on a geodataframe with 100000 rows takes 5 seconds for x coordinates.

from geopandas import GeoDataFrame
from shapely.geometry import MultiLineString

coord1 = [((0, 0), (1, 1))]
coord2 = [((-1, 0), (1, 0))]
coord3 = [((1, 0), (0, 1))]

lines1 = MultiLineString(coord1)
lines2 = MultiLineString(coord2)
lines3 = MultiLineString(coord3)

d = {'col1': ['name1', 'name2', 'name3'],
     'geometry': [lines1, lines2, lines3]}
gdf = GeoDataFrame(d, crs="EPSG:4326")

aa = list(gdf.geometry)
x_list = list(map(lambda x: list(list(x.geoms)[0].coords.xy[0]) , aa))
y_list = list(map(lambda x: list(list(x.geoms)[0].coords.xy[1]) , aa))
2
  • It's worth pointing out that the code you provided does not extract ALL of the coordinates of the MultiLineString geometries. It only extracts the coordinates of the first geom within each MultiLineString geometry. In the example you gave, the geometries only have one geom, so it works out fine. But depending on what you really want to extract, you might need to change your strategy.
    – Felipe D.
    Commented Apr 12, 2022 at 21:12
  • It will likely add A LOT of overhead, so I'm not sure this is even worth it, but one way to try to speed this up would be to use a multiprocessing approach.
    – Felipe D.
    Commented Apr 12, 2022 at 21:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.