After using GDAL with Python for a year or so now, I'm getting back into Java and trying to figure out how to use GDAL with it. I am wondering if anyone has had experience with reading data from a raster band with GDAL and Java. I can read the dataset and band in as follows:
Dataset theDataset = gdal.Open("k:/dem.asc", gdalconst.GA_ReadOnly); Band theBand = theDataset.GetRasterBand(1);
...but I am not sure how to read data from
theBand. From the API documentation it seems that I have to set up an array first, which means I have to know how many elements are in the block of data that I want to extract from the band. Like this:
double arr = new double; theBand.ReadRaster(0, 0, 3, 1, arr);
I guess it works fine if you only want to extract one row of data, as above, because you end up with a one-dimensional array. Referencing elements is pretty easy. But if I want to extract a multi-row block of data, it has to be forced into a one-dimensional array. Something like this:
double arr = new double; theBand.ReadRaster(0, 0, 3, 3, arr);
Then if I want to reference an individual raster cell in the array I just extracted (for example, if I want to process the block cell-by-cell), I need to convert my two-dimensional grid coordinate (proxy of easting and northing) into a one-dimensional integer array index.
I guess it's not too difficult to write a little method to convert from a grid reference to essentially a vector reference, but it just seems kind of convoluted, especially after being used to using grid references directly in Python-numpy-GDAL.
So is this the only way to extract data from a band in Java/GDAL? From what I can tell, it is, but I am hoping there is a more intuitive way of processing large blocks of data.