From: <des...@sv...> - 2005-10-28 01:37:51
|
Author: desruisseaux Date: 2005-10-27 18:37:36 -0700 (Thu, 27 Oct 2005) New Revision: 16537 Modified: geotools/trunk/gt/module/main/src/org/geotools/coverage/grid/GridCover= ageFactory.java Log: Added a convenience method for creating a GridCoverage from a matrix (GEO= T-63). Modified: geotools/trunk/gt/module/main/src/org/geotools/coverage/grid/Gr= idCoverageFactory.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/trunk/gt/module/main/src/org/geotools/coverage/grid/GridCove= rageFactory.java 2005-10-28 01:14:58 UTC (rev 16536) +++ geotools/trunk/gt/module/main/src/org/geotools/coverage/grid/GridCove= rageFactory.java 2005-10-28 01:37:36 UTC (rev 16537) @@ -23,6 +23,7 @@ import java.awt.Color; import java.awt.RenderingHints; import java.awt.image.ColorModel; +import java.awt.image.DataBuffer; import java.awt.image.BufferedImage; import java.awt.image.RenderedImage; import java.awt.image.WritableRaster; @@ -42,7 +43,6 @@ import org.opengis.referencing.crs.CoordinateReferenceSystem; import org.opengis.referencing.operation.MathTransform; import org.opengis.spatialschema.geometry.Envelope; -import org.opengis.spatialschema.geometry.MismatchedDimensionException; =20 // Geotools dependencies import org.geotools.factory.Hints; @@ -160,7 +160,6 @@ final GridGeometry2D gridGeome= try, final GridSampleDimension[] bands, final Map propertie= s) - throws MismatchedDimensionException { return create(name, function, new GridGeometry2D(gridGeometry.ge= tGridRange(), gridGeometry.getGridToCoordinateSystem(), crs), bands, p= roperties); @@ -177,9 +176,6 @@ * default sample dimensions. * @param properties The set of properties for this coverage, or {@c= ode null} if there is none. * - * @throws MismatchedDimensionException If the grid range's dimensio= n - * is not the same than the coordinate system's dimension. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, @@ -187,7 +183,6 @@ final GridGeometry2D gridGeometr= y, final GridSampleDimension[] bands, final Map properties) - throws MismatchedDimensionException { final MathTransform transform =3D gridGeometry.getGridToCoordina= teSystem2D(); if (!(transform instanceof AffineTransform)) { @@ -218,6 +213,48 @@ } =20 /** + * Constructs a grid coverage from the specified matrix and {@linkpl= ain Envelope envelope}. + * A default color palette is built from the minimal and maximal val= ues found in the matrix. + * + * @param name The grid coverage name. + * @param raster The matrix data in a {@code [row][column]} layout= . + * {@linkplain Float#NaN NaN} values are mapped to a= transparent color. + * @param envelope The envelope. + * + * @since 2.2 + */ + public GridCoverage2D create(final CharSequence name, + final float[][] matrix, + final Envelope envelope) + { + int width =3D 0; + int height =3D matrix.length; + for (int j=3D0; j<height; j++) { + final float[] row =3D matrix[j]; + if (row !=3D null) { + if (row.length > width) { + width =3D row.length; + } + } + } + final WritableRaster raster; + raster =3D WritableRaster.createBandedRaster(DataBuffer.TYPE_FLO= AT, width, height, 1, null); + for (int j=3D0; j<height; j++) { + int i=3D0; + final float[] row =3D matrix[j]; + if (row !=3D null) { + while (i < row.length) { + raster.setSample(i++, j, 0, row[i]); + } + } + while (i < width) { + raster.setSample(i++, j, 0, Float.NaN); + } + } + return create(name, raster, envelope); + } + + /** * Constructs a grid coverage from the specified {@linkplain Writabl= eRaster raster} and * {@linkplain Envelope envelope}. A default color palette is built = from the minimal and * maximal values found in the raster. @@ -226,13 +263,10 @@ * @param raster The data (may be floating point numbers). {@linkp= lain Float#NaN NaN} * values are mapped to a transparent color. * @param envelope The envelope. - * - * @throws MismatchedDimensionException If the envelope's dimension = is not 2. */ public GridCoverage2D create(final CharSequence name, final WritableRaster raster, final Envelope envelope) - throws MismatchedDimensionException { return create(name, raster, envelope, null, null, null, null, nu= ll); } @@ -251,7 +285,6 @@ final Unit units, final Color[][] colors, final RenderingHints hints) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { return create(name, raster, GridCoverage2D.toEnvelope(envelope, = crs), minValues, maxValues, units, colors, hints); @@ -290,11 +323,6 @@ * which can be one of {@link SampleDimensionType= #UNSIGNED_8BITS UNSIGNED_8BITS} * or {@link SampleDimensionType#UNSIGNED_16BITS = UNSIGNED_16BITS}. * - * @throws MismatchedDimensionException If the envelope's dimension - * is not the same than the coordinate system's dimension. - * @throws IllegalArgumentException if the number of bands differs - * from the number of sample dimensions. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, @@ -305,7 +333,6 @@ final Unit units, final Color[][] colors, final RenderingHints hints) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { final GridSampleDimension[] bands =3D Grid2DSampleDimension.create(name, raster, minValues, maxVal= ues, units, colors, hints); @@ -341,11 +368,6 @@ * specifies the {@link SampleDimensionType} to b= e used at rendering time, * which can be one of {@link SampleDimensionType= #UNSIGNED_8BITS UNSIGNED_8BITS} * or {@link SampleDimensionType#UNSIGNED_16BITS = UNSIGNED_16BITS}. - * - * @throws MismatchedDimensionException If the {@code gridToCRS} dim= ension - * is not the same than the coordinate system's dimension. - * @throws IllegalArgumentException if the number of bands differs - * from the number of sample dimensions. */ public GridCoverage2D create(final CharSequence name, final WritableRaster raster, @@ -356,7 +378,6 @@ final Unit units, final Color[][] colors, final RenderingHints hints) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { final GridSampleDimension[] bands =3D Grid2DSampleDimension.create(name, raster, minValues, maxVal= ues, units, colors, hints); @@ -383,18 +404,12 @@ * sample dimensions. If non-null, then this arr= ay's length must matches * the number of bands in {@code image}. * - * @throws MismatchedDimensionException If the envelope's dimension - * is not the same than the coordinate system's dimension. - * @throws IllegalArgumentException if the number of bands differs - * from the number of sample dimensions. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, final WritableRaster raster, final Envelope envelop= e, final GridSampleDimension[] bands) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { final ColorModel model =3D bands[0].getColorModel(0, bands.le= ngth); final RenderedImage image =3D new BufferedImage(model, raster, f= alse, null); @@ -416,11 +431,6 @@ * default sample dimensions. If non-null, then = this array's length * must matches the number of bands in {@code im= age}. * - * @throws MismatchedDimensionException If the transform's dimension - * is not the same than the coordinate system's dimension. - * @throws IllegalArgumentException if the number of bands differs - * from the number of sample dimensions. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, @@ -428,7 +438,6 @@ final CoordinateReferenceSystem crs, final MathTransform gridToC= RS, final GridSampleDimension[] bands) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { final ColorModel model =3D bands[0].getColorModel(0, bands.le= ngth); final RenderedImage image =3D new BufferedImage(model, raster, f= alse, null); @@ -443,7 +452,6 @@ final RenderedImage image, final CoordinateReferenceSystem crs, final Envelope envelope) - throws MismatchedDimensionException { return create(name, image, crs, envelope, null, null, null); } @@ -462,15 +470,11 @@ * @param image The image. * @param envelope The grid coverage cordinates. * - * @throws MismatchedDimensionException If the envelope's dimension - * is not the same than the coordinate system's dimension. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, final RenderedImage image, final Envelope envelope) - throws MismatchedDimensionException { return create(name, image, envelope, null, null, null); } @@ -487,7 +491,6 @@ final GridSampleDimension[] bands, final GridCoverage[] sources, final Map propertie= s) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { return create(name, image, GridCoverage2D.toEnvelope(envelope, c= rs), bands, sources, properties); @@ -528,11 +531,6 @@ * @param sources The sources for this grid coverage, or {@code n= ull} if none. * @param properties The set of properties for this coverage, or {@c= ode null} if there is none. * - * @throws MismatchedDimensionException If the envelope's dimension - * is not the same than the coordinate system's dimension. - * @throws IllegalArgumentException if the number of bands differs - * from the number of sample dimensions. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, @@ -541,7 +539,6 @@ final GridSampleDimension[] bands, final GridCoverage[] sources, final Map properties) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { /* * Makes sure that the specified envelope has a CRS. @@ -572,11 +569,6 @@ * of bands in the {@code image}. * @param sources The sources for this grid coverage, or {@code n= ull} if none. * @param properties The set of properties for this coverage, or {@c= ode null} if there is none. - * - * @throws MismatchedDimensionException If the transform's dimension - * is not the same than the coordinate system's dimension. - * @throws IllegalArgumentException if the number of bands differs - * from the number of sample dimensions. */ public GridCoverage2D create(final CharSequence name, final RenderedImage image, @@ -585,7 +577,6 @@ final GridSampleDimension[] bands, final GridCoverage[] sources= , final Map propert= ies) - throws MismatchedDimensionException, IllegalArgumentExceptio= n { final GridGeometry2D gm =3D new GridGeometry2D(new GeneralGridRa= nge(image), gridToCRS, crs); return create(name, image, gm, bands, sources, properties); @@ -614,9 +605,6 @@ * @param sources The sources for this grid coverage, or {@code= null} if none. * @param properties The set of properties for this coverage, or {= @code null} none. * - * @throws IllegalArgumentException if the number of bands differs f= rom the number of sample - * dimensions. - * * @since 2.2 */ public GridCoverage2D create(final CharSequence name, @@ -625,7 +613,6 @@ final GridSampleDimension[] bands, final GridCoverage[] sources, final Map properties) - throws IllegalArgumentException { /* * Makes sure that the specified grid geometry has a CRS. |