def _compose_rgb(self,):
        self.rgb_scale = 4
        r, g, b = self._toa_bands[self.ri].ReadAsArray() * self.ref_scale + self.ref_off, \
                  self._toa_bands[self.gi].ReadAsArray() * self.ref_scale + self.ref_off, \
                  self._toa_bands[self.bi].ReadAsArray() * self.ref_scale + self.ref_off
        alpha   = (r>0) & (g>0) & (b>0)
        rgba_array = np.clip([r * self.rgb_scale * 255, g * self.rgb_scale * 255, \
                              b * self.rgb_scale * 255, alpha * self.rgb_scale * 255], 0, 255).astype(np.uint8)
        name = self.toa_dir + '/TOA_RGB.tif'
        projection   = self._toa_bands[self.ri].GetProjectionRef()
        geotransform = self._toa_bands[self.ri].GetGeoTransform() 
        self._save_rgb(rgba_array, name, projection, geotransform)
        gdal.Translate(self.toa_dir +'/TOA_overview.png', self.toa_dir+'/TOA_RGB.tif', \
                       format = 'PNG', widthPct=25, heightPct=25, resampleAlg=gdal.GRA_Bilinear ).FlushCache()
        gdal.Translate(self.toa_dir +'/TOA_ovr.png', self.toa_dir+'/TOA_RGB.tif', \
                       format = 'PNG', widthPct=10, heightPct=10, resampleAlg=gdal.GRA_Bilinear ).FlushCache()

        rgba_array = np.clip([self.boa_rgb[0] * self.rgb_scale * 255, self.boa_rgb[1] * self.rgb_scale * 255, \
                              self.boa_rgb[2] * self.rgb_scale * 255, alpha * self.rgb_scale * 255], 0, 255).astype(np.uint8)
        name = self.toa_dir + '/BOA_RGB.tif'
        self._save_rgb(rgba_array, name, projection, geotransform)
        gdal.Translate(self.toa_dir+'/BOA_overview.png', self.toa_dir+'/BOA_RGB.tif', \
                       format = 'PNG', widthPct=25, heightPct=25, resampleAlg=gdal.GRA_Bilinear ).FlushCache()
        gdal.Translate(self.toa_dir+'/BOA_ovr.png', self.toa_dir+'/BOA_RGB.tif', \
                       format = 'PNG', widthPct=10, heightPct=10, resampleAlg=gdal.GRA_Bilinear ).FlushCache()
Exemple #2
0
def create_dataset(input_tif,
                   label_tif,
                   step_size,
                   dim,
                   x_dir,
                   y_dir,
                   threshold=0.15):
    ds = gdal.Open(input_tif, gdalconst.GA_ReadOnly)
    xsize = ds.RasterXSize
    ysize = ds.RasterYSize

    for x in range(0, xsize - dim, step_size):
        for y in range(0, ysize - dim, step_size):
            current_label = gdal.Translate('',
                                           label_tif,
                                           srcWin=[x, y, dim, dim],
                                           format='MEM')
            current_array = current_label.ReadAsArray()
            if np.count_nonzero(current_array) >= (threshold * dim * dim):
                print(x, y, xsize, ysize,
                      np.unique(current_array, return_counts=True))
                gdal.Translate(os.path.join(x_dir, '{}_{}.tif').format(x, y),
                               input_tif,
                               srcWin=[x, y, dim, dim])
                gdal.Translate(os.path.join(y_dir, '{}_{}.tif').format(x, y),
                               label_tif,
                               srcWin=[x, y, dim, dim])
    def __getitem__(self, idx):
        # here is more information about this:
        #https://pytorch.org/tutorials/beginner/data_loading_tutorial.html

        #get the key for the dict
        key = self.keys_list[idx]
        coords = self.dictTiles[key]["extent"]
        pop = self.dictTiles[key]["pop"]
        pops = np.array(pop, dtype=np.float32)
        pop = pops

        #create empty 8D list
        return_img = np.empty((2, self.width, self.height), dtype=np.float32)

        #loop thorugh the image names to generate the array from different images
        for band_id, band_name in enumerate(self.images):
            #get the imagedir
            imgdir = self.dictTiles[key][self.images[band_id]]

            if band_id == 0:
                tile = gdal.Translate('', imgdir, projWin=coords, format='VRT')
                tile_arr = tile.ReadAsArray()
                return_img[0, :, :] = tile_arr

            elif band_id == 1:
                tile = gdal.Translate('', imgdir, projWin=coords, format='VRT')
                tile_arr = tile.ReadAsArray()
                return_img[1, :, :] = tile_arr

            else:
                continue

        return return_img, pop
Exemple #4
0
def vsi_download(enclosure, bbox=None, username=None, api_key=None):

    vsi_url = get_vsi_url(enclosure, username, api_key)

    # load VSI URL in memory
    output = '/vsimem/subset.tif'

    ds = gdal.Open(vsi_url)

    if bbox is not None:
        ulx, uly, lrx, lry = bbox[0], bbox[3], bbox[2], bbox[1]

        ds = gdal.Translate(destName=output,
                            srcDS=ds,
                            projWin=[ulx, uly, lrx, lry],
                            projWinSRS='EPSG:4326',
                            outputType=gdal.GDT_Float32)

    else:

        ds = gdal.Translate(destName=output,
                            srcDS=ds,
                            outputType=gdal.GDT_Float32)
    ds = None

    # create a numpy array
    ds = gdal.Open(output)

    layers = []

    for i in range(1, ds.RasterCount + 1):
        layers.append(ds.GetRasterBand(i).ReadAsArray())

    return np.dstack(layers)
Exemple #5
0
def aoi(in_raster_path, out_raster, xmin, ymax, xmax, ymin, src_tif=None):
    r"""Crop original raster to bounding box so we can read in memory.

    Examples
    --------
    gssurgo.aoi(in_raster_path = "tifs", out_raster = "path/to/aoi.tif", \
                xmax = -88.34945, xmin = -88.35470, ymin = 38.70095, \
                ymax = 38.70498)

    Notes
    -----
    https://gis.stackexchange.com/a/237412/32531

    """
    if src_tif is None:
        src_tif = state_by_bbox(fpath=in_raster_path,
                                xmax=xmax,
                                xmin=xmin,
                                ymin=ymin,
                                ymax=ymax,
                                ext="tif")

    # https://gis.stackexchange.com/a/78944/32531
    inProj = Proj(init='epsg:4326')
    outProj = Proj('+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 \
                    +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 \
                    +units=m +no_defs')
    xmin, ymin = transform(inProj, outProj, xmin, ymin)
    xmax, ymax = transform(inProj, outProj, xmax, ymax)

    if (len(src_tif) == 1):
        src_tif = ''.join(src_tif)
        ds = gdal.Open(src_tif)
        # raster_res = ds.GetGeoTransform()[1]
        ds = gdal.Translate(out_raster, ds, projWin=[xmin, ymax, xmax, ymin])
    else:
        ds = gdal.Open(''.join(src_tif[0]))
        ds = gdal.Translate("temp1.tif", ds, projWin=[xmin, ymax, xmax, ymin])

        ds = gdal.Open(''.join(src_tif[1]))
        ds = gdal.Translate("temp2.tif", ds, projWin=[xmin, ymax, xmax, ymin])

        # https://gist.github.com/nishadhka/9bc758129c2949a3194b79570198f544
        d1 = rasterio.open("temp1.tif")
        d2 = rasterio.open("temp2.tif")

        dest, output_transform = rasterio.merge.merge([d1, d2])

        with d1 as src:
            out_meta = src.meta.copy()
        out_meta.update({
            "driver": "GTiff",
            "height": dest.shape[1],
            "width": dest.shape[2],
            "transform": output_transform
        })
        with rasterio.open(out_raster, "w", **out_meta) as dest1:
            dest1.write(dest)
    def tiling_raster(self, image, output_folder, width, height, strecthing=True):
        """
        Take a image with high dimensions, and tile it in small other pieces with dimension of width x height

        :param image: the absolute path to the image file (raster)
        :param output_folder: the absolute path to the outputs
        :param width: the width of the image
        :param height: the width of the image
        :param strecthing: default True. If False, the outputs will be save as it is the inputs
        """
        if os.path.isfile(image) and image.endswith(settings.VALID_RASTER_EXTENSION):
            filename = os.path.basename(image)
            name, file_extension = os.path.splitext(filename)
            ds = gdal.Open(image)
            datatype = ds.GetRasterBand(1).DataType
            dtype = gdal.GetDataTypeName(datatype)

            if ds is None:
                logging.warning(">>>>>> Could not open image file {}. Skipped!".format(image))

            rows = ds.RasterXSize
            cols = ds.RasterYSize
            tiles_cols = cols / width
            tiles_rows = rows / height

            logging.info(">>>> File {} opened! Image with [{}, {}] size and {} type!".format(image, rows, cols, dtype))
            logging.info(">>>> Tiling image {}. {} x {} pixels. Estimated {} tiles of {} x {}..."
                         .format(image, rows, cols, round(tiles_cols * tiles_rows), width, height))

            gdal.UseExceptions()
            for i in range(0, rows, width):
                for j in range(0, cols, height):
                    try:
                        output = os.path.join(output_folder, name + "_" + str(i) + "_" + str(j) + file_extension)

                        if strecthing is True:
                            stats = [ds.GetRasterBand(i + 1).GetStatistics(True, True) for i in range(ds.RasterCount)]
                            vmin, vmax, vmean, vstd = zip(*stats)
                            gdal.Translate(output, ds, format='GTIFF', srcWin=[i, j, width, height],
                                           outputType=datatype, scaleParams=[[list(zip(*[vmin, vmax]))]],
                                           options=['-epo', '-eco',
                                                    '-b', settings.RASTER_TILES_COMPOSITION[0],
                                                    '-b', settings.RASTER_TILES_COMPOSITION[1],
                                                    '-b', settings.RASTER_TILES_COMPOSITION[2]])
                        else:
                            gdal.Translate(output, ds, format='GTIFF', srcWin=[i, j, width, height],
                                           outputType=datatype,
                                           options=['-epo', '-eco',
                                                    '-b', settings.RASTER_TILES_COMPOSITION[0],
                                                    '-b', settings.RASTER_TILES_COMPOSITION[1],
                                                    '-b', settings.RASTER_TILES_COMPOSITION[2]])

                    except RuntimeError:
                        continue
        else:
            logging.info(">>>> Image file {} does not exist or is a invalid extension!".format(image))
    def convert_array_to_raster(self,
                                np_array,
                                transform,
                                out_f_name,
                                extension="GTiff",
                                epsg_code=4326,
                                bbox=None):
        try:
            if bbox is not None or extension == 'ascii':
                f_name = tempfile.NamedTemporaryFile('w+b').name

            else:
                f_name = out_f_name
            projection = osr.SpatialReference()
            projection.SetWellKnownGeogCS("EPSG:" + str(epsg_code))
            driver = gdal.GetDriverByName("GTiff")
            export_data = driver.Create(f_name, transform[1][1],
                                        transform[1][0], 1, gdal.GDT_Float32)
            # sets the extend
            export_data.SetGeoTransform(transform[0])
            # sets projection
            export_data.SetProjection(projection.ExportToWkt())
            export_data.GetRasterBand(1).WriteArray(np_array)

            # if you want these values transparent
            export_data.GetRasterBand(1).SetNoDataValue(999)
            # Save the data
            export_data.FlushCache()
            if bbox is not None and extension is 'GTiff':
                gdal.Translate(out_f_name, export_data, projWin=bbox)
            elif extension is 'ascii':
                asc_ = tempfile.NamedTemporaryFile('w+b').name
                gdal.Translate(asc_, export_data, projWin=bbox)
                if bbox is not None:
                    ds = gdal.Open(asc_)
                else:
                    ds = gdal.Open(f_name)
                t_array = ds.GetRasterBand(1).ReadAsArray()
                exts = self.get_extent(ds.GetGeoTransform(), t_array.shape[1],
                                       t_array.shape[0])
                header_array = [
                    t_array.shape[1], t_array.shape[0], exts[1][0], exts[1][1],
                    ds.GetGeoTransform()[1], 9999
                ]
                self.write_to_ascii(t_array, header_array, out_f_name)

