I tried rebuilding the coverage with a GridSampleDimension with the
background category set to the nodata category.
It was not pretty.
It appears that the CategoryList ctor only sets its (final) nodata
Category property if it has the minimum value Double.NaN.
GridSampleDimension.getBackground() returns CategoryList.nodata.
The workaround I made:
public static void setBackgroundToNoDataCategory(GridCoverage2D
coverage) throws Exception {
GridSampleDimension[] dims = coverage.getSampleDimensions();
for(int i = 0; i < dims.length; i++) {
GridSampleDimension dim = dims[i];
Category actualNoData = findNoDataCategory(dim);
Field f = GridSampleDimension.class.getDeclaredField("categories");
f.setAccessible(true);
Object categoryList = f.get(dim);
Class c = categoryList.getClass();
/* CategoryList is a pesky non-public class */
while(!c.getName().equals("org.geotools.coverage.CategoryList")) {
c = c.getSuperclass();
}
/* need to search array, getDeclaredField("nodata") doesn't work */
Field[] fields = c.getDeclaredFields();
f = null;
for(int j = 0; j < fields.length; j++) {
if(fields[j].getName().equals("nodata")) {
f = fields[j];
}
}
f.setAccessible(true);
f.set(categoryList, actualNoData);
}
}
public static Category findNoDataCategory(GridSampleDimension d) {
InternationalString s =
Vocabulary.formatInternational(VocabularyKeys.NODATA);
for(Category cat: d.getCategories()) {
if(s.equals(cat.getName())) {
return cat;
}
}
return null;
}
Matthijs
On 2011-05-26 10:32, Matthijs Laan wrote:
> According to the comments in CoverageUtilities.getBackgroundValues() it
> will try to fetch the background value from one of "no data" categories.
> Actually it returns the value from the background category.
>
> The background category is usually different from the no data category
> in coverages produced by some readers I tried (aaig/aig/tiff).
>
> I would like the method to return the actual nodata value. Should it not
> return that already? The reason is that when doing a crop operation with
> a ROI I want it to set the cropped out cells to the no data value. I
> guess I could rebuild the coverage and set the background category to
> the no data category.
>
> Example program output:
>
> ---
> Categories: GeophysicsCategoryList([-32,768 ... -32,768])
> ‣ GeophysicsCategory("No data":[-32768.0 ... -32768.0])
>
> Background category: Category("No data":[0...0])
> Actual no data value: -32768.0
> CoverageUtilities.getBackgroundValues(): [0.0]<---- I would like this
> to be -32768.0
> ---
>
> Example program
>
> public static void main(String[] args) throws Exception {
> InputStream aaigInput = new ByteArrayInputStream((
> "NCOLS 2\nNROWS 2\nXLLCORNER 0.0\nYLLCORNER 0.0\nCELLSIZE
> 1.0\nNODATA_VALUE -32768\n" +
> "0 -32768 " +
> "-32768 1").getBytes("US-ASCII"));
>
> GridCoverage2D c = new ArcGridReader(aaigInput).read(null);
>
> System.out.printf("Categories: %s\n",
> c.getSampleDimension(0).getCategories());
> System.out.printf("Background category: %s\n",
> c.getSampleDimension(0).getBackground());
>
> InternationalString s =
> Vocabulary.formatInternational(VocabularyKeys.NODATA);
> Double nodata = null;
> for(Category cat: c.getSampleDimension(0).getCategories()) {
> if(s.equals(cat.getName())) {
> nodata = cat.getRange().getMinimum();
> }
> }
>
> System.out.printf("Actual no data value: %s\n", nodata == null ?
> "unknown" : nodata);
> System.out.printf("CoverageUtilities.getBackgroundValues(): %s\n",
> Arrays.toString(CoverageUtilities.getBackgroundValues(c)));
> }
>
>
> Matthijs
>
>
> ------------------------------------------------------------------------------
> vRanger cuts backup time in half-while increasing security.
> With the market-leading solution for virtual backup and recovery,
> you get blazing-fast, flexible, and affordable data protection.
> Download your free trial now.
> http://p.sf.net/sfu/quest-d2dcopy1
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
------------------------------------------------------------------------------
vRanger cuts backup time in half-while increasing security.
With the market-leading solution for virtual backup and recovery,
you get blazing-fast, flexible, and affordable data protection.
Download your free trial now.
http://p.sf.net/sfu/quest-d2dcopy1
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users