Commit 6e08bde8 authored by Claude Paroz's avatar Claude Paroz
Browse files

Added RasterSource/GDALBand GDAL objects

Based on Daniel Wiesmann's raster branch. Thanks Daniel Wiesmann
and Tim Graham for the reviews. Refs #23804.
parent 9fecb86a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ recursive-include django/contrib/formtools/tests/templates *
recursive-include django/contrib/formtools/tests/wizard/wizardtests/templates *
recursive-include django/contrib/flatpages/fixtures *
recursive-include django/contrib/flatpages/tests/templates *
recursive-include django/contrib/gis/gdal/tests/data *
recursive-include django/contrib/gis/static *
recursive-include django/contrib/gis/templates *
recursive-include django/contrib/gis/tests/data *
+1 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ try:
    from django.contrib.gis.gdal.driver import Driver  # NOQA
    from django.contrib.gis.gdal.datasource import DataSource  # NOQA
    from django.contrib.gis.gdal.libgdal import gdal_version, gdal_full_version, GDAL_VERSION  # NOQA
    from django.contrib.gis.gdal.raster.source import GDALRaster  # NOQA
    from django.contrib.gis.gdal.srs import SpatialReference, CoordTransform  # NOQA
    from django.contrib.gis.gdal.geometries import OGRGeometry  # NOQA
    HAS_GDAL = True
+2 −2
Original line number Diff line number Diff line
@@ -54,8 +54,8 @@ get_band_ds = voidptr_output(lgdal.GDALGetBandDataset, [c_void_p])
get_band_datatype = int_output(lgdal.GDALGetRasterDataType, [c_void_p])
get_band_nodata_value = double_output(lgdal.GDALGetRasterNoDataValue, [c_void_p, POINTER(c_int)])
set_band_nodata_value = void_output(lgdal.GDALSetRasterNoDataValue, [c_void_p, c_double])
get_band_minimum = double_output(lgdal.GDALGetRasterMinimum, [c_void_p])
get_band_maximum = double_output(lgdal.GDALGetRasterMaximum, [c_void_p])
get_band_minimum = double_output(lgdal.GDALGetRasterMinimum, [c_void_p, POINTER(c_int)])
get_band_maximum = double_output(lgdal.GDALGetRasterMaximum, [c_void_p, POINTER(c_int)])

### Reprojection routine ###
reproject_image = void_output(lgdal.GDALReprojectImage, [c_void_p, c_char_p, c_void_p, c_char_p,
+0 −0

Empty file added.

+69 −0
Original line number Diff line number Diff line
from ctypes import byref, c_int

from django.contrib.gis.gdal.base import GDALBase
from django.contrib.gis.gdal.prototypes import raster as capi
from django.utils.encoding import force_text

from .const import GDAL_PIXEL_TYPES


class GDALBand(GDALBase):
    """
    Wraps a GDAL raster band, needs to be obtained from a GDALRaster object.
    """
    def __init__(self, source, index):
        self.source = source
        self.ptr = capi.get_ds_raster_band(source.ptr, index)

    @property
    def description(self):
        """
        Returns the description string of the band.
        """
        return force_text(capi.get_band_description(self.ptr))

    @property
    def width(self):
        """
        Width (X axis) in pixels of the band.
        """
        return capi.get_band_xsize(self.ptr)

    @property
    def height(self):
        """
        Height (Y axis) in pixels of the band.
        """
        return capi.get_band_ysize(self.ptr)

    def datatype(self, as_string=False):
        """
        Returns the GDAL Pixel Datatype for this band.
        """
        dtype = capi.get_band_datatype(self.ptr)
        if as_string:
            dtype = GDAL_PIXEL_TYPES[dtype]
        return dtype

    @property
    def min(self):
        """
        Returns the minimum pixel value for this band.
        """
        return capi.get_band_minimum(self.ptr, byref(c_int()))

    @property
    def max(self):
        """
        Returns the maximum pixel value for this band.
        """
        return capi.get_band_maximum(self.ptr, byref(c_int()))

    @property
    def nodata_value(self):
        """
        Returns the nodata value for this band, or None if it isn't set.
        """
        nodata_exists = c_int()
        value = capi.get_band_nodata_value(self.ptr, nodata_exists)
        return value if nodata_exists else None
Loading