-
-
Save AsgerPetersen/7e2f3a4e557c0151d028 to your computer and use it in GitHub Desktop.
Reading LAS file efficiently into numpy using cython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%%cython -I/usr/local/include -L/usr/local/lib -llas_c | |
# Created by Casey Goodlett | |
cimport cython | |
import numpy as np | |
cimport numpy as np | |
import liblas | |
DTYPE = np.float64 | |
ctypedef np.float64_t DTYPE_t | |
cdef extern from "liblas/capi/liblas.h": | |
struct LASReaderHS: | |
pass | |
struct LASPointHS: | |
pass | |
struct LASHeaderHS: | |
pass | |
LASReaderHS* LASReader_Create(const char* fname) | |
LASPointHS* LASReader_GetNextPoint(const LASReaderHS* hReader) | |
void LASReader_Destroy(LASReaderHS* hReader) | |
LASHeaderHS* LASReader_GetHeader(const LASReaderHS* hReader) | |
unsigned int LASHeader_GetPointRecordsCount(const LASHeaderHS* hHeader) | |
double LASPoint_GetX(const LASPointHS* hPoint) | |
double LASPoint_GetY(const LASPointHS* hPoint) | |
double LASPoint_GetZ(const LASPointHS* hPoint) | |
@cython.boundscheck(False) | |
def loadlastonumpy(fname): | |
f = LASReader_Create(fname) | |
h = LASReader_GetHeader(f) | |
cdef unsigned int cnt = LASHeader_GetPointRecordsCount(h) | |
cdef np.ndarray[DTYPE_t, ndim=2] ptdata = np.zeros( (cnt, 3), dtype=DTYPE) | |
cdef DTYPE_t px, py, pz | |
cdef unsigned int i | |
for i in range(cnt): | |
p = LASReader_GetNextPoint(f) | |
if p is NULL: | |
LASReader_Destroy(f) | |
raise Exception('Not enough point data') | |
px = LASPoint_GetX(p) | |
py = LASPoint_GetY(p) | |
pz = LASPoint_GetZ(p) | |
ptdata[i,<unsigned int>0] = px | |
ptdata[i,<unsigned int>1] = py | |
ptdata[i,<unsigned int>2] = pz | |
LASReader_Destroy(f) | |
return ptdata |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment