Class RasterGrid

java.lang.Object
dev.avelar.mapnik.RasterGrid

public final class RasterGrid extends Object
A grid of numbers over an area, such as an elevation model, read from a file without GDAL. Pure Java: it needs no native library, so it works anywhere. Turn it into a GrayImage to colour and draw it.

 RasterGrid dem = RasterGrid.read(Paths.get("dem.tif"));
 try (GrayImage g = dem.toGrayImage()) {
     Image picture = ramp.colorize(g);
 }
 

Supported files:

  • Esri ASCII grid (.asc): the header with ncols, nrows, the lower left corner or centre, cellsize and optional NODATA_value.
  • GeoTIFF (.tif, .tiff): classic TIFF (not BigTIFF), strips or tiles, 8, 16, 32 and 64-bit unsigned, signed and floating point samples, compression none, deflate, LZW and PackBits, with the horizontal and floating-point predictors. The first band is read. The extent comes from the model tiepoint and pixel scale (or transformation) tags, and the projection from the GeoKey directory when it is an EPSG code.
Anything else (JPEG or other compression, BigTIFF) is refused with an IllegalArgumentException that says so.
  • Field Details

    • MAX_PIXELS

      public static final long MAX_PIXELS
      The most pixels one grid may hold: more would need gigabytes of doubles.
      See Also:
  • Method Details

    • width

      public int width()
    • height

      public int height()
    • values

      public double[] values()
      Every value, row by row from the top left. This is the array itself, not a copy.
    • noData

      public OptionalDouble noData()
      The value that means "no data", if the file names one.
    • extent

      public Box2d extent()
      The area the grid covers, in its projection, or null if the file does not say.
    • srs

      public String srs()
      The projection as epsg:<code>, or null if the file has none or it is not an EPSG code.
    • type

      public GrayImage.Type type()
      The pixel type that holds the file's values exactly.
    • range

      public double[] range()
      Smallest and largest value as {min, max}, ignoring NaN and the no-data value. Throws if none.
    • get

      public double get(int x, int y)
      One value. Throws IndexOutOfBoundsException outside the grid.
    • valueAt

      public double valueAt(double mapX, double mapY)
      The value under a map position, or NaN if it is outside the extent or the grid has none.
    • toGrayImage

      public GrayImage toGrayImage()
      A GrayImage of the file's own pixel type. Close it.
    • toGrayImage

      public GrayImage toGrayImage(GrayImage.Type type)
      A GrayImage of the given type. Values that do not fit the type (a float grid into an integer image, say) are refused, not wrapped. Close it.
    • read

      public static RasterGrid read(Path file) throws IOException
      Read a file, choosing the format by its extension: .asc, .tif or .tiff.
      Throws:
      IOException
    • readGeoTiff

      public static RasterGrid readGeoTiff(Path file) throws IOException
      Throws:
      IOException
    • readGeoTiff

      public static RasterGrid readGeoTiff(byte[] data)
    • readAscii

      public static RasterGrid readAscii(Path file) throws IOException
      Throws:
      IOException
    • readAscii

      public static RasterGrid readAscii(String text)
      Read the text of an Esri ASCII grid.