def get_distance_raster(self,
                            base_layer,
                            output_path=None,
                            create_raster=True):
        if self.distance == 'proximity':
            with rasterio.open(base_layer) as src:
                bounds = src.bounds
                width = src.width
                height = src.height
                crs = src.crs
                transform = src.transform

            data, meta = self.rasterize(value=1,
                                        width=width,
                                        height=height,
                                        transform=transform)

            drv = gdal.GetDriverByName('MEM')
            src_ds = drv.Create('', width, height, 1,
                                gdal.GetDataTypeByName('Float32'))
            src_ds.SetGeoTransform(transform.to_gdal())
            src_ds.SetProjection(crs.wkt)
            src_ds.WriteArray(data)
            srcband = src_ds.GetRasterBand(1)

            drv = gdal.GetDriverByName('MEM')
            dst_ds = drv.Create('', width, height, 1,
                                gdal.GetDataTypeByName('Float32'))

            dst_ds.SetGeoTransform(transform.to_gdal())
            dst_ds.SetProjection(crs.wkt)

            dstband = dst_ds.GetRasterBand(1)

            gdal.ComputeProximity(srcband, dstband,
                                  ["VALUES=1", "DISTUNITS=GEO"])
            data = dstband.ReadAsArray()

            meta.update(nodata=np.nan, dtype='float32')

            if create_raster:
                self.distance_raster = RasterLayer(
                    self.category,
                    self.name + '_dist',
                    distance_limit=self.distance_limit,
                    inverse=self.inverse,
                    normalization=self.normalization)
                self.distance_raster.layer = data
                self.distance_raster.meta = meta
                self.distance_raster.bounds = bounds
                if output_path:
                    self.distance_raster.save(output_path)
            else:
                return data, meta

        elif self.distance == 'travel_time':
            self.travel_time(output_path)
def geoimwrite(filename, imdata, geoTransform, proj, drv_name):
    # Get the image Driver by its short name
    driver = gdal.GetDriverByName(drv_name)
    # Get image dimensions from array
    cols = np.size(imdata, 1)
    rows = np.size(imdata, 0)
    dims = np.shape(imdata.shape)[0]
    if dims == 2:
        bands = 1
    elif dims == 3:
        bands = np.size(imdata, 2)
    else:
        raise Exception('Error x01: Invalid image dimensions.')
    # Image datatype
    dt = imdata.dtype
    datatype = gdal.GetDataTypeByName(dt.name)
    # Prepare the output dataset
    if datatype == 0:
        # Unknown datatype, try to use uint8 code
        datatype = 1
    outDataset = driver.Create(filename, cols, rows, bands, datatype)
    # Set the Georeference first
    outDataset.SetGeoTransform(geoTransform)
    outDataset.SetProjection(proj)
    if bands == 1:
        outBand = outDataset.GetRasterBand(1)
        outBand.WriteArray(imdata, 0, 0)
    else:
        # Iterate the bands and write them to the file
        for b_idx in range(1, bands + 1):
            outBand = outDataset.GetRasterBand(b_idx)
            outBand.WriteArray(imdata[:, :, b_idx - 1], 0, 0)
    # Clear variables and close the file
    outBand = None
    outDataset = None
示例#3
0
 def fillNodata(self):
     #if not self.noData in self.data:
     if not np.ma.is_masked(self.data):
         #do not process it if its not necessary
         return
     if self.IFACE == 'GDAL':
         # gdal.FillNodata need a band object to apply on
         # so we create a memory datasource (1 band, float)
         height, width = self.data.shape
         ds = gdal.GetDriverByName('MEM').Create(
             '', width, height, 1, gdal.GetDataTypeByName('float32'))
         b = ds.GetRasterBand(1)
         b.SetNoDataValue(self.noData)
         self.data = np.ma.filled(
             self.data, self.noData)  # Fill mask with nodata value
         b.WriteArray(self.data)
         gdal.FillNodata(targetBand=b,
                         maskBand=None,
                         maxSearchDist=max(self.size.xy),
                         smoothingIterations=0)
         self.data = b.ReadAsArray()
         ds, b = None, None
     else:  #Call the inpainting function
         # Cast to float
         self.cast2float()
         # Fill mask with NaN (warning NaN is a special value for float arrays only)
         self.data = np.ma.filled(self.data, np.NaN)
         # Inpainting
         self.data = replace_nans(self.data,
                                  max_iter=5,
                                  tolerance=0.5,
                                  kernel_size=2,
                                  method='localmean')
示例#4
0
def proximity_raster(src_filename, dst_filename, values, compression):
    src_ds = gdal.Open(src_filename)
    srcband = src_ds.GetRasterBand(1)
    dst_filename = dst_filename

    drv = gdal.GetDriverByName('GTiff')
    dst_ds = drv.Create(dst_filename,
                        src_ds.RasterXSize,
                        src_ds.RasterYSize,
                        1,
                        gdal.GetDataTypeByName('Float32'),
                        options=['COMPRESS={}'.format(compression)])

    dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
    dst_ds.SetProjection(src_ds.GetProjectionRef())

    dstband = dst_ds.GetRasterBand(1)

    gdal.ComputeProximity(srcband, dstband, [
        "VALUES={}".format(','.join([str(i) for i in values])), "DISTUNITS=GEO"
    ])
    srcband = None
    dstband = None
    src_ds = None
    dst_ds = None
示例#5
0
def loadGDALImage(fname):
    '''
    Similar to loadImage but only returns metadata.
    '''

    from osgeo import gdal

    class Dummy(object):
        pass


    ds = gdal.Open(fname, gdal.GA_ReadOnly)
    drv = ds.GetDriver()
    bnd = ds.GetRasterBand(1)

    img = Dummy()
    img.bands = ds.RasterCount 
    img.width = ds.RasterXSize
    img.length = ds.RasterYSize
    img.scheme = drv.GetDescription()
    img.dataType = gdal.GetDataTypeByName(bnd.DataType)

    bnd = None
    drv = None
    ds = None

    return img
示例#6
0
文件: h5fake.py 项目: ieg-tech/popdyn
    def write_geotiff(raster_path, data, dtype, shape, nodata, gt, sr, chunks):
        """Create a raster dataset"""
        driver = gdal.GetDriverByName("GTiff")
        options = [
            "TILED=YES",
            "COPY_SRC_OVERVIEWS=YES",
            "COMPRESS=LZW",
            "BLOCKXSIZE=256",
            "BLOCKYSIZE=256",
        ]

        ds = driver.Create(
            raster_path,
            int(shape[1]),
            int(shape[0]),
            1,
            gdal.GetDataTypeByName(dtype_to_gdal(dtype)),
            options,
        )
        if ds is None:
            raise TypeError("GDAL error while creating new raster")

        ds.SetGeoTransform(gt)
        ds.SetProjection(sr)

        band = ds.GetRasterBand(1)
        band.WriteArray(data)
        band.SetNoDataValue(nodata)
        band.FlushCache()
        band = None
        ds = None
示例#7
0
def rasterize_vector(ds_name, field, outfile, resolution, dtype, no_data, of='GTiff', co=None, overwrite=False):
    # type: (str, str, str, int, str, int or float, str, list, bool) -> None
    """
    Rasterize a vector layer

    :param ds_name: Input vector file name
    :param field: Attribute field that shall be converted to raster values
    :param outfile: Output file name
    :param resolution: Output resolution
    :param dtype: Output data type. One of (Byte, UInt16, Int16, UInt32, Int32, Float32, Float64, CInt16, CInt32,
            CFloat32, CFloat64)
    :param no_data: NoData value for output raster
    :param of: Output format as defined at http://www.gdal.org/formats_list.html
    :param co: Advanced raster creation options such as band interleave or compression. <br<>
            Example: co=['compress=lzw']
    :param overwrite: Overwrite output file if it already exists
    :return: --
    """
    ds_shp = ogr.Open(ds_name)
    lyr = ds_shp.GetLayer()
    proj = lyr.GetSpatialRef()
    lyr = None
    ds_shp = None
    xmin, ymin, xmax, ymax = get_extent(ds_name)
    geotrans = (xmin, resolution, 0, ymax, 0, -resolution)
    cols = int((xmax - xmin) / resolution)
    rows = int((ymax - ymin) / resolution)
    ds = raster_tools.create_ds(outfile, cols, rows, 1, gdal.GetDataTypeByName(dtype), of=of, co=co, overwrite=overwrite)
    ds.SetGeoTransform(geotrans)
    ds.SetProjection(proj.ExportToWkt())
    gdal.RasterizeLayer(ds, [1], lyr, options=['ALL_TOUCHED=TRUE', 'ATTRIBUTE={f}'.format(f=field)])
    ds.GetRasterBand(1).SetNoDataValue(no_data)
    ds = None
    return
示例#8
0
def write_raster_output(strata,
                        cols,
                        rows,
                        map_ds,
                        output,
                        gdal_frmt='GTiff',
                        ndv=255):
    """
    """
    # Init and fill output array with samples
    raster = np.ones(
        (map_ds.RasterYSize, map_ds.RasterXSize), dtype=np.uint8) * ndv

    raster[rows, cols] = strata

    # Get output driver
    driver = gdal.GetDriverByName(gdal_frmt)

    # Create output dataset
    sample_ds = driver.Create(output, map_ds.RasterXSize, map_ds.RasterYSize,
                              1, gdal.GetDataTypeByName('Byte'))

    # Write out band
    sample_ds.GetRasterBand(1).SetNoDataValue(ndv)
    sample_ds.GetRasterBand(1).WriteArray(raster)

    # Port over metadata, projection, geotransform, etc
    sample_ds.SetProjection(map_ds.GetProjection())
    sample_ds.SetGeoTransform(map_ds.GetGeoTransform())
    sample_ds.SetMetadata(map_ds.GetMetadata())

    # Close
    sample_ds = None
