简体   繁体   English

在python中获取两个(X,Y)坐标之间所有点的最快方法

[英]Fastest way to get all the points between two (X,Y) coordinates in python

So I have a shapely LineString :所以我有一个shapelyLineString

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.这可以解释为一个numpy数组,如上所示。

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.我早些时候在gis.stackexchange上发布了这个,希望有一个shapely的解决方案。 The solution was good at first, however, the solution is now too slow as I run this over 50000 times in my code.解决方案一开始很好,但是现在解决方案太慢了,因为我在代码中运行了超过 50000 次。 On my computer each loop takes about 0.03s resulting in over a day of running.在我的电脑上,每个循环大约需要 0.03 秒,导致运行一天以上。 It is too slow for what I need here and was hoping to see if anyone knows of a vectorized solution to this.对于我在这里需要的东西来说太慢了,希望看看是否有人知道对此的矢量化解决方案。

Bresenham may be smart but I'm pretty sure brute force vectorization is faster. Bresenham可能很聪明,但我很确定蛮力矢量化更快。 I've written two variants - the first is easier to read, the second is faster (80 us vs 50 us). 我写了两个变体 - 第一个更容易阅读,第二个更快(80 us vs 50 us)。

Update Fixed a bug (thanks @Varlor) and added an nd variant. 更新修复了一个错误(感谢@Varlor)并添加了一个nd变体。

import numpy as np
from timeit import timeit

def connect(ends):
    d0, d1 = np.abs(np.diff(ends, axis=0))[0]
    if d0 > d1: 
        return np.c_[np.linspace(ends[0, 0], ends[1, 0], d0+1, dtype=np.int32),
                     np.round(np.linspace(ends[0, 1], ends[1, 1], d0+1))
                     .astype(np.int32)]
    else:
        return np.c_[np.round(np.linspace(ends[0, 0], ends[1, 0], d1+1))
                     .astype(np.int32),
                     np.linspace(ends[0, 1], ends[1, 1], d1+1, dtype=np.int32)]