#             print("Process has been finished")
            return True
        except Exception as e:
            # self.log_.error("There is a problem with exporting")
            print("There is a problem with exporting")
            print('Error on line {}: {} : {}'.format(
                sys.exc_info()[-1].tb_lineno,
                type(e).__name__, e))
            return False
Exemple #8
0
def create_preview(filename,
                   fnout=None,
                   preproj=False,
                   ccc=[2.0, 98.0],
                   exp=None,
                   nodata=0,
                   **kwargs):
    if fnout is None:
        fnout = op.splitext(filename)[0] + '_preview.tif'
    fntmp = fnout.replace('.tif', '_tmp.tif')

    _filename = filename
    if preproj:
        reproject(filename, _filename, crs='epsg:4326')

    try:
        logger.info(f"Creating preview {fnout} from {filename}")
        if exp is not None:
            ds = gdal.Open(_filename)
            band = ds.GetRasterBand(1)
            stats = band.GetStatistics(False, True)
            inmin = stats[0]
            inmax = stats[1]
            logger.debug(
                f"Stretching {inmin} - {inmax} to 1-255 with exp={exp}")
            gdal.Translate(fntmp,
                           filename,
                           noData=nodata,
                           format='GTiff',
                           outputType=gdal.GDT_Byte,
                           scaleParams=[[inmin, inmax, 1, 255]],
                           exponents=[exp])
        else:
            # ccc stretch
            inmin, inmax = calculate_ccc_values(_filename,
                                                lo=ccc[0],
                                                hi=ccc[1])
            logger.info(
                f"Stretching {inmin} - {inmax} to 1-255 with ccc={ccc}")
            gdal.Translate(fntmp,
                           _filename,
                           noData=nodata,
                           format='GTiff',
                           outputType=gdal.GDT_Byte,
                           scaleParams=[[inmin, inmax, 1, 255]])
        cogify(fntmp, fnout)
    except Exception as err:
        logger.error(f"Unable to create preview {filename}: {err}")
        raise (err)
    finally:
        if op.exists(fntmp):
            os.remove(fntmp)
        if preproj and op.exists(_filename):
            os.remove(_filename)
    return fnout
