def processInput(i):
	values=[]
	for var in varslist:	
		filetif= dirbase +"//"+var+"//"+var+"_"+str(i)+ ".tif"
		if os.path.exists(filetif):	
			src_filename = filetif
			src_ds=gdal.Open(src_filename) 
			gt=src_ds.GetGeoTransform()
			px = int((mx - gt[0]) / gt[1]) #x pixel
			py = int((my - gt[3]) / gt[5]) #y pixel			############### print "[ RASTER BAND COUNT ]: ", src_ds.RasterCount
			
			for band in range( src_ds.RasterCount ):
				band += 1
				srcband = src_ds.GetRasterBand(band)
				structval = srcband.ReadRaster(px,py,1,1,buf_type=srcband.DataType )	
				bandtype = gdal.GetDataTypeName(srcband.DataType)
				intval = struct.unpack(fmttypes[bandtype] , structval)
				val=int(intval[0])*0.1
				values.append(var+"_"+str(i)+"-"+str(band)+"_"+str(val))
	return values 
Пример #2
0
def make_r32(dem, regionWidth, regionHeight, resolution):
    band = dem.GetRasterBand(1)

    xoff = 0
    yoff = 0
    xsize = 9
    ysize = 9
    buf_xsize = regionWidth + resolution * 2
    buf_ysize = regionHeight + resolution * 2
    datatype = band.DataType
    data_types = {
        'Byte': 'B',
        'UInt16': 'H',
        'Int16': 'h',
        'UInt32': 'I',
        'Int32': 'i',
        'Float32': 'f',
        'Float64': 'd'
    }
    type_struct = data_types[gdal.GetDataTypeName(datatype)]
    print type_struct

    bufferStr = band.ReadRaster(xoff, yoff, xsize, ysize, buf_xsize, buf_ysize,
                                datatype)
    bufferArray = struct.unpack(type_struct * buf_xsize * buf_ysize, bufferStr)
    buffer2d = numpy.array(bufferArray).reshape(buf_xsize, buf_ysize)

    # interpolate data to upgrade from 30m to 1m
    interpolate(buffer2d, regionWidth, regionHeight, resolution)
    #remove buffer
    noBuffer = numpy.vsplit(
        numpy.hsplit(buffer2d, (resolution, resolution + regionWidth))[1],
        (resolution, resolution + regionHeight))[1]

    #.r32 file records heightfield from west to east, then from south to north
    r32 = numpy.zeros((regionHeight, regionWidth), dtype=numpy.float32)
    for j in range(regionHeight):
        r32[j] = noBuffer[regionHeight - 1 - j]
    regionName = 'xxx'
    r32.tofile(regionName + ".r32")
    print regionName + ".r32 generated!"
Пример #3
0
def get_ndv_b(b):
    """Get NoData value for GDAL band.

    If NoDataValue is not set in the band, 
    extract upper left and lower right pixel values.
    Otherwise assume NoDataValue is 0.
 
    Parameters
    ----------
    b : GDALRasterBand object 
        This is the input band.
 
    Returns
    -------
    b_ndv : float 
        NoData value 
    """

    b_ndv = b.GetNoDataValue()
    if b_ndv is None:
        #Check ul pixel for ndv
        ns = b.XSize
        nl = b.YSize
        ul = float(b.ReadAsArray(0, 0, 1, 1))
        #ur = float(b.ReadAsArray(ns-1, 0, 1, 1))
        lr = float(b.ReadAsArray(ns - 1, nl - 1, 1, 1))
        #ll = float(b.ReadAsArray(0, nl-1, 1, 1))
        #Probably better to use 3/4 corner criterion
        #if ul == ur == lr == ll:
        if np.isnan(ul) or ul == lr:
            b_ndv = ul
        else:
            #Assume ndv is 0
            b_ndv = 0
    elif np.isnan(b_ndv):
        b_dt = gdal.GetDataTypeName(b.DataType)
        if 'Float' in b_dt:
            b_ndv = np.nan
        else:
            b_ndv = 0
    return b_ndv
