<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hi all,</div><div><br></div><div>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.</div><div><br></div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco; "><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">package</span> be.milieuinfo.acdgis.grondwater;</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> <span style="color: rgb(147, 26, 104); ">static</span> org.junit.Assert.assertEquals;</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> java.awt.Color;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> java.awt.Graphics2D;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> java.awt.color.ColorSpace;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> java.awt.image.BufferedImage;</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> javax.measure.unit.SI;</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> org.geotools.coverage.grid.GridCoordinates2D;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> org.geotools.coverage.grid.GridCoverage2D;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> org.geotools.coverage.grid.GridCoverageBuilder;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> org.geotools.coverage.grid.GridCoverageBuilder.Variable;</div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">import</span> org.junit.Test;</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span style="color: rgb(147, 26, 104); ">public</span> <span style="color: rgb(147, 26, 104); ">class</span> GridBuilderTest {</div><div style="margin: 0px; color: rgb(119, 119, 119); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">   </span></span>@Test</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">        </span><span style="color: rgb(147, 26, 104); ">public</span> <span style="color: rgb(147, 26, 104); ">void</span> check_value_of_grid_point_after_drawing() {</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">          </span><span style="color: rgb(147, 26, 104); ">int</span> width = 100, height = 100;</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">         </span>GridCoverageBuilder builder = <span style="color: rgb(147, 26, 104); ">new</span> GridCoverageBuilder();</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">          </span>builder.setCoordinateReferenceSystem(<span style="color: rgb(57, 51, 255); ">"EPSG:31370"</span>);</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">                </span>builder.setEnvelope(0, 0, width, height);</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">                </span></span>// Will use sample value in the range -10000 inclusive to 10000 exclusive.</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">           </span>builder.setSampleRange(-10000, 10000);</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">           </span></span>// Defines elevation (m) = sample / 10</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">               </span>Variable elevation = builder.newVariable(<span style="color: rgb(57, 51, 255); ">"Elevation"</span>, SI.<span style="color: rgb(3, 38, 204); ">METER</span>);</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">               </span>elevation.setLinearTransform(0.01, 0);</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">            </span>elevation.addNodataValue(<span style="color: rgb(57, 51, 255); ">"No data"</span>, 10000);</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">               </span></span>// Gets the image, draw anything we want in it.</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">              </span>builder.setImageSize(width, height);</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">              </span>BufferedImage image = builder.getBufferedImage();</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">         </span>Graphics2D gr = image.createGraphics();</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">          </span></span>//How to define the grid value of the stuff we will draw? setColor??</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">                </span></span>//transform the float value to a Color using the ColorModel and ColorSpace</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">           </span>ColorSpace cs = image.getColorModel().getColorSpace();</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">            </span><span style="color: rgb(147, 26, 104); ">float</span>[] rgb = cs.toRGB(<span style="color: rgb(147, 26, 104); ">new</span> <span style="color: rgb(147, 26, 104); ">float</span>[] { 12.15f });</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">           </span></span>//check the 2-way conversion</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">         </span>assertEquals(12.15f, cs.fromRGB(rgb)[0], 0.001);</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">               </span>gr.setColor(<span style="color: rgb(147, 26, 104); ">new</span> Color(rgb[0], rgb[1], rgb[2]));</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">                </span>gr.fillRect(10, 10, 80, 80);</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">              </span>gr.dispose();</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">            </span></span>// Gets the coverage.</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">                </span>GridCoverage2D coverage = builder.getGridCoverage2D();</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">           </span></span>//Retrieving the value for grid point 20,20 (well in the filled rectangle</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">            </span><span style="color: rgb(147, 26, 104); ">float</span>[] gridPointValues = coverage.evaluate(<span style="color: rgb(147, 26, 104); ">new</span> GridCoordinates2D(20, 20), <span style="color: rgb(147, 26, 104); ">new</span> <span style="color: rgb(147, 26, 104); ">float</span>[1]);</div><div style="margin: 0px; min-height: 15px; "><br></div><div style="margin: 0px; color: rgb(78, 144, 114); "><span style="color: rgb(0, 0, 0); "><span class="Apple-tab-span" style="white-space: pre; ">         </span></span>//this assert fails because for some reason the grid value is 16448.0 instead of 12.15</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">               </span>assertEquals(12.15, gridPointValues[0], 0.001);</div><div style="margin: 0px; "><span class="Apple-tab-span" style="white-space: pre; ">   </span>}</div><div style="margin: 0px; ">}</div><div><br></div></div></div><div apple-content-edited="true">--<br>Thanks in advance</div><div apple-content-edited="true">Jacob</div>
<br></body></html>