def run(): """ Run tests of the ratapplier """ riostestutils.reportStart(TESTNAME) allOK = True imgfile = 'test.img' makeTestFile(imgfile) ok = testOutputSameFile(imgfile) if not ok: allOK = False imgfile2 = "test2.img" ok = testDifferentOutput(imgfile, imgfile2) if not ok: allOK = False imgfile3 = "test3.img" ok = testReduceRat(imgfile, imgfile3) if not ok: allOK = False imgfile4 = "test4.img" ok = testNewRat(imgfile4) if not ok: allOK = False for tmpfile in [imgfile, imgfile2, imgfile3, imgfile4]: os.remove(tmpfile) if allOK: riostestutils.report(TESTNAME, "Passed") return allOK
def run(): """ Run the test """ riostestutils.reportStart(TESTNAME) # Create a multi-band file with some data in it. tstfile = 'multilayer.img' numBands = 5 ds = riostestutils.createTestFile(tstfile, numBands=numBands) onelayerArr = riostestutils.genRampArray() lyrList = [] for i in range(numBands): lyr = (onelayerArr + 1) band = ds.GetRasterBand(i + 1) band.WriteArray(lyr) lyrList.append(lyr) del ds stack = numpy.array(lyrList) # Sum of all pixels in bands 2 & 4 layerList = [2, 4] correctSum = sum([(stack[i - 1].astype(numpy.float64)).sum() for i in layerList]) # Now do it using RIOS infiles = applier.FilenameAssociations() outfiles = applier.FilenameAssociations() otherargs = applier.OtherInputs() controls = applier.ApplierControls() infiles.img = tstfile controls.selectInputImageLayers(layerList) otherargs.total = 0 # We will use this to check the number of layers being read otherargs.numLayers = len(layerList) otherargs.numLayersIsOK = True applier.apply(doSum, infiles, outfiles, otherargs, controls=controls) if correctSum != otherargs.total: riostestutils.report( TESTNAME, "Totals do not match: %s != %s" % (correctSum, otherargs.total)) ok = False else: riostestutils.report(TESTNAME, "Passed") ok = True os.remove(tstfile) return ok
def testOutputSameFile(imgfile): # Now test the ratapplier inRats = ratapplier.RatAssociations() outRats = ratapplier.RatAssociations() controls = ratapplier.RatApplierControls() inRats.img = ratapplier.RatHandle(imgfile) outRats.img = inRats.img controls.setBlockLength(5) ratapplier.apply(myFunc, inRats, outRats, controls=controls) col = rat.readColumn(imgfile, 'Value') colSqrd = rat.readColumn(imgfile, 'sqrd') ok = True if (col**2 != colSqrd).any(): riostestutils.report(TESTNAME, "sqrd incorrect, in sameFile output") ok = False return ok
def testNewRat(imgfile4): makeTestFile(imgfile4, withRat=False) inRats = ratapplier.RatAssociations() outRats = ratapplier.RatAssociations() controls = ratapplier.RatApplierControls() controls.setRowCount(256) outRats.outimg = ratapplier.RatHandle(imgfile4) controls.setBlockLength(3) ratapplier.apply(myFuncNewRat, inRats, outRats, controls=controls) col = rat.readColumn(imgfile4, 'newCol') colIntended = numpy.arange(256, dtype=numpy.uint32) ok = (col == colIntended).all() if not ok: riostestutils.report(TESTNAME, "New RAT incorrect: %s, %s" % (col, colIntended)) return ok
def testDifferentOutput(imgfile, imgfile2): makeTestFile(imgfile2, withRat=False) inRats = ratapplier.RatAssociations() outRats = ratapplier.RatAssociations() controls = ratapplier.RatApplierControls() inRats.img = ratapplier.RatHandle(imgfile) outRats.outimg = ratapplier.RatHandle(imgfile2) controls.setBlockLength(3) ratapplier.apply(myFuncDiffFile, inRats, outRats, controls=controls) col = rat.readColumn(imgfile, 'Value') colSqrd = rat.readColumn(imgfile2, 'sqrd') ok = True if (col**2 != colSqrd).any(): riostestutils.report(TESTNAME, "sqrd incorrect, in differentFile output") ok = False return ok
def run(): """ Run tests of the rios.rat functions """ riostestutils.reportStart(TESTNAME) allOK = True imgfile = 'test.img' ratValues = makeTestFile(imgfile) nValues = len(ratValues) columnList = [("Int32", numpy.int32), ("Float32", numpy.float32), ("Unicode", numpy.dtype('U10'))] # Only test old string type for python 2 if sys.version_info.major < 3: columnList.append(("String", numpy.dtype('S10'))) allOK = True for (colName, arrayDtype) in columnList: # Write the array into the file, with the given datatype ratValues_type = ratValues.astype(arrayDtype) rat.writeColumn(imgfile, colName, ratValues_type) # Read it back, and check that the values are the same ratValues_fromFile = rat.readColumn(imgfile, colName)[:nValues].astype( ratValues.dtype) if not (ratValues_fromFile == ratValues).all(): riostestutils.report(TESTNAME, "Value mis-match for column %s" % (colName)) allOK = False if os.path.exists(imgfile): os.remove(imgfile) if allOK: riostestutils.report(TESTNAME, "Passed") return allOK
def testReduceRat(imgfile, imgfile3): """ This test creates a new output image, with all odd pixel values replaced with the even number above it. The RAT must then be copied across with the same reduction performed. In this case, only the even numbered rows are written """ # First we copy the raster, with the reduction of pixel values infiles = applier.FilenameAssociations() outfiles = applier.FilenameAssociations() infiles.inimg = imgfile outfiles.outimg = imgfile3 # Make sure we use a format which actually supports RAT's controls = applier.ApplierControls() controls.setOutputDriverName('HFA') applier.apply(rasterReduceFunc, infiles, outfiles, controls=controls) # Now use ratapplier to reduce the RAT inRats = ratapplier.RatAssociations() outRats = ratapplier.RatAssociations() controls = ratapplier.RatApplierControls() inRats.img = ratapplier.RatHandle(imgfile) outRats.outimg = ratapplier.RatHandle(imgfile3) controls.setBlockLength(3) ratapplier.apply(ratReduceFunc, inRats, outRats, controls=controls) col = rat.readColumn(imgfile, 'Value') colEven = col[::2] colReduced = rat.readColumn(imgfile3, 'Value')[:len(colEven)] ok = True if (colEven != colReduced).any(): riostestutils.report( TESTNAME, "Reduced RAT incorrect: %s, %s" % (colEven, colReduced)) ok = False return ok
def run(): """ Run tests of the rios.colortable functions """ riostestutils.reportStart(TESTNAME) allOK = True imgfile = 'test.img' ds = makeTestFile(imgfile) # test generating color tables tableNames = colortable.getRampNames() allOK = RAMPNAME in tableNames if not allOK: riostestutils.report(TESTNAME, "Cannot find {} color ramp".format(RAMPNAME)) if allOK: # write a table out table = colortable.genTable(NUMENTRIES, RAMPNAME, 0) colortable.setTable(ds, table) # now read it back table_fromfile = colortable.getTable(ds) if not (table == table_fromfile).all(): riostestutils.report(TESTNAME, "Value mis-match for color table") allOK = False if os.path.exists(imgfile): os.remove(imgfile) if allOK: riostestutils.report(TESTNAME, "Passed") return allOK
def run(): """ Run tests of a number of more common output format drivers """ riostestutils.reportStart(TESTNAME) usingExceptions = gdal.GetUseExceptions() gdal.UseExceptions() driverTestList = [ ('HFA', ['COMPRESS=YES'], '.img'), ('GTiff', ['COMPRESS=LZW', 'TILED=YES', 'INTERLEAVE=BAND'], '.tif'), ('ENVI', ['INTERLEAVE=BSQ'], ''), ('KEA', [], '.kea') ] # Remove any which current GDAL not suporting driverTestList = [(drvrName, options, suffix) for (drvrName, options, suffix) in driverTestList if gdal.GetDriverByName(drvrName) is not None] riostestutils.report( TESTNAME, 'Testing drivers {}'.format(str([d[0] for d in driverTestList]))) filename = 'test.img' riostestutils.genRampImageFile(filename) ok = True outfileList = [] errorList = [] for (drvr, creationOptions, suffix) in driverTestList: infiles = applier.FilenameAssociations() outfiles = applier.FilenameAssociations() infiles.inimg = filename outfiles.outimg = "testout" + suffix outfileList.append(outfiles.outimg) controls = applier.ApplierControls() controls.setOutputDriverName(drvr) try: applier.apply(copyImg, infiles, outfiles, controls=controls) except Exception as e: ok = False errorList.append("{}:{}".format(drvr, str(e))) if ok: riostestutils.report(TESTNAME, "Passed") else: riostestutils.report( TESTNAME, "Resulted in these apparent errors:\n {}".format( '\n '.join(errorList))) for fn in [filename] + outfileList: if os.path.exists(fn): drvr = gdal.Open(fn).GetDriver() # drvr.Delete(fn) if not usingExceptions: gdal.DontUseExceptions() return ok