So I have a shapely
LineString
:
print np.round(shapely_intersecting_lines.coords).astype(np.int)
>>> array([[ 1520, -1140],
[ 1412, -973]])
This can be interpreted as a numpy
array as well as seen above.
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. The output should be something like this:
array([[ 1520, -1140],
[ 1519, -1139],
[ 1519, -1138],
...,
[ 1413, -975],
[ 1412, -974],
[ 1412, -973]], dtype=int32)
I posted this earlier in gis.stackexchange hoping there was a solution in shapely
that was efficient. The solution was good at first, however, the solution is now too slow as I run this over 50000 times in my code. On my computer each loop takes about 0.03s resulting in over a day of running. It is too slow for what I need here and was hoping to see if anyone knows of a vectorized solution to this.
Using generators so that way you will save memory
Here's Bresenhams line algorithm as a generator. If you want a list just call list() on the output:
Bresenham may be smart but I'm pretty sure brute force vectorization is faster. I've written two variants - the first is easier to read, the second is faster (80 us vs 50 us).
Update Fixed a bug (thanks @Varlor) and added an nd variant.
Sample output: