So I have a shapely LineString:

print np.round(shapely_intersecting_lines.coords).astype(np.int) 
>>> array([[ 1520, -1140],
           [ 1412,  -973]])

I want to get all the points in between, that is I want to get the points of the line in between as integer values so I can use them in an image. Anyone know how I can do this with shapely? Or do I have to use another library. I need this to be as efficient as possible as I will be doing this many times.


I'm assuming you want each pixel point along each line. You can do this by interpolating each point along a distance of 1.0 units along the line. This might need to be refined, but try:

import numpy as np
from shapely.geometry import LineString

ls = LineString([(1520, -1140), (1412,  -973)])

xy = []
for f in range(0, int(ceil(ls.length)) + 1):
    p = ls.interpolate(f).coords[0]
    pr = map(round, p)
    if pr not in xy:

ar = np.array(xy, 'i')

Gives you:

array([[ 1520, -1140],
       [ 1519, -1139],
       [ 1519, -1138],
       [ 1413,  -975],
       [ 1412,  -974],
       [ 1412,  -973]], dtype=int32)

You may also be interested in rasterize() from rasterio to burn the shape of geometries into a raster image. It's a bit more complicated, as you would need to define an affine transform for the bounds of the geometry.

  • Thanks! This works very well but I wonder if this can be sped up through some sort of vectorization... – jlcv Dec 5 '17 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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