package uk.ac.leeds.ccg.andyt.DesertLinks; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.HashSet; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellAbstract; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDouble; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunk64CellMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkAbstractFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkArrayFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkJAIFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleChunkRAFFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellDoubleFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellInt; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunk64CellMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkAbstractFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkArrayFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkJAIFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkMapFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntChunkRAFFactory; import uk.ac.leeds.ccg.andyt.grids.core.Grid2DSquareCellIntFactory; import uk.ac.leeds.ccg.andyt.grids.core.GridStatistics0; import uk.ac.leeds.ccg.andyt.grids.core.GridStatistics1; import uk.ac.leeds.ccg.andyt.grids.core.GridStatisticsAbstract; import uk.ac.leeds.ccg.andyt.grids.exchange.ImageExporter; import uk.ac.leeds.ccg.andyt.grids.exchange.ESRIAsciiGridExporter; import uk.ac.leeds.ccg.andyt.grids.process.Grid2DSquareCellProcessorDEM; import uk.ac.leeds.ccg.andyt.grids.utilities.Utilities; import uk.ac.leeds.ccg.andyt.grids.utilities.FileCreator; public class DEMProcessing extends Grid2DSquareCellProcessorDEM { private long time; /** Creates a new ProcessorDEMRuns */ public DEMProcessing() { this( FileCreator.createNewFile() ); } /** * Creates a new ProcessorDEMRuns using specified workspace. * WARNING: Files in the specified workspace may get overwritten. */ public DEMProcessing( File workspace ) { super( workspace ); } /** * @param args the command line arguments */ public static void main(String[] args) { File workspace = new File( "C:/work/Projects/DesertLinks/workspace/" ); DEMProcessing dEMProcessing = new DEMProcessing( workspace ); dEMProcessing.run(); } public void run() { try { time = System.currentTimeMillis(); boolean handleOutOfMemoryError = true; //runLesbos(); //runGuadalentin(); //runAgri(); //runMertola(); //runCrete(); runMed( handleOutOfMemoryError ); //runSimon(); System.out.println( "Processing complete in " + Utilities.time( System.currentTimeMillis() - time ) ); } catch ( Exception e ) { e.printStackTrace(); } catch ( Error e ) { e.printStackTrace(); } } public void runMertola( boolean handleOutOfMemoryError ) { log( "runMertola();", handleOutOfMemoryError ); // Change workspace to Mertola setWorkspace( new File( getWorkspace( handleOutOfMemoryError ), "Mertola" ), handleOutOfMemoryError ); // Copy Data File workspace = getWorkspace( handleOutOfMemoryError ); // SRTM Data String filename = "srtm_mertola.asc"; String dataDirectory = "C:/work/Projects/DesertLinks/Data/portugal/dem/"; // "C:/Documents and Settings/geoagdt/My Documents/src/andyt/java/grids/data/TestData/Geomorphometrics/DEM/"; File file = new File( dataDirectory, filename ); File copy = new File( workspace, filename ); FileCreator.copyFile( file, copy ); //getMetrics1Test(); //getSlopeAndAspect( copy ); log( "Processing complete in " + Utilities.time( System.currentTimeMillis() - time ) , handleOutOfMemoryError ); } public void runCrete( boolean handleOutOfMemoryError ) throws Exception { log( "runCrete();", handleOutOfMemoryError ); // Change workspace to Crete setWorkspace( new File( getWorkspace( handleOutOfMemoryError ), "Crete" ), this.handleOutOfMemoryErrorFalse ); // Copy Data File workspace = getWorkspace( handleOutOfMemoryError ); // SRTM Data String filename = "crete_w0.asc"; String dataDirectory = "C:/work/Projects/DesertLinks/Data/crete/dem/srtm/"; // "C:/Documents and Settings/geoagdt/My Documents/src/andyt/java/grids/data/TestData/Geomorphometrics/DEM/"; File file = new File( dataDirectory, filename ); File copy = new File( workspace, filename ); FileCreator.copyFile( file, copy ); //doMetrics1( copy ); //getSlopeAndAspect( copy ); log( "Processing complete in " + Utilities.time( System.currentTimeMillis() - time ) , handleOutOfMemoryError); } public void runGuadalentin() { } public void runLesbos() { } public void runAgri() { } public void runMed( boolean handleOutOfMemoryError ) throws Exception { log( "runMed();" , handleOutOfMemoryError ); // Initialization int chunkNrows = 512; //64, 128, 256, 512, 1024, 2048 int chunkNcols = 512; //64, 128, 256, 512, 1024, 2048 this.grid2DSquareCellDoubleFactory.setChunkNrows( chunkNrows ); this.grid2DSquareCellDoubleFactory.setChunkNcols( chunkNcols ); String name = new String( "dem_masked_Grid2DSquareCellInt_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); this.grid2DSquareCellIntFactory.setDirectory( new File( "c:/Work/Projects/DesertLinks/Data/DEM/srtm/" + name ) ); this.grid2DSquareCellIntFactory.setChunkNrows( chunkNrows ); this.grid2DSquareCellIntFactory.setChunkNcols( chunkNcols ); this.grid2DSquareCellIntFactory.setHandleOutOfMemoryError( handleOutOfMemoryError ); boolean doSlopeAndAspect = true; if ( doSlopeAndAspect ) { Grid2DSquareCellInt dem = ( Grid2DSquareCellInt ) grid2DSquareCellIntFactory.create(); //dem.writeToFile( true, handleOutOfMemoryError ); //File doubleDirectory = new File( "c:/Work/Projects/DesertLinks/Data/DEM/srtm/dem_masked_Grid2DSquareCellDouble_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); //this.grid2DSquareCells.add( dem ); doSlopeAndAspect( this.grid2DSquareCellDoubleFactory, dem ); } log( "Processing complete in " + Utilities.time( System.currentTimeMillis() - time ), handleOutOfMemoryError ); } public void runSimon( boolean handleOutOfMemoryError ) { log( "runSimon();", handleOutOfMemoryError ); String dataDirectory = "C:/work/src/andyt/grids/data/Geomorphometrics/"; String filename = "dem5"; } public void getFlowAccumulation( String inDataDirectory, String outDataDirectory, String filename ) { /* gridFactory = f; AbstractGrid2DSquareCellDouble grid = jf.createGrid2DSquareCellDouble( new File( inDataDirectory, filename + ".asc" ) ); grid.setName( filename ); int iterations = 1000; double precipitation = 1.0d; int nrows = grid.getNrows(); HashSet outflowCellIDs = new HashSet(); //outflowCellIDs.add( new Integer( grid.getCellID( 53, 0 ) ) ); // Test //outflowCellIDs.add( new Integer( grid.getCellID( 992, 2983 ) ) ); // Agri //outflowCellIDs.add( new Integer( grid.getCellID( 993, 2983 ) ) ); // Agri //outflowCellIDs.add( new Integer( grid.getCellID( 994, 2983 ) ) ); // Agri outflowCellIDs.add( new Integer( grid.getCellID( 337, 0 ) ) ); // Simon AbstractGrid2DSquareCellDouble flowAccumulation = Grid2DSquareCellDoubleProcessorDEM.getFlowAccumulation( grid, iterations, precipitation, outflowCellIDs, gridFactory ); //flowAccumulation.setCell( 53, 0, flowAccumulation.getNoDataValue() ); System.out.println( flowAccumulation.toString() ); flowAccumulation.setName( grid.getName( handleOutOfMemoryError ) + "_flowAccumulation_" + iterations + "iterations" ); if ( gridFactory instanceof Grid2DSquareCellDoubleJAIFactory ) { flowAccumulation.setNoDataValue( flowAccumulation.getGridStatistics().getMin() - 1.0d ); } Grid2DSquareCellDoubleExchange.toImage( flowAccumulation, new File( outDataDirectory, flowAccumulation.getName( handleOutOfMemoryError ) + ".png" ), "PNG" ); Grid2DSquareCellDoubleExchange.toAsciiFile( flowAccumulation, new File( outDataDirectory, flowAccumulation.getName( handleOutOfMemoryError ) + ".asc" ) ); */} public void getHollowFilledSRTM3SecsLesbos( boolean handleOutOfMemoryError ) { /* //Initialisation gridFactory = jf; String demDataDirectory = "C:/work/Projects/DesertLinks/geomorphometry/dems/"; String maskDataDirectory = "C:/work/Projects/DesertLinks/geomorphometry/masks/"; String outDataDirectory = "C:/work/Projects/DesertLinks/geomorphometry/metrics0/"; ff.setDataDirectory( outDataDirectory ); //String filename = "adem"; String filename = "lsrtm"; AbstractGrid2DSquareCellDouble grid = gridFactory.createGrid2DSquareCellDouble( new File( demDataDirectory, filename + ".asc" ) ); //AbstractGrid2DSquareCellDouble mask = gridFactory.createGrid2DSquareCellDouble( new File( maskDataDirectory, "asrtm_a_box.asc" ) ); AbstractGrid2DSquareCellDouble mask = Grid2DSquareCellDoubleProcessor.mask( grid, -100.0d, 2.0d, gridFactory ); System.out.println( grid.toString() ); //System.out.println( mask.toString() ); // Kernel parameters double cellsize = grid.getCellsize(); double weightIntersect = 1.0d; double weightFactor = 1.0d; int gridNcols = grid.getNcols(); int gridNrows = grid.getNrows(); double outflowHeight = 0.0d; int maxIterations = 1000; HashSet outflowCellKeysSet = null; //for ( int distances = 2; distances <= 80; distances *=2 ) { AbstractGrid2DSquareCellDouble fillDEMHollows = Grid2DSquareCellDoubleProcessorDEM.getHollowFilledDEM( grid, outflowHeight, maxIterations, outflowCellKeysSet, gridFactory ); AbstractGrid2DSquareCellDouble fgrid = null; fgrid = f.createGrid2DSquareCellDouble( fillDEMHollows ); //Grid2DSquareCellDoubleExchange.toAsciiFile( fgrid, new File( outDataDirectory, filename + "_filled.asc" ) ); Grid2DSquareCellDoubleExchange.toImage( fgrid, new File( outDataDirectory, filename + "_filled.png" ), "PNG" ); Grid2DSquareCellDoubleProcessor.mask( fgrid, mask ); //Grid2DSquareCellDoubleExchange.toAsciiFile( fgrid, new File( outDataDirectory, filename + "_filled_masked.asc" ) ); Grid2DSquareCellDoubleExchange.toImage( fgrid, new File( outDataDirectory, filename + "_filled_masked.png" ), "PNG" ); fgrid = Grid2DSquareCellDoubleProcessor.minus( fillDEMHollows, grid, true, f ); //Grid2DSquareCellDoubleExchange.toAsciiFile( fgrid, new File( outDataDirectory, filename + "_diff.asc" ) ); Grid2DSquareCellDoubleExchange.toImage( fgrid, new File( outDataDirectory, filename + "_diff.png" ), "PNG" ); Grid2DSquareCellDoubleProcessor.mask( fgrid, mask ); //Grid2DSquareCellDoubleExchange.toAsciiFile( fgrid, new File( outDataDirectory, filename + "_diff_masked.asc" ) ); Grid2DSquareCellDoubleExchange.toImage( fgrid, new File( outDataDirectory, filename + "_diff_masked.png" ), "PNG" ); */} /** * Calculates the slope and aspect of dem and writes results as asciigrid files * @param dem */ public void doSlopeAndAspect( Grid2DSquareCellDoubleFactory grid2DSquareCellDoubleFactory, Grid2DSquareCellAbstract grid2DSquareCell ) throws IOException { boolean handleOutOfMemoryError = true; String message = new String( "doSlopeAndAspect( \n" + "Grid2DSquareCellAbstract( " + grid2DSquareCell.toString( true ) + " ) )" ); log( message, handleOutOfMemoryError ); // Initialisation BigDecimal[] dimensions = grid2DSquareCell.getDimensions( handleOutOfMemoryError ); double cellsize = Double.valueOf( dimensions[ 0 ].toString() ).doubleValue(); double weightIntersect = 1.0d; double weightFactor = 1.0d; long gridNrows = grid2DSquareCell.getNrows( handleOutOfMemoryError ); long gridNcols = grid2DSquareCell.getNcols( handleOutOfMemoryError ); log( "Initialised kernel parameters: cellize( " + cellsize + " ), weightIntersect( " + weightIntersect + " ), weightFactor( " + weightFactor + " ), gridNcols( " + gridNcols + " ), gridNrows( " + gridNrows + " )" , handleOutOfMemoryError); long longGridNrowsMinusOne = gridNrows - 1L; long longGridNcolsMinusOne = gridNcols - 1L; long longZero = 0L; long longOne = 1L; long longDistances = Long.MIN_VALUE; long long0 = Long.MIN_VALUE; boolean reinitialiseMemoryReserve = true; Grid2DSquareCellDouble dummyGrid = null; int distances = Integer.MIN_VALUE; double distance = Double.NEGATIVE_INFINITY; String logString = new String( "This string should be at least as long as any message that is " + "to be logged for memory handling reasons." ); Grid2DSquareCellDouble[] slopeAndAspect = new Grid2DSquareCellDouble[ 4 ]; for ( int i = 0; i < slopeAndAspect.length; i ++ ) { slopeAndAspect[ i ] = new Grid2DSquareCellDouble(); } ESRIAsciiGridExporter eSRIASciiGridExporter = new ESRIAsciiGridExporter(); File file = new File( logString ); // Calculate for ( distances = 2; distances <= 8; distances *=2 ) { distance = cellsize * ( double ) distances; logString = "Distance = " + distance; log( logString, handleOutOfMemoryError ); slopeAndAspect = getSlopeAndAspect( grid2DSquareCell, distance, weightIntersect, weightFactor, grid2DSquareCellDoubleFactory, handleOutOfMemoryError ); for ( int i = 0; i < slopeAndAspect.length; i ++ ) { dummyGrid = ( Grid2DSquareCellDouble ) slopeAndAspect[ i ]; logString = "Masking"; log( logString, handleOutOfMemoryError ); // Mask for edge effects // mask left longDistances = ( long ) ( distances ); long0 = longDistances - longOne; mask( dummyGrid, longZero, longZero, longGridNrowsMinusOne, long0, handleOutOfMemoryError ); // mask right long0 = gridNcols - longDistances; mask( dummyGrid, longZero, long0, longGridNrowsMinusOne, longGridNcolsMinusOne, handleOutOfMemoryError ); // mask top long0 = longDistances - longOne; mask( dummyGrid, longZero, longZero, long0, longGridNcolsMinusOne, handleOutOfMemoryError ); // mask bottom long0 = gridNrows - longDistances; mask( dummyGrid, long0, longZero, longGridNrowsMinusOne, longGridNcolsMinusOne, handleOutOfMemoryError ); log( "Writing Out Results", handleOutOfMemoryError ); file = new File( grid2DSquareCell.getDirectory(handleOutOfMemoryError).getParentFile(), slopeAndAspect[ i ].getName( handleOutOfMemoryError ) + "_" + distances + ".asc" ); eSRIASciiGridExporter.toAsciiFile( slopeAndAspect[ i ], file, handleOutOfMemoryError ); //new ImageExporter().toGreyScaleImage( slopeAndAspect[ i ], new File( dataDirectory, slopeAndAspect[ i ].getName( handleOutOfMemoryError ) + "_" + distances + ".png" ), "PNG" ); } } } /** * @param file Input data file */ public void doMetrics1( Grid2DSquareCellDoubleFactory grid2DSquareCellDoubleFactory, Grid2DSquareCellIntFactory grid2DSquareCellIntFactory, Grid2DSquareCellAbstract grid2DSquareCell, File dataDirectory, boolean handleOutOfMemoryError ) { // Initialistaion BigDecimal[] dimensions = grid2DSquareCell.getDimensions( handleOutOfMemoryError ); double cellsize = Double.valueOf( dimensions[ 0 ].toString() ).doubleValue(); double weightIntersect = 1.0d; double weightFactor = 1.0d; Grid2DSquareCellAbstract dummyGrid = null; long nrows = grid2DSquareCell.getNrows( handleOutOfMemoryError ); long ncols = grid2DSquareCell.getNcols( handleOutOfMemoryError ); File file = new File( dataDirectory, grid2DSquareCell.getName( handleOutOfMemoryError ) ); //int distances = 2; for ( int distances = 4; distances <= 16; distances *=2 ) { double distance = cellsize * ( double ) distances; Grid2DSquareCellProcessorDEM grid2DSquareCellProcessorDEM = new Grid2DSquareCellProcessorDEM(); Grid2DSquareCellAbstract[] metrics1 = getMetrics1( grid2DSquareCell, distance, weightIntersect, weightFactor, grid2DSquareCellDoubleFactory, grid2DSquareCellIntFactory, handleOutOfMemoryError ); for ( int i = 0; i < metrics1.length; i ++ ) { grid2DSquareCellProcessorDEM.mask( metrics1[ i ], 0L, 0L, ( long ) ( nrows - 1 ), ( long ) ( distances - 1 ), handleOutOfMemoryError ); // mask right grid2DSquareCellProcessorDEM.mask( metrics1[ i ], 0L, ncols - distances, nrows - 1, ncols - 1, handleOutOfMemoryError ); // mask top grid2DSquareCellProcessorDEM.mask( metrics1[ i ], 0L, 0L, distances - 1, ncols - 1, handleOutOfMemoryError ); // mask bottom grid2DSquareCellProcessorDEM.mask( metrics1[ i ], nrows - distances, 0, nrows - 1, ncols - 1, handleOutOfMemoryError ); ESRIAsciiGridExporter eSRIAsciiGridExporter = new ESRIAsciiGridExporter(); file = new File( dataDirectory, metrics1[ i ].getName( handleOutOfMemoryError ) + "_" + distances + ".asc" ); eSRIAsciiGridExporter.toAsciiFile( metrics1[ i ], file, new BigDecimal(-9999.0), handleOutOfMemoryError ); //file = new File( // dataDirectory, // metrics1[ i ].getName( handleOutOfMemoryError ) + "_" + distances + ".png" ); //imageExporter.toGreyScaleImage( // metrics1[ i ], // file, // "PNG", // handleOutOfMemoryError ); } } } }