示例#9
0
def get_data_type(data_type: Optional[DataTypeOrStr]):
    if data_type is None:
        return None
    if isinstance(data_type, str):
        return gdal.GetDataTypeByName(data_type)
    else:
        return data_type
示例#10
0
def CreateBILRawRasterVRT(filename,
                          nbands,
                          cols,
                          rows,
                          datatype,
                          nbits,
                          nodata=None,
                          headeroffset=0,
                          byteorder=None,
                          relativeToVRT=0):
    '''Create RawRaster VRT from a BIL

        BIL = Band-Interleaved-by-Line or Row-Interleaved

        For further info on VRT's, see the U{GDAL VRT Tutorial<http://www.gdal.org/gdal_vrttut.html>}

        @type filename:       C{str}
        @param filename:      File name
        @type nbands:         C{int}
        @param nbands:        The number of bands in the output VRT (<= nbands in input file)
        @type cols:           C{int}
        @param cols:          The number of columns in the output VRT
        @type rows:           C{int}
        @param rows:          The number of rows in the output VRT
        @type datatype:       C{str}
        @param datatype:      GDAL datatype name. Eg. Byte, Int32, UInt16
        @type nodata:         C{float}
        @param nodata:        No data/Null value
        @type headeroffset:   C{int}
        @param headeroffset:  Number of bytes to skip at the start of the file
        @type byteorder:      C{str}
        @param byteorder:     Byte order of the file (MSB or LSB)

        @rtype:               C{xml}
        @return:              VRT XML string
    '''
    try:
        vrt = []
        nbits = gdal.GetDataTypeSize(gdal.GetDataTypeByName(datatype))
        for i in range(nbands):
            vrt.append(
                '  <VRTRasterBand dataType="%s" band="%s" subClass="VRTRawRasterBand">'
                % (datatype, i + 1))
            vrt.append(
                '    <SourceFilename relativeToVRT="%s">%s</SourceFilename>' %
                (relativeToVRT, filename))
            if nodata is not None:
                vrt.append('    <NoDataValue>%s</NoDataValue>' %
                           (nodata))  #Fix for Issue 17
            vrt.append('    <ImageOffset>%s</ImageOffset>' %
                       (headeroffset + nbits / 8 * i * cols))
            vrt.append('    <PixelOffset>%s</PixelOffset>' % (nbits / 8))
            vrt.append('    <LineOffset>%s</LineOffset>' % (nbits / 8 * cols))
            if byteorder:
                vrt.append('    <ByteOrder>%s</ByteOrder>' % (byteorder))
            vrt.append('  </VRTRasterBand>')
        return CreateCustomVRT('\n'.join(vrt), cols, rows)
    except:
        return None
    return '\n'.join(vrt)
示例#11
0
文件: window.py 项目: WFP-VAM/modape
    def _get_metadata(reference, dataset):
        with h5py.File(reference, "r") as h5f_open:
            ds = h5f_open.get(dataset)
            assert ds

            attrs = dict(h5f_open.get("data").attrs)
            attrs["dataset_shape"] = ds.shape
            attrs["nodata"] = ds.fillvalue
            attrs["dtype"] = gdal.GetDataTypeByName(ds.dtype.name)

        return attrs
示例#12
0
    def writeRGB(self, outPath):
        '''Write FOTO results to a new RGB geotiff'''

        print('Write output raster ' + outPath)

        if self.RGB is None:
            raise ValueError('FOTO not yet computed')

        #build output dataset
        if os.path.isfile(outPath):
            os.remove(outPath)
        driver = gdal.GetDriverByName("GTiff")
        outNbBand = 3
        outDtype = gdal.GetDataTypeByName('Float32')
        outDataset = driver.Create(outPath, self.nbBlkx, self.nbBlky,
                                   outNbBand,
                                   outDtype)  #, ['TFW=YES', 'COMPRESS=LZW'])
        if self.method == 'BLOCK':
            topleftx, pxsizex, rotx, toplefty, roty, pxsizey = self.inDataset.GetGeoTransform(
            )
            outResx = pxsizex * self.blockSize
            outResy = pxsizey * self.blockSize
            outDataset.SetGeoTransform(
                (topleftx, outResx, rotx, toplefty, roty, outResy))
        elif self.method == 'MW':
            outDataset.SetGeoTransform(self.inDataset.GetGeoTransform())
        outDataset.SetProjection(self.inDataset.GetProjection())
        '''
		for i in range(outNbBand):
			outDataset.GetRasterBand(i+1).WriteArray(self.RGB[:,:,i])
		'''
        #write by block to avoid memory overflow
        blockSize = 512
        width, height = self.nbBlkx, self.nbBlky
        #Loop over blocks
        for j in range(0, height, blockSize):
            if j + blockSize < height:
                yBlockSize = blockSize
            else:
                yBlockSize = height - j
            for i in range(0, width, blockSize):
                if i + blockSize < width:
                    xBlockSize = blockSize
                else:
                    xBlockSize = width - i

                for b in range(outNbBand):
                    outBand = outDataset.GetRasterBand(b + 1)
                    data = self.RGB[j:j + yBlockSize, i:i + xBlockSize, b]
                    outBand.WriteArray(data, i, j)

        # Close
        outDataset = None  #needed to validate the modifications
