Skip to content

Instantly share code, notes, and snippets.

@AsgerPetersen
Forked from gcasey/lasreader.pxd
Created October 6, 2015 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsgerPetersen/7e2f3a4e557c0151d028 to your computer and use it in GitHub Desktop.
Save AsgerPetersen/7e2f3a4e557c0151d028 to your computer and use it in GitHub Desktop.
Reading LAS file efficiently into numpy using cython
#%%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