[geotk] GridCoverageBuilde using Graphics2D

Jacob Beeusaert jacob at beeusaert.com
Fri Aug 2 08:58:05 PDT 2013

Hi all,

I am having some issues using Graphics2D to draw a GridCoverage using the GridCoverageBuilder. I've written a Test for the behaviour. Hopefully someone can help me understand what is going on.

package be.milieuinfo.acdgis.grondwater;

import static org.junit.Assert.assertEquals;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;

import javax.measure.unit.SI;

import org.geotools.coverage.grid.GridCoordinates2D;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageBuilder;
import org.geotools.coverage.grid.GridCoverageBuilder.Variable;
import org.junit.Test;

public class GridBuilderTest {
	public void check_value_of_grid_point_after_drawing() {

		int width = 100, height = 100;
		GridCoverageBuilder builder = new GridCoverageBuilder();
		builder.setEnvelope(0, 0, width, height);

		// Will use sample value in the range -10000 inclusive to 10000 exclusive.
		builder.setSampleRange(-10000, 10000);

		// Defines elevation (m) = sample / 10
		Variable elevation = builder.newVariable("Elevation", SI.METER);
		elevation.setLinearTransform(0.01, 0);
		elevation.addNodataValue("No data", 10000);

		// Gets the image, draw anything we want in it.
		builder.setImageSize(width, height);
		BufferedImage image = builder.getBufferedImage();
		Graphics2D gr = image.createGraphics();

		//How to define the grid value of the stuff we will draw? setColor??

		//transform the float value to a Color using the ColorModel and ColorSpace
		ColorSpace cs = image.getColorModel().getColorSpace();
		float[] rgb = cs.toRGB(new float[] { 12.15f });

		//check the 2-way conversion
		assertEquals(12.15f, cs.fromRGB(rgb)[0], 0.001);

		gr.setColor(new Color(rgb[0], rgb[1], rgb[2]));
		gr.fillRect(10, 10, 80, 80);

		// Gets the coverage.
		GridCoverage2D coverage = builder.getGridCoverage2D();

		//Retrieving the value for grid point 20,20 (well in the filled rectangle
		float[] gridPointValues = coverage.evaluate(new GridCoordinates2D(20, 20), new float[1]);

		//this assert fails because for some reason the grid value is 16448.0 instead of 12.15
		assertEquals(12.15, gridPointValues[0], 0.001);

Thanks in advance

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/geotoolkit/attachments/20130802/2a820c86/attachment.html>

More information about the Geotoolkit mailing list