Пример #1
0
def test_basic_test_17():

    from osgeo import ogr

    for _ in range(2):
        ogr.UseExceptions()
        gdal.UseExceptions()
        try:
            gdal.Open('do_not_exist')
        except RuntimeError:
            pass
        gdal.DontUseExceptions()
        ogr.DontUseExceptions()
        assert not gdal.GetUseExceptions()
        assert not ogr.GetUseExceptions()

    for _ in range(2):
        ogr.UseExceptions()
        gdal.UseExceptions()
        try:
            gdal.Open('do_not_exist')
        except RuntimeError:
            pass
        flag = False
        try:
            ogr.DontUseExceptions()
            gdal.DontUseExceptions()
            flag = True
        except:
            gdal.DontUseExceptions()
            ogr.DontUseExceptions()
        assert not flag, 'expected failure'
        assert not gdal.GetUseExceptions()
        assert not ogr.GetUseExceptions()
Пример #2
0
def GetStatistics(band, *args, **kwargs):
    gdal.UseExceptions()
    try:
        stats = band.GetStatistics(*args, **kwargs)
    except:
        gdal.DontUseExceptions()
        raise geometry.GDALError
    gdal.DontUseExceptions()
    return stats
Пример #3
0
    def handler(self, err_level, err_type, err_msg):
        """ Making errors messages more readable """
        # available types
        err_class = {gdal.CE_None: 'None',
                     gdal.CE_Debug: 'Debug',
                     gdal.CE_Warning: 'Warning',
                     gdal.CE_Failure: 'Failure',
                     gdal.CE_Fatal: 'Fatal'
                     }
        # getting type
        err_type = err_class.get(err_type, 'None')

        # cleaning message
        err_msg = err_msg.replace('\n', ' ')

        # disabling GDAL exceptions raising to avoid future troubles
        gdal.DontUseExceptions()

        # propagating
        self.err_level = err_level
        self.err_type = err_type
        self.err_msg = err_msg

        # end of function
        return self.err_level, self.err_type, self.err_msg
Пример #4
0
def gdal_output_file_exists(pth: str):
    """
    here we could just do os.path.exists, but we also support vsimem virtual file systems for gdal
    https://gdal.org/user/virtual_file_systems.html
    therefore we should just try a gdal open to see if the file path exists

    Parameters
    ----------
    pth
        path to the file you want to check

    Returns
    -------
    bool
        True if the file exists
    """
    gdal.UseExceptions()
    try:
        openfil = gdal.Open(pth)
    except RuntimeError:
        openfil = None
    gdal.DontUseExceptions()
    if openfil is None:
        return False
    openfil = None
    return True
Пример #5
0
    def handler(self, err_level, err_type, err_msg):
        """Make errors messages more readable."""
        # available types
        err_class = {
            gdal.CE_None: "None",
            gdal.CE_Debug: "Debug",
            gdal.CE_Warning: "Warning",
            gdal.CE_Failure: "Failure",
            gdal.CE_Fatal: "Fatal",
        }
        # getting type
        err_type = err_class.get(err_type, "None")

        # cleaning message
        err_msg = err_msg.replace("\n", " ")

        # disabling GDAL exceptions raising to avoid future troubles
        gdal.DontUseExceptions()

        # propagating
        self.err_level = err_level
        self.err_type = err_type
        self.err_msg = err_msg

        # end of function
        return self.err_level, self.err_type, self.err_msg
Пример #6
0
def test_basic_test_7():
    old_use_exceptions_status = gdal.GetUseExceptions()
    gdal.UseExceptions()
    ret = basic_test_7_internal()
    if old_use_exceptions_status == 0:
        gdal.DontUseExceptions()
    return ret
Пример #7
0
def create_layers(data_source, schema=None, layers=None):
    '''Create layers in database'''
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(EPSG_CODE)
    gdal.UseExceptions()  # make gdal shut up...
    for key in LAYERS:
        if layers is not None and not key in layers:
            continue
        defn = LAYERS[key]
        name = defn.name
        if schema is not None:
            name = schema + "." + name
        try:
            layer = data_source.GetLayerByName(name)
        except Exception:
            layer = None

        if layer is None:
            print("Creating: " + name)
            layer = data_source.CreateLayer(name, srs, defn.geometry_type)
            for field_name, field_type in defn.field_list:
                if field_type == ogrOFTLongString:
                    # override usual layer definition mechanics
                    field_defn = ogr.FieldDefn(field_name, ogr.OFTString)
                    field_defn.SetWidth(128)
                else:
                    field_defn = ogr.FieldDefn(field_name, field_type)
                    if field_type == ogr.OFTString:
                        field_defn.SetWidth(32)

                okay = layer.CreateField(field_defn)
                assert okay == 0

    gdal.DontUseExceptions()
