1

I created a Centerline object from a shapely polygon, which is basically a multiline string which contains the starting and endpoint of the centerlines. How can I transform this multiline string into a [start_x start_y end_x end_y] 4-column n-row numpy array?

x_coords = cloud[:,0]
y_coords = cloud[:,1]
array_2d = np.column_stack([x_coords, y_coords])
c_hull = alphashape.alphashape(array_2d, alpha)
kozep_vonal = Centerline(c_hull)
1
  • 1
    Which python libraries do you use ?
    – gene
    Apr 23, 2021 at 15:45

1 Answer 1

2

Centerline returns a centerline.geometry.Centerline which is a multilinestring. You have to iterate through the multilinestring and get the coordinates of the first and last point from the line parts. Here is a simple code:

from shapely.geometry import Polygon
from centerline.geometry import Centerline
import numpy as np
polygon = Polygon([[0, 0], [0, 4], [4, 4], [4, 0]])
centerline = Centerline(polygon)
c_array = np.zeros((len(centerline),4), dtype=float)
for i, l in enumerate(centerline):
    ll = list(l.coords)
    c_array[i] = [ll[0][0], ll[0][1], ll[-1][0], ll[-1][1]]

c_array will be:

[[0.25 3.75 0.75 3.25]
 [3.75 3.75 3.25 3.25]
 [1.25 2.75 1.75 2.25]
 [0.75 3.25 1.25 2.75]
 [2.75 2.75 2.25 2.25]
 [3.25 3.25 2.75 2.75]
 [2.   2.   2.25 2.25]
 [2.   2.   1.75 2.25]
 [1.25 1.25 1.75 1.75]
 [2.   2.   1.75 1.75]
 [0.75 0.75 1.25 1.25]
 [0.25 0.25 0.75 0.75]
 [2.   2.   2.25 1.75]
 [2.75 1.25 3.25 0.75]
 [3.75 0.25 3.25 0.75]
 [2.25 1.75 2.75 1.25]]

p.s. it is funny to see some Hungarian words in your code :)

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.