def gdalReadAsArraySetsmSceneBand(raster_band, make_nodata_nan=False):
    scale = raster_band.GetScale()
    offset = raster_band.GetOffset()
    if scale is None:
        scale = 1.0
    if offset is None:
        offset = 0.0
    if scale == 1.0 and offset == 0.0:
        array_data = raster_band.ReadAsArray()
        if make_nodata_nan:
            nodata_val = raster_band.GetNoDataValue()
            if nodata_val is not None:
                array_data[array_data == nodata_val] = np.nan
    else:
        if raster_band.DataType != gdalconst.GDT_Int32:
            raise RasterIOError(
                "Expected GDAL raster band with scale!=1.0 or offset!=0.0 to be of Int32 data type"
                " (scaled int LERC_ZSTD-compressed 50cm DEM), but data type is {}".format(
                    gdal.GetDataTypeName(raster_band.DataType)
                )
            )
        if scale == 0.0:
            raise RasterIOError(
                "GDAL raster band has invalid parameters: scale={}, offset={}".format(scale, offset)
            )
        nodata_val = raster_band.GetNoDataValue()
        array_data = raster_band.ReadAsArray(buf_type=gdalconst.GDT_Float32)
        adjust_where = (array_data != nodata_val) if nodata_val is not None else True
        if scale != 1.0:
            np.multiply(array_data, scale, out=array_data, where=adjust_where)
        if offset != 0.0:
            np.add(array_data, offset, out=array_data, where=adjust_where)
        if make_nodata_nan:
            array_nodata = np.logical_not(adjust_where, out=adjust_where)
            array_data[array_nodata] = np.nan
        del adjust_where

    if array_data is None:
        raise RasterIOError("`raster_band.ReadAsArray()` returned None")

    return array_data
Пример #5
0
def defaultNoData(GDALdatatype, signedByte=False):
    '''returns a reasonable default NoData value for a given 
    GDAL data type.
    '''
    if signedByte:
        return -128

    crossWalk = {
        "Unknown": 0,
        "Byte": 255,
        "Int16": -32768,
        "UInt32": 4294967295,
        "Int32": -2147483648,
        "Float32": -3.4028235e+038,
        "Float64": 2.2250738585072014e-308,
        "CInt16": -32768,
        "CInt32": -2147483648,
        "CFloat32": -3.4028235e+038,
        "CFloat64": 2.2250738585072014e-308,
    }
    return crossWalk[gdal.GetDataTypeName(GDALdatatype)]
Пример #6
0
def gdal_project_raster(inputFileName, outputFileName, proj4stringSource,
                        proj4stringTarget):
    """
    Function to project a raster
    """
    rst = gdal.Open(inputFileName)
    b1 = rst.GetRasterBand(1)
    nodata = b1.GetNoDataValue()
    bandtype = gdal.GetDataTypeName(b1.DataType)

    if (os.path.isfile(outputFileName) == True):
        os.system("rm " + outputFileName)

    cmd = 'gdalwarp -s_srs \"%s\" -t_srs \"%s\" -dstnodata %i %s %s' % (
        proj4stringSource, proj4stringTarget, nodata, inputFileName,
        outputFileName)
    cmd = 'gdalwarp -ot ' + bandtype + ' -s_srs \"' + proj4stringSource + '\" -t_srs \"' + proj4stringTarget + '\" -srcnodata ' + str(
        nodata) + ' -dstnodata ' + str(
            nodata) + ' ' + inputFileName + ' ' + outputFileName
    print cmd
    os.system(cmd)
 def get_dem_z(dem_dataset, x_off, y_off, col_size, row_size):
     try:
         band = dem_dataset.GetRasterBand(1)
         data_types = {
             'Byte': 'B',
             'UInt16': 'H',
             'Int16': 'h',
             'UInt32': 'I',
             'Int32': 'i',
             'Float32': 'f',
             'Float64': 'd'
         }
         data_type = band.DataType
         data = band.ReadRaster(x_off, y_off, col_size, row_size, col_size,
                                row_size, data_type)
         data = struct.unpack(
             data_types[gdal.GetDataTypeName(band.DataType)] * col_size *
             row_size, data)
         return data
     except struct.error:
         return [0]