Пример #8
0
    def deleteIfExisting(filename):
        """
        Delete the filename if it already exists.
        If possible, use the appropriate GDAL driver to do so, to ensure
        that any associated files will also be deleted. 

        """
        if os.path.exists(filename):
            # Save the current exception-use state
            usingExceptions = gdal.GetUseExceptions()
            if not usingExceptions:
                gdal.UseExceptions()

            # Try opening it for read, to find out whether it is 
            # a valid GDAL file, and which driver it goes with
            try:
                ds = gdal.Open(str(filename))
            except RuntimeError:
                ds = None

            if ds is not None:
                # It is apparently a valid GDAL file, so get the driver appropriate for it.
                drvr = ds.GetDriver()
                del ds
                # Use this driver to delete the file
                drvr.Delete(filename)
            else:
                # Apparently not a valid GDAL file, for whatever reason, so just remove the file
                # directly. 
                os.remove(filename)

            # Restore exception-use state
            if not usingExceptions:
                gdal.DontUseExceptions()
Пример #9
0
def basic_test_17():

    from osgeo import ogr

    for _ in range(2):
        ogr.UseExceptions()
        gdal.UseExceptions()
        try:
            gdal.Open('do_not_exist')
        except RuntimeError:
            pass
        gdal.DontUseExceptions()
        ogr.DontUseExceptions()
        if gdal.GetUseExceptions():
            gdaltest.post_reason('fail')
            return 'fail'
        if ogr.GetUseExceptions():
            gdaltest.post_reason('fail')
            return 'fail'

    for _ in range(2):
        ogr.UseExceptions()
        gdal.UseExceptions()
        try:
            gdal.Open('do_not_exist')
        except RuntimeError:
            pass
        flag = False
        try:
            ogr.DontUseExceptions()
            gdal.DontUseExceptions()
            flag = True
        except:
            gdal.DontUseExceptions()
            ogr.DontUseExceptions()
        if flag:
            gdaltest.post_reason('expected failure')
            return 'fail'
        if gdal.GetUseExceptions():
            gdaltest.post_reason('fail')
            return 'fail'
        if ogr.GetUseExceptions():
            gdaltest.post_reason('fail')
            return 'fail'

    return 'success'
Пример #10
0
def mosaicAllTifsInFolder(inDir, outFileName):
    onlyfiles = [
        os.path.join(inDir, f) for f in os.listdir(inDir)
        if os.path.isfile(os.path.join(inDir, f)) and f.endswith(".tif")
    ]
    args = ["placeholder", "-o", outFileName] + onlyfiles
    gdal.DontUseExceptions()
    gdal_merge.main(args)
Пример #11
0
def _set_up_osgeo_use_exception(new, _):
    if new:
        gdal.UseExceptions()
        osr.UseExceptions()
        ogr.UseExceptions()
    else:
        gdal.DontUseExceptions()
        osr.DontUseExceptions()
        ogr.DontUseExceptions()
 def _create_base_map(self, ):
     '''
     Deal with different types way to define the AOI, if none is specified, then the image bound is used.
     '''
     gdal.UseExceptions()
     ogr.UseExceptions()
     if self.aoi is not None:
         if os.path.exists(self.aoi):
             try:
                 g = gdal.Open(self.aoi)
                 subprocess.call([
                     'gdaltindex', '-f', 'GeoJSON', '-t_srs', 'EPSG:4326',
                     self.toa_dir + '/AOI.json', self.aoi
                 ])
             except:
                 try:
                     gr = ogr.Open(self.aoi)
                     l = gr.GetLayer(0)
                     f = l.GetFeature(0)
                     g = f.GetGeometryRef()
                 except:
                     raise IOError(
                         'AOI file cannot be opened by gdal, please check it or transform into format can be opened by gdal'
                     )
         else:
             try:
                 g = ogr.CreateGeometryFromJson(self.aoi)
             except:
                 try:
                     g = ogr.CreateGeometryFromGML(self.aoi)
                 except:
                     try:
                         g = ogr.CreateGeometryFromWkt(self.aoi)
                     except:
                         try:
                             g = ogr.CreateGeometryFromWkb(self.aoi)
                         except:
                             raise IOError(
                                 'The AOI has to be one of GeoJSON, GML, Wkt or Wkb.'
                             )
         gjson_str = '''{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":%s}]}''' % g.ExportToJson(
         )
         with open(self.toa_dir + '/AOI.json', 'wb') as f:
             f.write(gjson_str.encode())
     ogr.DontUseExceptions()
     gdal.DontUseExceptions()
     if not os.path.exists(self.toa_dir + '/AOI.json'):
         subprocess.call([
             'gdaltindex', '-f', 'GeoJSON', '-t_srs', 'EPSG:4326',
             self.toa_dir + '/AOI.json', self.toa_bands[0]
         ])
         self.logger.warning(
             'AOI is not created and full band extend is used')
         self.aoi = self.toa_dir + '/AOI.json'
     else:
         self.aoi = self.toa_dir + '/AOI.json'
Пример #13
0
    def __init__(self):
        """
        Constructor.
        """
        super(InventoryThread, self).__init__()

        self.messenger = InventoryMessages(self)
        self.files = list()
        gdal.DontUseExceptions()
        ogr.DontUseExceptions()