Exemple #9
0
def Mask_it(rasterin, mask, geom, NoDataValue, mask_value):
    geom = geom.bounds
    geom = [geom[0], geom[3], geom[2], geom[1]]
    MRO = gdal.Translate("temp1.vrt", rasterin, projWin=geom).ReadAsArray()
    MR2 = gdal.Translate("temp2.vrt", mask, projWin=geom).ReadAsArray()
    r = rasterio.open("temp1.vrt")
    affineO = r.transform
    del r
    for item in mask_value:
        MRO[np.where(MR2 == int(item))] = NoDataValue
    return MRO, affineO
Exemple #10
0
def workFunc(i):
    ras_pth = r"O:\Student_Data\CJaenicke\00_MA\data\climate\dwd_evapo_p\time_series\EVAPO_2018_3035_resampled.tif"
    ras = gdal.Open(ras_pth)
    shp = ogr.Open(
        r'O:\Student_Data\CJaenicke\00_MA\data\vector\miscellaneous\force-tiles_ger_3035.shp'
    )
    lyr = shp.GetLayer()
    feat = lyr.GetFeature(i)

    geom = feat.geometry().Clone()
    name = feat.GetField('Name')

    # there is somehow a small shift in the force tiles, thus thus if clause
    if int(name[3:5]) > 57:
        print(i, name)
        extent = geom.GetEnvelope()

        x_min_ext = extent[0] + 30
        x_max_ext = extent[1] + 30
        y_min_ext = extent[2]
        y_max_ext = extent[3]

        output_path = r'O:\Student_Data\CJaenicke\00_MA\data\climate\dwd_evapo_p\tiles\\' + name + r'\\'
        createFolder(output_path)
        output_name_full = output_path + 'EVAPO_2018.tif'

        # projWin --- subwindow in projected coordinates to extract: [ulx, uly, lrx, lry]

        ras_cut = gdal.Translate(
            output_name_full,
            ras,
            projWin=[x_min_ext, y_max_ext, x_max_ext, y_min_ext])
        ras_cut = None
    else:
        print(i, name)
        extent = geom.GetEnvelope()

        x_min_ext = extent[0]
        x_max_ext = extent[1]
        y_min_ext = extent[2]
        y_max_ext = extent[3]

        output_path = r'O:\Student_Data\CJaenicke\00_MA\data\climate\dwd_evapo_p\tiles\\' + name + r'\\'
        createFolder(output_path)
        output_name_full = output_path + 'EVAPO_2018.tif'

        # projWin --- subwindow in projected coordinates to extract: [ulx, uly, lrx, lry]

        ras_cut = gdal.Translate(
            output_name_full,
            ras,
            projWin=[x_min_ext, y_max_ext, x_max_ext, y_min_ext])
        ras_cut = None
Exemple #11
0
def Mask_it(rasterin, mask, geom, NoDataValue, mask_value=0):
    geom = geom.bounds
    geom = [geom[0], geom[3], geom[2], geom[1]]
    MRO = gdal.Translate("temp1.vrt", rasterin, projWin=geom).ReadAsArray()
    MR2 = gdal.Translate("temp2.vrt", mask, projWin=geom).ReadAsArray()
    r = rasterio.open("temp1.vrt")
    affineO = r.affine
    del r
    #if more nodata values are required simply add another line or two to this:
    #MRO[np.where((MR2 == 2))] = -9999
    MRO[np.where((MR2 == mask_value))] = NoDataValue
    return MRO, affineO
Exemple #12
0
def convertToTif(filename):
    filen = os.path.basename(f)
    targetFilename = filename + ".tif"
    targetWarped = filename + ".wrapped.tif"
    srcDs = gdal.Open(filename)
    print("jp2 to geotiff")
    gdal.Translate(targetFilename, srcDs)
    srcDs = gdal.Open(targetFilename)
    print("warping to EOSG:4326")
    gdal.Warp(targetWarped, srcDs, dstSRS="EPSG:4326")
    srcDs = gdal.Open(targetWarped)
    os.remove(targetFilename)
    print("clipping")
    gdal.Translate(targetFolder + filen + ".tif", srcDs, projWin=clippingArea)
    os.remove(targetWarped)