def create_vrt_from_filelist(vrt_name, filelist):
    rootNode = ET.Element('VRTDataset')

    totalXSize = 512
    totalYSize = 512

    for filename in filelist:
        logger.debug(filename)
        ds = gdal.Open(filename)

        logger.debug("[ RASTER BAND COUNT ]: {}".format(ds.RasterCount))
        for band_number in range(ds.RasterCount):
            band_number += 1
            bandNode = ET.SubElement(rootNode, "VRTRasterBand", {'band': '1'})

            sourceNode = ET.SubElement(bandNode, 'SimpleSource')
            node = ET.SubElement(sourceNode, 'SourceFilename',
                                 {'relativeToVRT': '1'})
            node.text = filename
            node = ET.SubElement(sourceNode, 'SourceBand')
            node.text = str(band_number)

            band = ds.GetRasterBand(band_number)
            dataType = gdal.GetDataTypeName(band.DataType)
            bandNode.attrib['dataType'] = dataType

    rootNode.attrib['rasterXSize'] = str(totalXSize)
    rootNode.attrib['rasterYSize'] = str(totalYSize)

    node = ET.SubElement(rootNode, 'SRS')
    node.text = ds.GetProjection()  # projection

    stringToReturn = ET.tostring(rootNode)
    logger.debug(stringToReturn)

    #if not os.path.isfile( vrt_name):
    writer = open(vrt_name, 'w')
    writer.write(stringToReturn)
    writer.close()
Пример #9
0
    def __init__(self,
                 path,
                 data=None,
                 shape=None,
                 chunks=None,
                 dtype=None,
                 **kwargs):
        self.path = path
        self.attrs = Attrs(self.gp_path, os.path.basename(path))
        self.set_env(**kwargs)

        # Create the dataset if it does not exist
        if not os.path.isfile(self.raster_path):
            self.write_data(chunks, shape, data, dtype)
        else:
            ds = gdal.Open(self.raster_path)
            self.shape = (ds.RasterYSize, ds.RasterXSize)
            band = ds.GetRasterBand(1)
            self.chunks = band.GetBlockSize()
            self.dtype = dtype_to_np(gdal.GetDataTypeName(band.DataType))
            band = None
            ds = None
Пример #10
0
    def __init__(self, rasterLayer, feedback):
        self.feedback = feedback
        self.rasterLayer = rasterLayer
        self.dataProv = rasterLayer.dataProvider()

        self.myExtent = self.dataProv.extent()
        self.theWidth = self.rasterLayer.width()
        self.theHeight = self.rasterLayer.height()
        self.pixelSizeX = self.rasterLayer.rasterUnitsPerPixelX()
        self.pixelSizeY = self.rasterLayer.rasterUnitsPerPixelY()
        #mittler Rasterweite aus xSize und ySize
        self.rasterWidth = (self.pixelSizeX + self.pixelSizeY) / 2

        dataset = gdal.Open(self.rasterLayer.source(), GA_ReadOnly)
        geotransform = dataset.GetGeoTransform()
        bandNo = 1
        band = dataset.GetRasterBand(bandNo)
        self.nodata = band.GetNoDataValue()
        bandtype = gdal.GetDataTypeName(band.DataType)
        interpolMethod = 1  #0=nearestNeighbor, 1=linear 2x2 Pixel, 2=bicubic 4x4 Pixel
        self.interpolator = RasterInterpolator(rasterLayer, interpolMethod,
                                               bandNo, dataset, self.feedback)
Пример #11
0
 def getGdalInfos(self):
     '''Extract data type infos'''
     if self.path is None or not self.fileExists:
         raise IOError("Cannot find file on disk")
     # Open dataset
     ds = gdal.Open(self.path, gdal.GA_ReadOnly)
     # Get raster size
     self.size = xy(ds.RasterXSize, ds.RasterYSize)
     # Get format
     self.format = ds.GetDriver().ShortName
     if self.format in ['JP2OpenJPEG', 'JP2ECW', 'JP2KAK', 'JP2MrSID']:
         self.format = 'JPEG2000'
     # Get band
     self.nbBands = ds.RasterCount
     b1 = ds.GetRasterBand(
         1)  #first band (band index does not count from 0)
     self.noData = b1.GetNoDataValue()
     # Get data type
     ddtype = gdal.GetDataTypeName(
         b1.DataType)  #Byte, UInt16, Int16, UInt32, Int32, Float32, Float64
     if ddtype == "Byte":
         self.dtype = 'uint'
         self.depth = 8
     else:
         self.dtype = ddtype[0:len(ddtype) - 2].lower()
         self.depth = int(ddtype[-2:])
     #Get Georef
     params = ds.GetGeoTransform()
     if params is not None:
         topleftx, pxsizex, rotx, toplefty, roty, pxsizey = params
         #instead of worldfile, topleft geotag is at corner, so adjust it to pixel center
         topleftx += abs(pxsizex / 2)
         toplefty -= abs(pxsizey / 2)
         #assign to class properties
         self.origin = xy(topleftx, toplefty)
         self.pxSize = xy(pxsizex, pxsizey)
         self.rotation = xy(rotx, roty)
     #Close (gdal haven't garbage collector)
     ds, b1 = None, None
