Пример #1
0
	def doDegradator(self, law, *args, **kwargs):

		ns = self.fidIn.RasterXSize
		nl = self.fidIn.RasterYSize
		gdal.TermProgress(0)

		for il in xrange(1, nl-self.ks//2):
			countGrids = numpy.zeros(ns) + self.nodataCount
			thisStrip = self.fidIn.GetRasterBand(1).ReadAsArray(0, il-1, ns, self.ks)
			gdal.TermProgress(il/nl)

			for ii in xrange(self.ks//2, ns-self.ks//2):
				if thisStrip[self.ks//2][ii] in self.selectionCodes: #== detectedCode:
					thisCell = thisStrip[0:self.ks, ii- self.ks//2:ii+self.ks//2 + 1]
					if thisStrip[self.ks//2][ii] == self.TNT1:
						countGrids[ii] = self.weights[self.TNT1] * (numpy.sum( thisCell == self.TNT1)-1) + \
							self.weights[self.NTNT] * (numpy.sum(thisCell == self.NTNT))
					if thisStrip[self.ks//2][ii] == self.TNT2:
						countGrids[ii] = self.weights[self.TNT2] * (numpy.sum(thisCell == self.TNT2)-1) + \
							self.weights[self.TNT1] * (numpy.sum(thisCell == self.TNT1)) + \
							self.weights[self.NTNT] * (numpy.sum(thisCell == self.NTNT))

			self.fidOutCount.GetRasterBand(1).WriteArray(countGrids.reshape(1,-1), 0, il)
			outVal = [ law(y, *args, **kwargs) if y != self.nodataCount else self.nodataVal for y in countGrids ]
			self.fidOutVal.GetRasterBand(1).WriteArray( numpy.array(outVal).reshape(1,-1), 0, il )
		gdal.TermProgress(1)
def doDetectChange(indicator, lstFid, threshold, outDir):
    ns = lstFid[indicator][0].RasterXSize
    nl = lstFid[indicator][0].RasterYSize
    nb = 1
    outDrv = gdal.GetDriverByName('gtiff')
    outDs = outDrv.Create('{}/change_{}.tif'.format(outDir, indicator),
                          ns,
                          nl,
                          1,
                          gdalconst.GDT_Byte,
                          options=['compress=lzw', 'bigtiff=yes'])
    outDs.SetProjection(lstFid[indicator][0].GetProjection())
    outDs.SetGeoTransform(lstFid[indicator][0].GetGeoTransform())

    gdal.TermProgress = gdal.TermProgress_nocb
    print 'Processing ', indicator

    for il in xrange(nl):
        data0 = lstFid[indicator][0].GetRasterBand(1).ReadAsArray(0, il, ns, 1)
        data1 = lstFid[indicator][1].GetRasterBand(1).ReadAsArray(0, il, ns, 1)
        change = numpy.zeros((1, ns))
        wtp = numpy.isfinite(data0) * numpy.isfinite(data1)
        if wtp.any():
            change[wtp] = (data0[wtp] < threshold) * (data1[wtp] > threshold)
            outDs.GetRasterBand(1).WriteArray(change, 0, il)
        gdal.TermProgress(il / nl)
    gdal.TermProgress(1)
    outDs = None
def doDTW(name, lstFid, myMask, outDir):
    ns = lstFid[name][0].RasterXSize
    nl = lstFid[name][0].RasterYSize
    nb = [lstFid[name][0].RasterCount]
    nb.append(lstFid[name][1].RasterCount)

    outDrv = gdal.GetDriverByName('gtiff')
    outDs = outDrv.Create(os.path.join(outDir, 'dtw_{}.tif'.format(name)),
                          ns,
                          nl,
                          1,
                          gdalconst.GDT_Float32,
                          options=['compress=lzw', 'bigtiff=yes'])
    outDs.SetProjection(lstFid[name][0].GetProjection(1))
    outDs.SetGeoTransform(lstFid[name][0].SetProjection(1))

    gdal.TermProgress = gdal.TermProgress_nocb
    print 'Processing ', name

    for il in xrange(nl):
        data0 = numpy.zeros((nb[0], ns))
        data1 = numpy.zeros((nb[1], ns))
        for ib in xrange(nb[0]):
            data0[ib, :] = numpy.ravel(
                lstFid[name][0].GetRasterBand(ib + 1).ReadAsArray(
                    0, il, ns, 1))
        for ib in xrange(nb[1]):
            data1[ib, :] = numpy.ravel(
                lstFid[name][1].GetRasterBand(ib + 1).ReadAsArray(
                    0, il, ns, 1))
        data0Mask = numpy.prod(numpy.isfinite(data0), 0).ravel()
        data1Mask = numpy.prod(numpy.isfinite(data1), 0).ravel()

        outDTW = numpy.zeros(ns)
        # get Mask: first date
        maskData = numpy.ravel(
            lstFid[myMask['name']][0].GetRasterBand(1).ReadAsArray(
                0, il, ns, 1))
        isfinite = numpy.isfinite(maskData)
        thisMask = numpy.zeros(ns)
        thisMask[isfinite] = (maskData[isfinite] < myMask['threshold']
                              ) * data0Mask[isfinite] * data1Mask[isfinite]

        for ii in xrange(ns):
            if thisMask[ii]:
                try:
                    result = fastdtw.fastdtw(numpy.ravel(data0[:, ii]),
                                             numpy.ravel(data1[:, ii]))
                    outDTW[ii] = result[0]
                except:
                    print 'Error on line {}, column {}'.format(il, ii)
                    print data0[:, ii]
                    print data1[:, ii]
        outDs.GetRasterBand(1).WriteArray(outDTW.reshape(1, -1), 0, il)
        gdal.TermProgress(il / nl)

    gdal.TermProgress(1)
    outDS = None
Пример #4
0
 def reportProgress(complete):
     if progress is None:
         gdal.TermProgress(complete)
     else:
         if progress.ProgressCB(complete, '') == 0:
             progress.destroy()
             return 1
def doCondDiff(name, condition, lstFid, outDir):
    ns = lstFid[name][0].RasterXSize
    nl = lstFid[name][0].RasterYSize
    print 'ns=', ns
    print 'nl=', nl
    outDrv = gdal.GetDriverByName('gtiff')
    outDs = outDrv.Create('{}/cond_diff_{}.tif'.format(outDir, name),
                          ns,
                          nl,
                          1,
                          gdalconst.GDT_Float32,
                          options=['compress=lzw', 'bigtiff=yes'])
    outDs.SetProjection(lstFid[name][0].GetProjection())
    outDs.SetGeoTransform(lstFid[name][0].GetGeoTransform())

    gdal.TermProgress = gdal.TermProgress_nocb
    print 'Processing ', name

    for il in xrange(nl):
        data0 = lstFid[name][0].GetRasterBand(1).ReadAsArray(0, il, ns, 1)
        data1 = lstFid[name][1].GetRasterBand(1).ReadAsArray(0, il, ns, 1)
        cond = lstFid[condition[name]][0].GetRasterBand(1).ReadAsArray(
            0, il, ns, 1)
        diff = numpy.zeros((1, ns))
        wtp = numpy.isfinite(data0) * numpy.isfinite(data1) * numpy.isfinite(
            cond)
        if wtp.any():
            wcond = cond[wtp] < condition['threshold']
            if wcond.any():
                diff[1, wtp[wcond]] = data1[wtp[wcond]] - data0[wtp[wcond]]
                outDs.GetRasterBand(1).WriteArray(diff, 0, il)
        gdal.TermProgress(il / nl)

    gdal.TermProgress(1)

    outDs = None
Пример #6
0
for ifile in listFile:
    thisFid = gdal.Open(rootDir+ifile, gdalconst.GA_ReadOnly)
    if thisFid is not None:
        lstFid.append(thisFid)
    else:
        print 'error opening {}'.format(ifile)
        sys.exit(1)
        
ns = thisFid.RasterXSize
nl = thisFid.RasterYSize
nb = 1
outDrv = gdal.GetDriverByName('gtiff')
outDs = outDrv.Create(rootDir+outFile, ns, nl, nb, gdalconst.GDT_Float32, options=['compress=lzw','BIGTIFF=YES'])
outDs.SetProjection(thisFid.GetProjection())
outDs.SetGeoTransform(thisFid.GetGeoTransform())

gdal.TermProgress(0.0)
for il in xrange(0, nl, 1):
    dataNan=numpy.zeros((len(lstFid), ns))
    for iband in range(len(lstFid)):
        dataNan[iband, :]= numpy.ravel(lstFid[iband].GetRasterBand(1).ReadAsArray(0, il, ns, 1))
    data = numpy.ma.masked_array(dataNan, numpy.isnan(dataNan))
    data[~data.mask] = [10**x for x in data[~data.mask]]
    cumul = numpy.ma.MaskedArray.sum(data, 0)
    cumul[~cumul.mask] = numpy.log10(cumul[~cumul.mask])
    outDs.GetRasterBand(1).WriteArray(numpy.ma.asarray(cumul.reshape(1,-1)), 0, il)
    gdal.TermProgress(il/nl)
    
gdal.TermProgress(1.0)
outDs = None
Пример #7
0
        if wtsum.any():
            data = numpy.ravel(
                inFid.GetRasterBand(1).ReadAsArray(0, il, ns, lineHeight))
            NTotal = NTotal + numpy.sum(wtsum)
            total = total + sum(data[wtsum])
            if collection is None:
                collection = numpy.array(data[wtsum])
            else:
                collection = numpy.append(collection, numpy.ravel(data[wtsum]))
        #mask = data > 0.854
        #outDs.GetRasterBand(1).WriteArray(mask.reshape(1,-1), 0, il)
            if NTotal > 0:
                print il, total, NTotal, total / NTotal

        #gdal.TermProgress(il/nl)
    except:
        print 'Error!', il
        print data
        print 'return from error'
        #print wtp

print total, NTotal, total / NTotal
collection = numpy.array(collection)
print collection.shape
print 'Collection.size = {}; min = {}, mean = {}, max = {}, std = {}'.format(
    collection.size, collection.min(), collection.mean(), collection.max(),
    collection.std())

gdal.TermProgress(1)
outDs = None
Пример #8
0
projection = radarFID.GetProjection()
print geotrans
print projection

# create output
outDrv = gdal.GetDriverByName('Gtiff')
outDs = outDrv.Create(output,
                      ns,
                      nl,
                      1,
                      gdal.GDT_Byte,
                      options=['compress=lzw', 'bigtiff=yes'])
outDs.SetProjection(projection)
outDs.SetGeoTransform(geotrans)

gdal.TermProgress(0)
for il in xrange(nl):
    gdal.TermProgress(il / float(nl))
    thisRadar = numpy.ravel(
        radarFID.GetRasterBand(1).ReadAsArray(0, il, ns, 1))
    thisNdvi = numpy.ravel(
        ndviCumul.GetRasterBand(1).ReadAsArray(0, il, ns, 1))
    maskData = (thisRadar > 0) * (thisNdvi >= 2)
    maskData = maskData.astype(int)
    #print maskData.shape, min(maskData), max(maskData)
    outDs.GetRasterBand(1).WriteArray(maskData.reshape(1, -1), 0, il)

gdal.TermProgress(1)
# close files
outDs = None
radarFid = None
Пример #9
0
gtiff_driver = gdal.GetDriverByName('GTiff')

tif_ds = gtiff_driver.Create(tif_filename, src_ds.RasterXSize,
                             src_ds.RasterYSize, out_bands)

# ----------------------------------------------------------------------------
# We should copy projection information and so forth at this point.

tif_ds.SetProjection(src_ds.GetProjection())
tif_ds.SetGeoTransform(src_ds.GetGeoTransform())

# ----------------------------------------------------------------------------
# Do the processing one scanline at a time.

gdal.TermProgress(0.0)
for iY in range(src_ds.RasterYSize):
    src_data = src_band.ReadAsArray(0, iY, src_ds.RasterXSize, 1)

    for iBand in range(out_bands):
        band_lookup = lookup[iBand]

        dst_data = Numeric.take(band_lookup, src_data)
        tif_ds.GetRasterBand(iBand + 1).WriteArray(dst_data, 0, iY)

    gdal.TermProgress((iY + 1.0) / src_ds.RasterYSize)

tif_ds = None

# ----------------------------------------------------------------------------
# Translate intermediate file to output format if desired format is not TIFF.
Пример #10
0
    def fuseBands(self, panDS, outDS, progress=None):
        from _gdal import GDALReadRaster, GDALWriteRaster
        rgbDS = self.rgbds
        try:
            from numarray import clip, maximum, zeros
        except:
            from Numeric import clip, maximum, zeros
        # input bands
        if self.swaprb:
            redBand = rgbDS.GetRasterBand(3)
            blueBand = rgbDS.GetRasterBand(1)
        else:
            redBand = rgbDS.GetRasterBand(1)
            blueBand = rgbDS.GetRasterBand(3)
        greenBand = rgbDS.GetRasterBand(2)
        panBand = panDS.GetRasterBand(1)

        # output bands
        redBandOut = outDS.GetRasterBand(1)
        greenBandOut = outDS.GetRasterBand(2)
        blueBandOut = outDS.GetRasterBand(3)

        xbloff, ybloff, xsz, ysz = 0, 0, 0, 0
        # for clarity...
        xSize = self.xSize
        ySize = self.ySize
        blkX = self.blockX
        blkY = self.blockY

        def ihsMergePix(R, G, B, I):
            scale = 3.0 * I / (R + G + B + 1.0)
            scaleFn(R * scale, G * scale, B * scale)

        def mergePix(R, G, B, I):
            scaleFn(I + R, I + G, I + B)

        def clipToByte(R, G, B):
            writeBands(
                clip(R, 0, 255).astype('b'),
                clip(G, 0, 255).astype('b'),
                clip(B, 0, 255).astype('b'))

        def clipAndScale(R, G, B):
            writeBands(
                clip(R * 0.124573, 0, 255).astype('b'),
                clip(G * 0.124573, 0, 255).astype('b'),
                clip(B * 0.124573, 0, 255).astype('b'))

        def clipToInt(R, G, B):
            writeBands(
                clip(R, 0, 2047).astype('w'),
                clip(G, 0, 2047).astype('w'),
                clip(B, 0, 2047).astype('w'))

        if self.dtype == 'Byte':
            outtype = gdal.GDT_Byte
            if self.srcDt == gdal.GDT_Byte:
                scaleFn = clipToByte
                typecode = 'b'
            else:
                scaleFn = clipAndScale
                typecode = 'w'
        else:
            outtype = gdal.GDT_UInt16
            scaleFn = clipToInt
            typecode = 'w'

        def writeBands(outR, outG, outB):
            GDALWriteRaster(redBandOut._o, xbloff, ybloff, xsz, ysz,
                            outR.tostring(), xsz, ysz, outtype)
            GDALWriteRaster(greenBandOut._o, xbloff, ybloff, xsz, ysz,
                            outG.tostring(), xsz, ysz, outtype)
            GDALWriteRaster(blueBandOut._o, xbloff, ybloff, xsz, ysz,
                            outB.tostring(), xsz, ysz, outtype)

        def processBands():
            # buffer arrays
            red = zeros((ysz, xsz), typecode)
            green = zeros((ysz, xsz), typecode)
            blue = zeros((ysz, xsz), typecode)
            pan = zeros((ysz, xsz), 'f')
            # read into buffers
            GDALReadRaster(redBand._o, xbloff, ybloff, xsz, ysz, xsz, ysz,
                           self.srcDt, red)
            GDALReadRaster(greenBand._o, xbloff, ybloff, xsz, ysz, xsz, ysz,
                           self.srcDt, green)
            GDALReadRaster(blueBand._o, xbloff, ybloff, xsz, ysz, xsz, ysz,
                           self.srcDt, blue)
            GDALReadRaster(panBand._o, xbloff, ybloff, xsz, ysz, xsz, ysz,
                           gdal.GDT_Float32, pan)
            # merge
            mergeFn(red.astype('f'), green.astype('f'), blue.astype('f'), pan)

        def reportProgress(complete):
            if progress is None:
                gdal.TermProgress(complete)
            else:
                if progress.ProgressCB(complete, '') == 0:
                    progress.destroy()
                    return 1

        if self.method == 'IHS':
            mergeFn = ihsMergePix
        else:
            mergeFn = mergePix

        if progress is None:
            gdal.TermProgress(0.0, msg='Merging...\n')

        if blkX == 0:
            denom = ySize - 1
            xbloff = 0
            xsz = xSize
            ysz = 1
            for line in range(ySize):
                ybloff = line
                processBands()
                if reportProgress(float(line) / denom):
                    return 0
        else:
            fullX = int(xSize / blkX)
            fullY = int(ySize / blkY)
            for yblk in range(fullY):
                xsz = blkX
                ysz = blkY
                ybloff = yblk * blkY
                for xblk in range(fullX):
                    xbloff = xblk * blkX
                    processBands()

                # deal with last Xblock, if any
                xbloff = fullX * blkX
                if xSize > xbloff:
                    xsz = xSize - xbloff
                    ysz = max(blkY, ySize - fullY * blkY)
                    processBands()

                if reportProgress(float(ybloff) / ySize):
                    return 0

            # deal with last Yblock, if any
            ybloff = fullY * blkY
            if ySize > ybloff:
                xsz = blkX
                ysz = ySize - ybloff
                for xblk in range(fullX):
                    xbloff = xblk * blkX
                    processBands()

                # deal with last Xblock, if any
                xbloff = fullX * blkX
                if xSize > xbloff:
                    xsz = xSize - xbloff
                    processBands()

                if reportProgress(float(ybloff) / ySize):
                    return 0

        if progress is not None:
            progress.destroy()

        return 1