Beispiel #1
0
def read_raster(path, band_number=1):
    """
    Create a raster dataset from a single raster file

    Parameters
    ----------
    path: string
        Path to the raster file.  Can be either local or s3/gs.

    band_number: int
        The band number to use

    Returns
    -------

    RasterDataset
    """

    path = fileutils.get_path(path)
    ds = gdal.Open(path, GA_ReadOnly)
    xsize = ds.RasterXSize
    ysize = ds.RasterYSize
    proj = SpatialReference()
    proj.ImportFromWkt(ds.GetProjection())
    geo_transform = ds.GetGeoTransform()
    return RasterDataset(ds, xsize, ysize, geo_transform, proj)
Beispiel #2
0
def read_raster(path, band_number=1):
    """
    Create a raster dataset from a single raster file

    Parameters
    ----------
    path: string
        Path to the raster file.  Can be either local or s3/gs.

    band_number: int
        The band number to use

    Returns
    -------

    RasterDataset
    """

    path = fileutils.get_path(path)
    ds = gdal.Open(path, GA_ReadOnly)
    xsize = ds.RasterXSize
    ysize = ds.RasterYSize
    proj = SpatialReference()
    proj.ImportFromWkt(ds.GetProjection())
    geo_transform = ds.GetGeoTransform()
    return RasterDataset(ds, xsize, ysize, geo_transform, proj)
Beispiel #3
0
    def __new__(cls, ds, band_number=1):
        """
        Create an in-memory representation for a single band in
        a raster. (0,0) in pixel coordinates represents the
        upper left corner of the raster which corresponds to
        (min_lon, max_lat).  Inherits from ndarray, so you can
        use it like a numpy array.

        Parameters
        ----------
        ds: gdal.Dataset

        band_number: int
            The band number to use

        Attributes
        ----------
        data: np.ndarray[xsize, ysize]
             The raster data
        """

        if not isinstance(ds, gdal.Dataset):
            path = fileutils.get_path(ds)
            ds = gdal.Open(path, GA_ReadOnly)

        band = ds.GetRasterBand(band_number)
        if band is None:
            msg = "Unable to load band %d " % band_number
            msg += "in raster %s" % ds.GetDescription()
            raise ValueError(msg)

        gdal_type = band.DataType
        dtype = np.dtype(GDAL2NP_CONVERSION[gdal_type])
        self = np.asarray(band.ReadAsArray().astype(dtype)).view(cls)

        self.gdal_type = gdal_type
        proj = SpatialReference()
        proj.ImportFromWkt(ds.GetProjection())
        geo_transform = ds.GetGeoTransform()

        # Initialize the base class with coordinate information.
        RasterBase.__init__(self, ds.RasterXSize, ds.RasterYSize,
                            geo_transform, proj)

        self.nan = band.GetNoDataValue()

        #self = np.ma.masked_equal(self, band.GetNoDataValue(), copy=False)
        ctable = band.GetColorTable()
        if ctable is not None:
            self.colors = np.array([ctable.GetColorEntry(i)
                                    for i in range(256)],
                                   dtype=np.uint8)
        else:
            self.colors = None

        ds = None
        return self
Beispiel #4
0
    def __new__(cls, ds, band_number=1):
        """
        Create an in-memory representation for a single band in
        a raster. (0,0) in pixel coordinates represents the
        upper left corner of the raster which corresponds to
        (min_lon, max_lat).  Inherits from ndarray, so you can
        use it like a numpy array.

        Parameters
        ----------
        ds: gdal.Dataset

        band_number: int
            The band number to use

        Attributes
        ----------
        data: np.ndarray[xsize, ysize]
             The raster data
        """

        if not isinstance(ds, gdal.Dataset):
            path = fileutils.get_path(ds)
            ds = gdal.Open(path, GA_ReadOnly)

        band = ds.GetRasterBand(band_number)
        if band is None:
            msg = "Unable to load band %d " % band_number
            msg += "in raster %s" % ds.GetDescription()
            raise ValueError(msg)

        gdal_type = band.DataType
        dtype = np.dtype(GDAL2NP_CONVERSION[gdal_type])
        self = np.asarray(band.ReadAsArray().astype(dtype)).view(cls)

        self.gdal_type = gdal_type
        proj = SpatialReference()
        proj.ImportFromWkt(ds.GetProjection())
        geo_transform = ds.GetGeoTransform()

        # Initialize the base class with coordinate information.
        RasterBase.__init__(self, ds.RasterXSize, ds.RasterYSize,
                            geo_transform, proj)

        self.nan = band.GetNoDataValue()

        #self = np.ma.masked_equal(self, band.GetNoDataValue(), copy=False)
        ctable = band.GetColorTable()
        if ctable is not None:
            self.colors = np.array(
                [ctable.GetColorEntry(i) for i in range(256)], dtype=np.uint8)
        else:
            self.colors = None

        ds = None
        return self
Beispiel #5
0
def read_band(path, band_number=1):
    """
    Read a single band from a raster into memory.

    Parameters
    ----------
    path: string
        Path to the raster file.  Can be either local or s3/gs.

    band_number: int
        The band number to use

    Returns
    -------

    RasterBand
    """

    path = fileutils.get_path(path)
    ds = gdal.Open(path, GA_ReadOnly)
    return RasterBand(ds, band_number=band_number)
Beispiel #6
0
def read_band(path, band_number=1):
    """
    Read a single band from a raster into memory.

    Parameters
    ----------
    path: string
        Path to the raster file.  Can be either local or s3/gs.

    band_number: int
        The band number to use

    Returns
    -------

    RasterBand
    """

    path = fileutils.get_path(path)
    ds = gdal.Open(path, GA_ReadOnly)
    return RasterBand(ds, band_number=band_number)