Пример #12
0
def get_statistics_for_file(filename):
    from osgeo import gdal
    raster = gdal.Open(filename)
    return_dict = {}
    for i in range(1, 4):
        (r_min, r_max, r_mean,
         r_std) = raster.GetRasterBand(i).GetStatistics(True, True)
        data_type = gdal.GetDataTypeName(raster.GetRasterBand(i).DataType)
        range_min = 0.0
        range_max = 255.0
        if data_type == 'byte':
            range_min = 0.0
            range_max = 255.0
        return_dict[str(i)] = {
            'min': r_min,
            'max': r_max,
            'mean': r_mean,
            'std': r_std,
            'range_min': range_min,
            'range_max': range_max
        }
    return return_dict
Пример #13
0
def test_mrf_overview_nnb_fact_2():

    expected_cs = 1087
    for dt in [
            gdal.GDT_Byte, gdal.GDT_Int16, gdal.GDT_UInt16, gdal.GDT_Int32,
            gdal.GDT_UInt32, gdal.GDT_Float32, gdal.GDT_Float64
    ]:

        out_ds = gdal.Translate(
            '/vsimem/out.mrf',
            'data/byte.tif',
            format='MRF',
            creationOptions=['COMPRESS=NONE', 'BLOCKSIZE=10'],
            outputType=dt)
        out_ds.BuildOverviews('NEARNB', [2])
        out_ds = None

        ds = gdal.Open('/vsimem/out.mrf')
        cs = ds.GetRasterBand(1).GetOverview(0).Checksum()
        assert cs == expected_cs, gdal.GetDataTypeName(dt)
        ds = None
        cleanup()
Пример #14
0
def add_raster_to_item(item: Item) -> Item:
    """Adds raster extension values to an item.

    Args:
        item (Item): The PySTAC Item to extend.

    Returns:
        Item: Returns an updated Item.
            This operation mutates the Item.
    """
    RasterExtension.add_to(item)
    for asset in item.assets.values():
        if asset.roles and "data" in asset.roles:
            raster = RasterExtension.ext(asset)
            bands = []
            href = make_absolute_href(asset.href, item.get_self_href())
            dataset = gdal.Open(href, gdal.GA_ReadOnly)
            for nband in range(dataset.RasterCount):
                gdal_band = dataset.GetRasterBand(nband + 1)
                band = RasterBand.create()
                band.nodata = gdal_band.GetNoDataValue()
                band.spatial_resolution = dataset.GetGeoTransform()[1]
                band.data_type = DataType(
                    gdal.GetDataTypeName(gdal_band.DataType).lower())
                minimum = gdal_band.GetMinimum()
                maximum = gdal_band.GetMaximum()
                if not minimum or not max:
                    minimum, maximum = gdal_band.ComputeRasterMinMax(True)
                band.statistics = Statistics.create(minimum=minimum,
                                                    maximum=maximum)
                hist_data = gdal_band.GetHistogram(minimum, maximum,
                                                   NUM_BUCKETS)
                band.histogram = Histogram.create(NUM_BUCKETS, minimum,
                                                  maximum, hist_data)
                bands.append(band)
            if bands:
                raster.apply(bands)
    return item
Пример #15
0
def make_tmp(md, force=False, return_command=False):
    if not md['need_tmp']:
        return

    if not force:
        if md['tmp_specs']['tmp_updated'] is not None:
            if md['tmp_specs']['tmp_updated'] > md['updated']:
                print('Tmp file is newer then source file. Skipping building tmp file!')
                return

    # First we need to merge all sublayers into one image
    sources = [l['filename'] for l in md['sublayers']]
    mdtmp = md['tmp_specs']
    # osp.join(tmp_fld,'{}_{}.txt'.format(lun,gee_id))
    vrt_sources = mdtmp['vrt_sources']
    # osp.join(tmp_fld,'{}_{}.vrt'.format(lun,gee_id))
    vrt_filename = mdtmp['vrt_filename']
    # osp.join(tmp_fld,'{}_{}.tif'.format(lun,gee_id))
    tmp_filename = mdtmp['tmp_filename']
    # gcs_filename=md['gcs_filename']

    open(vrt_sources, 'w').write('\n'.join(sources))
    run_cmd(['gdalbuildvrt', '-separate',
             '-input_file_list', vrt_sources, vrt_filename])

    cmd = ['gdal_translate', vrt_filename, tmp_filename]
    if md['gdal_type'] is not None:
        cmd.extend(['-ot', gdal.GetDataTypeName(md['gdal_type'])])
    for opt in gdal_options:  # [md['type_value']]:
        cmd.extend(['-co', opt])
    for opt in gdal_config:
        cmd.extend(['--config']+opt.split(" "))

    if return_command:
        return ' '.join(cmd)
    else:
        run_cmd(cmd)
        md['tmp_specs']['tmp_updated'] = modified_time(tmp_filename)