Exemple #13
0
def generate_vrt_and_geotiff(input_file, planet, date):
    title_pattern = "_geo"
    aullr = ""
    srs = ""
    file_name, ext = os.path.splitext(input_file)
    output_file_location = "/data/" + planet + "/" + date + "/"
    output_geo_file = output_file_location + file_name + title_pattern + ext
    print("INFO:getting geo spatial co-ordinates for planet " + planet)

    if (planet == "MARS"):
        aullr = '-180 90 180 -90'
        srs = 'EPSG:4326'
        print("INFO:MARS geo spatial co-ordinates found")
    if (planet == "EARTH"):
        aullr = '-155.79295349 -80.91002823000001 5.505262509999994 80.29821777'
        srs = 'EPSG:4326'
        print("INFO:EARTH geo spatial co-ordinates found")
    if (planet == "MARS" | planet == "EARTH"):
        print("INFO:Loading input data set " + input_file)
        ds = gdal.Open(input_file)
        print(
            "INFO:Translating input data set to geo tiff data set with output bounds "
            + aullr + " output srs " + srs)
        gdal.Translate(output_geo_file,
                       ds,
                       format='GTiff',
                       outputBounds=aullr,
                       outputSRS=srs)
        print("INFO:Building vrt for generated geo tiff data set")
        gdal.BuildVRT(output_file_location + file_name + ".vrt",
                      output_geo_file)
        create_info_file(output_file_location, file_name)
        print("INFO:Completed geo tiff, vrt and info files generation")
    else:
        print("DEBUG:Unknown planet")
Exemple #14
0
def clip(product, geo_extent, native_epsg=4326, epsg=4326):
    """Clip a product.

    Clips a Gdal product in memory to a given extent. The
     operation is performed in-place.

    :param geo_extent: the extent that the product will be clipped to
    :type geo_extent: osgeo.ogr.Geometry
    :param epsg: the epsg code of the extent
    :type epsg: int
    """
    # Build the projection data
    prj_in = Proj('epsg:%s' % native_epsg)
    prj_out = Proj('epsg:%s' % epsg)

    # Get max min coords of the shape
    minX, maxX, minY, maxY = geo_extent.GetEnvelope()

    # Product extent
    xmin, ymin = transform(prj_in, prj_out, minY, minX)
    xmax, ymax = transform(prj_in, prj_out, maxY, maxX)

    bbox = [xmin, ymax, xmax, ymin]

    clipped = gdal.Translate('/vsimem/clip.tif', product, projWin=bbox)

    return (clipped, clipped.GetGeoTransform())
def cog(input_tif, output_tif,no_data=None):
    
    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                    '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                    '-co COMPRESS=LZW '))
    
    if no_data != None:
        translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                        '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                        '-co COMPRESS=LZW '\
                                                                        '-a_nodata {}'.format(no_data)))
    ds = gdal.Open(input_tif, gdal.OF_READONLY)

    gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
    ds.BuildOverviews('NEAREST', [2,4,8,16,32])
    
    ds = None

    ds = gdal.Open(input_tif)
    gdal.Translate(output_tif,
                   ds, 
                   options=translate_options)
    ds = None

    os.remove('{}.ovr'.format(input_tif))
    os.remove(input_tif)
Exemple #16
0
    def __init__(self, dictionary):
        # create a list with all keys that have data in all bands
        self.keys_list = []
        with open(f'{mapdir}/{dictionary}') as json_file:
            self.dictTiles = json.load(json_file)

        for element in self.dictTiles:
            if self.dictTiles[element].get("pop") != None:
                if self.dictTiles[element].get("S2") != None:
                    if self.dictTiles[element].get("vv") != None:
                        if self.dictTiles[element].get("vh") != None:
                            if self.dictTiles[element].get(
                                    "Coherence") != None:
                                self.keys_list.append(element)

        #check how long the list is
        print(len(self.keys_list))

        #get width and height of a tile so that it can be further used
        for i in self.keys_list:
            imgdir = self.dictTiles[i]["S2"]
            coords = self.dictTiles[i]["extent"]
            tile = gdal.Translate('', imgdir, projWin=coords, format='VRT')
            tile_arr = tile.ReadAsArray()
            self.S2bands, self.width, self.height = tile_arr.shape
            print(self.S2bands, self.width, self.height)
            break

        #shuffle the list so that tiles next to each other are apart
        random.seed(430)
        random.shuffle(self.keys_list)
Exemple #17
0
 def transform(self, pin):
     row_min = self.bounds[3]
     row_max = self.bounds[1]
     col_min = self.bounds[0]
     col_max = self.bounds[2]
     if self.mode in ['pixel', 'p', 0]:
         srcWin = [
             col_min, row_min, col_max - col_min + 1, row_max - row_min + 1
         ]
         projWin = None
     elif self.mode in ['geo', 'g', 1]:
         srcWin = None
         projWin = [col_min, row_min, col_max, row_max]
     else:
         raise Exception('! Invalid mode in Crop')
     drivername = 'GTiff'
     srcpath = '/vsimem/crop_input_' + str(uuid.uuid4()) + '.tif'
     dstpath = '/vsimem/crop_output_' + str(uuid.uuid4()) + '.tif'
     (pin * SaveImage(srcpath, driver=drivername))()
     gdal.Translate(dstpath, srcpath, srcWin=srcWin, projWin=projWin)
     pout = LoadImage(dstpath)()
     pout.name = pin.name
     if pin.data.dtype in (bool, np.dtype('bool')):
         pout.data = pout.data.astype('bool')
     driver = gdal.GetDriverByName(drivername)
     driver.Delete(srcpath)
     driver.Delete(dstpath)
     return pout
