2017-10-20 56 views
1

我知道这听起来像一个简单的问题,但一个简单的解决方案,但我不能包裹我的头。由laspy创建排序numpy数组

laspy的文档有点稀疏,但目前我的管理得很好。我认为这里的问题现在对于numpy不够熟悉。

我想排序基于GPS时间的numpy数组。

这里是我的立场:

我使用自带的laspy用于测试的sample.las。

import laspy 
import numpy as np 

#open the file 
lasFile = laspy.file.File("C:/Anaconda3/Lib/site-packages/laspytest/data/simple.las", mode = "rw") 

#put points in numpy array 
lasPoints = lasFile.points 

我试图做的是按照gps_time列对数组进行排序。

print(lasPoints.dtype) 

给我

[('point', [('X', '<i4'), ('Y', '<i4'), ('Z', '<i4'), ('intensity', '<u2'), ('flag_byte', 'u1'), ('raw_classification', 'u1'), ('scan_angle_rank', 'i1'), ('user_data', 'u1'), ('pt_src_id', '<u2'), ('gps_time', '<f8'), ('red', '<u2'), ('green', '<u2'), ('blue', '<u2')])] 

print(lasPoints) 

给我

[ ((63701224, 84902831, 43166, 143, 73, 1, -9, 132, 7326, 245380.78254963, 68, 77, 88),) 
((63689633, 84908770, 44639, 18, 81, 1, -11, 128, 7326, 245381.45279924, 54, 66, 68),) 
((63678474, 84910666, 42671, 118, 9, 1, -10, 122, 7326, 245382.13595007, 112, 97, 114),) 
..., 
((63750167, 85337575, 41752, 43, 9, 1, 11, 124, 7334, 249772.21013494, 100, 96, 120),) 
((63743327, 85323084, 42408, 31, 9, 1, 11, 125, 7334, 249772.70733372, 176, 138, 164),) 
((63734285, 85324032, 42392, 116, 73, 1, 9, 124, 7334, 249773.20172407, 138, 107, 136),)] 

要访问gps_time我可以运行

lasPoints[0][0][9] ## first gps_time in array 
lasPoints[1][0][9] ## second gps_time in array 

替换“gps_time”为9会得到相同的结果。

现在,当我来想我的数据进行排序,它实际上并没有看起来的东西进行排序:

np.sort(lasPoints["point"]["gps_time"]) 
print(lasPoints) 

阵列被打印出来无序和As,

lasPoints=np.sort(lasPoints["point"]["gps_time"]) 
print(lasPoints) 

结果在gps_time被排序如下:

[ 245370.41706456 245370.74331403 245371.06452222 ..., 249782.07498673 
    249782.64531958 249783.16215837] 

我在哪里错了?

回答

2

np.sort似乎不支持就地排序,据我了解的文件。 np.ndarray.sort但是。 所以

np.sort(lasPoints["point"]["gps_time"]) 
print(lasPoints) 

将始终未排序。

但是对于你的问题: 你可以从列表中切出GPS时间列表,并使用argsort获取排序列表的索引。 这些可以用来排序你的laspoints。 例如: -

sorted_ind = np.argsort(list_of_gpstimes) 
laspoints = laspoints[sorted_ind] 
+1

点上,欢呼队友。 np.ndarray.sort(point_records [“point”] [“gps_time”])按照我的需要对数组进行排序。 – Ingwe

1

只是完全关闭这一点,并建立在dudakl的回答,利用NP。ndarray,排序,这是对我工作:

np.ndarray.sort(lasPoints["point"],kind='mergesort',order='gps_time') 

的这里关键是要指定lasPoint [“点”],然后通过gps_time订购。

在这里,这将只排序gps_time coloumn,没有别的

np.ndarray.sort(lasPoints["point"]["gps_time])