def write(self, data, pixelbuffer=0): if self.output.data_type == "vector": write_vector(self, self.tile.config, data, pixelbuffer=pixelbuffer, overwrite=self.config.overwrite) elif self.output.data_type == "raster": write_raster(self, self.tile.profile(pixelbuffer=pixelbuffer), data, pixelbuffer=pixelbuffer)
def execute(self, tile, overwrite=True): """ Processes and saves tile. Returns a tuple of three values: - Tile ID: zoom, row, col - status: "exists", "empty", "failed" or "processed" - error message: optional or None """ # Do nothing if tile exists or overwrite is turned off. if not overwrite and tile.exists(): return tile.id, "exists", None try: new_process = imp.load_source(self.process_name + "Process", self.config.process_file) tile_process = new_process.Process(config=self.config, tile=tile, params=self.config.at_zoom(tile.zoom)) except: return tile.id, "failed", traceback.print_exc() # Generate tile using the user defined process. if not self.config.baselevel or (tile.zoom == self.config.baselevel["zoom"]): try: result = tile_process.execute() except: return tile.id, "failed", traceback.print_exc() finally: tile_process = None message = None if result: if result == "empty": status = "empty" else: status = "custom" message = result else: status = "processed" return tile.id, status, message # If baselevel interpolation is activated, generate from neighbor zooms. else: if tile.zoom < self.config.baselevel["zoom"]: # determine tiles from next zoom level # TODO create option for on demand processing if subtile is not # available. # create temporary VRT and create new tile from resampled # subtiles. children_paths = [child.path for child in tile.get_children() if child.exists()] if len(children_paths) == 0: return tile.id, "empty", None temp_vrt = NamedTemporaryFile() build_vrt = "gdalbuildvrt %s %s > /dev/null" % (temp_vrt.name, " ".join(children_paths)) try: os.system(build_vrt) assert os.path.isfile(temp_vrt.name) except: build_vrt = "gdalbuildvrt %s %s" % (temp_vrt.name, " ".join(children_paths)) os.system(build_vrt) return tile.id, "failed", "GDAL VRT building" try: assert os.path.isfile(temp_vrt.name) bands = tuple( read_raster_window(temp_vrt.name, tile, resampling=self.config.baselevel["resampling"]) ) except: return tile.id, "failed", traceback.print_exc() try: write_raster(tile_process, self.output.profile, bands) return tile.id, "processed", None except: return tile.id, "failed", traceback.print_exc() elif tile.zoom > self.config.baselevel["zoom"]: # determine tiles from previous zoom level # check if tiles exist and if not, execute subtiles parent = tile.get_parent() if not overwrite and parent.exists(): # LOGGER.info((tile.id, "exists", None)) pass else: pass if not parent.exists(): # TODO create option for on demand processing return tile.id, "empty", "source tile does not exist" try: bands = tuple(read_raster_window(parent.path, tile, resampling=self.config.baselevel["resampling"])) write_raster(tile_process, self.output.profile, bands) return tile.id, "processed", None except: return tile.id, "failed", traceback.print_exc()