Exemple #1
0
    def single_band_calculator(self, rlist, expression):
        """ Raster calculator """

        logging.debug('Starting single_band_calculator... ')

        array_list = [GdalReader().ds2array(gdal_import.src2ds(r))[0] if np.array(r).ndim is 2 else
                      GdalReader().ds2array(gdal_import.src2ds(r)) for r in rlist]

        calculation = tools.array_calculator(array_list, expression)

        return self._array2raster(calculation, name=str(expression), mask=rlist[0])
Exemple #2
0
    def histogram_matching(self, input_raster, reference, output='histogram_matching'):

        logging.debug('Starting histogram_matching... ')

        src_ds = gdal_import.src2ds(input_raster)
        raster_array = GdalReader().ds2array(src_ds)

        ref_ds = gdal_import.src2ds(reference)
        ref_array = GdalReader().ds2array(ref_ds)
        nodata = ref_ds.GetRasterBand(1).GetNoDataValue()

        matched_array= tools.histogram_matching(raster_array, ref_array, nodata=nodata)
        return self._array2raster(matched_array, name=output, mask=input_raster)
def raster_stats(input_raster, output=None):

    logging.debug('Starting histogram_matching... ')

    src_ds = gdal_import.src2ds(input_raster)
    raster_array = GdalReader().ds2array(src_ds)
    return numpy_tools.array_stats(raster_array)
Exemple #4
0
    def rgb_intensity(self, input_raster):
        """ Intensity for rgb images 
        :param input_raster dictionary with the RGB bands
        """
        logging.debug('Starting rgb_intensity... ')

        src_ds = gdal_import.src2ds(input_raster)
        raster_array = GdalReader().ds2array(src_ds)

        intensity = tools.rgb_intensity(raster_array)
        return self._array2raster(intensity, name='intensity', mask=input_raster)
Exemple #5
0
    def zonal_stats(self, raster, zones, resize=True):

        logging.debug('Starting zonal_stats... ')

        src_ds = gdal_import.src2ds(raster)
        raster_array = GdalReader().ds2array(src_ds)

        zones_ds = gdal_import.src2ds(zones)
        zones_array = GdalReader().ds2array(zones_ds)

        outputs = tools.zonal_stats(valuesarr=raster_array[0], zonesarr=zones_array[0],
                                    resize=resize)

        outputs_dict = {}
        name = 'stats'
        for stat in ['count', 'mean', 'stdev']:
            outputs_dict[str(stat)] = (self._array2raster(outputs[stat], name=str(name + '_' + str(stat)),
                                                          mask=raster))

        return outputs_dict
Exemple #6
0
    def normalisation(self, input_raster):
        """Normalisation of a 2D array to 0-255 
        :param input_raster 2D array"""

        logging.debug('Starting normalisation... ')

        src_ds = gdal_import.src2ds(input_raster)
        raster_arrays = GdalReader().ds2array(src_ds)

        nodata = src_ds.GetRasterBand(1).GetNoDataValue()
        d_bands = tools.normalisation(raster_arrays, nodata=nodata)
        return self._array2raster(d_bands, name="normalization", mask=input_raster)
Exemple #7
0
    def equalization(self, input_raster):
        """Equalization of a 2D array with finite values
        :param input_raster 2D array"""

        logging.debug('Starting equalization... ')

        src_ds = gdal_import.src2ds(input_raster)
        raster_arrays = GdalReader().ds2array(src_ds)
        d_bands = tools.equalization(bands_list=raster_arrays)
        # return gdal_import.gdal_import(toolbox.raster.gdal_utils.poly_clip(raster, polygons, output))

        return self._array2raster(d_bands, name='equalization', mask=input_raster)
Exemple #8
0
    def vegetation_index(self, input_raster):
        """ Vegetation Index (GRVI, NDVI) for rgb/rgbnir images 
        :param input_raster dictionary with the RGB bands
        """
        logging.debug('Starting vegetation_index... ')

        src_ds = gdal_import.src2ds(input_raster)
        raster_bands = GdalReader().ds2array(src_ds)

        if len(raster_bands) == 3:
            vi = tools.vegetation_index(red=raster_bands[0], green=raster_bands[1])

        elif len(raster_bands) >= 4:
            vi = tools.vegetation_index(red=raster_bands[0], green=raster_bands[1], nir=raster_bands[3])

        else:
            logging.warning('Not enough bands to create vegetation index.')
            return

        return self._array2raster(vi, name='vi', mask=input_raster)
def get_number_tiles(src, tile_size=500):
    # Get number of tiles

    src_ds = gdal_import.src2ds(src)
    xsize = src_ds.RasterXSize
    ysize = src_ds.RasterYSize

    if np.round(xsize / tile_size, 0) - (xsize / tile_size) == 0.0:
        nxtiles = np.round(xsize / tile_size, 0)

    else:
        nxtiles = np.round(xsize / tile_size, 0) + 1

    if np.round(ysize / tile_size, 0) - (ysize / tile_size) == 0.0:
        nytiles = np.round(ysize / tile_size, 0)

    else:
        nytiles = np.round(ysize / tile_size, 0) + 1

    return nxtiles, nytiles