Пример #14
0
def resolve_ambiguous_raster_path(uri, raise_error=True):
    """Get the real uri for a raster when we don't know the extension of how
        the raster may be represented.

        uri - a python string of the file path that includes the name of the
              file but not its extension

        raise_error - a Boolean that indicates whether the function should
            raise an error if a raster file could not be opened.

        return - the resolved uri to the rasster"""

    # Turning on exceptions so that if an error occurs when trying to open a
    # file path we can catch it and handle it properly
    gdal.UseExceptions()

    # a list of possible suffixes for raster datasets. We currently can handle
    # .tif and directory paths
    possible_suffixes = ['', '.tif', '.img']

    # initialize dataset to None in the case that all paths do not exist
    dataset = None
    for suffix in possible_suffixes:
        full_uri = uri + suffix
        if not os.path.exists(full_uri):
            continue
        try:
            dataset = gdal.Open(full_uri, gdal.GA_ReadOnly)
        except:
            dataset = None

        # return as soon as a valid gdal dataset is found
        if dataset is not None:
            break

    # Turning off exceptions because there is a known bug that will hide
    # certain issues we care about later
    gdal.DontUseExceptions()

    # If a dataset comes back None, then it could not be found / opened and we
    # should fail gracefully
    if dataset is None and raise_error:
        raise Exception(
            'There was an Error locating a threat raster in the '
            'input folder. One of the threat names in the CSV table does not match'
            ' to a threat raster in the input folder. Please check that the names '
            'correspond. The threat raster that could not be found is : %s',
            uri)

    if dataset is None:
        full_uri = None

    dataset = None
    return full_uri
Пример #15
0
def vsifile_16():

    old_val = gdal.GetUseExceptions()
    gdal.UseExceptions()
    try:
        gdal.Rename('/tmp/i_dont_exist_vsifile_16.tif', '/tmp/me_neither.tif')
        ret = 'fail'
    except RuntimeError:
        ret = 'success'
    if not old_val:
        gdal.DontUseExceptions()
    return ret
Пример #16
0
def has_geos():
    pnt1 = ogr.CreateGeometryFromWkt('POINT(10 20)')
    pnt2 = ogr.CreateGeometryFromWkt('POINT(30 20)')
    ogrex = ogr.GetUseExceptions()
    gdalex = gdal.GetUseExceptions()
    gdal.DontUseExceptions()
    ogr.DontUseExceptions()
    hasgeos = pnt1.Union(pnt2) is not None
    if ogrex:
        ogr.UseExceptions()
    if gdalex:
        gdal.UseExceptions()
    return hasgeos
Пример #17
0
def test_basic_test_17_part_2():

    # For some odd reason, this fails on the Travis CI targets after unrelated
    # changes (https://travis-ci.com/github/OSGeo/gdal/jobs/501940381)
    if gdaltest.skip_on_travis():
        pytest.skip()

    from osgeo import ogr

    for _ in range(2):
        ogr.UseExceptions()
        gdal.UseExceptions()
        flag = False
        try:
            ogr.DontUseExceptions()
            gdal.DontUseExceptions()
            flag = True
        except:
            gdal.DontUseExceptions()
            ogr.DontUseExceptions()
        assert not flag, 'expected failure'
        assert not gdal.GetUseExceptions()
        assert not ogr.GetUseExceptions()
Пример #18
0
def opensAsRaster(filename):
    """
    Return True if filename opens as a GDAL raster, False otherwise
    """
    usingExceptions = False
    if hasattr(gdal, 'GetUseExceptions'):
        usingExceptions = gdal.GetUseExceptions()
    gdal.UseExceptions()
    try:
        ds = gdal.Open(filename)
    except Exception:
        ds = None
    opensOK = (ds is not None)

    if not usingExceptions:
        gdal.DontUseExceptions()
    return opensOK
Пример #19
0
def schema_exists(schema):
    '''Check if a schema is already existing in database.'''
    if PG_CONNECTION is None:
        raise ValueError("Define PG_CONNECTION in pg_connection.py")
    test_connection = PG_CONNECTION + " active_schema=" + schema
    data_source = ogr.Open(test_connection)
    if data_source is None:
        raise ValueError("Failed to open " + PG_CONNECTION)
    gdal.UseExceptions()
    layers_ok = True
    for key in LAYERS:
        defn = LAYERS[key]
        layer = data_source.GetLayerByName(defn.name)
        if layer is None:
            layers_ok = False
            break
    data_source = None
    gdal.DontUseExceptions()
    return layers_ok
Пример #20
0
    def __init__(self):
        """
        Constructor.
        """
        super(InventoryThread, self).__init__()

        self.messenger = InventoryMessages(self)
        self.files = list()
        gdal.DontUseExceptions()
        ogr.DontUseExceptions()
        self.layer_attributes = [
            QgsField('filename', QVariant.String),
            QgsField('date', QVariant.String),
            QgsField('size', QVariant.String),
            QgsField('extension', QVariant.String)
        ]
        self.qgsattr = QgsFields()
        for i in self.layer_attributes:
            self.qgsattr.append(i)
Пример #21
0
    def _warp(source_filename):
        with tempfile.NamedTemporaryFile(suffix=".%s" % driver.extension) as tmp_file:
            try:
                gdal.UseExceptions()
                gdal.Warp(
                    tmp_file.name, source_filename,
                    options=gdal.WarpOptions(
                        format=driver.name, dstSRS=srs.wkt,
                        creationOptions=driver.options
                    ),
                )
            except RuntimeError as e:
                raise ValidationError(str(e))
            finally:
                gdal.DontUseExceptions()

            response = FileResponse(tmp_file.name, content_type=driver.mime)
            response.content_disposition = content_disposition
            return response