Exemple #18
0
def create_singleband_geotif(dataset, hdf_path):
    h5spot = hdf_path.find('.h5')
    folder_path = hdf_path[:h5spot]
    if ".L30." in str(hdf_path):
        if "QA" in dataset:
            band = "QA"
        else:
            band = dataset[-6:]
        datasetFirst = dataset.find('HLS.L30')
        datsaetLast = dataset.find('v1.4')
        path = dataset[datasetFirst:datsaetLast + 4]
        band_path = band.replace(band, BAND_NAMES["L30"][band])
    elif ".S30." in str(hdf_path):
        if "QA" in dataset:
            band = "QA"
        else:
            band = dataset[-3:]
        datasetFirst = dataset.find('HLS.S30')
        datsaetLast = dataset.find('v1.4')
        path = dataset[datasetFirst:datsaetLast + 4]
        band_path = band.replace(band, BAND_NAMES["S30"][band])
    else:
        return
    file_path = folder_path + '/' + path + '_' + band_path + '.tif'
    if os.path.isfile(file_path):
        return
    gdal.Translate(file_path, dataset, options=[("-of"), ("GTiff")])
Exemple #19
0
def rmf_31c():

    ds_name = 'tmp/rmf_31c.rsw'
    gdal.Translate(ds_name,
                   'data/small_world.tif',
                   format='RMF',
                   options='-co COMPRESS=JPEG')

    ds = gdal.Open(ds_name)
    if ds is None:
        gdaltest.post_reason('Can\'t open ' + ds_name)
        return 'fail'
    expected_cs1 = [25789, 27405, 31974]
    expected_cs2 = [23764, 25264, 33585]  # osx
    cs = [
        ds.GetRasterBand(1).Checksum(),
        ds.GetRasterBand(2).Checksum(),
        ds.GetRasterBand(3).Checksum()
    ]

    if cs != expected_cs1 and cs != expected_cs2:
        gdaltest.post_reason('Invalid checksum %s expected %s or %s.' %
                             (str(cs), str(expected_cs1), str(expected_cs2)))
        return 'fail'
    return 'success'
Exemple #20
0
def create_DEM_mosaic(DEM,
                      DEM_dir,
                      dstfile,
                      product="NED",
                      vrt_only=False,
                      format="GTiff"):
    """ Create a Mosaic from a list of DEM urls or NTS tiles. Missing tiles will
    be downloaded """

    files = download_multiple_DEM(DEM, DEM_dir, product)

    # build VRT
    VRT_path = path.join(path.dirname(dstfile), "tmp.VRT")
    VRT = gdal.BuildVRT(VRT_path, files)
    VRT.FlushCache()
    VRT = None

    # return VRT-only if desired
    if vrt_only:
        return (VRT_path)

    # set warp parameters

    ds = gdal.Translate(dstfile, VRT_path, format=format)

    ds.FlushCache()
    ds = None

    remove(VRT_path)
    return (dstfile)
Exemple #21
0
def cop_predict(f, threshold, raw_dir, temp_dir, dest_dir, model):
    src = raw_dir + f
    dst = temp_dir + f[:-4] + ".png"

    final_dst = dest_dir + f[:-4] + ".png"
    if (not os.path.isfile(final_dst)):
        try:
            gdal.Translate(dst, src)
        except:
            print("Exception gdal " + f)
            return (f)

        print(src)
        print(dst)
        print(final_dst)

        image = load(dst)
        predict = model.predict(image)[0]
        print(predict)

        if (predict < threshold):
            copyfile(dst, final_dst)

        os.remove(dst)
        os.remove(dst + ".aux.xml")

        return ("")

    return ("")