Пример #16
0
    def __init__(self, filename):
        if os.path.isfile(filename):
            self.filename = filename if os.path.isabs(
                filename) else os.path.join(os.getcwd(), filename)
            self.raster = gdal.Open(filename, GA_ReadOnly)
        else:
            raise OSError('file does not exist')

        self.cols = self.raster.RasterXSize
        self.rows = self.raster.RasterYSize
        self.bands = self.raster.RasterCount
        self.dim = [self.rows, self.cols, self.bands]
        self.driver = self.raster.GetDriver()
        self.format = self.driver.ShortName
        self.dtype = gdal.GetDataTypeName(
            self.raster.GetRasterBand(1).DataType)
        self.projection = self.raster.GetProjection()
        self.srs = osr.SpatialReference(wkt=self.projection)
        self.proj4 = self.srs.ExportToProj4()
        try:
            self.epsg = crsConvert(self.srs, 'epsg')
        except RuntimeError:
            self.epsg = None
        self.geogcs = self.srs.GetAttrValue('geogcs')
        self.projcs = self.srs.GetAttrValue(
            'projcs') if self.srs.IsProjected() else None
        self.geo = dict(
            zip(['xmin', 'xres', 'rotation_x', 'ymax', 'rotation_y', 'yres'],
                self.raster.GetGeoTransform()))

        # note: yres is negative!
        self.geo['xmax'] = self.geo['xmin'] + self.geo['xres'] * self.cols
        self.geo['ymin'] = self.geo['ymax'] + self.geo['yres'] * self.rows

        self.res = [abs(float(self.geo['xres'])), abs(float(self.geo['yres']))]
        self.nodata = self.raster.GetRasterBand(1).GetNoDataValue()

        self.__data = []
Пример #17
0
 def check_flt32(self, setting_id, rast_item, check_id, src_ds):
     # Is the raster datatype float32 ?
     detail = ""
     try:
         srcband = src_ds.GetRasterBand(1)
         data_type = srcband.DataType
         data_type_name = gdal.GetDataTypeName(data_type)
         if data_type_name == "Float32":
             result = True
         else:
             result = False
             detail = "data_type is %s" % data_type_name
     except Exception:
         logger.exception("Error checking float32")
         result = False
     finally:
         self.results._add(
             setting_id=setting_id,
             raster=rast_item,
             check_id=check_id,
             result=result,
             detail=detail,
         )
Пример #18
0
def load_pfpr(pfpr_file):
    """

    Args:
        pfpr_file (Path):

    Returns:
        A dataset and band that is Float64.
    """
    if not pfpr_file.exists():
        LOGGER.error(f"The given file doesn't exist: {pfpr_file}")
    dataset = gdal.Open(str(pfpr_file), gdal.GA_ReadOnly)
    if not dataset:
        LOGGER.error(f"Could not open {pfpr_file}.")

    band = dataset.GetRasterBand(1)
    # If these change, then check how the band is used later.
    data_type = str(gdal.GetDataTypeName(band.DataType))
    if data_type != "Float64":
        raise RuntimeError(f"Data type for pfpr is {data_type}")
    projection = dataset.GetProjection()
    assert "WGS 84" in str(projection)
    return Raster(dataset, band)
Пример #19
0
	def _fromGDAL(self):
		'''Use GDAL to extract raster infos and init'''
		if self.path is None or not self.fileExists:
			raise IOError("Cannot find file on disk")
		ds = gdal.Open(self.path, gdal.GA_ReadOnly)
		self.size = xy(ds.RasterXSize, ds.RasterYSize)
		self.format = ds.GetDriver().ShortName
		if self.format in ['JP2OpenJPEG', 'JP2ECW', 'JP2KAK', 'JP2MrSID'] :
			self.format = 'JPEG2000'
		self.nbBands = ds.RasterCount
		b1 = ds.GetRasterBand(1) #first band (band index does not count from 0)
		self.noData = b1.GetNoDataValue()
		ddtype = gdal.GetDataTypeName(b1.DataType)#Byte, UInt16, Int16, UInt32, Int32, Float32, Float64
		if ddtype == "Byte":
			self.dtype = 'uint'
			self.depth = 8
		else:
			self.dtype = ddtype[0:len(ddtype)-2].lower()
			self.depth = int(ddtype[-2:])
		#Get Georef
		self.georef = GeoRef.fromGDAL(ds)
		#Close (gdal has no garbage collector)
		ds, b1 = None, None