Пример #22
0
def open_ds(ds_path):
    """
    Given a path to a raster or vector dataset, returns an opened GDAL or OGR dataset.
    The caller has the responsibility of closing/deleting the dataset when finished.
    :param ds_path: Path to dataset
    :return: Handle to open dataset
    """

    # TODO: Can be a DB Connection
    # if not os.path.isfile(ds_path):
    #    raise Exception("Could not find file {}".format(ds_path))

    # Attempt to open as gdal dataset (raster)
    use_exceptions = gdal.GetUseExceptions()
    gdal.UseExceptions()

    logger.info("Opening the dataset: {}".format(ds_path))
    try:
        gdal_dataset = gdal.Open(ds_path)
        if gdal_dataset:
            return gdal_dataset
    except RuntimeError as ex:
        if ('not recognized as a supported file format' not in str(ex)) or \
                ('Error browsing database for PostGIS Raster tables' in str(ex)):
            raise ex
    finally:
        if not use_exceptions:
            gdal.DontUseExceptions()

    # Attempt to open as ogr dataset (vector)
    # ogr.UseExceptions doesn't seem to work reliably, so just check for Open returning None
    ogr_dataset = ogr.Open(ds_path)

    if not ogr_dataset:
        logger.debug("Unknown file format: {0}".format(ds_path))
        return None

    return ogr_dataset
Пример #23
0
        print('Computing stats for ' + fn)
        ds.GetRasterBand(1).ComputeStatistics(False)

# Or you could use exceptions and a try/except block.
gdal.UseExceptions()
for fn in file_list:
    try:
        ds = gdal.Open(fn)
        ds.GetRasterBand(1).ComputeStatistics(False)
    except:
        print('Could not compute stats for ' + fn)
        # Uncomment this if you also want it to print the gdal error message.
        # print(gdal.GetLastErrorMsg())

# Turn exceptions off.
gdal.DontUseExceptions()

# How about the second example, but without the gdal error message?
gdal.PushErrorHandler('CPLQuietErrorHandler')
for fn in file_list:
    ds = gdal.Open(fn)
    if ds is None:
        print('Could not compute stats for ' + fn)
    else:
        print('Computing stats for ' + fn)
        ds.GetRasterBand(1).ComputeStatistics(False)

# Get the default error handler back.
gdal.PopErrorHandler()

