3

I have a GeoDataFrame of a street network with the geometry of each row as a LineString. Say something like:

 type id tags geometry lanes bridge name highway
0 way 34953479 {'access': 'private', 'highway': 'service', 'i... LINESTRING (282025.712 6242798.110, 282029.830... 2.0 None Engineering Way service
1 way 34953670 {'access': 'private', 'highway': 'service', 'i... LINESTRING (281935.657 6242783.921, 281936.328... 2.0 None Science Way service

I want each vertex as a row in a np.array.

With

pt = [ np.array(line.coords) for line in rd.geometry ]
print(pt)

I get:

[array([[ 282025.71154782, 6242798.11011274],[ 282029.82990925, 6242798.7208016 ],[ 282033.94827067, 6242799.33149047],...

which is a list of np.array.

pt[0] is: array([[ 282025.71154782, 6242798.11011274],[ 282029.82990925, 6242798.7208016 ],[ 282033.94827067, 6242799.33149047],...

pt[1] is: array([[ 281935.65690061, 6242783.92136821],[ 281936.32837562, 6242780.16379144],[ 281936.99985063, 6242776.40621467],...

I want one np.array with the coordinates stacked.

How do I extract/convert a gdf of LineString geometry into one np.array where the coordinates are stacked

array([[ x, y],[ x, y ],[ x, y],...
Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Jun 14, 2022 at 9:11
0

1 Answer 1

2

There is a native NumPy's method concatenate():

Join a sequence of arrays along an existing axis.

import numpy as np
import geopandas as gpd
from shapely.geometry import LineString
d = {
 'attr1': ['line1', 'line2'],
 'geometry': [
 LineString([
 (350630.319649, 5333438.389906),
 (410389.192817, 5298093.202727),
 (466139.848883, 5303194.569949),
 (500391.885943, 5310482.237409),
 (522254.888322, 5300643.886338),
 (547761.72443, 5287890.468284),
 (594038.412798, 5288254.851657),
 (620638.399026, 5307931.553798),
 (643230.16815, 5355301.392285)
 ]),
 LineString([
 (465059.389802, 5337643.246403),
 (428840.7437, 5395427.193238)]
 )]
 }
gdf = gpd.GeoDataFrame(d, crs="EPSG:25832")
pt = list(gdf.geometry) #[<shapely.geometry.linestring.LineString object at 0x000001F3F082FD00>, <shapely.geometry.linestring.LineString object at 0x000001F3F0822CA0>]
# pt = gdf.geometry.tolist() 
pt_array = np.concatenate(pt)
print(type(pt_array))
print(pt_array)

The above code will result in:

<class 'numpy.ndarray'>
[[ 350630.319649 5333438.389906]
 [ 410389.192817 5298093.202727]
 [ 466139.848883 5303194.569949]
 [ 500391.885943 5310482.237409]
 [ 522254.888322 5300643.886338]
 [ 547761.72443 5287890.468284]
 [ 594038.412798 5288254.851657]
 [ 620638.399026 5307931.553798]
 [ 643230.16815 5355301.392285]
 [ 465059.389802 5337643.246403]
 [ 428840.7437 5395427.193238]]
answered Jun 14, 2022 at 9:52
1
  • As of 2024年09月16日 with numpy 1.26.4: pt_array = np.concatenate(pt) raises ValueError: zero-dimensional arrays cannot be concatenated. Commented Sep 16, 2024 at 17:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.