def reclassify(input_raster,
               new_value,
               old_value_min=None,
               old_value_max=None,
               output=None):

    logging.debug('Reclassify raster... ')

    src_ds = gdal_import.src2ds(input_raster)
    raster_array = gdal_reader.GdalReader().ds2array(src_ds)
    nodata = src_ds.GetRasterBand(1).GetNoDataValue()

    reclass = numpy_tools.reclassify(raster_array,
                                     new_value,
                                     old_value_min,
                                     old_value_max,
                                     nodata=nodata)

    if not output:
        output = 'reclassified'

    return gdal_import.gdal_import(gdal_reader.GdalReader().array2ds(
        reclass, output, mask_ds=input_raster))
Exemple #11
0
 def nir(self, input_raster):
     src_ds = gdal_import.src2ds(input_raster)
     raster_array = GdalReader().ds2array(src_ds)
     return self._array2raster(raster_array[3], name="nir", mask=input_raster)
def gdal2tiles(src, tile_size=500, tile_id_y=None, tile_id_x=None):
    """ Load a gdal file to local python instance
    
    :param src: source gdal dataset
    :param tile_size: Tile size
    :param tile_id_y: Single tile ID for Y
    :param tile_id_x: Single tile ID for X
    """

    src_ds = gdal_import.src2ds(src)
    xsize = src_ds.RasterXSize
    ysize = src_ds.RasterYSize
    bands = src_ds.RasterCount
    nxtiles, nytiles = get_number_tiles(src_ds)

    # Read raster as arrays
    dtype = [
        key for key, value in GdalReader().np2gdal.iteritems()
        if value == src_ds.GetRasterBand(1).DataType
    ][0]

    if tile_id_x and str(tile_id_x).isdigit() and tile_id_y and str(
            tile_id_y).isdigit():
        x_tile_range = [tile_id_x]
        y_tile_range = [tile_id_y]

    else:
        x_tile_range = range(nxtiles + 1)
        y_tile_range = range(nytiles + 1)

    tiles = {}
    for xtile in x_tile_range:
        for ytile in y_tile_range:

            # DatasetReadAsArray(ds, xoff=0, yoff=0, win_xsize=None, win_ysize=None)

            if (tile_size * xtile + tile_size) < xsize and (tile_size * ytile +
                                                            tile_size) < ysize:

                # arr_i = np.array(gdal_array.DatasetReadAsArray(src_ds, xoff=tile_size * xtile, yoff=tile_size * ytile,
                # win_xsize=tile_size, win_ysize=tile_size)).astype(dtype)

                arr_i = np.array(
                    gdal_array.DatasetReadAsArray(src_ds, tile_size * xtile,
                                                  tile_size * ytile, tile_size,
                                                  tile_size)).astype(dtype)

            else:

                win_xsize = min(tile_size, xsize - tile_size * xtile)
                win_ysize = min(tile_size, ysize - tile_size * ytile)

                if win_xsize < 0 or win_ysize < 0:
                    # Not square shape
                    continue

                # arr_src = np.array(gdal_array.DatasetReadAsArray(src_ds, xoff=tile_size * xtile,
                # yoff=tile_size * ytile, win_xsize=win_xsize, win_ysize=win_ysize)).astype(dtype)
                arr_src = np.array(
                    gdal_array.DatasetReadAsArray(src_ds, tile_size * xtile,
                                                  tile_size * ytile, win_xsize,
                                                  win_ysize)).astype(dtype)

                arr_i = np.zeros((bands, win_ysize, win_xsize), dtype=dtype)
                arr_i[:, 0:arr_src.shape[1], 0:arr_src.shape[2]] = arr_src

            # Create raster
            # Geotransform
            geotransform = src_ds.GetGeoTransform()
            top_left_x = geotransform[0] + geotransform[
                1] * tile_size * xtile + geotransform[2] * tile_size * ytile
            top_left_y = geotransform[3] + geotransform[
                5] * tile_size * ytile + geotransform[4] * tile_size * xtile

            new_geotransform = [
                top_left_x, geotransform[1], geotransform[2], top_left_y,
                geotransform[4], geotransform[5]
            ]

            projection = src_ds.GetProjection()
            nodata = src_ds.GetRasterBand(1).GetNoDataValue()
            name = src_ds.GetMetadataItem('FilePath')

            tiles[str(xtile) + '_' + str(ytile)] = GdalReader().array2ds(
                arr_i,
                name + '_' + str(xtile) + '_' + str(ytile),
                geotransform=new_geotransform,
                projection=projection)

    return tiles