Пример #24
0
def addStatistics(ds, progress, ignore=None, approx_ok=False):
    """
    Calculates statistics and adds them to the image
    
    Uses gdal.Band.ComputeStatistics() for mean, stddev, min and max,
    and gdal.Band.GetHistogram() to do histogram calculation. 
    The median and mode are estimated using the histogram, and so 
    for larger datatypes, they will be approximate only. 
    
    For thematic layers, the histogram is calculated with as many bins 
    as required, for athematic integer and float types, a maximum
    of 256 bins is used. 
    
    """
    progress.setLabelText("Computing Statistics...")
    progress.setProgress(0)
    percent = 0
    percentstep = 100.0 / (ds.RasterCount * 2)  # 2 steps for each layer

    # flush the cache. The ensures that any unwritten data is
    # written to file so we get the right stats. It also
    # makes sure any metdata is written on HFA. This means
    # the LAYER_TYPE setting will be picked up by rat.SetLinearBinning()
    ds.FlushCache()

    for bandnum in range(ds.RasterCount):
        band = ds.GetRasterBand(bandnum + 1)

        # fill in the metadata
        tmpmeta = band.GetMetadata()

        if ignore is not None:
            # tell QGIS that the ignore value was ignored
            band.SetNoDataValue(ignore)
            tmpmeta["STATISTICS_EXCLUDEDVALUES"] = repr(
                ignore)  # doesn't seem to do anything

        # get GDAL to calculate statistics - force recalculation. Trap errors
        useExceptions = gdal.GetUseExceptions()
        gdal.UseExceptions()
        try:
            if approx_ok and "LAYER_TYPE" in tmpmeta and tmpmeta[
                    "LAYER_TYPE"] == "thematic":
                warnings.warn(
                    'WARNING: approx_ok specified for stats but image is thematic (this could be a bad idea)'
                )

            (minval, maxval, meanval,
             stddevval) = band.ComputeStatistics(approx_ok)
        except RuntimeError as e:
            if str(e).endswith(
                    'Failed to compute statistics, no valid pixels found in sampling.'
            ):
                minval = ignore
                maxval = ignore
                meanval = ignore
                stddevval = 0
            else:
                raise e
        if not useExceptions:
            gdal.DontUseExceptions()

        percent = percent + percentstep
        progress.setProgress(percent)

        tmpmeta["STATISTICS_MINIMUM"] = repr(minval)
        tmpmeta["STATISTICS_MAXIMUM"] = repr(maxval)
        tmpmeta["STATISTICS_MEAN"] = repr(meanval)
        tmpmeta["STATISTICS_STDDEV"] = repr(stddevval)
        # because we did at full res - these are the default anyway

        if approx_ok:
            tmpmeta["STATISTICS_APPROXIMATE"] = "YES"
        else:
            tmpmeta["STATISTICS_SKIPFACTORX"] = "1"
            tmpmeta["STATISTICS_SKIPFACTORY"] = "1"

        # create a histogram so we can do the mode and median
        if band.DataType == gdal.GDT_Byte:
            # if byte data use 256 bins and the whole range
            histmin = 0
            histmax = 255
            histstep = 1.0
            histCalcMin = -0.5
            histCalcMax = 255.5
            histnbins = 256
            tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'direct'
        elif "LAYER_TYPE" in tmpmeta and tmpmeta["LAYER_TYPE"] == 'thematic':
            # all other thematic types a bin per value
            histmin = 0
            histmax = int(numpy.ceil(maxval))
            histstep = 1.0
            histCalcMin = -0.5
            histCalcMax = maxval + 0.5
            histnbins = histmax + 1
            tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'direct'
        elif band.DataType in gdalLargeIntTypes:
            histrange = int(numpy.ceil(maxval) - numpy.floor(minval)) + 1
            (histmin, histmax) = (minval, maxval)
            if histrange <= 256:
                histnbins = histrange
                histstep = 1.0
                tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'direct'
                histCalcMin = histmin - 0.5
                histCalcMax = histmax + 0.5
            else:
                histnbins = 256
                tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'linear'
                histCalcMin = histmin
                histCalcMax = histmax
                histstep = float(histCalcMax - histCalcMin) / histnbins
        elif band.DataType in gdalFloatTypes:
            histnbins = 256
            (histmin, histmax) = (minval, maxval)
            tmpmeta["STATISTICS_HISTOBINFUNCTION"] = 'linear'
            histCalcMin = minval
            histCalcMax = maxval
            histstep = float(histCalcMax - histCalcMin) / histnbins
        # Note that the complex number data types are not handled, as I am not sure
        # what a histogram or a median would mean for such types.

        userdata = ProgressUserData()
        userdata.progress = progress
        userdata.nbands = ds.RasterCount * 2
        userdata.curroffset = percent

        # get histogram and force GDAL to recalculate it
        hist = band.GetHistogram(histCalcMin, histCalcMax, histnbins, False,
                                 approx_ok, progressFunc, userdata)

        # Check if GDAL's histogram code overflowed. This is not a fool-proof test,
        # as some overflows will not result in negative counts.
        histogramOverflow = (min(hist) < 0)

        # we may use this ratObj reference for the colours below also
        # may be None if format does not support RATs
        ratObj = band.GetDefaultRAT()

        if not histogramOverflow:
            # comes back as a list for some reason
            hist = numpy.array(hist)

            # Note that we have explicitly set histstep in each datatype case
            # above. In principle, this can be calculated, as it is done in the
            # float case, but for some of the others we need it to be exactly
            # equal to 1, so we set it explicitly there, to avoid rounding
            # error problems.

            # do the mode - bin with the highest count
            modebin = numpy.argmax(hist)
            modeval = modebin * histstep + histmin
            if band.DataType == gdal.GDT_Float32 or band.DataType == gdal.GDT_Float64:
                tmpmeta["STATISTICS_MODE"] = repr(modeval)
            else:
                tmpmeta["STATISTICS_MODE"] = repr(int(round(modeval)))

            if ratObj is not None:
                histIndx, histNew = findOrCreateColumn(ratObj,
                                                       gdal.GFU_PixelCount,
                                                       "Histogram",
                                                       gdal.GFT_Real)
                # write the hist in a single go
                ratObj.SetRowCount(histnbins)
                ratObj.WriteArray(hist, histIndx)

                ratObj.SetLinearBinning(histmin, (histCalcMax - histCalcMin) /
                                        histnbins)

                # The HFA driver still honours the STATISTICS_HISTOBINVALUES
                # metadata item. If we are recalculating the histogram the old
                # values will be copied across with the metadata so clobber it
                if "STATISTICS_HISTOBINVALUES" in tmpmeta:
                    del tmpmeta["STATISTICS_HISTOBINVALUES"]
            else:
                # old method
                tmpmeta["STATISTICS_HISTOBINVALUES"] = '|'.join(map(
                    repr, hist)) + '|'

                tmpmeta["STATISTICS_HISTOMIN"] = repr(histmin)
                tmpmeta["STATISTICS_HISTOMAX"] = repr(histmax)
                tmpmeta["STATISTICS_HISTONUMBINS"] = repr(histnbins)

            # estimate the median - bin with the middle number
            middlenum = hist.sum() / 2
            gtmiddle = hist.cumsum() >= middlenum
            medianbin = gtmiddle.nonzero()[0][0]
            medianval = medianbin * histstep + histmin
            if band.DataType == gdal.GDT_Float32 or band.DataType == gdal.GDT_Float64:
                tmpmeta["STATISTICS_MEDIAN"] = repr(medianval)
            else:
                tmpmeta["STATISTICS_MEDIAN"] = repr(int(round(medianval)))

        # set the data
        band.SetMetadata(tmpmeta)

        if ratObj is not None and not ratObj.ChangesAreWrittenToFile():
            # For drivers that require the in memory thing
            band.SetDefaultRAT(ratObj)

        percent = percent + percentstep
        progress.setProgress(percent)

        if progress.wasCancelled():
            raise ProcessCancelledError()

    progress.setProgress(100)
