def set_header(self, header): '''Sets the laspy.header.Header for the file. If the file is in \ append mode, the header will be overwritten in the file.''' # append mode if self._mode == "w+": self._writer.set_header(header) return True raise util.LaspyException("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:`laspy.util.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, util.Point): raise util.LaspyException('cannot write %s, it must ' 'be of type laspy.point.Point' % pt) if self._mode != "r": # core.las.LASWriter_WritePoint(self.handle, pt.handle) pass
def open(self): '''Open the file for processing, called by __init__ ''' if self._mode in ("r", "r-"): if not os.path.exists(self.filename): raise OSError("No such file or directory: '%s'" % self.filename) ## Make sure we have a header if self._header is None: self._reader = base.Reader(self.filename, mode=self._mode) self._header = self._reader.get_header() else: raise util.LaspyException( "Headers must currently be stored in the file, you provided: " + str(self._header)) if self.in_srs: self._reader.SetInputSRS(self.in_srs) if self.out_srs: self._reader.SetOutputSRS(self.out_srs) ## Wire up API for extra dimensions if self._reader.extra_dimensions: for dimension in self._reader.extra_dimensions: dimname = dimension.name.decode().replace( "\x00", "").replace(" ", "_").lower() self.addProperty(dimname) elif self._mode == 'rw': if self._header is None: self._writer = base.Writer(self.filename, mode=self._mode) self._reader = self._writer self._header = self._reader.get_header() ## Wire up API for any extra Dimensions if self._writer.extra_dimensions: for dimension in self._writer.extra_dimensions: dimname = dimension.name.decode().replace( "\x00", "").replace(" ", "_").lower() self.addProperty(dimname) else: raise util.LaspyException( "Headers must currently be stored in the file, you provided: " + str(self._header)) elif self._mode == 'w': if self._header is None: raise util.LaspyException( "Creation of a file in write mode requires a header object." ) if isinstance(self._header, header.HeaderManager): vlrs = self._header.vlrs evlrs = self._header.evlrs self._header = self._header.copy() if self._vlrs: self._vlrs.extend(vlrs) else: self._vlrs = vlrs if self._evlrs: self._evlrs.extend(evlrs) else: self._evlrs = evlrs self._writer = base.Writer(self.filename, mode="w", header=self._header, vlrs=self._vlrs, evlrs=self._evlrs) self._reader = self._writer ## Wire up API for any extra Dimensions if self._writer.extra_dimensions: for dimension in self._writer.extra_dimensions: dimname = dimension.name.decode().replace( "\x00", "").replace(" ", "_").lower() self.addProperty(dimname) elif self._mode == 'w+': raise NotImplementedError else: raise util.LaspyException("Mode %s not supported" % self._mode) if self._reader.compressed and self._mode not in ("r", "r-"): raise NotImplementedError( "Compressed files / buffer objects can only be opened in mode 'r' for now" )
def read(self, index, nice=True): '''Reads the point at the given index''' if self._reader.get_pointrecordscount() >= index: return self._reader.get_point(index, nice) else: raise util.LaspyException("Index greater than point records count")
def assertWriteMode(self): if self._mode == "r": raise util.LaspyException("File is not opened in a write mode.")