converting a list of shapely geometry to numpy array [closed]

How can I convert a shapely point geometry like following to numpy array?

[<shapely.geometry.point.Point object at 0x000001CEF6978DA0>, <shapely.geometry.point.Point object at 0x000001CEF6978CF8>, <shapely.geometry.point.Point object at 0x000001CEF6978E10>]

or in str:

['POINT (656822.0796426814 -185003.7243437266)', 'POINT (656917.7545657885 -184985.6672704536)', 'POINT (656997.7888963958 -185001.578684116)']

to a numpy array with shape (3,2)like this:

[[ 656822.0796426814 -185003.7243437266]
[ 656917.7545657885 -184985.6672704536]
[ 656997.7888963958 -185001.578684116]]

closed as off-topic by Fran Raga, whyzar, PolyGeo♦May 8 at 9:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

• What have you already tried, where are you stuck? – DirkB. Aug 21 '17 at 20:50

2 Answers

I created a shapefile with your point coordinates and the following code produces those numpy arrays:

import fiona
from shapely.geometry import shape
import numpy as np

path = '/home/zeito/pyqgis_data/points_list.shp' #your points

points = fiona.open(path)

geoms = [ shape(feat["geometry"]) for feat in points ]

list_arrays = [ np.array((geom.xy[0][0], geom.xy[1][0])) for geom in geoms ]

for array in list_arrays:
print array

Result after running it is:

[ 656822.07964268 -185003.72434373]
[ 656917.75456579 -184985.66727045]
[ 656997.7888964  -185001.57868412]
• Just for completion: if you want to have the coordinates in one big array, you have to define an empty one with arr=np.empty((0,2)) and then append it in the for loop with arr = np.append(arr,np.array([array]),axis=0) – Leo Apr 10 at 7:12

Create a list of the coordinates and convert into a numpy array using np.array().

import numpy as np
from shapely.geometry import Point
mypoints = [Point(1, 2), Point(1.123, 2.234), Point(2.234, 4.32432)]

listarray = []
for pp in mypoints:
listarray.append([pp.x, pp.y])
nparray = np.array(listarray)

print mypoints
print nparray

Printed output:

[<shapely.geometry.point.Point object at 0x10f1bd350>, <shapely.geometry.point.Point object at 0x10f1bd410>, <shapely.geometry.point.Point object at 0x10f1bd3d0>]
[[ 1.       2.     ]
[ 1.123    2.234  ]
[ 2.234    4.32432]]