Пример #25
0
def writeColumnToBand(gdalBand,
                      colName,
                      sequence,
                      colType=None,
                      colUsage=gdal.GFU_Generic):
    """
    Given a GDAL band, Writes the data specified in sequence 
    (can be list, tuple or array etc)
    to the named column in the attribute table assocated with the
    gdalBand. colType must be one of gdal.GFT_Integer,gdal.GFT_Real,gdal.GFT_String.
    can specify one of the gdal.GFU_* constants for colUsage - default is 'generic'
    GDAL dataset must have been created, or opened with GA_Update
    """

    if colType is None:
        colType = inferColumnType(sequence)
    if colType is None:
        msg = "Can't infer type of column for sequence of %s" % type(
            sequence[0])
        raise rioserrors.AttributeTableTypeError(msg)

    # check it is acually a valid type
    elif colType not in (gdal.GFT_Integer, gdal.GFT_Real, gdal.GFT_String):
        msg = "coltype must be a valid gdal column type"
        raise rioserrors.AttributeTableTypeError(msg)

    # things get a bit weird here as we need different
    # behaviour depending on whether we have an RFC40
    # RAT or not.
    if hasattr(gdal.RasterAttributeTable, "WriteArray"):
        # new behaviour
        attrTbl = gdalBand.GetDefaultRAT()
        if attrTbl is None:
            # some formats eg ENVI return None
            # here so we need to be able to cope
            attrTbl = gdal.RasterAttributeTable()
            isFileRAT = False
        else:

            isFileRAT = True

            # but if it doesn't support dynamic writing
            # we still ahve to call SetDefaultRAT
            if not attrTbl.ChangesAreWrittenToFile():
                isFileRAT = False

    else:
        # old behaviour
        attrTbl = gdal.RasterAttributeTable()
        isFileRAT = False

    # thanks to RFC40 we need to ensure colname doesn't already exist
    colExists = False
    for n in range(attrTbl.GetColumnCount()):
        if attrTbl.GetNameOfCol(n) == colName:
            colExists = True
            colNum = n
            break
    if not colExists:
        # preserve usage
        attrTbl.CreateColumn(colName, colType, colUsage)
        colNum = attrTbl.GetColumnCount() - 1

    rowsToAdd = len(sequence)
    # Imagine has trouble if not 256 items for byte
    if gdalBand.DataType == gdal.GDT_Byte:
        rowsToAdd = 256

    # another hack to hide float (0-1) and int (0-255)
    # color table handling.
    # we assume that the column has already been created
    # of the right type appropriate for the format (maybe by calcstats)
    # Note: this only works post RFC40 when we have an actual reference
    # to the RAT rather than a new one so we can ask GetTypeOfCol
    usage = attrTbl.GetUsageOfCol(colNum)
    if (isColorColFromUsage(usage)
            and attrTbl.GetTypeOfCol(colNum) == gdal.GFT_Real
            and colType == gdal.GFT_Integer):
        sequence = numpy.array(sequence, dtype=numpy.float)
        sequence = sequence / 255.0

    if hasattr(attrTbl, "WriteArray"):
        # if GDAL > 1.10 has these functions
        # thanks to RFC40
        attrTbl.SetRowCount(rowsToAdd)
        attrTbl.WriteArray(sequence, colNum)

    elif HAVE_TURBORAT:
        # use turborat to write values to RAT if available
        if not isinstance(sequence, numpy.ndarray):
            # turborat.writeColumn needs an array
            sequence = numpy.array(sequence)

        # If the dtype of the array is some unicode type, then convert to simple string type,
        # as turborat does not cope with the unicode variant.
        if 'U' in str(sequence.dtype):
            sequence = sequence.astype(numpy.character)

        turborat.writeColumn(attrTbl, colNum, sequence, rowsToAdd)
    else:
        defaultValues = {
            gdal.GFT_Integer: 0,
            gdal.GFT_Real: 0.0,
            gdal.GFT_String: ''
        }

        # go thru and set each value into the RAT
        for rowNum in range(rowsToAdd):
            if rowNum >= len(sequence):
                # they haven't given us enough values - fill in with default
                val = defaultValues[colType]
            else:
                val = sequence[rowNum]

            if colType == gdal.GFT_Integer:
                # appears that swig cannot convert numpy.int64
                # to the int type required by SetValueAsInt
                # so we need to cast.
                # This is a problem as readColumn returns numpy.int64
                # for integer columns.
                # Seems fine converting numpy.float64 to
                # float however for SetValueAsDouble.
                attrTbl.SetValueAsInt(rowNum, colNum, int(val))
            elif colType == gdal.GFT_Real:
                attrTbl.SetValueAsDouble(rowNum, colNum, float(val))
            else:
                attrTbl.SetValueAsString(rowNum, colNum, val)

    if not isFileRAT:
        # assume existing bands re-written
        # Use GDAL's exceptions to trap the error message which arises when
        # writing to a format which does not support it
        usingExceptions = gdal.GetUseExceptions()
        gdal.UseExceptions()
        try:
            gdalBand.SetDefaultRAT(attrTbl)
        except Exception:
            pass
        if not usingExceptions:
            gdal.DontUseExceptions()
