def close(self): """Closes the LAS file """ if self.mode == 0: try: files['read'][self.filename] -= 1 if files['read'][self.filename] == 0: files['read'].pop(self.filename) except KeyError: raise core.LASException( "File %s was not found in accounting dictionary!" % self.filename) core.las.LASReader_Destroy(self.handle) else: try: files['append'].remove(self.filename) except: files['write'].remove(self.filename) core.las.LASWriter_Destroy(self.handle) if self.ownheader: core.las.LASHeader_Destroy(self._header) self._header = None self.handle = None
def set_dataformatid(self, value): """Sets the point format value It can only be 1 (for 1.1 compatible) or 0 (for 1.0 compatible). """ if value not in [3, 2,1,0]: raise core.LASException("Format ID must be 3, 2, 1, or 0") return core.las.LASHeader_SetDataFormatId(self.handle, value)
def set_header(self, header): # append mode if mode == 2: core.las.LASWriter_Destroy(self.handle) self.handle = core.las.LASWriter_Create(self.handle, header, 2) self._header = header return True raise core.LASException("The header can only be set after file creation for files in append mode")
def __init__(self, filename, header=None, mode='r'): self.filename = os.path.abspath(filename) self._header = header self.handle = None self._mode = mode # print self.filename # print sys.stderr.write('%s %s %s'%(files, self.filename, mode)) # sys.stderr.flush() #check in the registry if we already have the file open if mode=='r': for f in files['write'] + files['append']: if f == self.filename: raise core.LASException("File %s is already open for write. Close the file or delete the reference to it" % filename) else: for f in files['read']: if f == self.filename: raise core.LASException("File %s is already open for read. Close the file or delete the reference to it" % filename) self.open()
def get_xmlsummary(self): """Returns an XML string summarizing all of the points in the reader .. note:: This method will reset the reader's read position to the 0th point to summarize the entire file, and it will again reset the read position to the 0th point upon completion.""" if self.mode != 0: raise core.LASException("file must be in read mode, not append or write mode to provide xml summary") return core.las.LASReader_GetSummaryXML(self.handle)
def __eq__(self, other): """Test GUID for equality against another :obj:`liblas.guid.GUID` instance :param other: The :obj:`liblas.guid.GUID` instance to test against """ if isinstance(other, GUID): return bool(core.las.LASGuid_Equals(self.handle, other.handle)) raise core.LASException("GUID can only be compared to other " "GUIDs, not %s" % type(other))
def set_header(self, header): """Sets the liblas.header.Header for the file. If the file is in \ append mode, the header will be overwritten in the file.""" # append mode if mode == 2: core.las.LASWriter_Destroy(self.handle) self.handle = core.las.LASWriter_Create(self.handle, header, 2) self._header = core.las.LASHeader_Copy(header.handle) return True raise core.LASException("The header can only be set " "after file creation for files in append mode")
def write(self, pt): """Writes the point to the file if it is append or write mode. LAS files are written sequentially starting from the first point (in pure write mode) or from the last point that exists (in append mode). :param pt: The point to write. :type pt: :obj:`liblas.point.Point` instance to write .. note:: At this time, it is not possible to duck-type point objects and have them be written into the LAS file (from say numpy or something). You have to take care of this adaptation yourself. """ if not isinstance(pt, point.Point): raise core.LASException('cannot write %s, it must ' 'be of type liblas.point.Point' % pt) if self.mode == 1 or self.mode == 2: core.las.LASWriter_WritePoint(self.handle, pt.handle)
def write(self, pt): if not isinstance(pt, point.Point): raise core.LASException('cannot write %s, it must be of type liblas.point.Point' % pt) if self.mode == 1 or self.mode == 2: core.las.LASWriter_WritePoint(self.handle, pt.handle)
def set_dataformatid(self, value): if value not in [3, 2, 1, 0]: raise core.LASException("Format ID must be 3, 2, 1, or 0") return core.las.LASHeader_SetDataFormatId(self.handle, value)
def __eq__(self, other): if isinstance(other, GUID): return bool(core.las.LASGuid_Equals(self.handle, other.handle)) raise core.LASException( "GUID can only be compared to other GUIDs, not %s" % type(other))
def __init__(self, filename, header=None, mode='r', in_srs=None, out_srs=None): """Instantiate a file object to represent an LAS file. :arg filename: The filename to open :keyword header: A header open the file with :type header: an :obj:`liblas.header.header.Header` instance :keyword mode: "r" for read, "w" for write, and "w+" for append :type mode: string :keyword in_srs: Input SRS to override the existing file's SRS with :type in_srs: an :obj:`liblas.srs.SRS` instance :keyword out_srs: Output SRS to reproject points on-the-fly to as \ they are read/written. :type out_srs: an :obj:`liblas.srs.SRS` instance .. note:: To open a file in write mode, you must provide a liblas.header.Header instance which will be immediately written to the file. If you provide a header instance in read mode, the values of that header will be used in place of those in the actual file. .. note:: If a file is open for write, it cannot be opened for read and vice versa. >>> from liblas import file >>> f = file.File('file.las', mode='r') >>> for p in f: ... print 'X,Y,Z: ', p.x, p.y, p.z >>> h = f.header >>> f2 = file.File('file2.las', header=h) >>> for p in f: ... f2.write(p) >>> f2.close() """ self.filename = os.path.abspath(filename) self._header = None self.ownheader = True # import pdb;pdb.set_trace() if header != None: self.ownheader = False self._header = header.handle self.handle = None self._mode = mode.lower() self.in_srs = in_srs self.out_srs = out_srs #check in the registry if we already have the file open if mode == 'r': for f in files['write'] + files['append']: if f == self.filename: raise core.LASException("File %s is already open for " "write. Close the file or delete " "the reference to it" % filename) else: # we're in some kind of write mode, and if we already have the # file open, complain to the user. for f in files['read'].keys() + files['append'] + files['write']: if f == self.filename: raise core.LASException("File %s is already open. " "Close the file or delete the " "reference to it" % filename) self.open()