在 Python 中:我有 2 个数组:lon、lat(超过 500 000 个点)。
lon = numpy.array()
lat = numpy.array()
flag = True
while flag:
lon1 = lon[:-1]
lon2 = lon[1:]
lat1 = lat[:-1]
lat2 = lat[1:]
'''distance'''
x = (lon2 - lon1)
y = (lat2 - lat1)
d = numpy.sqrt(x * x + y * y)
min = numpy.min(d)
if min < 0.015:
j = numpy.where(d == min)[0][0]
lon[j] = (lon[j] + lon[j + 1]) / 2
lat[j] = (lat[j] + lat[j + 1]) / 2
lon = numpy.delete(lon, j + 1)
lat = numpy.delete(lat, j + 1)
else:
flag = False
这段代码运行得很慢!
请提示,如何更快地执行代码?
我知道有scipy.weave。请提示,如何使用scipy.weave更改代码?
import scipy from scipy import weave codeC = """ ??? """ scipy.weave.inline(codeC, ['lon', 'lat'], compiler='gcc')
谢谢。
更新:
lon1 = lon[:-1]
lon2 = lon[1:]
lat1 = lat[:-1]
lat2 = lat[1:]
x = (lon2 - lon1)
y = (lat2 - lat1)
d = x * x + y * y
while True and d.size > 1:
j = np.argmin(d)
if d[j] > min_radius:
break
lon[j] = (lon[j] + lon[j + 1]) / 2
lat[j] = (lat[j] + lat[j + 1]) / 2
'''lon = np.delete(lon, j + 1)
lat = np.delete(lat, j + 1)
d = np.delete(d, j)'''
if j == (d.size - 1):
lon = lon[:j + 1]
lat = lat[:j + 1]
d = d[:j]
else:
lon = np.hstack((lon[:j + 1], lon[j + 2:]))
lat = np.hstack((lat[:j + 1], lat[j + 2:]))
d = np.hstack((d[:j], d[j + 1:]))
if j > 0:
x = lon[j] - lon[j - 1]
y = lat[j] - lat[j - 1]
d[j - 1] = x * x + y * y
if j < (d.size - 1):
x = lon[j + 1] - lon[j]
y = lat[j + 1] - lat[j]
d[j] = x * x + y * y
@ilmiacs、@Jaime、@Luke,谢谢!它工作得更快!
但作为一个整体,代码在 500 个点的数组上工作的时间很长......:(((