def connect2(ends):
    d0, d1 = np.diff(ends, axis=0)[0]
    if np.abs(d0) > np.abs(d1): 
        return np.c_[np.arange(ends[0, 0], ends[1,0] + np.sign(d0), np.sign(d0), dtype=np.int32),
                     np.arange(ends[0, 1] * np.abs(d0) + np.abs(d0)//2,
                               ends[0, 1] * np.abs(d0) + np.abs(d0)//2 + (np.abs(d0)+1) * d1, d1, dtype=np.int32) // np.abs(d0)]
    else:
        return np.c_[np.arange(ends[0, 0] * np.abs(d1) + np.abs(d1)//2,
                               ends[0, 0] * np.abs(d1) + np.abs(d1)//2 + (np.abs(d1)+1) * d0, d0, dtype=np.int32) // np.abs(d1),
                     np.arange(ends[0, 1], ends[1,1] + np.sign(d1), np.sign(d1), dtype=np.int32)]


def connect_nd(ends):
    d = np.diff(ends, axis=0)[0]
    j = np.argmax(np.abs(d))
    D = d[j]
    aD = np.abs(D)
    return ends[0] + (np.outer(np.arange(aD + 1), d) + (aD>>1)) // aD


ends = np.array([[ 1520, -1140],
                 [ 1412,  -73]])

ends_4d = np.array([[  100, -302, 101, -49],
                    [ -100,  -45, 112, 100]])

print(connect(ends))
print(connect_nd(ends_4d))


assert np.all(connect(ends)==connect2(ends))
assert np.all(connect(ends)==connect_nd(ends))
assert np.all(connect(ends)==connect(ends[:, ::-1])[:, ::-1])
assert np.all(connect(ends)==connect(ends[::-1])[::-1])

print(timeit('f(ends)', globals={'f': connect, 'ends': ends}, number=10000)*100, 'us')
print(timeit('f(ends)', globals={'f': connect2, 'ends': ends}, number=10000)*100, 'us')
print(timeit('f(ends)', globals={'f': connect_nd, 'ends': ends}, number=10000)*100, 'us')

Sample output: 样本输出:

[[ 1520 -1140]
 [ 1520 -1139]
 [ 1520 -1138]
 ..., 
 [ 1412   -75]
 [ 1412   -74]
 [ 1412   -73]]
[[ 100 -302  101  -49]
 [  99 -301  101  -48]
 [  98 -300  101  -48]
 ..., 
 [ -98  -47  112   99]
 [ -99  -46  112   99]
 [-100  -45  112  100]]
78.8237597000034 us
48.02509490000375 us
62.78072760001123 us

Good base code to start:好的基本代码开始:


def points_between(x1, y1, x2, y2):
    
    d0 = x2 - x1
    d1 = y2 - y1
    
    count = max(abs(d1+1), abs(d0+1))

    if d0 == 0:
        return (
            np.full(count, x1),
            np.round(np.linspace(y1, y2, count)).astype(np.int32)
        )

    if d1 == 0:
        return (
            np.round(np.linspace(x1, x2, count)).astype(np.int32),
            np.full(count, y1),  
        )

    return (
        np.round(np.linspace(x1, x2, count)).astype(np.int32),
        np.round(np.linspace(y1, y2, count)).astype(np.int32)

Using generators so that way you will save memory 使用生成器,这样你将节省内存

import numpy as np
import math

d = np.array(
    [
        [1520,-1140],
        [1412,-973]
    ],dtype=float);
rise = d[0,1]-d[1,1];
run = d[0,0]-d[1,0];
slope = rise/run;
print slope

fromPt = d[0];
sign = 1
if (slope<0):
    sign = -1;
points = ([d[0,0]+sign*i,math.floor(d[0,1]+(sign*i*slope))] for i in xrange(1+int(math.ceil(abs(d[0,0]-d[1,0])))))
for pt in points:
    print pt

Here's Bresenhams line algorithm as a generator. 这里是Bresenhams线算法作为生成器。 If you want a list just call list() on the output: 如果你想要一个列表,只需在输出上调用list():

def line(x0, y0, x1, y1):
    deltax = x1-x0
    dxsign = int(abs(deltax)/deltax)
    deltay = y1-y0
    dysign = int(abs(deltay)/deltay)
    deltaerr = abs(deltay/deltax)
    error = 0
    y = y0
    for x in range(x0, x1, dxsign):
        yield x, y
        error = error + deltaerr
        while error >= 0.5:
            y += dysign
            error -= 1
    yield x1, y1

here is a short solution using np library这是使用 np 库的简短解决方案

    import numpy as np
    x = [1,2,3]
    y = [5,6,7]
    ks,cs = np.meshgrid(x,y)
    np.vstack((ks.flatten(),cs.flatten())).T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在香草 python 中找到两个 [[X,Y]、[X,Y]、...] 坐标列表之间的最近点 - Find closest points between two lists of [[X,Y], [X,Y], ...] coordinates, in vanilla python 获取2点之间xyz坐标列表的最快方法 - Fastest way to get list of xyz coordinates between 2 points 使用Python代码并行计算两点之间距离的最快方法 - Fastest way with parallelization to compute distances between two points with Python code 蟒蛇。 如何从ax,y列表和偏移距离获取偏移样条的x,y坐标 - Python. How to get the x,y coordinates of a offset spline from a x,y list of points and offset distance Python:获取两个3D点之间的所有坐标或绘制3D线 - Python: Get all coordinates between two 3D points or draw a 3D Line Python 3.6。获得所有相同X坐标的平均Y. - Python 3.6. Get average Y for all same X coordinates 两个函数的交点,多个x轴和y轴坐标 - Intersection points between two functions, multiple x-axis and y-axis coordinates 如何使用两点的 x 和 y 坐标绘制一条线? - How can draw a line using the x and y coordinates of two points? 如何使用python获取x,y坐标? - How to get x,y coordinates using python? 对于较大的 x/y 值,有没有办法在两点之间实现平滑曲线? - Is there a way to achieve a smooth curve between two points for larger x/y values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM