Skip to content

Create constant raster

create_constant_raster(constant_value, template_raster=None, coord_west=None, coord_north=None, coord_east=None, coord_south=None, target_epsg=None, target_pixel_size=None, raster_width=None, raster_height=None, nodata_value=None)

Create a constant raster based on a user-defined value.

Provide 3 methods for raster creation: 1. Set extent and coordinate system based on a template raster. 2. Set extent from origin, based on the western and northern coordinates and the pixel size. 3. Set extent from bounds, based on western, northern, eastern and southern points.

Always provide values for height and width for the last two options, which correspond to the desired number of pixels for rows and columns.

Parameters:

Name Type Description Default
constant_value Number

The constant value to use in the raster.

required
template_raster Optional[DatasetReader]

An optional raster to use as a template for the output.

None
coord_west Optional[Number]

The western coordinate of the output raster in [m].

None
coord_east Optional[Number]

The eastern coordinate of the output raster in [m].

None
coord_south Optional[Number]

The southern coordinate of the output raster in [m].

None
coord_north Optional[Number]

The northern coordinate of the output raster in [m].

None
target_epsg Optional[int]

The EPSG code for the output raster.

None
target_pixel_size Optional[int]

The pixel size of the output raster.

None
raster_width Optional[int]

The width of the output raster.

None
raster_height Optional[int]

The height of the output raster.

None
nodata_value Optional[Number]

The nodata value of the output raster.

None

Returns:

Type Description
Tuple[ndarray, dict]

A tuple containing the output raster as a NumPy array and updated metadata.

Raises:

Type Description
InvalidParameterValueException

Provide invalid input parameter.

Source code in eis_toolkit/raster_processing/create_constant_raster.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
@beartype
def create_constant_raster(  # type: ignore[no-any-unimported]
    constant_value: Number,
    template_raster: Optional[rasterio.io.DatasetReader] = None,
    coord_west: Optional[Number] = None,
    coord_north: Optional[Number] = None,
    coord_east: Optional[Number] = None,
    coord_south: Optional[Number] = None,
    target_epsg: Optional[int] = None,
    target_pixel_size: Optional[int] = None,
    raster_width: Optional[int] = None,
    raster_height: Optional[int] = None,
    nodata_value: Optional[Number] = None,
) -> Tuple[np.ndarray, dict]:
    """Create a constant raster based on a user-defined value.

    Provide 3 methods for raster creation:
    1. Set extent and coordinate system based on a template raster.
    2. Set extent from origin, based on the western and northern coordinates and the pixel size.
    3. Set extent from bounds, based on western, northern, eastern and southern points.

    Always provide values for height and width for the last two options, which correspond to
    the desired number of pixels for rows and columns.

    Args:
        constant_value: The constant value to use in the raster.
        template_raster: An optional raster to use as a template for the output.
        coord_west: The western coordinate of the output raster in [m].
        coord_east: The eastern coordinate of the output raster in [m].
        coord_south: The southern coordinate of the output raster in [m].
        coord_north: The northern coordinate of the output raster in [m].
        target_epsg: The EPSG code for the output raster.
        target_pixel_size: The pixel size of the output raster.
        raster_width: The width of the output raster.
        raster_height: The height of the output raster.
        nodata_value: The nodata value of the output raster.

    Returns:
        A tuple containing the output raster as a NumPy array and updated metadata.

    Raises:
        InvalidParameterValueException: Provide invalid input parameter.
    """

    if template_raster is not None:
        out_array, out_meta = _create_constant_raster_from_template(constant_value, template_raster, nodata_value)

    elif all(coords is not None for coords in [coord_west, coord_east, coord_south, coord_north]):
        if raster_height <= 0 or raster_width <= 0:
            raise InvalidParameterValueException("Invalid raster extent provided.")
        if not check_minmax_position((coord_west, coord_east) or not check_minmax_position((coord_south, coord_north))):
            raise InvalidParameterValueException("Invalid coordinate values provided.")

        out_array, out_meta = _create_constant_raster_from_bounds(
            constant_value,
            coord_west,
            coord_north,
            coord_east,
            coord_south,
            target_epsg,
            raster_width,
            raster_height,
            nodata_value,
        )

    elif all(coords is not None for coords in [coord_west, coord_north]) and all(
        coords is None for coords in [coord_east, coord_south]
    ):
        if raster_height <= 0 or raster_width <= 0:
            raise InvalidParameterValueException("Invalid raster extent provided.")
        if target_pixel_size <= 0:
            raise InvalidParameterValueException("Invalid pixel size.")

        out_array, out_meta = _create_constant_raster_from_origin(
            constant_value,
            coord_west,
            coord_north,
            target_epsg,
            target_pixel_size,
            raster_width,
            raster_height,
            nodata_value,
        )

    else:
        raise InvalidParameterValueException("Suitable parameter values were not provided for any of the 3 methods.")

    constant_value = cast_scalar_to_int(constant_value)
    nodata_value = cast_scalar_to_int(out_meta["nodata"])

    if isinstance(constant_value, int) and isinstance(nodata_value, int):
        target_dtype = np.result_type(get_min_int_type(constant_value), get_min_int_type(nodata_value))
        out_array = out_array.astype(target_dtype)
        out_meta["dtype"] = out_array.dtype
    elif isinstance(constant_value, int) and isinstance(nodata_value, float):
        out_array = out_array.astype(get_min_int_type(constant_value))
        out_meta["dtype"] = np.float64.__name__
    elif isinstance(constant_value, float):
        out_array = out_array.astype(np.float64)
        out_meta["dtype"] = out_array.dtype

    return out_array, out_meta