Exemple #22
0
def resample_assets_to_ssbn_tiles(ssbn_fname,
                                  asset_fname=None,
                                  resampleAlg='near'):
    """transforms an asset grid to the dimensions (x,y,step) of the ssbn tif.
    """
    if not asset_fname:
        asset_fname = get_asset_fname()
    else:
        pass
    ssbn_info = get_basic_info(ssbn_fname)
    asset_info = gpw_basic_info(asset_fname)
    # Need to check that file doesn't already exist.
    outdir = './data_exposures/gpw/'
    outfile = '{}_{}_{}.tif'.format(ssbn_info['iso2'], asset_info['type'],
                                    ssbn_info['tile'])
    if exists(outdir + outfile):
        print('skipping {}'.format(outfile))
    # Use nearest neighbor method to create a file
    else:
        # a,gt,ts = array, geotransform, xy_tiles
        a, gt, ts = get_ssbn_array(ssbn_fname, True)
        # secs = str(int(gt[1]*60*60))
        bounds = get_bounds(ssbn_fname)
        print('creating {}'.format(outfile))
        gdal.Translate(outdir + outfile,
                       get_asset_tif(),
                       xRes=gt[1],
                       yRes=gt[5],
                       projWin=bounds,
                       resampleAlg=resampleAlg,
                       creationOptions=["COMPRESS=DEFLATE"])
Exemple #23
0
def pre_process(item, s_expression):

    assets = OrderedDict()

    resolution = get_resolution(item, s_expression)

    common_band_names = parse_expression(s_expression)

    for index, common_name in enumerate(common_band_names):

        _, asset_href = get_asset(item, common_name)

        if not asset_href:

            continue

        _ds = gdal.Open(asset_href)

        if _ds.GetGeoTransform()[1] == resolution:

            assets[common_name] = asset_href

        else:

            gdal.Translate(
                "{}_{}.tif".format(common_name, resolution),
                _ds,
                xRes=resolution,
                yRes=resolution,
            )

            assets[common_name] = "{}_{}.tif".format(common_name, resolution)

    return assets
Exemple #24
0
def PlotSWBD(list):
    f = open(list.GIAnTDirectory + '/demfloat32.crop.rsc', 'r')
    content = f.read()
    lines = content.split("\n")
    for line in lines:
        if len(line.split()) > 0:
            var = line.split()[0]
            if var == "LON_REF2":
                lon_start = float(line.split()[1])
            if var == "LON_REF1":
                lon_end = float(line.split()[1])
            if var == "LAT_REF1":
                lat_start = float(line.split()[1])
            if var == "LAT_REF3":
                lat_end = float(line.split()[1])
            if var == "Y_STEP":
                pixelHeight = float(line.split()[1])
            if var == "X_STEP":
                pixelWidth = float(line.split()[1])
    print(lon_start, lon_end, lat_start, lat_end, pixelHeight, pixelWidth)

    swbd = glob.glob(list.ISCEDirectory + '/swbdLat*.wbd.vrt')
    ds = gdal.Open(swbd[0], gdal.GA_ReadOnly)
    data = ds.GetRasterBand(1).ReadAsArray()
    gt = ds.GetGeoTransform()
    ds = gdal.Translate('new.tif',
                        ds,
                        projWin=[lon_start, lat_start, lon_end, lat_end])
    swbd0 = ds.GetRasterBand(1).ReadAsArray()

    return swbd0
Exemple #25
0
    def apply_roi(self, ulx, uly, lrx, lry):
        """Applies a region of interest (ROI) window to the state mask, which
        is then used to subset the data spatially. Useful for spatial
        windowing/chunking

        Parameters
        ----------
        ulx : integer
            The Upper Left corner of the state mask (in pixels). Easting.
        uly : integer
            The Upper Left corner of the state mask (in pixels). Northing.
        lrx : integer
            The Lower Right corner of the state mask (in pixels). Easting.
        lry : integer
            The Lower Right corner of the state mask (in pixels). Northing.
        Returns
        -------
        None
        Doesn't return anything, but changes `self.state_mask`
        """
        self.ulx = ulx
        self.uly = uly
        self.lrx = lrx
        self.lry = lry
        width = lrx - ulx
        height = uly - lry

        self.state_mask = gdal.Translate(
            "",
            self.original_mask,
            srcWin=[ulx, uly, width, abs(height)],
            format="MEM",
        )
def cog(input_tif):
    
    temp_tif = 'temp.tif'
    
    shutil.move(input_tif, temp_tif)
    
    translate_options = gdal.TranslateOptions(gdal.ParseCommandLine('-co TILED=YES ' \
                                                                    '-co COPY_SRC_OVERVIEWS=YES ' \
                                                                    ' -co COMPRESS=DEFLATE'))

    ds = gdal.Open(temp_tif, gdal.OF_READONLY)

    gdal.SetConfigOption('COMPRESS_OVERVIEW', 'DEFLATE')
    ds.BuildOverviews('NEAREST', [2,4,8,16,32])
    
    ds = None

    ds = gdal.Open(temp_tif)
    gdal.Translate(input_tif,
                   ds, 
                   options=translate_options)
    ds = None

    os.remove('{}.ovr'.format(temp_tif))
    os.remove(temp_tif)