Пример #26
0
def basic_test_11():

    ds = gdal.OpenEx('data/byte.tif')
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_VECTOR)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_ALL)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_UPDATE)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR | gdal.OF_UPDATE | gdal.OF_VERBOSE_ERROR)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', allowed_drivers = [] )
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', allowed_drivers = ['GTiff'] )
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('data/byte.tif', allowed_drivers = ['PNG'] )
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    with gdaltest.error_handler():
        ds = gdal.OpenEx('data/byte.tif', open_options = ['FOO'] )
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ar_ds = [ gdal.OpenEx('data/byte.tif', gdal.OF_SHARED) for i in range(1024) ]
    if ar_ds[1023] is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ar_ds = None

    ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER)
    if ds is not None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_VECTOR)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetLayerCount() != 1:
        gdaltest.post_reason('fail')
        return 'fail'
    if ds.GetLayer(0) is None:
        gdaltest.post_reason('fail')
        return 'fail'
    ds.GetLayer(0).GetMetadata()

    ds = gdal.OpenEx('../ogr/data/poly.shp', allowed_drivers = ['ESRI Shapefile'] )
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER | gdal.OF_VECTOR)
    if ds is None:
        gdaltest.post_reason('fail')
        return 'fail'

    ds = gdal.OpenEx('non existing')
    if ds is not None or gdal.GetLastErrorMsg() != '':
        gdaltest.post_reason('fail')
        return 'fail'

    gdal.PushErrorHandler('CPLQuietErrorHandler')
    ds = gdal.OpenEx('non existing', gdal.OF_VERBOSE_ERROR)
    gdal.PopErrorHandler()
    if ds is not None or gdal.GetLastErrorMsg() == '':
        gdaltest.post_reason('fail')
        return 'fail'

    old_use_exceptions_status = gdal.GetUseExceptions()
    gdal.UseExceptions()
    got_exception = False
    try:
        ds = gdal.OpenEx('non existing')
    except:
        got_exception = True
    if old_use_exceptions_status == 0:
        gdal.DontUseExceptions()
    if not got_exception:
        gdaltest.post_reason('fail')
        return 'fail'

    return 'success'
Пример #27
0
def mosaicTiledOutputs(outputDirectory):
    import imp
    curDir = os.path.dirname(__file__)
    parentDir = os.path.dirname(curDir)
    gdal_mergePy = os.path.join(parentDir, "GDAL_Resources", "Utilities", "gdal_merge.py")
    
    gdal_merge = imp.load_source("gdal_merge", gdal_mergePy)
    
    
    for m in ['Bin', 'Prob', 'Resid', 'MESS', 'Mod']:
            tilesFolder = os.path.join(outputDirectory, m + "Tiff")
            print "tilesFolder", tilesFolder
            if os.path.exists(tilesFolder):
                modelAbbrev = os.path.split(outputDirectory)[1].split("_")[0]
                outFname = os.path.join(outputDirectory, "_".join([modelAbbrev, m.lower(), "map.tif"]))
                print "outFname", outFname
                
                onlyfiles = [os.path.join(tilesFolder,f) for f in os.listdir(tilesFolder) 
                             if os.path.isfile(os.path.join(tilesFolder,f)) and f.endswith(".tif") ]
                
                NDValue = getNDVal(onlyfiles[0])
                 
                args = ["placeholder", "-o", outFname] + onlyfiles
                gdal.DontUseExceptions()
                gdal_merge.main(args)
                
                dataset = gdal.Open( outFname, gdal.GA_Update )
                dataset.GetRasterBand(1).SetNoDataValue(float(NDValue))
                dataset.GetRasterBand(1).ComputeStatistics(1)
                
                if m == 'Mod':
                    #we must merge the dbf files as well.  ugg.
                    only_dbf_files = [os.path.join(tilesFolder,f) for f in os.listdir(tilesFolder) 
                             if os.path.isfile(os.path.join(tilesFolder,f)) and f.endswith(".vat.dbf") ]
                    dbf_0_f = open(only_dbf_files[0], "rb")
                    dbf_0 = list(utilities.dbfreader(dbf_0_f))
                    dbf_0_f.close()
                    
                    fieldnames, fieldspecs = (dbf_0)[:2]
                    all_values = set([val[1].strip() for val in dbf_0[2:]])
                    
                    for dbf_file in only_dbf_files[1:]:
                        dbf_f = open(dbf_file, 'rb')
                        dbf_list = list(utilities.dbfreader(dbf_f))
                        dbf_f.close()
                    
                        dbf_n_values = set([val[1].strip() for val in dbf_list[2:]])
                        all_values = all_values | dbf_n_values
                        
                    dbf_out_fname = outFname.replace(".tif", ".tif.vat.dbf")
                    dbf_out_f = open(dbf_out_fname, 'wb')
                    all_values = zip(range(1, len(all_values) + 1), list(all_values))
                    utilities.dbfwriter(dbf_out_f, fieldnames, fieldspecs, all_values)
                    dbf_out_f.close()
                    
                try:
                    shutil.rmtree(tilesFolder)
                except:
                    #can run into latency problems with the thumbs.db in windows.
                    #if we can't clean up this folder it's not the end of the world.
                    pass
