Skip to content

Commit

Permalink
AddFlipRaster
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderJuestel committed Mar 4, 2024
1 parent 7ec87b7 commit cc810de
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pyheatdemand/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
______
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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)

Expand Down

0 comments on commit cc810de

Please sign in to comment.