Search code examples
pythonsortingnumpylaspy

Sorting numpy array created by laspy


I know this reads like a simple problem with a simple solution but I just can't wrap my head around it.

The documentation for laspy is a little sparse but I've managed well so far. I think that the issue here is now simply not being familiar enough with numpy.

I want to sort a numpy array based on the GPS Time.

Here is where I stand:

I am using the sample.las that comes with laspy for testing.

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

What I am attempting to do is sort the array by the gps_time column.

print(lasPoints.dtype)

gives me

[('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')])]

and

print(lasPoints)

gives me

[ ((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),)]

To access the gps_time I can run

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

Replacing "gps_time" for 9 gives the same result.

Now when I come to trying to sort my data it doesn't actually seem to sort the anything:

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

The array is printed out unsorted and as is,

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

Results in the gps_time being sorted as such:

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

Where am I going wrong here?


Solution

  • np.sort does not seem to support inplace sorting as far as i understand the documentation. np.ndarray.sort however does. So

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

    will always be unsorted.

    But to your problem: You could slice the list of GPS-times out of your list and use argsort to get the indices for a sorted list. These can then be used to sort your laspoints. e.g.:

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