def Clip_Dataset_GDAL(input_name, output_name, latlim, lonlim):
    """
    Clip the data to the defined extend of the user (latlim, lonlim) by using the gdal_translate executable of gdal.

    Keyword Arguments:
    input_name -- input data, input directory and filename of the tiff file
    output_name -- output data, output filename of the clipped file
    latlim -- [ymin, ymax]
    lonlim -- [xmin, xmax]

    # Get environmental variable
    WA_env_paths = os.environ["WA_PATHS"].split(';')
    GDAL_env_path = WA_env_paths[0]
    GDALTRANSLATE_PATH = os.path.join(GDAL_env_path, 'gdal_translate.exe') #!!! Vervang deze door gdal.Translate

    # find path to the executable    
    fullCmd = ' '.join(["%s" %(GDALTRANSLATE_PATH), '-projwin %s %s %s %s -of GTiff %s %s'  %(lonlim[0], latlim[1], lonlim[1], latlim[0], input_name, output_name)])
    Run_command_window(fullCmd)
    """

    options_list = [
        '-projwin %s %s %s %s' % (lonlim[0], latlim[1], lonlim[1], latlim[0])
    ]
    options_string = " ".join(options_list)
    gdal.UseExceptions()
    gdal.Translate(output_name, input_name, options=options_string)

    return ()
Exemple #28
0
    def convertImage(self, pathname, out_path, options=None):
        """
        convert image with gdal translate functionality
        """

        # create output pathname
        out_pathname = os.path.join(out_path, os.path.basename(pathname))
        if not os.path.exists(out_pathname):

            # open existing image
            src_ds = gdal.Open(pathname, gdal.GA_ReadOnly)
            if src_ds is not None:

                # create out path if required
                if not os.path.exists(out_path):
                    os.makedirs(out_path)

                # execute translation - report error to log
                ds = gdal.Translate(
                    out_pathname,
                    src_ds,
                    options=gdal.TranslateOptions(creationOptions=options))
                if ds is None and self._log_file is not None:
                    self._log_file.write('Error converting {} -> {} {}'.format(
                        pathname, out_pathname, ','.join(options)))

                # copy geom file
                if os.path.exists(pathname.replace('.TIF', '.geom')):
                    shutil.copy(pathname.replace('.TIF', '.geom'), out_path)

            # free src_ds
            src_ds = None

        return out_pathname
Exemple #29
0
def Georegistr(i, files, gcplist):
    pbar3 = tqdm(total=1, position=0, desc="Georeg    ")
    temp = files[i][::-1]
    temp2 = temp[:temp.find("/")]
    src = temp2[::-1]
    dest = files[i].strip(".tif") + "_GR.vrt"
    if os.path.isfile(dest.replace("\\", "/")):
        os.remove(dest)
    temp = gdal.Translate('',
                          files[i],
                          format='VRT',
                          outputSRS='EPSG:4326',
                          GCPs=gcplist)
    gdal.Warp(dest, temp, tps=True, resampleAlg='bilinear')
    pattern = "    <SourceDataset relativeToVRT=\"0\"></SourceDataset>"
    subst = "    <SourceDataset relativeToVRT=\"1\">" + src + "</SourceDataset>"
    fh, abs_path = mkstemp()
    with os.fdopen(fh, 'w') as new_file:
        with open(dest) as old_file:
            for line in old_file:
                new_file.write(line.replace(pattern, subst))
    os.remove(dest)
    move(abs_path, dest)
    pbar3.update(1)
    pbar3.close()
Exemple #30
0
def save_array_as_image(image_path, image_array):

    image_array = image_array.astype(np.uint8)
    if not image_path.lower().endswith(".png") and not image_path.lower(
    ).endswith(".jpg") and not image_path.lower().endswith(".tif"):
        print(image_path)
        print("Error! image_path has to end with .png, .jpg or .tif")
    height = image_array.shape[0]
    width = image_array.shape[1]
    if height * width < Image.MAX_IMAGE_PIXELS:
        newIm = Image.fromarray(image_array, "RGB")
        newIm.save(image_path)

    else:
        gdal.AllRegister()
        driver = gdal.GetDriverByName('MEM')
        ds1 = driver.Create('', width, height, 3, gdal.GDT_Byte)
        ds = driver.CreateCopy(image_path, ds1, 0)

        image_array = np.swapaxes(image_array, 2, 1)
        image_array = np.swapaxes(image_array, 1, 0)
        ds.GetRasterBand(1).WriteArray(image_array[0], 0, 0)
        ds.GetRasterBand(2).WriteArray(image_array[1], 0, 0)
        ds.GetRasterBand(3).WriteArray(image_array[2], 0, 0)
        gdal.Translate(image_path,
                       ds,
                       options=gdal.TranslateOptions(bandList=[1, 2, 3],
                                                     format="png"))