From cc810de5204ec317ea348d471426b2ffd9533286 Mon Sep 17 00:00:00 2001 From: AlexanderJuestel Date: Mon, 4 Mar 2024 17:09:28 +0100 Subject: [PATCH] AddFlipRaster --- pyheatdemand/processing.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pyheatdemand/processing.py b/pyheatdemand/processing.py index f6d381d..97ae1f7 100644 --- a/pyheatdemand/processing.py +++ b/pyheatdemand/processing.py @@ -700,7 +700,8 @@ def rasterize_gdf_hd(gdf_hd: gpd.GeoDataFrame, path_out: str, crs: Union[str, pyproj.crs.crs.CRS] = 'EPSG:3034', xsize: int = 100, - ysize: int = 100): + ysize: int = 100, + flip_raster : bool = True): """Rasterize Heat Demand GeoDataFrame and save as raster. Parameters @@ -715,6 +716,8 @@ def rasterize_gdf_hd(gdf_hd: gpd.GeoDataFrame, Cell size of the output raster, e.g. ``xsize=100``. ysize : int, default: ``100`` Cell size of the output raster, e.g. ``ysize=100``. + flip_raster : bool, default: ``True`` + Boolean value to flip the raster. Raises ______ @@ -747,6 +750,10 @@ def rasterize_gdf_hd(gdf_hd: gpd.GeoDataFrame, if not isinstance(ysize, int): raise TypeError('The ysize must be provided as int') + # Checking that the flip_raster variable is of type bool + if not isinstance(flip_raster, int): + raise TypeError('The flip_raster value must be provided as bool') + # Creating array with the length of polygons in x and y direction x = np.arange(gdf_hd.total_bounds[0], gdf_hd.total_bounds[2], xsize) y = np.arange(gdf_hd.total_bounds[1], gdf_hd.total_bounds[3], ysize) @@ -755,7 +762,10 @@ def rasterize_gdf_hd(gdf_hd: gpd.GeoDataFrame, matrix = np.zeros(len(y) * len(x)).reshape(len(y), len(x)) # Creating transform - transform = rasterio.transform.from_origin(x[0], y[1], xsize, -ysize) + if flip_raster: + transform = rasterio.transform.from_origin(x[0], y[-1], xsize, ysize) + else: + transform = rasterio.transform.from_origin(x[0], y[0], xsize, -ysize) # Saving mask raster with rasterio.open( @@ -777,7 +787,7 @@ def rasterize_gdf_hd(gdf_hd: gpd.GeoDataFrame, meta = rst.meta.copy() meta.update(compress='lzw') - # Rasterisation of the quadratic-polygon-shapefile using the rasterize-function from rasterio + # Rasterization of the quadratic-polygon-shapefile using the rasterize-function from rasterio with rasterio.open(path_out, 'w+', **meta) as out: out_arr = out.read(1)