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))
geom
within each MultiLineString geometry. In the example you gave, the geometries only have onegeom
, so it works out fine. But depending on what you really want to extract, you might need to change your strategy.