示例#13
0
def from_array(data, path, xsize, ysize, nodata=-9999, trans='', proj=''):
    geotiff = gdal.GetDriverByName("GTiff")
    dst_ds = geotiff.Create(path, xsize, ysize, 1,
                            gdal.GetDataTypeByName('Float32'),
                            ['COMPRESS=lzw', 'PREDICTOR=2'])
    if dst_ds is None:
        raise RuntimeError("failed to create output raster '%s'" % oname)
    dst_ds.SetProjection(proj)
    dst_ds.SetGeoTransform(trans)
    dst_ds.GetRasterBand(1).SetNoDataValue(nodata)
    dst_ds.GetRasterBand(1).WriteArray(data)
    return dst_ds
示例#14
0
def proximity_rast_ds(ds_raster_in, val=1):

    drv = gdal.GetDriverByName('MEM')
    proxy_ds = drv.Create('', ds_raster_in.RasterXSize,
                          ds_raster_in.RasterYSize, 1,
                          gdal.GetDataTypeByName('Float32'))

    gdal.ComputeProximity(ds_raster_in.GetRasterBand(1),
                          proxy_ds.GetRasterBand(1),
                          ["VALUES=" + str(val), "DISTUNITS=GEO"])

    return proxy_ds
示例#15
0
def CreateRawRasterVRT(bands,
                       cols,
                       rows,
                       datatype,
                       headeroffset=0,
                       byteorder=None,
                       relativeToVRT=0,
                       nodata=None):
    ''' Create RawRaster VRT from one or more _single_ band files

        For further info on VRT's, see the U{GDAL VRT Tutorial<http://www.gdal.org/gdal_vrttut.html>}

        @type bands:          C{[str,...,str]}
        @param bands:         List of files. The first file becomes the first band and so forth.
        @type cols:           C{int}
        @param cols:          The number of columns in the output VRT
        @type rows:           C{int}
        @param rows:          The number of rows in the output VRT
        @type datatype:       C{str}
        @param datatype:      GDAL datatype name. Eg. Byte, Int32, UInt16
        @type headeroffset:   C{int}
        @param headeroffset:  Number of bytes to skip at the start of the file
        @type byteorder:      C{str}
        @param byteorder:     Byte order of the file (MSB or LSB)

        @rtype:               C{xml}
        @return:              VRT XML string
    '''

    try:
        vrt = []
        nbits = gdal.GetDataTypeSize(gdal.GetDataTypeByName(datatype))
        for i, band in enumerate(bands):
            vrt.append(
                '  <VRTRasterBand dataType="%s" band="%s" subClass="VRTRawRasterBand">'
                % (datatype, i + 1))
            vrt.append(
                '    <SourceFilename relativeToVRT="%s">%s</SourceFilename>' %
                (relativeToVRT, band))
            vrt.append('    <ImageOffset>%s</ImageOffset>' % (headeroffset))
            vrt.append('    <PixelOffset>%s</PixelOffset>' % (nbits / 8))
            vrt.append('    <LineOffset>%s</LineOffset>' % (nbits / 8 * cols))
            if nodata is not None:
                vrt.append('    <NoDataValue>%s</NoDataValue>' %
                           (nodata))  #Fix for Issue 17
            if byteorder:
                vrt.append('    <ByteOrder>%s</ByteOrder>' % (byteorder))
            vrt.append('  </VRTRasterBand>')
        return CreateCustomVRT('\n'.join(vrt), cols, rows)
    except:
        return None
    return '\n'.join(vrt)
示例#16
0
	def fillNodata(self, data):
		'''
		Call gdal fillnodata function on an given np array
		return an array with nodata filled
		'''
		# gdal.FillNodata need a band object to apply on
		# so we create a memory datasource (1 band, float)
		height, width = data.shape
		ds = gdal.GetDriverByName('MEM').Create('', width, height, 1, gdal.GetDataTypeByName('float32'))
		b = ds.GetRasterBand(1)
		b.SetNoDataValue(self.noData)
		b.WriteArray(data)
		gdal.FillNodata(targetBand=b, maskBand=None, maxSearchDist=max(self.size.xy), smoothingIterations=0)
		data = b.ReadAsArray()
		ds, b = None, None
		return data