Пример #28
0
def test_basic_test_11():

    ds = gdal.OpenEx('data/byte.tif')
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER)
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_VECTOR)
    assert ds is None

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR)
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_ALL)
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', gdal.OF_UPDATE)
    assert ds is not None

    ds = gdal.OpenEx(
        'data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR | gdal.OF_UPDATE
        | gdal.OF_VERBOSE_ERROR)
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', allowed_drivers=[])
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['GTiff'])
    assert ds is not None

    ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['PNG'])
    assert ds is None

    with gdaltest.error_handler():
        ds = gdal.OpenEx('data/byte.tif', open_options=['FOO'])
    assert ds is not None

    ar_ds = [gdal.OpenEx('data/byte.tif', gdal.OF_SHARED) for _ in range(1024)]
    assert ar_ds[1023] is not None
    ar_ds = None

    ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER)
    assert ds is None

    ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_VECTOR)
    assert ds is not None
    assert ds.GetLayerCount() == 1
    assert ds.GetLayer(0) is not None
    ds.GetLayer(0).GetMetadata()

    ds = gdal.OpenEx('../ogr/data/poly.shp',
                     allowed_drivers=['ESRI Shapefile'])
    assert ds is not None

    ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER | gdal.OF_VECTOR)
    assert ds is not None

    ds = gdal.OpenEx('non existing')
    assert ds is None and gdal.GetLastErrorMsg() == ''

    gdal.PushErrorHandler('CPLQuietErrorHandler')
    ds = gdal.OpenEx('non existing', gdal.OF_VERBOSE_ERROR)
    gdal.PopErrorHandler()
    assert ds is None and gdal.GetLastErrorMsg() != ''

    old_use_exceptions_status = gdal.GetUseExceptions()
    gdal.UseExceptions()
    got_exception = False
    try:
        ds = gdal.OpenEx('non existing')
    except RuntimeError:
        got_exception = True
    if old_use_exceptions_status == 0:
        gdal.DontUseExceptions()
    assert got_exception
Пример #29
0
 def DontUseExceptions():
     gdal.DontUseExceptions()
     gdal.SetConfigOption('PYHTON_USE_EXCEPTIONS', 'FALSE')
    def processAlgorithm(self, parameters, context, feedback):
        inp_rast = self.parameterAsRasterLayer(parameters, self.INPUT, context)

        grib_filename = self.parameterAsString(parameters, self.OUTPUT, context)
        if not grib_filename:
            raise QgsProcessingException(self.tr('You need to specify output filename.'))

        idp = inp_rast.dataProvider()
        crs = idp.crs()
        outputFormat = QgsRasterFileWriter.driverForExtension('.tif')

        height = inp_rast.height()
        width = inp_rast.width()
        inp_block = idp.block(1, idp.extent(), width, height)

        rfw = QgsRasterFileWriter(grib_filename + '.tif')
        rfw.setOutputProviderKey('gdal')
        rfw.setOutputFormat(outputFormat)
        rdp = rfw.createMultiBandRaster(
            Qgis.Float32,
            width,
            height,
            idp.extent(),
            crs,
            2
        )

        rdp.setEditable(True)
        x_block = QgsRasterBlock(Qgis.Float32, width, height)
        y_block = QgsRasterBlock(Qgis.Float32, width, height)
        diag = 1. / sqrt(2)

        # resulting raster has no NODATA value set, which
        # is not treated correctly in MDAL 0.2.0. See
        # see https://github.com/lutraconsulting/MDAL/issues/104
        # therefore set some small value to overcome the issue
        dir_map = {
            8: (1e-7, 1),
            9: (diag, diag),
            6: (1, 1e-7),
            3: (diag, -diag),
            2: (1e-7, -1),
            1: (-diag, -diag),
            4: (-1, 1e-7),
            7: (-diag, diag),
            5: (0, 0)
        }
        for row in range(height):
            for col in range(width):
                dir_ind = inp_block.value(row, col)
                try:
                    x_val, y_val = dir_map.get(dir_ind, 255)
                except TypeError:
                    x_val, y_val = (0, 0)
                x_block.setValue(row, col, x_val)
                y_block.setValue(row, col, y_val)

        rdp.writeBlock(x_block, 1)
        rdp.writeBlock(y_block, 2)
        rdp.setNoDataValue(1, inp_block.noDataValue())
        rdp.setNoDataValue(2, inp_block.noDataValue())
        rdp.setEditable(False)

        # rewrite the resulting raster as GRIB using GDAL for setting metadata
        gdal.UseExceptions()
        try:
            res_tif = gdal.Open(grib_filename + '.tif')
            grib_driver = gdal.GetDriverByName('GRIB')
            grib = grib_driver.CreateCopy(grib_filename, res_tif)
        except Exception as e:
            raise QgsProcessingException('Unable to convert to grib file with GDAL: ' + str(e))
        gdal.DontUseExceptions()

        band_names = ('x-flow', 'y-flow')
        for i in range(2):
            band_nr = i + 1
            band_name = band_names[i]
            grib_band = grib.GetRasterBand(band_nr)
            grib_band.SetMetadataItem('grib_comment', band_name)
            grib_band.SetNoDataValue(255)
            grib_band.SetDescription(band_name)
            res_tif_band_array = res_tif.GetRasterBand(band_nr).ReadAsArray()
            grib_band.WriteArray(res_tif_band_array)
            feedback.setProgress(band_nr * 50)
        grib = None
        res_tif = None

        return {self.OUTPUT: grib_filename}