3

New to laspy and encountering an error. The first code block describes the file header, the second (In[24]) fails to open the file with laspy:

In [21]:
import numpy
import laspy
from laspy.file import File

In [22]:
import struct
headerstruct = ( ('filesig', 4,'c',4) ,
         . . .  omitted for brevity, see result for fields . . . 
                   ('zmin'         , 8,'d',1) )

def parseHeader(filename, verbose=True):
    # courtesy Phil Wilkes
    fh = open(filename,'rb')
    header = {'infile':filename}
    with open(filename, 'rb') as fh:
        for i in headerstruct:
            if i[2] == 'c':
                value = fh.read(i[1])
            elif i[3] > 1:    
                value = struct.unpack( '=' + str(i[3]) + i[2] , fh.read(i[1]) )
            else:
                value = struct.unpack( '=' + i[2] , fh.read(i[1]) )[0]

            if verbose:
                print i[0] + '\t', i[2] + '\t', value

            header[i[0]] = value

In [23]:
filename = r'./sf_shared/40095_Flightline_002_Z.las'
parseHeader(filename, verbose=True)
filesig   c   LASF
filesourceid  H   0
reserved  H   0
guid1 L   0
guid2 H   0
guid3 H   0
guid4 B   (0, 0, 0, 0, 0, 0, 0, 0)
vermajor  B   1
verminor  B   2
sysid c   
gensoftware   c   TerraScan
fileday   H   349
fileyear  H   2011
headersize    H   227
offset    L   229
numvlrecords  L   0
pointformat   B   1
pointreclen   H   28
numptrecords  L   8917629
numptbyreturn L   (6711217, 2002830, 196359, 7135, 88)
xscale    d   0.01
yscale    d   0.01
zscale    d   0.01
xoffset   d   -0.0
yoffset   d   -0.0
zoffset   d   -0.0
xmax  d   606205.36
xmin  d   605000.63
ymax  d   6599629.15
ymin  d   6596922.71
zmax  d   129.27
zmin  d   43.74
In [24]:
infile = File(filename, mode='r')  #laspy.file.File
---------------------------------------------------------------------------
LaspyException                            Traceback (most recent call last)
<ipython-input-24-19025bf517df> in <module>()
----> 1 infile = File(filename, mode='r')

/usr/local/lib/python2.7/dist-packages/laspy/file.pyc in __init__(self, filename, header, vlrs, mode, in_srs, out_srs, evlrs)
     60         self.out_srs = out_srs
     61 
---> 62         self.open()
     63 
     64     def open(self):

/usr/local/lib/python2.7/dist-packages/laspy/file.pyc in open(self)
     71             ## Make sure we have a header
     72             if self._header == None:
---> 73                 self._reader = base.Reader(self.filename,mode= self._mode)
     74                 self._header = self._reader.get_header()
     75             else:

/usr/local/lib/python2.7/dist-packages/laspy/base.pyc in __init__(self, filename, mode, header, vlrs, evlrs)
    151         self.padded = False
    152         if self.mode in ("r", "rw"):
--> 153             self.setup_read_write(vlrs, evlrs)
    154             return
    155 

/usr/local/lib/python2.7/dist-packages/laspy/base.pyc in setup_read_write(self, vlrs, evlrs)
    162     def setup_read_write(self, vlrs, evlrs):
    163         self.data_provider.open("r+b")
--> 164         self.data_provider.map()
    165         self.header_format = Format("h" + self.grab_file_version())
    166         self.get_header(self.grab_file_version())

/usr/local/lib/python2.7/dist-packages/laspy/base.pyc in map(self)
     93                 raise LaspyException("Invalid Mode: " + str(self.mode))
     94         except(Exception):
---> 95             raise LaspyException("Error mapping file.")
     96 
     97     def remap(self,flush = True, point_map = False):

LaspyException: Error mapping file.

Unfortunately, the laspy documentation leaves you at "When a file is opened in read mode, laspy first reads the header, processes any VLR and EVLR records, and then maps the point records with numpy. If no errors are produced when calling the File constructor, you’re ready to read data!" But it doesn't suggest what to do if there are errors!

Can you see an error in my code or suggest the source of the problem?

  • Unfortunately laspy eats all exceptions with that except(Exception). Assuming that this code is (or can be) in a stand-alone script, can you run it with the python debugger enabled to halt execution when that exception gets thrown? Something like python -m pdb myscript.py should work. When you're in the debugger you can inspect variables by printing them: p self.fileref. I'd start by seeing if the fileref is closed, p self.fileref.closed, and what the filename is, p self.filename. I don't know what the issue is but that's a way to do a bit of debugging yourself. – Pete Gadomski Feb 7 '15 at 0:16
  • Thanks, Pete, I will update, even though I haven't been making much use of laspy lately. – J Kelly Mar 1 '16 at 13:39
2

Since this question was asked, that cryptic error message from laspy was improved. Update to the latest laspy and try again, you should get a more informative error message.

Not the answer you're looking for? Browse other questions tagged or ask your own question.