示例#17
0
def create_vrt_stack(file_list, outfile="slcs_base.vrt"):
    # Use the first file in the stack to get size, transform info
    ds = gdal.Open(file_list[0])
    geotrans = ds.GetGeoTransform()
    proj = ds.GetProjection()
    xsize = ds.RasterXSize
    ysize = ds.RasterYSize
    ds = None

    # Start with the empty VRT, and add 1 band at a time per file
    vrt_driver = gdal.GetDriverByName("VRT")
    out_raster = vrt_driver.Create(outfile, xsize=xsize, ysize=ysize, bands=0)
    out_raster.SetGeoTransform(geotrans)
    out_raster.SetProjection(proj)

    # Options for all VRT bands:
    gdal_dtype = gdal.GetDataTypeByName("CFloat32")
    bytes_per_pixel = 8  # complex float32
    image_offset = 0
    pixel_offset = bytes_per_pixel
    line_offset = xsize * bytes_per_pixel

    print(f"Writing VRT to {outfile}")
    for band_num, filename in enumerate(file_list, start=1):
        source_filename = os.path.abspath(filename)
        print(f"Adding {source_filename}")
        options = [
            "subClass=VRTRawRasterBand",
            f"SourceFilename={source_filename}",
            f"ImageOffset={image_offset}",
            f"PixelOffset={pixel_offset}",
            f"LineOffset={line_offset}",
        ]
        out_raster.AddBand(gdal_dtype, options)

        # Set the metada on the newly created bands
        date = _get_date(filename)
        metadata = {
            "Date": date,
            "AcquisitionTime": date,
            "Wavelength": str(SENTINEL_WAVELENGTH),
        }
        band = out_raster.GetRasterBand(band_num)
        metadata_domain = "slc"
        band.SetMetadata(metadata, metadata_domain)

    out_raster = None  # Force write
示例#18
0
    def __getmetadata__(self):
        '''Read Metadata for a ENVI image as GDAL doesn't work if you pass the header file...'''
        try:__default__.Dataset.__getmetadata__(self, self._datafile) #autopopulate basic metadata
        except IOError,err:               #Handle the odd ENVI files held by SSD
            hdr=self.__parseheader__()    #"file type = ENVI" instead of "file type = ENVI standard"
            if hdr['file type']=='ENVI':  #I assume they were generated by some external software

                #make a dummy hdr with a dummy 1 byte image file
                import tempfile,geometry,shutil

                tmpd=tempfile.mkdtemp(prefix='gdal')
                tmph=open(tmpd+'/dummy.hdr','w')
                tmpf=open(tmpd+'/dummy','wb')
                hdr['file type']+=' Standard'
                tmph.write('ENVI\n')
                for key in hdr:
                    if '\n' in hdr[key]:tmph.write(key+' = {'+hdr[key]+'}\n')
                    else: tmph.write(key+' = '+hdr[key]+'\n')
                tmph.close()
                tmpf.write('\x00\x00')
                tmpf.close()
                __default__.Dataset.__getmetadata__(self, tmpd+'/dummy')
                del self._gdaldataset
                shutil.rmtree(tmpd, ignore_errors=True)

                #Make a VRT
                md=self.metadata
                byteorder={'':None,'0':'LSB','1':'MSB'}
                byteorder=byteorder[hdr.get('byte order','')]
                min=GetDataTypeRange(gdal.GetDataTypeByName(md['datatype']))[0]
                nodata=hdr.get('data ignore value',str(min))
                if hdr.get('interleave','').upper()=='BSQ':
                    vrt=geometry.CreateBSQRawRasterVRT(self._datafile,md['nbands'],md['cols'],md['rows'],md['datatype'],nodata,headeroffset=0,byteorder=byteorder,relativeToVRT=0)
                elif hdr.get('interleave','').upper()=='BIP':
                    vrt=geometry.CreateBIPRawRasterVRT(self._datafile,md['nbands'],md['cols'],md['rows'],md['datatype'],nodata,headeroffset=0,byteorder=byteorder,relativeToVRT=0)
                else:#Assume bil
                    vrt=geometry.CreateBILRawRasterVRT(self._datafile,md['nbands'],md['cols'],md['rows'],md['datatype'],nodata,headeroffset=0,byteorder=byteorder,relativeToVRT=0)

                self._gdaldataset=geometry.OpenDataset(vrt)
                #Fix for Issue 17
                for i in range(1,self._gdaldataset.RasterCount+1):
                    self._gdaldataset.GetRasterBand(i).SetNoDataValue(nodata)

                pass
                
            else:raise #not the dodgy SSD files, re-raise the orig. error
