Class RasterUtils

java.lang.Object
org.apache.sedona.common.utils.RasterUtils

public class RasterUtils extends Object
Utility functions for working with GridCoverage2D objects.
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.geotools.coverage.grid.GridCoverage2D
    applyRasterMask(org.geotools.coverage.grid.GridCoverage2D raster, org.geotools.coverage.grid.GridCoverage2D mask)
    Superimposes the mask values onto the original raster, maintaining the original values where the mask is NaN.
    static org.geotools.coverage.grid.GridCoverage2D
    clone(RenderedImage image, org.geotools.coverage.grid.GridGeometry2D gridGeometry2D, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
     
    static org.geotools.coverage.grid.GridCoverage2D
    clone(RenderedImage image, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
     
    static org.geotools.coverage.grid.GridCoverage2D
    clone(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry2D, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
     
    static org.geotools.coverage.grid.GridCoverage2D
    clone(WritableRaster raster, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
     
    static org.locationtech.jts.geom.Geometry
    convertCRSIfNeeded(org.locationtech.jts.geom.Geometry geometry, org.geotools.api.referencing.crs.CoordinateReferenceSystem targetCRS)
     
    static org.geotools.coverage.grid.GridCoverage2D
    copyRasterAndAppendBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, Object bandValues)
     
    static org.geotools.coverage.grid.GridCoverage2D
    copyRasterAndAppendBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, Object bandValues, Double noDataValue)
    This is an experimental method as it does not copy the original raster properties (e.g.
    static org.geotools.coverage.grid.GridCoverage2D
    copyRasterAndReplaceBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, int bandIndex, Object bandValues)
     
    static org.geotools.coverage.grid.GridCoverage2D
    copyRasterAndReplaceBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, int bandIndex, Object bandValues, Double noDataValue, boolean removeNoDataIfNull)
     
    static org.geotools.coverage.grid.GridCoverage2D
    create(RenderedImage image, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands, Double noDataValue)
     
    static org.geotools.coverage.grid.GridCoverage2D
    create(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands)
    Create a new empty raster from the given WritableRaster object.
    static org.geotools.coverage.grid.GridCoverage2D
    create(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands, Double noDataValue)
    Create a new empty raster from the given WritableRaster object.
    static org.geotools.coverage.grid.GridCoverage2D
    create(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands, Double noDataValue, Map properties)
     
    static org.geotools.coverage.GridSampleDimension
    createSampleDimensionWithNoDataValue(String description, double noDataValue)
     
    static org.geotools.coverage.GridSampleDimension
    createSampleDimensionWithNoDataValue(org.geotools.coverage.GridSampleDimension sampleDimension, double noDataValue)
    Create a sample dimension using a given sampleDimension as template, with the give no data value.
    static void
    ensureBand(org.geotools.coverage.grid.GridCoverage2D raster, int band)
    * Throws an exception if band index is greater than the number of bands in a raster
    static org.geotools.coverage.grid.GridCoverage2D
    extractNoDataValueMask(org.geotools.coverage.grid.GridCoverage2D raster)
    Filters out the noDataValue pixels from each band as a grid coverage
    static org.geotools.referencing.operation.transform.AffineTransform2D
    getAffineTransform(org.geotools.coverage.grid.GridCoverage2D raster, org.geotools.api.metadata.spatial.PixelOrientation orientation)
     
    static int
    Converts data types to data type codes
    static org.geotools.referencing.operation.transform.AffineTransform2D
    getGDALAffineTransform(org.geotools.coverage.grid.GridCoverage2D raster)
    Get a GDAL-compliant affine transform from the given raster, where the grid coordinate indicates the upper left corner of the pixel.
    static int[]
    getGridCoordinatesFromWorld(org.geotools.coverage.grid.GridCoverage2D raster, double longitude, double latitude)
     
    static List<Double>
    getNeighboringPixels(int x, int y, int band, Raster raster, Double noDataValue)
    Retrieves a List of neighboring pixel values excluding noDataValue neighbors
    static double
    getNoDataValue(org.geotools.coverage.GridSampleDimension sampleDimension)
    Get the no data value from the given sample dimension.
    static Raster
    getRaster(RenderedImage renderedImage)
     
    static Point2D
    getWorldCornerCoordinates(org.geotools.coverage.grid.GridCoverage2D raster, int colX, int rowY)
     
    static Point2D
    getWorldCornerCoordinatesWithRangeCheck(org.geotools.coverage.grid.GridCoverage2D raster, int colX, int rowY)
    * Returns the world coordinates of the given grid coordinate.
    static boolean
    isDataTypeIntegral(int dataTypeCode)
     
    static void
    isRasterSameShape(org.geotools.coverage.grid.GridCoverage2D raster1, org.geotools.coverage.grid.GridCoverage2D raster2)
    Check if the two rasters are of the same shape
    static org.geotools.coverage.GridSampleDimension
    removeNoDataValue(org.geotools.coverage.GridSampleDimension sampleDimension)
    Remove no data value from the given sample dimension.
    static org.geotools.coverage.grid.GridCoverage2D
    replaceNoDataValues(org.geotools.coverage.grid.GridCoverage2D raster)
    Replaces noDataValue pixels in each band with mean of neighboring pixel values
    static org.apache.commons.lang3.tuple.Pair<org.geotools.coverage.grid.GridCoverage2D,org.locationtech.jts.geom.Geometry>
    setDefaultCRSAndTransform(org.geotools.coverage.grid.GridCoverage2D raster, org.locationtech.jts.geom.Geometry geom)
    If the raster has a CRS, then it transforms the geom to the raster's CRS.
    static org.geotools.coverage.grid.GridCoverage2D
    shiftRasterToZeroOrigin(org.geotools.coverage.grid.GridCoverage2D raster, Double noDataValue)
    Shift the rendered image inside the grid coverage to have a zero origin.
    static org.geotools.referencing.operation.transform.AffineTransform2D
    translateAffineTransform(org.geotools.referencing.operation.transform.AffineTransform2D affine, int offsetX, int offsetY)
    Translate an affine transformation by a given offset.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static org.geotools.coverage.grid.GridCoverage2D create(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands)
      Create a new empty raster from the given WritableRaster object.
      Parameters:
      raster - The raster object to be wrapped as an image.
      gridGeometry - The grid geometry of the raster.
      bands - The bands of the raster.
      Returns:
      A new GridCoverage2D object.
    • clone

      public static org.geotools.coverage.grid.GridCoverage2D clone(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry2D, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
      Parameters:
      raster - WriteableRaster to be used while creating the new raster.
      gridGeometry2D - gridGeometry2D to be used while cloning
      bands - bands to be used while cloning
      referenceRaster - referenceRaster to clone from
      noDataValue - noDataValue (if any) to be applied to the bands. If provided null. bands are unchanged.
      keepMetadata - if passed true, clone all possible metadata from the referenceRaster. keepMetaData controls the presence/absence of the following metadata of the referenceRasterObject: raster name: Name of the raster (GridCoverage2D level) raster properties: A Map of raster and image properties combined. raster sources
      Returns:
      A cloned raster
    • clone

      public static org.geotools.coverage.grid.GridCoverage2D clone(WritableRaster raster, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
    • clone

      public static org.geotools.coverage.grid.GridCoverage2D clone(RenderedImage image, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
    • clone

      public static org.geotools.coverage.grid.GridCoverage2D clone(RenderedImage image, org.geotools.coverage.grid.GridGeometry2D gridGeometry2D, org.geotools.coverage.GridSampleDimension[] bands, org.geotools.coverage.grid.GridCoverage2D referenceRaster, Double noDataValue, boolean keepMetadata)
      Parameters:
      image - Rendered image to create the raster from
      gridGeometry2D - gridGeometry2D to be used while cloning
      bands - bands to be used while cloning
      referenceRaster - referenceRaster to clone from
      noDataValue - noDataValue (if any) to be applied to the bands. If provided null. bands are unchanged.
      keepMetadata - if passed true, clone all possible metadata from the referenceRaster. keepMetaData controls the presence/absence of the following metadata of the referenceRasterObject: raster name: Name of the raster (GridCoverage2D level) raster properties: A Map of raster and image properties combined. raster sources
      Returns:
      A cloned raster
    • create

      public static org.geotools.coverage.grid.GridCoverage2D create(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands, Double noDataValue)
      Create a new empty raster from the given WritableRaster object.
      Parameters:
      raster - The raster object to be wrapped as an image.
      gridGeometry - The grid geometry of the raster.
      bands - The bands of the raster.
      noDataValue - the noDataValue (if any) to be applied to all bands. If provided null, bands are unchanged. keepMetaData controls the presence/absence of the following metadata of the referenceRasterObject: raster name: Name of the raster (GridCoverage2D level) raster properties: A Map of raster and image properties combined. raster sources
      Returns:
      A new GridCoverage2D object.
    • create

      public static org.geotools.coverage.grid.GridCoverage2D create(WritableRaster raster, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands, Double noDataValue, Map properties)
    • create

      public static org.geotools.coverage.grid.GridCoverage2D create(RenderedImage image, org.geotools.coverage.grid.GridGeometry2D gridGeometry, org.geotools.coverage.GridSampleDimension[] bands, Double noDataValue)
    • createSampleDimensionWithNoDataValue

      public static org.geotools.coverage.GridSampleDimension createSampleDimensionWithNoDataValue(org.geotools.coverage.GridSampleDimension sampleDimension, double noDataValue)
      Create a sample dimension using a given sampleDimension as template, with the give no data value.
      Parameters:
      sampleDimension - The sample dimension to be used as template.
      noDataValue - The no data value.
      Returns:
      A new sample dimension with the given no data value.
    • createSampleDimensionWithNoDataValue

      public static org.geotools.coverage.GridSampleDimension createSampleDimensionWithNoDataValue(String description, double noDataValue)
    • removeNoDataValue

      public static org.geotools.coverage.GridSampleDimension removeNoDataValue(org.geotools.coverage.GridSampleDimension sampleDimension)
      Remove no data value from the given sample dimension.
      Parameters:
      sampleDimension - The sample dimension to be processed.
      Returns:
      A new sample dimension without no data value, or the original sample dimension if it does not contain no data value.
    • getNoDataValue

      public static double getNoDataValue(org.geotools.coverage.GridSampleDimension sampleDimension)
      Get the no data value from the given sample dimension. Please use this method to retrieve the no data value of a raster band, instead of GridSampleDimension.getNoDataValues(). The reason is that the latter method has a strange semantics: it treats whatever qualitative categories as no data value, which is not what we want. Additionally, the GeoTiff writer and ArcGrid writer uses the same algorithm as our method for finding no data values when writing the metadata of raster bands.
      Parameters:
      sampleDimension - The sample dimension to be processed.
      Returns:
      The no data value, or Double.NaN if the sample dimension does not contain no data value.
    • getGDALAffineTransform

      public static org.geotools.referencing.operation.transform.AffineTransform2D getGDALAffineTransform(org.geotools.coverage.grid.GridCoverage2D raster)
      Get a GDAL-compliant affine transform from the given raster, where the grid coordinate indicates the upper left corner of the pixel. PostGIS also follows GDAL convention.
      Parameters:
      raster - The raster to get the affine transform from.
      Returns:
      The affine transform.
    • getAffineTransform

      public static org.geotools.referencing.operation.transform.AffineTransform2D getAffineTransform(org.geotools.coverage.grid.GridCoverage2D raster, org.geotools.api.metadata.spatial.PixelOrientation orientation) throws UnsupportedOperationException
      Throws:
      UnsupportedOperationException
    • translateAffineTransform

      public static org.geotools.referencing.operation.transform.AffineTransform2D translateAffineTransform(org.geotools.referencing.operation.transform.AffineTransform2D affine, int offsetX, int offsetY)
      Translate an affine transformation by a given offset.
      Parameters:
      affine - the affine transformation
      offsetX - the offset in x direction
      offsetY - the offset in y direction
      Returns:
      the translated affine transformation
    • getWorldCornerCoordinates

      public static Point2D getWorldCornerCoordinates(org.geotools.coverage.grid.GridCoverage2D raster, int colX, int rowY) throws org.geotools.api.referencing.operation.TransformException
      Throws:
      org.geotools.api.referencing.operation.TransformException
    • getWorldCornerCoordinatesWithRangeCheck

      public static Point2D getWorldCornerCoordinatesWithRangeCheck(org.geotools.coverage.grid.GridCoverage2D raster, int colX, int rowY) throws IndexOutOfBoundsException, org.geotools.api.referencing.operation.TransformException
      * Returns the world coordinates of the given grid coordinate. The expected grid coordinates are 1 indexed. The function also enforces a range check to make sure given grid coordinates are actually inside the grid.
      Parameters:
      raster -
      colX -
      rowY -
      Returns:
      Throws:
      IndexOutOfBoundsException
      org.geotools.api.referencing.operation.TransformException
    • getGridCoordinatesFromWorld

      public static int[] getGridCoordinatesFromWorld(org.geotools.coverage.grid.GridCoverage2D raster, double longitude, double latitude) throws org.geotools.api.referencing.operation.TransformException
      Throws:
      org.geotools.api.referencing.operation.TransformException
    • ensureBand

      public static void ensureBand(org.geotools.coverage.grid.GridCoverage2D raster, int band) throws IllegalArgumentException
      * Throws an exception if band index is greater than the number of bands in a raster
      Parameters:
      raster -
      band -
      Throws:
      IllegalArgumentException
    • getRaster

      public static Raster getRaster(RenderedImage renderedImage)
    • convertCRSIfNeeded

      public static org.locationtech.jts.geom.Geometry convertCRSIfNeeded(org.locationtech.jts.geom.Geometry geometry, org.geotools.api.referencing.crs.CoordinateReferenceSystem targetCRS)
    • setDefaultCRSAndTransform

      public static org.apache.commons.lang3.tuple.Pair<org.geotools.coverage.grid.GridCoverage2D,org.locationtech.jts.geom.Geometry> setDefaultCRSAndTransform(org.geotools.coverage.grid.GridCoverage2D raster, org.locationtech.jts.geom.Geometry geom) throws org.geotools.api.referencing.FactoryException
      If the raster has a CRS, then it transforms the geom to the raster's CRS. If any of the inputs, raster or geom doesn't have a CRS, it defaults to 4326.
      Parameters:
      raster -
      geom -
      Returns:
      Throws:
      org.geotools.api.referencing.FactoryException
    • getDataTypeCode

      public static int getDataTypeCode(String s)
      Converts data types to data type codes
      Parameters:
      s - pixel data type possible values {D, I, B, F, S, US}

      Update: add support to convert RS_BandPixelType data type string to data type code possible values:
      {REAL_64BITS, SIGNED_32BITS, UNSIGNED_8BITS, REAL_32BITS, SIGNED_16BITS, UNSIGNED_16BITS}
      Returns:
      Data type code
    • isDataTypeIntegral

      public static boolean isDataTypeIntegral(int dataTypeCode)
    • copyRasterAndAppendBand

      public static org.geotools.coverage.grid.GridCoverage2D copyRasterAndAppendBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, Object bandValues, Double noDataValue)
      This is an experimental method as it does not copy the original raster properties (e.g. color model, sample model, etc.) moved from MapAlgebra.java TODO: Copy the original raster properties
      Parameters:
      gridCoverage2D -
      bandValues -
      Returns:
    • copyRasterAndAppendBand

      public static org.geotools.coverage.grid.GridCoverage2D copyRasterAndAppendBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, Object bandValues)
    • copyRasterAndReplaceBand

      public static org.geotools.coverage.grid.GridCoverage2D copyRasterAndReplaceBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, int bandIndex, Object bandValues, Double noDataValue, boolean removeNoDataIfNull)
    • copyRasterAndReplaceBand

      public static org.geotools.coverage.grid.GridCoverage2D copyRasterAndReplaceBand(org.geotools.coverage.grid.GridCoverage2D gridCoverage2D, int bandIndex, Object bandValues)
    • isRasterSameShape

      public static void isRasterSameShape(org.geotools.coverage.grid.GridCoverage2D raster1, org.geotools.coverage.grid.GridCoverage2D raster2)
      Check if the two rasters are of the same shape
      Parameters:
      raster1 -
      raster2 -
    • shiftRasterToZeroOrigin

      public static org.geotools.coverage.grid.GridCoverage2D shiftRasterToZeroOrigin(org.geotools.coverage.grid.GridCoverage2D raster, Double noDataValue)
      Shift the rendered image inside the grid coverage to have a zero origin. This won't alter the cell values of the raster, but will shift the affine transformation to cancel with the origin shift.
      Parameters:
      raster - The raster to shift
      noDataValue - The no data value to use for the new raster
      Returns:
      A new grid coverage with the same cell values but its rendered image has a zero origin
    • getNeighboringPixels

      public static List<Double> getNeighboringPixels(int x, int y, int band, Raster raster, Double noDataValue)
      Retrieves a List of neighboring pixel values excluding noDataValue neighbors
      Parameters:
      x - grid coordinate
      y - grid coordinate
      band - Band index of raster
      raster -
      noDataValue -
      Returns:
    • replaceNoDataValues

      public static org.geotools.coverage.grid.GridCoverage2D replaceNoDataValues(org.geotools.coverage.grid.GridCoverage2D raster)
      Replaces noDataValue pixels in each band with mean of neighboring pixel values
      Parameters:
      raster -
      Returns:
      A new grid coverage with noDataValues pixels replaced
    • extractNoDataValueMask

      public static org.geotools.coverage.grid.GridCoverage2D extractNoDataValueMask(org.geotools.coverage.grid.GridCoverage2D raster)
      Filters out the noDataValue pixels from each band as a grid coverage
      Parameters:
      raster -
      Returns:
      Returns a grid coverage with noDataValues and valid data values as Double.Nan
    • applyRasterMask

      public static org.geotools.coverage.grid.GridCoverage2D applyRasterMask(org.geotools.coverage.grid.GridCoverage2D raster, org.geotools.coverage.grid.GridCoverage2D mask)
      Superimposes the mask values onto the original raster, maintaining the original values where the mask is NaN.
      Parameters:
      raster - The original raster to which the mask will be applied.
      mask - Grid coverage mask to be applied, containing the values to overlay. This mask should have the same dimensions and number of bands as the original raster.
      Returns:
      A new GridCoverage2D object with the mask applied.