def rasterize(in_lyr_path, out_raster_path, template_path): """Rasterizing an input Args: in_lyr_path ([type]): [description] out_raster_ ([type]): [description] template_path ([type]): [description] """ log = Logger('VBETRasterize') ds_path, lyr_path = VectorBase.path_sorter(in_lyr_path) progbar = ProgressBar(100, 50, "Rasterizing ") with rasterio.open(template_path) as raster: t = raster.transform raster_bounds = raster.bounds def poly_progress(progress, _msg, _data): progbar.update(int(progress * 100)) # Rasterize the features (roads, rail etc) and calculate a raster of Euclidean distance from these features progbar.update(0) # Rasterize the polygon to a temporary file with TempRaster('vbet_rasterize') as tempfile: log.debug('Temporary file: {}'.format(tempfile.filepath)) gdal.Rasterize( tempfile.filepath, ds_path, layers=[lyr_path], xRes=t[0], yRes=t[4], burnValues=1, outputType=gdal.GDT_Int16, creationOptions=['COMPRESS=LZW'], # outputBounds --- assigned output bounds: [minx, miny, maxx, maxy] outputBounds=[ raster_bounds.left, raster_bounds.bottom, raster_bounds.right, raster_bounds.top ], callback=poly_progress) progbar.finish() # Now mask the output correctly mask_rasters_nodata(tempfile.filepath, template_path, out_raster_path)
def hand_rasterize(in_lyr_path: str, template_dem_path: str, out_raster_path: str): log = Logger('hand_rasterize') ds_path, lyr_path = VectorBase.path_sorter(in_lyr_path) g = gdal.Open(template_dem_path) geo_t = g.GetGeoTransform() width, height = g.RasterXSize, g.RasterYSize xmin = min(geo_t[0], geo_t[0] + width * geo_t[1]) xmax = max(geo_t[0], geo_t[0] + width * geo_t[1]) ymin = min(geo_t[3], geo_t[3] + geo_t[-1] * height) ymax = max(geo_t[3], geo_t[3] + geo_t[-1] * height) # Close our dataset g = None progbar = ProgressBar(100, 50, "Rasterizing for HAND") def poly_progress(progress, _msg, _data): progbar.update(int(progress * 100)) # https://gdal.org/programs/gdal_rasterize.html # https://gdal.org/python/osgeo.gdal-module.html#RasterizeOptions gdal.Rasterize( out_raster_path, ds_path, layers=[lyr_path], height=height, width=width, burnValues=1, outputType=gdal.GDT_CFloat32, creationOptions=['COMPRESS=LZW'], # outputBounds --- assigned output bounds: [minx, miny, maxx, maxy] outputBounds=[xmin, ymin, xmax, ymax], callback=poly_progress) progbar.finish() # Rasterize the features (roads, rail etc) and calculate a raster of Euclidean distance from these features progbar.update(0)
def raster_warp(inraster: str, outraster: str, epsg, clip=None, warp_options: dict = {}): """ Reproject a raster to a different coordinate system. :param inraster: Input dataset :param outraster: Output dataset :param epsg: Output spatial reference EPSG identifier :param log: Log file object :param clip: Optional Polygon dataset to clip the output. :param warp_options: Extra GDALWarpOptions. :return: None https://gdal.org/python/osgeo.gdal-module.html#WarpOptions """ log = Logger('Raster Warp') if os.path.isfile(outraster): log.info( 'Skipping raster warp because output exists {}'.format(outraster)) return None log.info('Raster Warp input raster {}'.format(inraster)) log.info('Raster Warp output raster {}'.format(outraster)) log.info('Output spatial reference EPSG: {}'.format(epsg)) output_folder = os.path.dirname(outraster) if not os.path.isdir(output_folder): os.mkdir(output_folder) warpvrt = os.path.join(os.path.dirname(outraster), 'temp_gdal_warp_output.vrt') log.info('Performing GDAL warp to temporary VRT file.') if clip: log.info('Clipping to polygons using {}'.format(clip)) clip_ds, clip_layer = VectorBase.path_sorter(clip) warp_options_obj = gdal.WarpOptions(dstSRS='EPSG:{}'.format(epsg), format='vrt', cutlineDSName=clip_ds, cutlineLayer=clip_layer, cropToCutline=True, **warp_options) else: warp_options_obj = gdal.WarpOptions(dstSRS='EPSG:{}'.format(epsg), format='vrt', **warp_options) ds = gdal.Warp(warpvrt, inraster, options=warp_options_obj) log.info( 'Using GDAL translate to convert VRT to compressed raster format.') translateoptions = gdal.TranslateOptions( gdal.ParseCommandLine("-of Gtiff -co COMPRESS=DEFLATE")) gdal.Translate(outraster, ds, options=translateoptions) # Cleanup the temporary VRT file os.remove(warpvrt) if ds: log.info('Process completed successfully.') else: log.error('Error running GDAL Warp')