示例#19
0
    def run(self):
        #delete our output if it exists
        outputFName = os.path.join(self.outDir, self.outName)
        gdal.Unlink(outputFName)
        rasterProcessor.run(self)

        outputRaster = SpatialUtilities.SAHMRaster(outputFName)
        outputRaster.pullParamsFromRaster(self.inputFname)
        outputRaster.pixelType = gdal.GetDataTypeByName("int32")
        outputRaster.NoData = SpatialUtilities.defaultNoData(
            outputRaster.pixelType)
        outputRaster.createNewRaster()

        if self.templateFName != "":
            pass
            #TODO: what we're going to have to do here is first PARC the input to match the template
            #with nearest neighbor, majority
            #Then use this use this tmpPARC output as our input

        for blockData in self.inputRaster.iterBlocks():
            #              print blockData
            #              print SpatialUtilities.GDALToNPDataType(outputRaster.pixelType)
            outBlock = np.ma.asarray(blockData.copy(),
                                     dtype=SpatialUtilities.GDALToNPDataType(
                                         outputRaster.pixelType))
            for k, v in self.reclassDict.iteritems():
                if v == "NoData":
                    v = outputRaster.NoData
                if k[0] == "NoData":
                    outBlock = np.where(np.ma.getmask(blockData), v, outBlock)
                elif len(k) == 1:
                    outBlock[(blockData == k[0]).filled(False)] = v
#                    outBlock = np.where(blockData == k[0], v, outBlock)
                elif len(k) == 2:
                    outBlock[((blockData >= k[0]) &
                              (blockData < k[1])).filled(False)] = v


##                    outBlock = np.where((blockData >= k[0]) & (blockData < k[1]), v, outBlock)
#            print outBlock
            outputRaster.putBlock(outBlock, self.inputRaster.curCol,
                                  self.inputRaster.curRow)

        outputRaster.calcStats()
        outputRaster.band.FlushCache()
        outputRaster = None
示例#20
0
def array2image(Array,
                outFile,
                Rast=None,
                ncols=None,
                nrows=None,
                Xll=None,
                Yll=None,
                cellsize=None,
                nodata=None,
                proj=None,
                dr='GTiff',
                dataType='float32'):
    '''Writes an array to an image with or without a raster as a reference'''

    driver = gdal.GetDriverByName(dr)
    if Rast != None:
        if ncols == None:
            ncols = Rast.ncols
        if nrows == None:
            nrows = Rast.nrows
        if Xll == None:
            Xll = Rast.Xll
        if Yll == None:
            Yll = Rast.Yll
        if cellsize == None:
            cellsize = Rast.cellsize
        if nodata == None:
            nodata = Rast.nodata
        if proj == None:
            proj = Rast.proj

    output = driver.Create(outFile, ncols, nrows, 1,
                           gdal.GetDataTypeByName(dataType))

    geo = [Xll, cellsize, 0, Yll + cellsize * nrows, 0, cellsize * (-1)]

    output.SetGeoTransform(geo)
    output.SetProjection(proj)

    output.GetRasterBand(1).WriteArray(Array)
    output.GetRasterBand(1).SetNoDataValue(nodata)

    #    output.GetRasterBand(1).SetStatistics(Rast.stat['min'], Rast.stat['max'], Rast.stat['mean'], Rast.stat['std'])

    output = None
示例#21
0
def proximity_raster(src_raster_path: str,
                     out_raster_path: str,
                     dist_units="PIXEL",
                     preserve_nodata=True):
    """Create a proximity raster

    Args:
        src_raster_path ([type]): [description]
        out_raster_path ([type]): [description]
        dist_units (str, optional): set to "GEO" for distance in length . Defaults to "PIXEL".
    """
    log = Logger('proximity_raster')
    tmr = Timer()
    src_ds = gdal.Open(src_raster_path)
    srcband = src_ds.GetRasterBand(1)

    drv = gdal.GetDriverByName('GTiff')
    with TempRaster('vbet_proximity_raster') as tempfile:
        dst_ds = drv.Create(tempfile.filepath, src_ds.RasterXSize,
                            src_ds.RasterYSize, 1,
                            gdal.GetDataTypeByName('Float32'))

        dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
        dst_ds.SetProjection(src_ds.GetProjectionRef())

        dstband = dst_ds.GetRasterBand(1)

        log.info('Creating proximity raster')
        gdal.ComputeProximity(
            srcband, dstband,
            ["VALUES=1", f"DISTUNITS={dist_units}", "COMPRESS=DEFLATE"])

        srcband = None
        dstband = None
        src_ds = None
        dst_ds = None

        # Preserve the nodata from the source
        if preserve_nodata:
            mask_rasters_nodata(tempfile.filepath, src_raster_path,
                                out_raster_path)
        else:
            shutil.copyfile(tempfile.filepath, out_raster_path)

        log.info('completed in {}'.format(tmr.toString()))
示例#22
0
def from_pd(df, path, nodata=-9999, trans='', proj=''):
    if isinstance(df, pd.Series):
        arr = df.as_matrix().reshape(df.ysize, df.xsize)
        return from_array(arr, path, df.xsize, df.ysize, nodata, trans, proj)
    if isinstance(df, pd.DataFrame):
        geotiff = gdal.GetDriverByName("GTiff")
        dst_ds = geotiff.Create(path, df.xsize, df.ysize, len(df.columns),
                                gdal.GetDataTypeByName('Float32'),
                                ['COMPRESS=lzw', 'PREDICTOR=2'])
        if dst_ds is None:
            raise RuntimeError("failed to create output raster '%s'" % oname)
        dst_ds.SetProjection(proj)
        dst_ds.SetGeoTransform(trans)
        for idx, col in enumerate(df.columns):
            arr = df[col].as_matrix().reshape(df.ysize, df.xsize)
            dst_ds.GetRasterBand(idx + 1).SetNoDataValue(nodata)
            dst_ds.GetRasterBand(idx + 1).WriteArray(arr)
    return dst_ds