Пример #20
0
def gdal_merge_raster(outputFileName, *inputfileNames):
    """
    Function to merge different raster images into a single image.
    Arguments:
    - outputFileName - filename of the output raster
    - inputfileNames - filenames of input rasters to merge (separated by comma as different function arguments)
    """
    # open first file to get no data value
    rst = gdal.Open(inputfileNames[0])
    b1 = rst.GetRasterBand(1)
    nodata = b1.GetNoDataValue()
    bandtype = gdal.GetDataTypeName(b1.DataType)

    if (os.path.isfile(outputFileName) == True):
        os.system("rm " + outputFileName)
    cmd = 'gdalwarp -ot ' + bandtype + ' -srcnodata ' + str(
        nodata) + ' -dstnodata ' + str(nodata)
    for arg in inputfileNames:
        cmd += " " + arg

    cmd += " " + outputFileName
    print cmd
    os.system(cmd)
Пример #21
0
 def NumPyDataType(self):
     """Returns the NumPy type associated with gdal.Band.DataType"""
     datatype = self.DataType
     if datatype == gdalconst.GDT_Byte:
         pixeltype = self.GetMetadataItem('PIXELTYPE', 'IMAGE_STRUCTURE')
         if pixeltype == 'SIGNEDBYTE':
             return numpy.int8
         return numpy.uint8
     elif datatype == gdalconst.GDT_UInt16:
         return numpy.uint16
     elif datatype == gdalconst.GDT_UInt32:
         return numpy.uint32
     elif datatype == gdalconst.GDT_Int16:
         return numpy.int16
     elif datatype == gdalconst.GDT_Int32:
         return numpy.int32
     elif datatype == gdalconst.GDT_Float32:
         return numpy.float32
     elif datatype == gdalconst.GDT_Float64:
         return numpy.float64
     else:
         raise ValueError("Cannot handle DataType: {0}".format(
             gdal.GetDataTypeName(datatype)))
Пример #22
0
def _memWrapFilename(raster):
    """
	Generates the GDAL filename to create an in-memory dataset that wraps a NumPy array.
	"""

    # Verify that the NumPy's memory is contiguous
    if raster.flags['C_CONTIGUOUS'] != True:
        raise RuntimeError('NumPy array must be row-major and contiguous')

    # Retrieve the underlying pointer for the NumPy array
    pointer, read_only_flag = raster.__array_interface__['data']

    # Retrieve the image dimensions
    rows = raster.shape[0]
    cols = raster.shape[1]
    bands = raster.shape[2] if len(raster.shape) > 2 else 1

    # Build the filename for the memory-wrap dataset
    return 'MEM:::DATAPOINTER={},PIXELS={},LINES={},BANDS={},DATATYPE={},PIXELOFFSET={},LINEOFFSET={},BANDOFFSET={}'.format(
        hex(pointer), cols, rows, bands,
        gdal.GetDataTypeName(_numpyTypeToGdalType(raster.dtype)),
        bands * raster.dtype.itemsize, cols * bands * raster.dtype.itemsize,
        raster.dtype.itemsize)
Пример #23
0
 def write_complex(f, geot):
     """Write a complex source to VRT file"""
     out.write('\t\t<ComplexSource>\n')
     out.write('\t\t\t<SourceFilename relativeToVRT="0">{name}'
               '</SourceFilename>\n'.format(name=f.filename.replace('"', '')))
     out.write('\t\t\t<SourceBand>1</SourceBand>\n')
     out.write('\t\t\t<SourceProperties RasterXSize="{x}" '
               'RasterYSize="{y}" DataType="{typ}" '
               'BlockXSize="{bx}" BlockYSize="{by}" />'
               '\n'.format(x=f.xsize, y=f.ysize,
                           typ=gdal.GetDataTypeName(f.band_type),
                           bx=f.block_size[0], by=f.block_size[1]))
     out.write('\t\t\t<SrcRect xOff="0" yOff="0" xSize="{x}" '
               'ySize="{y}" />\n'.format(x=f.xsize, y=f.ysize))
     xoff, yoff = self._calculateOffset(f, geot)
     out.write('\t\t\t<DstRect xOff="{xoff}" yOff="{yoff}" '
               'xSize="{x}" ySize="{y}" />'
               '\n'.format(xoff=xoff, yoff=yoff, x=f.xsize,
                           y=f.ysize))
     if l1.fill_value:
         out.write('\t\t\t<NODATA>{va}</NODATA>'
                   '\n'.format(va=f.fill_value))
     out.write('\t\t</ComplexSource>\n')
