Skip to content

Reclassify raster

reclassify_with_defined_intervals(raster, interval_size, bands=None)

Classify raster with defined intervals.

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
interval_size int

The number of units in each interval.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with defined intervals.

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

InvalidParameterValueException

Interval size is less than 1.

Source code in eis_toolkit/raster_processing/reclassify.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 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
@beartype
def reclassify_with_defined_intervals(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader,
    interval_size: int,
    bands: Optional[Sequence[int]] = None,
) -> Tuple[np.ndarray, dict]:
    """Classify raster with defined intervals.

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        interval_size: The number of units in each interval.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with defined intervals.
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
        InvalidParameterValueException: Interval size is less than 1.
    """

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    if interval_size < 1:
        raise InvalidParameterValueException("Interval size must be 1 or more.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_defined_intervals(band_data, interval_size)

    return out_image, out_meta

reclassify_with_equal_intervals(raster, number_of_intervals, bands=None)

Classify raster with equal intervals.

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
number_of_intervals int

The number of intervals.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with equal intervals.

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

InvalidParameterValueException

Number of intervals is less than 2.

Source code in eis_toolkit/raster_processing/reclassify.py
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
@beartype
def reclassify_with_equal_intervals(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader,
    number_of_intervals: int,
    bands: Optional[Sequence[int]] = None,
) -> Tuple[np.ndarray, dict]:
    """Classify raster with equal intervals.

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        number_of_intervals: The number of intervals.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with equal intervals.
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
        InvalidParameterValueException: Number of intervals is less than 2.
    """

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    if number_of_intervals < 2:
        raise InvalidParameterValueException("Number of intervals must be 2 or more.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_equal_intervals(band_data, number_of_intervals)

    return out_image, out_meta

reclassify_with_geometrical_intervals(raster, number_of_classes, bands=None)

Classify raster with geometrical intervals.

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
number_of_classes int

The number of classes. The true number of classes is at most double the amount, depending how symmetrical the input data is.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with geometrical intervals.

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

InvalidParameterValueException

Number of classes is less than 2.

Source code in eis_toolkit/raster_processing/reclassify.py
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@beartype
def reclassify_with_geometrical_intervals(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader, number_of_classes: int, bands: Optional[Sequence[int]] = None
) -> Tuple[np.ndarray, dict]:
    """Classify raster with geometrical intervals.

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        number_of_classes: The number of classes. The true number of classes is at most double the amount,
            depending how symmetrical the input data is.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with geometrical intervals.
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
        InvalidParameterValueException: Number of classes is less than 2.
    """

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    if number_of_classes < 2:
        raise InvalidParameterValueException("Number of classes must be 2 or more.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()
    nodata_value = raster.nodata

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_geometrical_intervals(band_data, number_of_classes, nodata_value)

    return out_image, out_meta

reclassify_with_manual_breaks(raster, breaks, bands=None)

Classify raster with manual breaks.

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
breaks Sequence[int]

List of break values for the classification.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with manual breaks.

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

Source code in eis_toolkit/raster_processing/reclassify.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
@beartype
def reclassify_with_manual_breaks(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader,
    breaks: Sequence[int],
    bands: Optional[Sequence[int]] = None,
) -> Tuple[np.ndarray, dict]:
    """Classify raster with manual breaks.

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        breaks: List of break values for the classification.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with manual breaks.
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
    """
    # Add check for input breaks at some point?

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_manual_breaks(band_data, breaks)

    return out_image, out_meta

reclassify_with_natural_breaks(raster, number_of_classes, bands=None)

Classify raster with natural breaks (Jenks Caspall).

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
number_of_classes int

The number of classes.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with natural breaks (Jenks Caspall).

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

InvalidParameterValueException

Number of classes is less than 2.

Source code in eis_toolkit/raster_processing/reclassify.py
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
@beartype
def reclassify_with_natural_breaks(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader,
    number_of_classes: int,
    bands: Optional[Sequence[int]] = None,
) -> Tuple[np.ndarray, dict]:
    """Classify raster with natural breaks (Jenks Caspall).

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        number_of_classes: The number of classes.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with natural breaks (Jenks Caspall).
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
        InvalidParameterValueException: Number of classes is less than 2.
    """

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    if number_of_classes < 2:
        raise InvalidParameterValueException("Number of classes must be 2 or more.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_natural_breaks(band_data, number_of_classes)

    return out_image, out_meta

reclassify_with_quantiles(raster, number_of_quantiles, bands=None)

Classify raster with quantiles.

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
number_of_quantiles int

The number of quantiles.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with quantiles.

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

InvalidParameterValueException

Number of quantiles is less than 2.

Source code in eis_toolkit/raster_processing/reclassify.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
@beartype
def reclassify_with_quantiles(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader,
    number_of_quantiles: int,
    bands: Optional[Sequence[int]] = None,
) -> Tuple[np.ndarray, dict]:
    """Classify raster with quantiles.

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        number_of_quantiles: The number of quantiles.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with quantiles.
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
        InvalidParameterValueException: Number of quantiles is less than 2.
    """

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    if number_of_quantiles < 2:
        raise InvalidParameterValueException("Number of quantiles must be 2 or more.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_quantiles(band_data, number_of_quantiles)

    return out_image, out_meta

reclassify_with_standard_deviation(raster, number_of_intervals, bands=None)

Classify raster with standard deviation.

If bands are not given, all bands are used for classification.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be classified.

required
number_of_intervals int

The number of intervals.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None

Returns:

Type Description
ndarray

Raster data classified with standard deviation.

dict

Raster metadata.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

InvalidParameterValueException

Number of intervals is less than 2.

Source code in eis_toolkit/raster_processing/reclassify.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
@beartype
def reclassify_with_standard_deviation(  # type: ignore[no-any-unimported]
    raster: rasterio.io.DatasetReader,
    number_of_intervals: int,
    bands: Optional[Sequence[int]] = None,
) -> Tuple[np.ndarray, dict]:
    """Classify raster with standard deviation.

    If bands are not given, all bands are used for classification.

    Args:
        raster: Raster to be classified.
        number_of_intervals: The number of intervals.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.

    Returns:
        Raster data classified with standard deviation.
        Raster metadata.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
        InvalidParameterValueException: Number of intervals is less than 2.
    """

    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    if number_of_intervals < 2:
        raise InvalidParameterValueException("Number of intervals must be 2 or more.")

    out_image = np.empty((len(bands), raster.height, raster.width))
    out_meta = raster.meta.copy()

    for i, band in enumerate(bands):
        band_data = raster.read(band)
        out_image[i] = _reclassify_with_standard_deviation(band_data, number_of_intervals)

    return out_image, out_meta