1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/******************************************************************************
* $Id: GDALTestIO.java 25018 2012-09-30 13:15:35Z rouault $
*
* Name: GDALTestIO.java
* Project: GDAL Java Interface
* Purpose: A sample app to test ReadRaster_Direct / WriteRaster_Direct
* Author: Even Rouault, <even dot rouault at mines dash paris dot org>
*
* Adapted from a sample by Ivan Lucena
*
******************************************************************************
* Copyright (c) 2009, Even Rouault
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import org.gdal.gdal.gdal;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.Driver;
import org.gdal.gdalconst.gdalconst;
public class GDALTestIO implements Runnable
{
String filename;
int nbIters;
static final int METHOD_DBB = 1;
static final int METHOD_JAVA_ARRAYS = 2;
static int method;
static volatile boolean bWait = true;
static volatile int nReady = 0;
static Object waiter = new Object();
static Object notifier = new Object();
public GDALTestIO(String filename, int nbIters)
{
this.filename = filename;
this.nbIters = nbIters;
}
public void run()
{
Dataset dataset = null;
Driver driver = null;
Band band = null;
int xsize = 4000;
int ysize = 400;
synchronized(notifier)
{
nReady ++;
notifier.notify();
}
synchronized(waiter)
{
while( bWait )
{
try
{
waiter.wait();
}
catch(InterruptedException ie)
{
}
}
}
driver = gdal.GetDriverByName("GTiff");
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * xsize);
byteBuffer.order(ByteOrder.nativeOrder());
FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
int[] intArray = new int[xsize];
float[] floatArray = new float[xsize];
dataset = driver.Create(filename, xsize, ysize, 1, gdalconst.GDT_Float32);
band = dataset.GetRasterBand(1);
for(int iter = 0; iter < nbIters; iter++)
{
if (method == METHOD_DBB)
{
for( int i = 0; i < ysize; i++) {
for( int j = 0; j < xsize; j++) {
floatBuffer.put(j, (float) (i + j));
}
band.WriteRaster_Direct(0, i, xsize, 1, gdalconst.GDT_Float32, byteBuffer);
}
}
else
{
for( int i = 0; i < ysize; i++) {
for( int j = 0; j < xsize; j++) {
floatArray[j] = (float) (i + j);
}
band.WriteRaster(0, i, xsize, 1, floatArray);
}
}
}
dataset.delete();
/* Open the file to check the values */
dataset = gdal.Open(filename);
band = dataset.GetRasterBand(1);
for(int iter = 0; iter < nbIters; iter++)
{
if (method == METHOD_DBB)
{
for( int i = 0; i < ysize; i++) {
band.ReadRaster_Direct(0, i, xsize, 1, xsize, 1, gdalconst.GDT_Int32, byteBuffer);
for( int j = 0; j < xsize; j++) {
int val = byteBuffer.getInt(j*4);
if (val != (i + j))
throw new RuntimeException("Bad value for (" + j + "," + i + ") : " + val);
}
}
}
else
{
for( int i = 0; i < ysize; i++) {
band.ReadRaster(0, i, xsize, 1, intArray);
for( int j = 0; j < xsize; j++) {
int val = intArray[j];
if (val != (i + j))
throw new RuntimeException("Bad value for (" + j + "," + i + ") : " + val);
}
}
}
}
dataset.delete();
/* Free the memory occupied by the /vsimem file */
gdal.Unlink(filename);
}
public static void main(String[] args) throws InterruptedException
{
gdal.AllRegister();
int nbIters = 50;
method = METHOD_JAVA_ARRAYS;
if (args.length >= 1 && args[0].equalsIgnoreCase("-dbb"))
method = METHOD_DBB;
Thread t1 = new Thread(new GDALTestIO("/vsimem/test1.tif", nbIters));
Thread t2 = new Thread(new GDALTestIO("/vsimem/test2.tif", nbIters));
t1.start();
t2.start();
synchronized(notifier)
{
while( nReady != 2 )
{
try
{
notifier.wait();
}
catch(InterruptedException ie)
{
}
}
}
synchronized(waiter)
{
bWait = false;
waiter.notifyAll();
}
t1.join();
t2.join();
System.out.println("Success !");
}
}
|