/** * * Copyright (C) 2005 Andy Turner, CCG, University of Leeds, UK * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package uk.ac.leeds.ccg.andyt.projects.geomorphometrics; 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/geomorphometrics/workspace/" ); DEMProcessing dEMProcessing = new DEMProcessing( workspace ); dEMProcessing.run(); } public void run() { try { time = System.currentTimeMillis(); boolean handleOutOfMemoryError = true; runTest( handleOutOfMemoryError ); System.out.println( "Processing complete in " + Utilities.time( System.currentTimeMillis() - time ) ); } catch ( Exception e ) { e.printStackTrace(); } catch ( Error e ) { e.printStackTrace(); } } public void runTest( boolean handleOutOfMemoryError ) throws Exception { log( "runTest();" , handleOutOfMemoryError ); //String filename = "catchment1.asc"; String filename = "geom_rast.asc"; //String filename = "crete_w0.asc"; String dataDirectory = "C:/Work/src/andyt/java/projects/geomorphometrics/data/"; String filenameWithoutExtension = filename.substring(0,filename.length() - 4); time = System.currentTimeMillis(); int chunkNrows = 64; //64, 128, 256, 512, 1024, 2048 int chunkNcols = 64; //64, 128, 256, 512, 1024, 2048 this.grid2DSquareCellDoubleFactory.setChunkNrows( chunkNrows ); this.grid2DSquareCellDoubleFactory.setChunkNcols( chunkNcols ); this.grid2DSquareCellDoubleFactory.setHandleOutOfMemoryError( handleOutOfMemoryError ); this.grid2DSquareCellIntFactory.setChunkNrows( chunkNrows ); this.grid2DSquareCellIntFactory.setChunkNcols( chunkNcols ); this.grid2DSquareCellIntFactory.setHandleOutOfMemoryError( handleOutOfMemoryError ); Grid2DSquareCellInt grid2DSquareCellInt = null; Grid2DSquareCellDouble grid2DSquareCellDouble = null; // boolean load1 = true; // if ( load1 ) { // File file = new File( dataDirectory + filename ); // grid2DSquareCellInt = ( Grid2DSquareCellInt ) grid2DSquareCellIntFactory.create( file ); // boolean swapToFileCache = true; // grid2DSquareCellInt.writeToFile( // swapToFileCache, // handleOutOfMemoryError ); // } boolean load1d = true; if ( load1d ) { File file = new File( dataDirectory + filename ); grid2DSquareCellDouble = ( Grid2DSquareCellDouble ) grid2DSquareCellDoubleFactory.create( file ); boolean swapToFileCache = true; grid2DSquareCellDouble.writeToFile( swapToFileCache, handleOutOfMemoryError ); } // boolean load2 = true; // if ( load2 ) { // String directoryName = new String( // dataDirectory + "/" + filenameWithoutExtension + // grid2DSquareCellIntFactory.getClass().getName() + // "_chunkNrows(" + chunkNrows + ")_chunkNcols(" + chunkNcols + ")" ); // File directory = new File( directoryName ); // this.grid2DSquareCellIntFactory.setDirectory( directory ); // grid2DSquareCellInt = ( Grid2DSquareCellInt ) this.grid2DSquareCellIntFactory.create(); // } // 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 ); // } boolean doMetrics1 = true; if ( doMetrics1 ) { //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 ); doMetrics1( this.grid2DSquareCellDoubleFactory, this.grid2DSquareCellIntFactory, grid2DSquareCellDouble, new File( dataDirectory ), handleOutOfMemoryError ); } log( "Processing complete in " + Utilities.time( System.currentTimeMillis() - time ), handleOutOfMemoryError ); } /** * 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(); slopeAndAspect[ i ] = ( Grid2DSquareCellDouble ) grid2DSquareCellDoubleFactory.create( this.grid2DSquareCellDoubleFactory.getGridStatistics(), this.grid2DSquareCellDoubleFactory.getDirectory(), grid2DSquareCell, this.grid2DSquareCellDoubleFactory.getGrid2DSquareCellDoubleChunkFactory(), 0, 0, grid2DSquareCell.getNrows( handleOutOfMemoryError ) - 1, grid2DSquareCell.getNcols( handleOutOfMemoryError ), grid2DSquareCells, handleOutOfMemoryError ); //slopeAndAspect[ i ] = ( Grid2DSquareCellDouble ) grid2DSquareCellDoubleFactory.create( grid2DSquareCell ); this.grid2DSquareCells.add( slopeAndAspect[ i ] ); System.out.println("Initialised slopeAndAspect[ " + i + "]"); } System.out.println("Initialised slopeAndAspect[]"); ESRIAsciiGridExporter eSRIASciiGridExporter = new ESRIAsciiGridExporter(); ImageExporter imageExporter = new ImageExporter(); 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( //slopeAndAspect, 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 ); file = new File( grid2DSquareCell.getDirectory(handleOutOfMemoryError).getParentFile(), slopeAndAspect[ i ].getName( handleOutOfMemoryError ) + "_" + distances + ".png" ); imageExporter.toGreyScaleImage( slopeAndAspect[ i ], file, "PNG", handleOutOfMemoryError ); } } } /** * @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; ESRIAsciiGridExporter eSRIAsciiGridExporter = new ESRIAsciiGridExporter(); ImageExporter imageExporter = new ImageExporter(); for ( int distances = 2; distances <= 16; distances *=2 ) { double distance = cellsize * ( double ) distances; Grid2DSquareCellAbstract[] metrics1 = getMetrics1( grid2DSquareCell, distance, weightIntersect, weightFactor, grid2DSquareCellDoubleFactory, grid2DSquareCellIntFactory, handleOutOfMemoryError ); for ( int i = 0; i < metrics1.length; i ++ ) { mask( metrics1[ i ], 0L, 0L, ( long ) ( nrows - 1 ), ( long ) ( distances - 1 ), handleOutOfMemoryError ); // mask right mask( metrics1[ i ], 0L, ncols - distances, nrows - 1, ncols - 1, handleOutOfMemoryError ); // mask top mask( metrics1[ i ], 0L, 0L, distances - 1, ncols - 1, handleOutOfMemoryError ); // mask bottom mask( metrics1[ i ], nrows - distances, 0, nrows - 1, ncols - 1, handleOutOfMemoryError ); 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 ); } } } }