Пример #24
0
def GetGeoInfo(FileName):
    """This gets information from the raster file using gdal

    Args:
        FileName (str): The filename (with path and extension) of the raster.

    Return:
        float: A vector that contains:
            * NDV: the nodata values
            * xsize: cellsize in x direction
            * ysize: cellsize in y direction
            * GeoT: the tranform (a string)
            * Projection: the Projection (a string)
            * DataType: The type of data (an int explaing the bits of each data element)

    Author: SMM
    """


    if exists(FileName) is False:
            raise Exception('[Errno 2] No such file or directory: \'' + FileName + '\'')


    SourceDS = gdal.Open(FileName, gdal.GA_ReadOnly)
    if SourceDS == None:
        raise Exception("Unable to read the data file")

    NDV = SourceDS.GetRasterBand(1).GetNoDataValue()
    xsize = SourceDS.RasterXSize
    ysize = SourceDS.RasterYSize
    GeoT = SourceDS.GetGeoTransform()
    Projection = osr.SpatialReference()
    Projection.ImportFromWkt(SourceDS.GetProjectionRef())
    DataType = SourceDS.GetRasterBand(1).DataType
    DataType = gdal.GetDataTypeName(DataType)

    return NDV, xsize, ysize, GeoT, Projection, DataType
Пример #25
0
def load_img_as_array(filename, verbose=False):
    """
    Load the whole image into an numpy array with gdal

    Paremeters
    ----------
    filename : str
        Path of the input image

    Returns
    -------
    array : numpy.ndarray
        Image as array
    """

    # Get size of output array
    data_set = open_image(filename, verbose=verbose)
    nb_col, nb_lignes, nb_band = get_image_dimension(data_set, verbose=verbose)

    # Get data type
    band = data_set.GetRasterBand(1)
    gdal_data_type = gdal.GetDataTypeName(band.DataType)
    numpy_data_type = convert_data_type_from_gdal_to_numpy(gdal_data_type)

    # Initialize an empty array
    array = np.empty((nb_lignes, nb_col, nb_band), dtype=numpy_data_type)

    # Fill the array
    for idx_band in range(nb_band):
        idx_band_gdal = idx_band + 1
        array[:, :, idx_band] = data_set.GetRasterBand(idx_band_gdal).ReadAsArray()

    # close data_set
    data_set = None
    band = None

    return array
Пример #26
0
    def classify_result(self, band):
        fmttypes = {'Byte': 'B', 'UInt16': 'H', 'Int16': 'h', 'UInt32': 'I', 'Int32': 'i', 'Float32': 'f',
                    'Float64': 'd'}

        data_type = band.DataType

        BandType = gdal.GetDataTypeName(band.DataType)

        raster = []

        for y in range(band.YSize):
            scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1, data_type)
            values = struct.unpack(fmttypes[BandType] * band.XSize, scanline)
            raster.append(values)

        raster = [list(item) for item in raster]

        # changing raster values
        for i, item in enumerate(raster):
            for j, value in enumerate(item):
                if 0 <= value <= 6:
                    raster[i][j] = 1
                elif 6 < value <= 9:
                    raster[i][j] = 2
                elif 9 < value <= 12:
                    raster[i][j] = 3
                elif 12 < value <= 15:
                    raster[i][j] = 4
                elif value > 15:
                    raster[i][j] = 5
                elif value < 0:
                    raster[i][j] = -9999

        # transforming list in array
        raster = np.asarray(raster)

        return raster