示例#23
0
def regularize(src_fn, dst_fn, src_band=1, offset=1.0, low=0.0, high=1):
    src_ds = gdal.Open(src_fn)
    if src_ds is None:
        raise RuntimeError("Error: could not open raster file '%s'" % src_fn)
    if dst_fn is not None:
        geotiff = gdal.GetDriverByName("GTiff")
        if geotiff is None:
            raise RuntimeError('could not open GeoTiff gdal driver')
        dst_ds = geotiff.Create(
            dst_fn,
            src_ds.RasterXSize,
            src_ds.RasterYSize,
            2,  # Store the unscaled (but log-ed) value in band 2
            gdal.GetDataTypeByName('Float32'),
            ['COMPRESS=lzw', 'PREDICTOR=2'])
        if dst_ds is None:
            raise RuntimeError("Error: could not open raster file '%s'" %
                               dst_fn)
        dst_ds.SetProjection(src_ds.GetProjection())
        dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
    src_nodata = src_ds.GetRasterBand(src_band).GetNoDataValue()
    src_data = src_ds.GetRasterBand(src_band).ReadAsArray()
    src_mask = src_data == src_nodata

    # This is a bit awkward.  A masked_array (ma) seemed like the ideal
    # way to compute the log of a raster with NoData values.
    # Unfortunately, ma performs the operation on *all* elements of the
    # array and then does the masking.  So resorting to this goofiness.
    #
    # Need to be careful not to introduce an artificial minimum, i.e. can
    # simply use 1.0 for NoData cells because the minimum in the unmasked
    # portion of the array may be > 1.0.
    src_min = np.min(ma.masked_where(src_mask, src_data))
    X = np.log(np.where(src_mask, src_min + offset, src_data + offset))
    X_min = X.min()
    X_std = (X - X_min) / (X.max() - X_min)
    scaled = X_std * (high - low) + low
    masked = np.where(src_mask, src_nodata, scaled)
    if dst_fn is None:
        return masked
    dst_ds.GetRasterBand(1).WriteArray(masked)
    dst_ds.GetRasterBand(1).SetNoDataValue(src_nodata)
    dst_ds.GetRasterBand(2).WriteArray(np.where(src_mask, src_nodata, X))
    dst_ds.GetRasterBand(2).SetNoDataValue(src_nodata)
def writeGeoTiff(arrayData, extent, pixelFormat, path, layer):
    format = "GTiff"
    driver = gdal.GetDriverByName(format)
    metadata = driver.GetMetadata()
    if metadata.has_key(
            gdal.DCAP_CREATE) and metadata[gdal.DCAP_CREATE] == "YES":
        pass
    else:
        print "Driver %s does not support Create() method." % format
        return False

    # get rows and columns
    dims = arrayData.shape
    if len(dims) == 2:
        rows = dims[0]
        cols = dims[1]
        nbands = 1
    else:
        rows = dims[1]
        cols = dims[2]
        nbands = dims[0]

    pixFormat = gdal.GetDataTypeByName(pixelFormat)

    proj = projection(layer)
    trans = geotransform(layer)

    # could possible to use CreateCopy from one of the input rasters
    dst_ds = driver.Create(path, cols, rows, nbands, pixFormat)

    dst_ds.SetGeoTransform([
        extent[0], (extent[2] - extent[0]) / cols, 0, extent[3], 0,
        (extent[1] - extent[3]) / rows
    ])
    dst_ds.SetProjection(proj)

    if nbands > 1:
        for i in range(nbands):
            dst_ds.GetRasterBand(i + 1).WriteArray(arrayData[i])
    else:
        dst_ds.GetRasterBand(1).WriteArray(arrayData)

    dst_ds = None
    return True
示例#25
0
def outDataset( path, pixelFormat, layer, x, y ):
  format = "GTiff"
  driver = gdal.GetDriverByName( format )
  metadata = driver.GetMetadata()
  if metadata.has_key( gdal.DCAP_CREATE ) and metadata[ gdal.DCAP_CREATE ] == "YES":
    pass
  else:
    print "Driver %s does not support Create() method." % format
    return False

  pixFormat = gdal.GetDataTypeByName( pixelFormat )
  proj = projection( layer )
  trans = geotransform( layer )
  dst_ds = driver.Create( path, x, y, 1, pixFormat )
  #dst_ds.SetGeoTransform( [ extent[ 0 ], ( extent[ 2 ] - extent[ 0 ] ) / cols, 0,
  #                          extent[ 3 ], 0, ( extent[ 1 ] - extent[ 3 ] ) / rows ] )
  dst_ds.SetProjection( proj )
  dst_ds.SetGeoTransform( trans )
  return dst_ds
