Class CoverageUtilities


  • public class CoverageUtilities
    extends Object

    A class of utilities bound to raster analysis

    Since:
    0.1
    Author:
    Andrea Antonello - www.hydrologis.com
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.geotools.coverage.grid.GridCoverage2D apply​(org.geotools.coverage.grid.GridCoverage2D coverage, DoubleUnaryOperator operator)
      Apply an operation to the coverage's values.
      static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name, double[][] dataMatrix, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, boolean matrixIsRowCol)
      Creates a coverage from a double[][] matrix and the necessary geographic Information.
      static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name, float[][] dataMatrix, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, boolean matrixIsRowCol)
      Creates a coverage from a float[][] matrix and the necessary geographic Information.
      static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name, int[][] dataMatrix, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, boolean matrixIsRowCol)  
      static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name, RenderedImage renderedImage, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Creates a coverage from the image and the necessary geographic Information.
      static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name, WritableRaster writableRaster, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Creates a coverage from the writable raster and the necessary geographic Information.
      static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name, double[][] dataMatrix, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, boolean matrixIsRowCol, double novalue)  
      static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name, int[][] dataMatrix, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, boolean matrixIsRowCol, int novalue)  
      static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name, RenderedImage renderedImage, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, double novalue)  
      static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name, WritableRaster writableRaster, HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs, double novalue)
      Creates a coverage from the writable raster and the necessary geographic Information.
      static org.geotools.coverage.grid.GridCoverage2D buildDummyCoverage()
      Creates a useless GridCoverage2D that might be usefull as placeholder.
      static double[][] calculateHypsographic​(org.geotools.coverage.grid.GridCoverage2D elevationCoverage, int bins, IHMProgressMonitor pm)
      Calculates the hypsographic curve for the given raster, using the supplied bins.
      static org.geotools.coverage.grid.GridCoverage2D clipCoverage​(org.geotools.coverage.grid.GridCoverage2D coverage, org.geotools.geometry.jts.ReferencedEnvelope envelope)
      Clip a coverage to a given envelope.
      static int[] colRowFromCoordinate​(org.locationtech.jts.geom.Coordinate coordinate, org.geotools.coverage.grid.GridGeometry2D gridGeometry, Point point)
      Utility method to get col and row of a coordinate from a GridGeometry2D.
      static org.locationtech.jts.geom.Coordinate coordinateFromColRow​(int col, int row, org.geotools.coverage.grid.GridGeometry2D gridGeometry)
      Utility method to get the coordinate of a col and row from a GridGeometry2D.
      static org.geotools.coverage.grid.GridCoverage2D coverageValuesMapper​(org.geotools.coverage.grid.GridCoverage2D valuesMap, org.geotools.coverage.grid.GridCoverage2D maskMap)
      Mappes the values of a map (valuesMap) into the valid pixels of the second map (maskMap).
      static org.geotools.coverage.grid.GridCoverage2D createCoverageFromTemplate​(org.geotools.coverage.grid.GridCoverage2D template, Double value, WritableRaster[] writableRasterHolder)
      Creates a new GridCoverage2D using an existing as template.
      static org.opengis.parameter.GeneralParameterValue[] createGridGeometryGeneralParameter​(double xres, double yres, double north, double south, double east, double west, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Utility method to create read parameters for GridCoverageReader
      static org.opengis.parameter.GeneralParameterValue[] createGridGeometryGeneralParameter​(int width, int height, double north, double south, double east, double west, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Utility method to create read parameters for GridCoverageReader
      static org.opengis.parameter.GeneralParameterValue[] createGridGeometryGeneralParameter​(RegionMap regionMap, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Utility method to create read parameters for GridCoverageReader
      static org.geotools.coverage.grid.GridCoverage2D createSubCoverageFromTemplate​(org.geotools.coverage.grid.GridCoverage2D template, org.geotools.geometry.Envelope2D subregion, Double value, WritableRaster[] writableRasterHolder)
      Create a subcoverage given a template coverage and an envelope.
      static WritableRaster createWritableRaster​(int width, int height, Class<?> dataClass, SampleModel sampleModel, Object value)
      Creates a writable raster.
      static WritableRaster createWritableRasterFromArray​(int width, int height, int[] pixels)
      Create a WritableRaster from a int array.
      static WritableRaster createWritableRasterFromMatrix​(double[][] matrix, boolean matrixIsRowCol)
      Create a WritableRaster from a double matrix.
      static WritableRaster createWritableRasterFromMatrix​(float[][] matrix, boolean matrixIsRowCol)
      Create a WritableRaster from a float matrix.
      static WritableRaster createWritableRasterFromMatrix​(int[][] matrix, boolean matrixIsRowCol)  
      static List<ProfilePoint> doProfile​(javax.media.jai.iterator.RandomIter mapIter, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.locationtech.jts.geom.Coordinate... coordinates)
      Calculates the profile of a raster map between given coordinates.
      static List<ProfilePoint> doProfile​(org.geotools.coverage.grid.GridCoverage2D coverage, org.locationtech.jts.geom.Coordinate... coordinates)
      Calculates the profile of a raster map between given coordinates.
      static WritableRaster doubleArray2WritableRaster​(double[] array, int width, int height)
      Transforms an array of values into a WritableRaster.
      static boolean equals​(WritableRaster wr1, WritableRaster wr2)
      Checks two rasters for equality of content.
      static List<org.locationtech.jts.geom.Coordinate> extractPolygonOnCoverage​(javax.media.jai.iterator.RandomIter coverageIterator, int cols, int rows, double xRes, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.locationtech.jts.geom.Polygon polygon, double defaultValue)
      Extracts the positions and values of a polygon mapped on a raster(rasterization).
      static RegionMap generalParameterValues2RegionParamsMap​(org.opengis.parameter.GeneralParameterValue[] params)  
      static org.geotools.coverage.grid.GridCoverage2D getGridCoverage​(org.geotools.coverage.grid.io.GridCoverage2DReader reader, double north, double south, double east, double west, double xRes, double yRes, org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      Get a GridCoverage2D from a reader.
      static int[] getLoopColsRowsForSubregion​(org.geotools.coverage.grid.GridCoverage2D gridCoverage, org.geotools.geometry.Envelope2D subregion)
      Get the cols and rows ranges to use to loop the original gridcoverage.
      static Double getNovalue​(org.geotools.coverage.grid.GridCoverage2D raster)
      Get the novalue defined for the raster.
      static javax.media.jai.iterator.RandomIter getRandomIterator​(org.geotools.coverage.grid.GridCoverage2D coverage)
      Creates a RandomIter for the given GridCoverage2D.
      static double[] getRegionArrayFromGridCoverage​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
      Get the array of region parameters covered by the coverage.
      static int[] getRegionColsRows​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
      Get the array of rows and cols.
      static RegionMap getRegionParamsFromGridCoverage​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
      Get the parameters of the region covered by the coverage.
      static RegionMap getRegionParamsFromImageMosaicReader​(org.geotools.gce.imagemosaic.ImageMosaicReader reader)
      Get the parameters of the region covered by the ImageMosaicReader.
      static org.locationtech.jts.geom.Polygon getRegionPolygon​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
      Create a bounds polygon of a GridCoverage2D.
      static double getValue​(org.geotools.coverage.grid.GridCoverage2D raster, double easting, double northing)
      Simple method to get a value from a single band raster.
      static double getValue​(org.geotools.coverage.grid.GridCoverage2D raster, int col, int row)
      Simple method to get a value from a single band raster.
      static double getValue​(org.geotools.coverage.grid.GridCoverage2D raster, org.locationtech.jts.geom.Coordinate coordinate)  
      static javax.media.jai.iterator.WritableRandomIter getWritableRandomIterator​(int width, int height)
      Creates a WritableRandomIter.
      static javax.media.jai.iterator.WritableRandomIter getWritableRandomIterator​(WritableRaster raster)
      Creates a WritableRandomIter.
      static List<org.locationtech.jts.geom.Polygon> gridcoverageToCellPolygons​(org.geotools.coverage.grid.GridCoverage2D coverage, Predicate<org.locationtech.jts.geom.Coordinate> keepCoordinatePredicate, boolean doIncrementalMerge, IHMProgressMonitor pm)
      Extracts a list of polygons from the cell bounds of a given coverage.
      static RegionMap gridGeometry2RegionParamsMap​(org.geotools.coverage.grid.GridGeometry2D gridGeometry)  
      static org.geotools.coverage.grid.GridGeometry2D gridGeometryFromRegionParams​(HashMap<String,​Double> envelopeParams, org.opengis.referencing.crs.CoordinateReferenceSystem crs)  
      static org.geotools.coverage.grid.GridGeometry2D gridGeometryFromRegionValues​(double north, double south, double east, double west, int cols, int rows, org.opengis.referencing.crs.CoordinateReferenceSystem crs)  
      static Point2D gridToWorld​(org.geotools.coverage.grid.GridGeometry2D gridGeometry, int x, int y)
      Utility to tranform row/col to easting/westing.
      static WritableRaster integerArray2WritableRaster​(int[] array, double divide, int width, int height)
      Transforms an array of integer values into a WritableRaster.
      static org.geotools.coverage.grid.GridCoverage2D invert​(org.geotools.coverage.grid.GridCoverage2D raster, double max)  
      static boolean isGrass​(String path)
      Checks if the given path is a GRASS raster file.
      static RegionMap makeRegionParamsMap​(double north, double south, double west, double east, double xRes, double yRes, int width, int height)  
      static org.geotools.coverage.grid.GridCoverage2D mergeCoverages​(org.geotools.coverage.grid.GridCoverage2D valuesMap, org.geotools.coverage.grid.GridCoverage2D onMap)
      Coverage merger.
      static javax.media.jai.ROI prepareROI​(org.locationtech.jts.geom.Geometry roi, AffineTransform mt2d)
      Utility method for transforming a geometry ROI into the raster space, using the provided affine transformation.
      static byte[] renderedImage2ByteArray​(RenderedImage renderedImage, boolean doRowsThenCols)
      Transform a double values rendered image in its byte array.
      static double[] renderedImage2DoubleArray​(RenderedImage renderedImage)
      Transform a rendered image in its array representation.
      static double[] renderedImage2DoubleArray​(RenderedImage renderedImage, int bandNumber)
      Transform a rendered image in its array representation.
      static WritableRaster renderedImage2DoubleWritableRaster​(RenderedImage renderedImage, boolean nullBorders)  
      static int[] renderedImage2IntegerArray​(RenderedImage renderedImage, double multiply)
      Transform a double values rendered image in its integer array representation by scaling the values.
      static WritableRaster renderedImage2IntWritableRaster​(RenderedImage renderedImage, boolean nullBorders)  
      static WritableRaster renderedImage2ShortWritableRaster​(RenderedImage renderedImage, boolean nullBorders)  
      static WritableRaster renderedImage2WritableRaster​(RenderedImage renderedImage, boolean nullBorders)
      Creates a compatible WritableRaster from a RenderedImage.
      static WritableRaster replaceNovalue​(RenderedImage renderedImage, double newValue)
      Replace the current internal novalue with a given value.
      static void setNovalueBorder​(WritableRaster raster)
      Creates a border of novalues.
      static org.geotools.coverage.grid.GridCoverage2D sumCoverages​(org.geotools.coverage.grid.GridCoverage2D gc1, org.geotools.coverage.grid.GridCoverage2D gc2)  
      static void writeWorldFiles​(org.geotools.coverage.grid.GridCoverage2D coverage, String outPath)  
    • Constructor Detail

      • CoverageUtilities

        public CoverageUtilities()
    • Method Detail

      • getRandomIterator

        public static javax.media.jai.iterator.RandomIter getRandomIterator​(org.geotools.coverage.grid.GridCoverage2D coverage)
        Creates a RandomIter for the given GridCoverage2D.

        It is important to use this method since it supports also large GRASS rasters.

        Parameters:
        coverage - the coverage on which to wrap a RandomIter.
        Returns:
        the iterator.
      • getWritableRandomIterator

        public static javax.media.jai.iterator.WritableRandomIter getWritableRandomIterator​(int width,
                                                                                            int height)
        Creates a WritableRandomIter.

        It is important to use this method since it supports also large GRASS rasters.

        If the size would throw an integer overflow, a GrassLegacyRandomIter will be proposed to try to save the saveable.

        Parameters:
        raster - the coverage on which to wrap a WritableRandomIter.
        Returns:
        the iterator.
      • getWritableRandomIterator

        public static javax.media.jai.iterator.WritableRandomIter getWritableRandomIterator​(WritableRaster raster)
        Creates a WritableRandomIter.

        It is important to use this method since it supports also large GRASS rasters.

        If the size would throw an integer overflow, a GrassLegacyRandomIter will be proposed to try to save the saveable.

        Parameters:
        raster - the coverage on which to wrap a WritableRandomIter.
        Returns:
        the iterator.
      • getGridCoverage

        public static org.geotools.coverage.grid.GridCoverage2D getGridCoverage​(org.geotools.coverage.grid.io.GridCoverage2DReader reader,
                                                                                double north,
                                                                                double south,
                                                                                double east,
                                                                                double west,
                                                                                double xRes,
                                                                                double yRes,
                                                                                org.opengis.referencing.crs.CoordinateReferenceSystem crs)
                                                                         throws Exception
        Get a GridCoverage2D from a reader.
        Parameters:
        reader - the reader to use.
        north - north bound.
        south - south bound.
        east - east bound.
        west - west bound.
        xRes - x resolution.
        yRes - y resolution.
        crs - the CoordinateReferenceSystem.
        Returns:
        the read coverage.
        Throws:
        Exception
      • createWritableRaster

        public static WritableRaster createWritableRaster​(int width,
                                                          int height,
                                                          Class<?> dataClass,
                                                          SampleModel sampleModel,
                                                          Object value)
        Creates a writable raster.
        Parameters:
        width - width of the raster to create.
        height - height of the raster to create.
        dataClass - data type for the raster. If null, defaults to double.
        sampleModel - the samplemodel to use. If null, defaults to new ComponentSampleModel(dataType, width, height, 1, width, new int[]{0});.
        value - value to which to set the raster to. If null, the default of the raster creation is used, which is 0.
        Returns:
        a writable raster.
      • createCoverageFromTemplate

        public static org.geotools.coverage.grid.GridCoverage2D createCoverageFromTemplate​(org.geotools.coverage.grid.GridCoverage2D template,
                                                                                           Double value,
                                                                                           WritableRaster[] writableRasterHolder)
        Creates a new GridCoverage2D using an existing as template.
        Parameters:
        template - the template to use.
        value - the value to set the new raster to, if not null.
        writableRasterHolder - an array of length 1 to place the writable raster in, that was can be used to populate the coverage. If null, it is ignored.
        Returns:
        the new coverage.
      • createSubCoverageFromTemplate

        public static org.geotools.coverage.grid.GridCoverage2D createSubCoverageFromTemplate​(org.geotools.coverage.grid.GridCoverage2D template,
                                                                                              org.geotools.geometry.Envelope2D subregion,
                                                                                              Double value,
                                                                                              WritableRaster[] writableRasterHolder)
        Create a subcoverage given a template coverage and an envelope.
        Parameters:
        template - the template coverage used for the resolution.
        subregion - the envelope to extract to the new coverage. This should be snapped on the resolution of the coverage, in order to avoid shifts.
        value - the value to set the new raster to, if not null.
        writableRasterHolder - an array of length 1 to place the writable raster in, that was can be used to populate the coverage. If null, it is ignored.
        Returns:
        the new coverage.
      • clipCoverage

        public static org.geotools.coverage.grid.GridCoverage2D clipCoverage​(org.geotools.coverage.grid.GridCoverage2D coverage,
                                                                             org.geotools.geometry.jts.ReferencedEnvelope envelope)
                                                                      throws Exception
        Clip a coverage to a given envelope. The region is cut to keep the resolution consistent.
        Throws:
        Exception
      • apply

        public static org.geotools.coverage.grid.GridCoverage2D apply​(org.geotools.coverage.grid.GridCoverage2D coverage,
                                                                      DoubleUnaryOperator operator)
                                                               throws Exception
        Apply an operation to the coverage's values.
        Parameters:
        coverage - the original coverage.
        operator - the operator to apply.
        Returns:
        the new coverage.
        Throws:
        Exception
      • getRegionParamsFromGridCoverage

        public static RegionMap getRegionParamsFromGridCoverage​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
        Get the parameters of the region covered by the coverage.
        Parameters:
        gridCoverage - the coverage.
        Returns:
        the map of parameters. ( NORTH and the other static vars can be used to retrieve them.
      • getRegionParamsFromImageMosaicReader

        public static RegionMap getRegionParamsFromImageMosaicReader​(org.geotools.gce.imagemosaic.ImageMosaicReader reader)
                                                              throws IOException
        Get the parameters of the region covered by the ImageMosaicReader.
        Parameters:
        reader - the ImageMosaicReader.
        Returns:
        the map of parameters. ( NORTH and the other static vars can be used to retrieve them.
        Throws:
        IOException
      • getRegionArrayFromGridCoverage

        public static double[] getRegionArrayFromGridCoverage​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
        Get the array of region parameters covered by the coverage.
        Parameters:
        gridCoverage - the coverage.
        Returns:
        the array of region parameters as [n, s, w, e, xres, yres, cols, rows]
      • getRegionColsRows

        public static int[] getRegionColsRows​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
        Get the array of rows and cols.
        Parameters:
        gridCoverage - the coverage.
        Returns:
        the array as [cols, rows]
      • getRegionPolygon

        public static org.locationtech.jts.geom.Polygon getRegionPolygon​(org.geotools.coverage.grid.GridCoverage2D gridCoverage)
        Create a bounds polygon of a GridCoverage2D.
        Parameters:
        gridCoverage - the coverage to use.
        Returns:
        the bounding polygon.
      • getLoopColsRowsForSubregion

        public static int[] getLoopColsRowsForSubregion​(org.geotools.coverage.grid.GridCoverage2D gridCoverage,
                                                        org.geotools.geometry.Envelope2D subregion)
                                                 throws Exception
        Get the cols and rows ranges to use to loop the original gridcoverage.
        Parameters:
        gridCoverage - the coverage.
        subregion - the sub region of the coverage to get the cols and rows to loop on.
        Returns:
        the array of looping values in the form [minCol, maxCol, minRow, maxRow].
        Throws:
        Exception
      • generalParameterValues2RegionParamsMap

        public static RegionMap generalParameterValues2RegionParamsMap​(org.opengis.parameter.GeneralParameterValue[] params)
      • gridGeometry2RegionParamsMap

        public static RegionMap gridGeometry2RegionParamsMap​(org.geotools.coverage.grid.GridGeometry2D gridGeometry)
      • makeRegionParamsMap

        public static RegionMap makeRegionParamsMap​(double north,
                                                    double south,
                                                    double west,
                                                    double east,
                                                    double xRes,
                                                    double yRes,
                                                    int width,
                                                    int height)
      • gridGeometryFromRegionParams

        public static org.geotools.coverage.grid.GridGeometry2D gridGeometryFromRegionParams​(HashMap<String,​Double> envelopeParams,
                                                                                             org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      • gridGeometryFromRegionValues

        public static org.geotools.coverage.grid.GridGeometry2D gridGeometryFromRegionValues​(double north,
                                                                                             double south,
                                                                                             double east,
                                                                                             double west,
                                                                                             int cols,
                                                                                             int rows,
                                                                                             org.opengis.referencing.crs.CoordinateReferenceSystem crs)
      • createGridGeometryGeneralParameter

        public static org.opengis.parameter.GeneralParameterValue[] createGridGeometryGeneralParameter​(int width,
                                                                                                       int height,
                                                                                                       double north,
                                                                                                       double south,
                                                                                                       double east,
                                                                                                       double west,
                                                                                                       org.opengis.referencing.crs.CoordinateReferenceSystem crs)
        Utility method to create read parameters for GridCoverageReader
        Parameters:
        width - the needed number of columns.
        height - the needed number of columns.
        north - the northern boundary.
        south - the southern boundary.
        east - the eastern boundary.
        west - the western boundary.
        crs - the CoordinateReferenceSystem. Can be null, even if it should not.
        Returns:
        the array of parameters.
      • createGridGeometryGeneralParameter

        public static org.opengis.parameter.GeneralParameterValue[] createGridGeometryGeneralParameter​(RegionMap regionMap,
                                                                                                       org.opengis.referencing.crs.CoordinateReferenceSystem crs)
        Utility method to create read parameters for GridCoverageReader
        Parameters:
        regionMap - the RegionMap.
        crs - the CoordinateReferenceSystem. Can be null, even if it should not.
        Returns:
        the array of parameters.
      • createGridGeometryGeneralParameter

        public static org.opengis.parameter.GeneralParameterValue[] createGridGeometryGeneralParameter​(double xres,
                                                                                                       double yres,
                                                                                                       double north,
                                                                                                       double south,
                                                                                                       double east,
                                                                                                       double west,
                                                                                                       org.opengis.referencing.crs.CoordinateReferenceSystem crs)
        Utility method to create read parameters for GridCoverageReader
        Parameters:
        xres - the X resolution.
        yres - the Y resolution.
        north - the northern boundary.
        south - the southern boundary.
        east - the eastern boundary.
        west - the western boundary.
        crs - the CoordinateReferenceSystem. Can be null, even if it should not.
        Returns:
        the array of parameters.
      • createWritableRasterFromMatrix

        public static WritableRaster createWritableRasterFromMatrix​(double[][] matrix,
                                                                    boolean matrixIsRowCol)
        Create a WritableRaster from a double matrix.
        Parameters:
        matrix - the matrix to take the data from.
        matrixIsRowCol - a flag to tell if the matrix has rowCol or colRow order.
        Returns:
        the produced raster.
      • createWritableRasterFromMatrix

        public static WritableRaster createWritableRasterFromMatrix​(float[][] matrix,
                                                                    boolean matrixIsRowCol)
        Create a WritableRaster from a float matrix.
        Parameters:
        matrix - the matrix to take the data from.
        matrixIsRowCol - a flag to tell if the matrix has rowCol or colRow order.
        Returns:
        the produced raster.
      • createWritableRasterFromMatrix

        public static WritableRaster createWritableRasterFromMatrix​(int[][] matrix,
                                                                    boolean matrixIsRowCol)
      • createWritableRasterFromArray

        public static WritableRaster createWritableRasterFromArray​(int width,
                                                                   int height,
                                                                   int[] pixels)
        Create a WritableRaster from a int array.
        Parameters:
        width - the width of the raster to create.
        height - the height of the raster to create.
        pixels - the array of data.
        Returns:
        the produced raster.
      • buildCoverage

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name,
                                                                              double[][] dataMatrix,
                                                                              HashMap<String,​Double> envelopeParams,
                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                              boolean matrixIsRowCol)
        Creates a coverage from a double[][] matrix and the necessary geographic Information.
        Parameters:
        name - the name of the coverage.
        dataMatrix - the matrix containing the data.
        envelopeParams - the map of boundary parameters.
        crs - the CoordinateReferenceSystem.
        matrixIsRowCol - a flag to tell if the matrix has rowCol or colRow order.
        Returns:
        the coverage.
      • buildCoverageWithNovalue

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name,
                                                                                         double[][] dataMatrix,
                                                                                         HashMap<String,​Double> envelopeParams,
                                                                                         org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                         boolean matrixIsRowCol,
                                                                                         double novalue)
      • buildCoverage

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name,
                                                                              float[][] dataMatrix,
                                                                              HashMap<String,​Double> envelopeParams,
                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                              boolean matrixIsRowCol)
        Creates a coverage from a float[][] matrix and the necessary geographic Information.
        Parameters:
        name - the name of the coverage.
        dataMatrix - the matrix containing the data.
        envelopeParams - the map of boundary parameters.
        crs - the CoordinateReferenceSystem.
        matrixIsRowCol - a flag to tell if the matrix has rowCol or colRow order.
        Returns:
        the coverage.
      • buildCoverage

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name,
                                                                              int[][] dataMatrix,
                                                                              HashMap<String,​Double> envelopeParams,
                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                              boolean matrixIsRowCol)
      • buildCoverageWithNovalue

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name,
                                                                                         int[][] dataMatrix,
                                                                                         HashMap<String,​Double> envelopeParams,
                                                                                         org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                         boolean matrixIsRowCol,
                                                                                         int novalue)
      • buildCoverage

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name,
                                                                              RenderedImage renderedImage,
                                                                              HashMap<String,​Double> envelopeParams,
                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs)
        Creates a coverage from the image and the necessary geographic Information.
        Parameters:
        name - the name of the coverage.
        renderedImage - the image containing the data.
        envelopeParams - the map of boundary parameters.
        crs - the CoordinateReferenceSystem.
        Returns:
        the coverage.
      • buildCoverageWithNovalue

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name,
                                                                                         RenderedImage renderedImage,
                                                                                         HashMap<String,​Double> envelopeParams,
                                                                                         org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                         double novalue)
      • buildCoverage

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverage​(String name,
                                                                              WritableRaster writableRaster,
                                                                              HashMap<String,​Double> envelopeParams,
                                                                              org.opengis.referencing.crs.CoordinateReferenceSystem crs)
        Creates a coverage from the writable raster and the necessary geographic Information.
        Parameters:
        name - the name of the coverage.
        writableRaster - the raster containing the data.
        envelopeParams - the map of boundary parameters.
        crs - the CoordinateReferenceSystem.
        Returns:
        the coverage.
      • buildCoverageWithNovalue

        public static org.geotools.coverage.grid.GridCoverage2D buildCoverageWithNovalue​(String name,
                                                                                         WritableRaster writableRaster,
                                                                                         HashMap<String,​Double> envelopeParams,
                                                                                         org.opengis.referencing.crs.CoordinateReferenceSystem crs,
                                                                                         double novalue)
        Creates a coverage from the writable raster and the necessary geographic Information.

        This also sets the nodata value in the metadata (useful for tiffs for example).

        Parameters:
        name - the name of the coverage.
        writableRaster - the raster containing the data.
        envelopeParams - the map of boundary parameters.
        crs - the CoordinateReferenceSystem.
        novalue - the novalue to set in the metadata.
        Returns:
        the coverage.
      • buildDummyCoverage

        public static org.geotools.coverage.grid.GridCoverage2D buildDummyCoverage()
        Creates a useless GridCoverage2D that might be usefull as placeholder.
        Returns:
        the dummy grod coverage.
      • renderedImage2WritableRaster

        public static WritableRaster renderedImage2WritableRaster​(RenderedImage renderedImage,
                                                                  boolean nullBorders)
        Creates a compatible WritableRaster from a RenderedImage.
        Parameters:
        renderedImage - the image to convert.
        nullBorders - a flag that indicates if the borders should be set to null.
        Returns:
        the converted writable raster.
      • renderedImage2DoubleWritableRaster

        public static WritableRaster renderedImage2DoubleWritableRaster​(RenderedImage renderedImage,
                                                                        boolean nullBorders)
      • renderedImage2IntWritableRaster

        public static WritableRaster renderedImage2IntWritableRaster​(RenderedImage renderedImage,
                                                                     boolean nullBorders)
      • renderedImage2ShortWritableRaster

        public static WritableRaster renderedImage2ShortWritableRaster​(RenderedImage renderedImage,
                                                                       boolean nullBorders)
      • renderedImage2DoubleArray

        public static double[] renderedImage2DoubleArray​(RenderedImage renderedImage)
        Transform a rendered image in its array representation.
        Parameters:
        renderedImage - the rendered image to transform.
        Returns:
        the array holding the data.
      • renderedImage2DoubleArray

        public static double[] renderedImage2DoubleArray​(RenderedImage renderedImage,
                                                         int bandNumber)
        Transform a rendered image in its array representation.
        Parameters:
        renderedImage - the rendered image to transform.
        bandNumber - the the number of the band to consider..
        Returns:
        the array holding the data.
      • renderedImage2IntegerArray

        public static int[] renderedImage2IntegerArray​(RenderedImage renderedImage,
                                                       double multiply)
        Transform a double values rendered image in its integer array representation by scaling the values.
        Parameters:
        renderedImage - the rendered image to transform.
        multiply - value by which to multiply the values before casting to integer.
        Returns:
        the array holding the data.
      • renderedImage2ByteArray

        public static byte[] renderedImage2ByteArray​(RenderedImage renderedImage,
                                                     boolean doRowsThenCols)
        Transform a double values rendered image in its byte array.

        No check is done if the double value fits in a byte.

        Parameters:
        renderedImage - the rendered image to transform.
        doRowsThenCols - if true, rows are processed in the outer loop.
        Returns:
        the array holding the data.
      • integerArray2WritableRaster

        public static WritableRaster integerArray2WritableRaster​(int[] array,
                                                                 double divide,
                                                                 int width,
                                                                 int height)
        Transforms an array of integer values into a WritableRaster.
        Parameters:
        array - the values to transform.
        divide - the factor by which to divide the values.
        width - the width of the resulting image.
        height - the height of the resulting image.
        Returns:
        the raster.
      • doubleArray2WritableRaster

        public static WritableRaster doubleArray2WritableRaster​(double[] array,
                                                                int width,
                                                                int height)
        Transforms an array of values into a WritableRaster.
        Parameters:
        array - the values to transform.
        divide - the factor by which to divide the values.
        width - the width of the resulting image.
        height - the height of the resulting image.
        Returns:
        the raster.
      • setNovalueBorder

        public static void setNovalueBorder​(WritableRaster raster)
        Creates a border of novalues.
        Parameters:
        raster - the raster to process.
      • doProfile

        public static List<ProfilePoint> doProfile​(org.geotools.coverage.grid.GridCoverage2D coverage,
                                                   org.locationtech.jts.geom.Coordinate... coordinates)
                                            throws Exception
        Calculates the profile of a raster map between given coordinates.
        Parameters:
        coverage - the coverage from which to extract the profile.
        coordinates - the coordinates to use to trace the profile.
        Returns:
        the list of ProfilePoints.
        Throws:
        Exception
      • doProfile

        public static List<ProfilePoint> doProfile​(javax.media.jai.iterator.RandomIter mapIter,
                                                   org.geotools.coverage.grid.GridGeometry2D gridGeometry,
                                                   org.locationtech.jts.geom.Coordinate... coordinates)
                                            throws org.opengis.referencing.operation.TransformException
        Calculates the profile of a raster map between given coordinates.

        Note that novalues and points outside of the given raster region are added to the list with a novalue elevation.

        Parameters:
        mapIter - the map iterator.
        gridGeometry - the gridgeometry of the map.
        coordinates - the Coordinates to create the profile on.
        Returns:
        the list of ProfilePoints.
        Throws:
        org.opengis.referencing.operation.TransformException
      • gridToWorld

        public static Point2D gridToWorld​(org.geotools.coverage.grid.GridGeometry2D gridGeometry,
                                          int x,
                                          int y)
                                   throws org.geotools.coverage.grid.InvalidGridGeometryException,
                                          org.opengis.referencing.operation.TransformException
        Utility to tranform row/col to easting/westing.
        Parameters:
        gridGeometry -
        x -
        y -
        Returns:
        the world easting and northing.
        Throws:
        org.geotools.coverage.grid.InvalidGridGeometryException
        org.opengis.referencing.operation.TransformException
      • replaceNovalue

        public static WritableRaster replaceNovalue​(RenderedImage renderedImage,
                                                    double newValue)
        Replace the current internal novalue with a given value.
        Parameters:
        renderedImage - a RenderedImage.
        newValue - the value to put in instead of the novalue.
        Returns:
        the rendered image with the substituted novalue.
      • prepareROI

        public static javax.media.jai.ROI prepareROI​(org.locationtech.jts.geom.Geometry roi,
                                                     AffineTransform mt2d)
                                              throws Exception
        Utility method for transforming a geometry ROI into the raster space, using the provided affine transformation.
        Parameters:
        roi - a Geometry in model space.
        mt2d - an AffineTransform that maps from raster to model space. This is already referred to the pixel corner.
        Returns:
        a ROI suitable for using with JAI.
        Throws:
        org.geotools.process.ProcessException - in case there are problems with ivnerting the provided AffineTransform. Very unlikely to happen.
        Exception
      • isGrass

        public static boolean isGrass​(String path)
        Checks if the given path is a GRASS raster file.

        Note that there is no check on the existence of the file.

        Parameters:
        path - the path to check.
        Returns:
        true if the file is a grass raster.
      • colRowFromCoordinate

        public static int[] colRowFromCoordinate​(org.locationtech.jts.geom.Coordinate coordinate,
                                                 org.geotools.coverage.grid.GridGeometry2D gridGeometry,
                                                 Point point)
        Utility method to get col and row of a coordinate from a GridGeometry2D.
        Parameters:
        coordinate - the coordinate to transform.
        gridGeometry - the gridgeometry to use.
        point - if not null, the row col values are put inside the supplied point's x and y.
        Returns:
        the array with [col, row] or null if something went wrong.
      • coordinateFromColRow

        public static org.locationtech.jts.geom.Coordinate coordinateFromColRow​(int col,
                                                                                int row,
                                                                                org.geotools.coverage.grid.GridGeometry2D gridGeometry)
        Utility method to get the coordinate of a col and row from a GridGeometry2D.
        Parameters:
        col - the col to transform.
        row - the row to transform.
        gridGeometry - the gridgeometry to use.
        Returns:
        the coordinate or null if something went wrong.
      • coverageValuesMapper

        public static org.geotools.coverage.grid.GridCoverage2D coverageValuesMapper​(org.geotools.coverage.grid.GridCoverage2D valuesMap,
                                                                                     org.geotools.coverage.grid.GridCoverage2D maskMap)
        Mappes the values of a map (valuesMap) into the valid pixels of the second map (maskMap).
        Parameters:
        valuesMap - the map holding the values that are needed in the resulting map.
        maskMap - the map to use as mask for the values.
        Returns:
        the map containing the values of the valuesMap, but only in the places in which the maskMap is valid.
      • mergeCoverages

        public static org.geotools.coverage.grid.GridCoverage2D mergeCoverages​(org.geotools.coverage.grid.GridCoverage2D valuesMap,
                                                                               org.geotools.coverage.grid.GridCoverage2D onMap)
        Coverage merger.

        Values from valuesMap are placed into the onMap coverage, if they are valid.

        Parameters:
        valuesMap - the map from which to take teh valid values to place in the output map.
        onMap - the base map on which to place the valuesMap values.
        Returns:
        the merged map of valuesMap over onMap.
      • sumCoverages

        public static org.geotools.coverage.grid.GridCoverage2D sumCoverages​(org.geotools.coverage.grid.GridCoverage2D gc1,
                                                                             org.geotools.coverage.grid.GridCoverage2D gc2)
      • calculateHypsographic

        public static double[][] calculateHypsographic​(org.geotools.coverage.grid.GridCoverage2D elevationCoverage,
                                                       int bins,
                                                       IHMProgressMonitor pm)
        Calculates the hypsographic curve for the given raster, using the supplied bins.
        Parameters:
        elevationCoverage - the elevation raster.
        bins - the bins to use.
        pm - the monitor.
        Returns:
        the matrix containing the hypsographic curve in [elev, area] pairs per row.
      • getValue

        public static double getValue​(org.geotools.coverage.grid.GridCoverage2D raster,
                                      int col,
                                      int row)
        Simple method to get a value from a single band raster.

        Note that this method does always return a value. If invalid, a novalue is returned.

        Parameters:
        raster - the single band raster.
        col - the column.
        row - the row.
        Returns:
        the value in the [col, row] of the first band.
      • getValue

        public static double getValue​(org.geotools.coverage.grid.GridCoverage2D raster,
                                      double easting,
                                      double northing)
        Simple method to get a value from a single band raster.

        Note that this method does always return a value. If invalid, a novalue is returned.

        Parameters:
        raster -
        easting -
        northing -
        Returns:
      • getValue

        public static double getValue​(org.geotools.coverage.grid.GridCoverage2D raster,
                                      org.locationtech.jts.geom.Coordinate coordinate)
      • invert

        public static org.geotools.coverage.grid.GridCoverage2D invert​(org.geotools.coverage.grid.GridCoverage2D raster,
                                                                       double max)
      • equals

        public static boolean equals​(WritableRaster wr1,
                                     WritableRaster wr2)
        Checks two rasters for equality of content.
        Parameters:
        wr1 - the first raster.
        wr2 - the second raster.
        Returns:
        true, if the rasters contain the same values.
      • writeWorldFiles

        public static void writeWorldFiles​(org.geotools.coverage.grid.GridCoverage2D coverage,
                                           String outPath)
                                    throws Exception
        Throws:
        Exception
      • gridcoverageToCellPolygons

        public static List<org.locationtech.jts.geom.Polygon> gridcoverageToCellPolygons​(org.geotools.coverage.grid.GridCoverage2D coverage,
                                                                                         Predicate<org.locationtech.jts.geom.Coordinate> keepCoordinatePredicate,
                                                                                         boolean doIncrementalMerge,
                                                                                         IHMProgressMonitor pm)
        Extracts a list of polygons from the cell bounds of a given coverage.

        Note that the cells are added in a rows and cols order (for each row evaluate each column).

        The userdata of the geometry contains the value of the raster.

        Parameters:
        coverage - the coverage to use.
        keepCoordinatePredicate - an optional predicate to filter out some of the cells.
        Returns:
        the list of envelope geometries.
      • extractPolygonOnCoverage

        public static List<org.locationtech.jts.geom.Coordinate> extractPolygonOnCoverage​(javax.media.jai.iterator.RandomIter coverageIterator,
                                                                                          int cols,
                                                                                          int rows,
                                                                                          double xRes,
                                                                                          org.geotools.coverage.grid.GridGeometry2D gridGeometry,
                                                                                          org.locationtech.jts.geom.Polygon polygon,
                                                                                          double defaultValue)
                                                                                   throws Exception
        Extracts the positions and values of a polygon mapped on a raster(rasterization).

        Bound checks (polygon completely covering the raster) need to be made before running this method.

        Parameters:
        coverageIterator - the raster from which to extract the values.
        cols - the cols of the raster.
        rows - the rows of the raster.
        xRes - the resolution of the raster.
        gridGeometry - the GridGeometry2D of the raster.
        polygon - the polygon to extract.
        defaultValue - a defaultvalue in case no raster is supplied.
        Returns:
        the list of coordinates, with the z values set to the raster or default value.
        Throws:
        Exception
      • getNovalue

        public static Double getNovalue​(org.geotools.coverage.grid.GridCoverage2D raster)
        Get the novalue defined for the raster.
        Parameters:
        raster -
        Returns:
        the novalue or null if none defined.