Пример #27
0
def GDALReadBlock(dataset, blocno, BSx=-1, BSy=-1, verbose=False):
    """
    GDALReadBlock
    """
    dataset = gdal.Open(dataset,
                        gdal.GA_ReadOnly) if isstring(dataset) else dataset
    if dataset:
        band = dataset.GetRasterBand(1)

        BSx, BSy = (BSx, BSy) if BSx > 0 else band.GetBlockSize()
        M, N = int(dataset.RasterYSize), int(dataset.RasterXSize)

        Nb = int(N / BSx) + (0 if N % BSx == 0 else 1)

        x0 = (blocno % Nb) * BSx
        y0 = int(blocno / Nb) * BSy

        QSx = BSx if x0 + BSx <= N else N % BSx
        QSy = BSy if y0 + BSy <= M else M % BSy

        data = band.ReadAsArray(x0, y0, QSx, QSy)

        # Manage No Data
        nodata = band.GetNoDataValue()
        bandtype = gdal.GetDataTypeName(band.DataType)
        if bandtype in ('Byte', 'Int16', 'Int32', 'UInt16', 'UInt32', 'CInt16',
                        'CInt32'):
            data = data.astype("Float32", copy=False)
        if bandtype in ('Float32', 'Float64', 'CFloat32', 'CFloat64'):
            data[data == nodata] = np.nan

        if verbose and blocno % 100 == 0:
            sys.stdout.write('r')
        if isstring(dataset):
            dataset, band = None, None
        return data
    return None
Пример #28
0
    def total_study_pixels(self):
        study_area = self.study_layer()
        provider = study_area.dataProvider()

        fmttypes = {
            'Byte': 'B',
            'UInt16': 'H',
            'Int16': 'h',
            'UInt32': 'I',
            'Int32': 'i',
            'Float32': 'f',
            'Float64': 'd'
        }

        lyr_path = provider.dataSourceUri()
        dataset = gdal.Open(lyr_path)
        band = dataset.GetRasterBand(1)
        BandType = gdal.GetDataTypeName(band.DataType)
        count_value = 0

        for y in range(band.YSize):

            scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1,
                                       band.DataType)
            values = struct.unpack(fmttypes[BandType] * band.XSize, scanline)

            for value in values:
                if value == 1:
                    count_value += 1
        if count_value == 0:
            self.userWarning(
                "Error",
                "Study Area raster layer must be in boolean (0, 1) format, See Help"
            )
        dataset = None
        return count_value
Пример #29
0
    def _add_band(self, idx, ds, bidx):
        """ Add a band to VRT

        Args:
            idx (int): Index of band in VRT
            ds (RasterReader): `rasterio` dataset
            bidx (int): Band index of `ds`
        """
        _band = ds.GetRasterBand(bidx)
        _dtype_name = gdal.GetDataTypeName(_band.DataType)

        band = SubElement(self.root, 'VRTRasterBand')
        band.set('dataType', _dtype_name)
        band.set('band', str(idx + 1))
        # Color interpretation
        ci = SubElement(band, 'ColorInterp')
        ci.text = COLOR_INTERP[idx]
        # Add NoDataValue
        if _band.GetNoDataValue() is not None:
            ndv = SubElement(band, 'NoDataValue')
            ndv.text = str(_band.GetNoDataValue())
        # Add SimpleSource
        source = SubElement(band, 'SimpleSource')
        source_path = SubElement(source, 'SourceFilename')
        source_path.text = ds.GetDescription()  # no abspath with NETCDF
        source_band = SubElement(source, 'SourceBand')
        source_band.text = str(bidx)
        source_props = SubElement(source, 'SourceProperties')
        source_props.set('RasterXSize', str(ds.RasterXSize))
        source_props.set('RasterYSize', str(ds.RasterYSize))
        source_props.set('DataType', _dtype_name)
        blocks = _band.GetBlockSize()
        source_props.set('BlockXSize', str(blocks[1]))
        source_props.set('BlockYSize', str(blocks[0]))

        return source
Пример #30
0
    def open(self, filename):
        self.bands = []
        self.filename = filename

        f: gdal.Dataset = gdal.Open(filename)
        if f is None:
            self.valid = False
            return
        self.valid = True

        self.size_x = f.RasterXSize
        self.size_y = f.RasterYSize

        self.projection = f.GetProjection()
        self.geotransform = f.GetGeoTransform()

        for band_index in range(1, f.RasterCount + 1):
            band: gdal.Band = f.GetRasterBand(band_index)

            band_name = band.GetDescription()
            band_data_type = gdal.GetDataTypeName(band.DataType)

            band_info = RasterBandInfo(band_index, band_name, band_data_type)
            self.bands.append(band_info)