示例#26
0
def writeImage(Rast, outFile, dr='GTiff', dataType='float32'):
    '''Writes a raster image from Raster'''

    driver = gdal.GetDriverByName(dr)
    output = driver.Create(outFile, Rast.ncols, Rast.nrows,\
                           1, gdal.GetDataTypeByName(dataType))

    geo = [
        Rast.Xll, Rast.cellsize, 0, Rast.Yll + Rast.cellsize * Rast.nrows, 0,
        Rast.cellsize * (-1)
    ]

    output.SetGeoTransform(geo)
    output.SetProjection(Rast.proj)

    output.GetRasterBand(1).WriteArray(Rast.elements.data)
    output.GetRasterBand(1).SetNoDataValue(Rast.nodata)
    #    output.GetRasterBand(1).SetStatistics(Rast.stat['min'], Rast.stat['max'], Rast.stat['mean'], Rast.stat['std'])

    output = None
示例#27
0
 def writeToFile(self, rasterOb, filepath, gdal_output_format='GTiff'):
     ro = rasterOb
     filepath = os.path.splitext(filepath)[0]
     # Need to create a RasterBAnd object and write to that
     driver = gdal.GetDriverByName(gdal_output_format)
     metadata = driver.GetMetadata()
     suffix = metadata.get(gdal.DMD_EXTENSION, '')
     create_data_types = metadata[gdal.DMD_CREATIONDATATYPES].split()
     if suffix:
         outfilepath = filepath + '.' + suffix
     else:
         outfilepath = filepath
     # Create(<filename>, <xsize>, <ysize>, [<bands>], [<GDALDataType>])
     output_gdt = None
     if ro.gdt in create_data_types:
         output_gdt = ro.gdt
     else:
         for dfmt in self.dmap_data_formats[ro.gdt]:
             if dfmt in create_data_types:
                 output_gdt = dfmt
                 break
     try:
         ds_out = driver.Create(outfilepath, ro.width, ro.height, 1,
                                gdal.GetDataTypeByName(output_gdt))
     except RuntimeError:
         #            print('\tOUTPUT DATA_FORMAT:', output_gdt)
         #            print('\tro.width:', ro.width)
         #            print('\tro.height:', ro.height)
         #            print('\toutfilepath:', outfilepath)
         raise MCEliteError("Results file %s cannot be written. \
         \nMake sure that it is not opened or write-protected, and that the format is supported."
                            % (outfilepath))
     ds_out.SetGeoTransform(ro.geotransform)
     ds_out.SetProjection(ro.projection)
     # Writes as many bands as we have
     for band_id in ro.datad:
         ds_out.GetRasterBand(band_id).WriteArray(rasterOb.datad[band_id])
         ds_out.GetRasterBand(band_id).SetNoDataValue(0.0)
     return outfilepath
示例#28
0
 def toGDAL(self):
     '''Get GDAL memory driver dataset'''
     w, h = self.size
     n = self.nbBands
     dtype = str(self.dtype)
     if dtype == 'uint8': dtype = 'byte'
     dtype = gdal.GetDataTypeByName(dtype)
     mem = gdal.GetDriverByName('MEM').Create('', w, h, n, dtype)
     #writearray is available only at band level
     if self.isOneBand:
         mem.GetRasterBand(1).WriteArray(self.data)
     else:
         for bandIdx in range(n):
             bandArray = self.data[:, :, bandIdx]
             mem.GetRasterBand(bandIdx + 1).WriteArray(bandArray)
     #write georef
     if self.isGeoref:
         mem.SetGeoTransform(self.georef.toGDAL())
         if self.georef.crs is not None:
             mem.SetProjection(
                 self.georef.crs.getOgrSpatialRef().ExportToWkt())
     return mem
示例#29
0
def manual(image, pct=None, minmax=None, out_datatype='uint8'):
    """ Manual scaling of image from minmax to range of datatype specified """
    # Get output datatype as np & GDAL
    try:
        np_dt = np.dtype(out_datatype.lower())
        gdal_dt = gdal.GetDataTypeByName(out_datatype)
    except:
        print('Error: could not understand datatype {dt}'.format(
            dt=out_datatype))
        sys.exit(1)

    # Get data type min/max
    try:
        dt_max = np.iinfo(np_dt).max
        dt_min = np.iinfo(np_dt).min
    except:
        dt_max = np.finfo(np_dt).max
        dt_min = np.finfo(np.dt).min

    # Calculate scale factor & offset
    scale = (dt_max - dt_min) / (minmax[1] - minmax[0])
    offset = dt_max - (scale * minmax[1])

    return image * scale + offset
示例#30
0
def ParseType(type):
    gdal_dt = gdal.GetDataTypeByName(type)
    if gdal_dt is GDT_Unknown:
        gdal_dt = GDT_Byte
    return gdal_dt