Exemple #13
0
def poly_clip(raster, polygons, outuput):
    """Clip raster with polygons"""

    src_ds = gdal_import.src2ds(raster)
    poly_ds = ogr_import.src2ogr(polygons)

    # 1.- Reproject vector geometry to same projection as raster
    projection = src_ds.GetProjection()
    poly_reprojected = ogr_utils.reproject(poly_ds,
                                           wtk_projection=projection,
                                           outname='polygons_reprojected')

    poly_ds = ogr_import.src2ogr(poly_reprojected)
    poly_lyr = poly_ds.GetLayer()

    # Bound box (debbuging code)
    # geom_type = poly_lyr.GetGeomType()
    # outDataSource = ogrr.create_layer('bound_box', geom_type=geom_type, wkt_proj=projection, file_path=None)
    # outLayer = outDataSource.GetLfpayer()
    # outLayerDefn = outLayer.GetLayerDefn()
    # outFeature = ogr.Feature(outLayerDefn)
    # outFeature.SetGeometry(geom)
    # outLayer.CreateFeature(outFeature)
    # outFeature = None
    # outDataSource = None

    # 2.- Filter and extract features
    # Get Raster Extent
    nodata = src_ds.GetRasterBand(1).GetNoDataValue()

    r_min_x, r_max_x, r_min_y, r_max_y = GdalReader().get_extent(src_ds)

    wkt = 'POLYGON((' + ','.join([
        ' '.join([str(r_min_x), str(r_max_y)]), ' '.join([
            str(r_min_x), str(r_min_y)
        ]), ' '.join([str(r_max_x), str(r_min_y)]), ' '.join([
            str(r_max_x), str(r_max_y)
        ]), ' '.join([str(r_min_x), str(r_max_y)])
    ]) + '))'

    geom = ogr.CreateGeometryFromWkt(wkt)

    poly_lyr.SetSpatialFilter(geom)
    mem_driver = ogr.GetDriverByName('MEMORY')
    filtered_poly_ds = mem_driver.CreateDataSource('filered_polygons')

    # Open the memory datasource with write access and copy content
    mem_driver = ogr.GetDriverByName('MEMORY')
    mem_driver.Open('filered_polygons', 1)
    filtered_poly_ds.CopyLayer(poly_lyr, 'filered_polygons', ['OVERWRITE=YES'])

    poly_lyr.SetSpatialFilter(None)

    # Intersect geometries with boundary box
    geom_type = poly_lyr.GetGeomType()
    clipped_poly_ds = ogrr.create_layer('clipped_polygons',
                                        geom_type=geom_type,
                                        wkt_proj=projection,
                                        file_path=None)

    clipped_poly_lyr = clipped_poly_ds.GetLayer()
    filtered_poly_lyr = filtered_poly_ds.GetLayer()

    clipped_lyr_defn = clipped_poly_lyr.GetLayerDefn()
    infeature = filtered_poly_lyr.GetNextFeature()
    while infeature:
        feat_geom = infeature.GetGeometryRef()
        intersection_geom = feat_geom.Intersection(geom)

        out_feature = ogr.Feature(clipped_lyr_defn)
        out_feature.SetGeometry(intersection_geom)
        clipped_poly_lyr.CreateFeature(out_feature)

        out_feature = None
        infeature = filtered_poly_lyr.GetNextFeature()

    filtered_poly_lyr.ResetReading()
    filtered_poly_lyr = None

    # Bound box (debbuging code)
    # geom_type = poly_lyr.GetGeomType()
    # filtered_poly_ds = ogrr.create_layer('filered_polygons', geom_type=geom_type, wkt_proj=projection,
    #                                     file_path=None)

    # Clip raster to layer extent
    lyr = clipped_poly_lyr
    extent = lyr.GetExtent()

    # Convert the _vector extent to image pixel coordinates
    geo_trans = src_ds.GetGeoTransform()
    # projection = rds.GetProjection()
    ul_x, ul_y = GdalReader().world2pixel(geo_trans, extent[0], extent[3])
    lr_x, lr_y = GdalReader().world2pixel(geo_trans, extent[1], extent[2])

    # Create a new geomatrix for the _raster
    geo_trans = list(geo_trans)
    geo_trans[0] = extent[0]
    geo_trans[3] = extent[3]

    # Get the new array to layer extent
    rarray = gdal_array.DatasetReadAsArray(src_ds)

    if len(rarray.shape) == 3:
        clip = rarray[:, ul_y:lr_y, ul_x:lr_x]

    elif len(rarray.shape) == 2:
        clip = rarray[ul_y:lr_y, ul_x:lr_x]

    else:
        return logging.error('Error in array shape.')

    new_array = clip_raster_array(vds=filtered_poly_ds,
                                  raster_array=clip,
                                  geotransform=geo_trans,
                                  nodata=nodata)

    return GdalReader().array2ds(src_array=np.array(new_array),
                                 output=outuput,
                                 geotransform=geo_trans,
                                 projection=projection,
                                 nodata=nodata)
def single_bands_to_multiband(bands_list, output=None):

    gdal_bands_list = [gdal_import.src2ds(i) for i in bands_list]
    return gdal_import.gdal_import(
        gdal_utils.single_bands_to_multiband(gdal_bands_list, output=output))