Class Gdal

java.lang.Object
com.eco.bio7.gdal.Gdal

public final class Gdal extends Object
GDAL for Java: the GDAL library of an installation (conda, Homebrew, the packages of Linux, QGIS, OSGeo4W) called with the Foreign Function and Memory API (Java 22+; no JNI library, no compiled code).
Gdal.load();                                   // the installed GDAL (or Gdal.load(path))
try (GdalDataset ds = Gdal.open("dem.tif")) {
    float[] heights = ds.band(1).readFloats(); // row by row
    double[] gt = ds.geoTransform();           // the georeferencing
    String crs = ds.spatialReference().authority();  // e.g. "EPSG:32632"
}
Gdal.warp("dem.tif", "dem_wgs84.tif", "-t_srs", "EPSG:4326");   // the command line tools
Gdal.translate("dem.tif", "dem.png", "-of", "PNG", "-scale");
Gdal.vectorTranslate("roads.shp", "roads.gpkg", "-f", "GPKG");
All errors of GDAL are GdalExceptions (with the message of GDAL). Datasets, spatial references and transformations are AutoCloseable.
  • Method Details

    • isLoaded

      public static boolean isLoaded()
      True if GDAL is loaded in this process.
    • library

      public static Path library()
      The loaded library (null if GDAL is not loaded).
    • load

      public static String load()
      Loads the GDAL library which is found first (GdalLibraryFinder).
      Returns:
      the version (e.g., "3.10.2")
    • load

      public static String load(Path library)
      Loads the GDAL library (a file, or a folder with the library), sets the data folders of GDAL and PROJ of its installation and registers all drivers. Only one library can be loaded in a process.
      Returns:
      the version (e.g., "3.10.2")
    • version

      public static String version()
      The version of the loaded GDAL (e.g., "3.10.2").
    • versionInfo

      public static String versionInfo(String request)
      A value of GDALVersionInfo (e.g., "--version", "BUILD_INFO", "LICENSE").
    • setConfigOption

      public static void setConfigOption(String key, String value)
      Sets a configuration option of GDAL (CPLSetConfigOption, e.g., GDAL_NUM_THREADS).
    • open

      public static GdalDataset open(String path)
      Opens a raster or vector dataset for reading (a file, a folder, a URL with /vsicurl/, ...).
    • open

      public static GdalDataset open(Path path)
    • openForUpdate

      public static GdalDataset openForUpdate(String path)
      Opens a dataset for reading and writing.
    • create

      public static GdalDataset create(String path, String driver, int width, int height, int bands, GdalDataType type, String... options)
      Creates a raster dataset (e.g., driver "GTiff"; options of the driver, e.g. "COMPRESS=DEFLATE"). Write the bands and close it (or GdalDataset.flush()).
    • createVector

      public static GdalDataset createVector(String path, String driver, String... options)
      Creates a vector dataset (e.g., driver "GPKG", "ESRI Shapefile", "GeoJSON", "FlatGeobuf", or "MEM" in memory with the name ""): add layers with GdalDataset.createLayer(String, SpatialReference, GeometryType, String...).
    • drivers

      public static List<GdalDriver> drivers()
      The drivers (formats) of this GDAL.
    • info

      public static String info(String path, String... options)
      gdalinfo: the description of a dataset (options of gdalinfo, e.g. "-stats", "-json").
    • translate

      public static void translate(String source, String destination, String... options)
      gdal_translate: converts a raster (options of gdal_translate, e.g. "-of", "PNG", "-scale").
    • translate

      public static GdalDataset translate(GdalDataset source, String destination, String... options)
      gdal_translate of an open dataset; the result is open (e.g., "-of", "MEM" with destination "").
    • warp

      public static void warp(String source, String destination, String... options)
      gdalwarp: reprojects, cuts or resamples a raster (options of gdalwarp, e.g. "-t_srs", "EPSG:4326").
    • warp

      public static GdalDataset warp(GdalDataset source, String destination, String... options)
      gdalwarp of an open dataset; the result is open (e.g., "-of", "MEM" with destination "").
    • vectorTranslate

      public static void vectorTranslate(String source, String destination, String... options)
      ogr2ogr: converts vectors (options of ogr2ogr, e.g. "-f", "GPKG", "-t_srs", "EPSG:4326").
    • vectorTranslate

      public static GdalDataset vectorTranslate(GdalDataset source, String destination, String... options)
      ogr2ogr of an open dataset; the result is open (e.g., "-f", "Memory" with destination "").
    • dem

      public static void dem(String source, String destination, String processing, String... options)
      gdaldem: "hillshade", "slope", "aspect", "color-relief", "TRI", "TPI", "roughness" of an elevation model (options of gdaldem, e.g. "-z", "2", "-alg", "Horn", "-compute_edges").
    • colorRelief

      public static void colorRelief(String source, String destination, String colorFile, String... options)
      gdaldem color-relief: the colors of a color file (lines "elevation r g b [a]", also "nv" for no data).
    • dem

      public static GdalDataset dem(GdalDataset source, String destination, String processing, String colorFile, String... options)
      gdaldem of an open dataset; the result is open (e.g., "-of", "MEM" with destination "").
    • buildVrt

      public static void buildVrt(String destination, List<String> sources, String... options)
      gdalbuildvrt: a virtual mosaic (or stack with "-separate") of raster files (options of gdalbuildvrt).
    • buildVrt

      public static GdalDataset buildVrt(String destination, GdalDataset[] sources, String... options)
      gdalbuildvrt of open datasets; the result is open (destination "" for a virtual raster in memory).
    • rasterize

      public static void rasterize(String vectorSource, String destination, String... options)
      gdal_rasterize: vectors into a new raster (options of gdal_rasterize: the size "-tr" or "-ts", the values "-burn" or "-a field", "-of", "-ot", ...).
    • rasterize

      public static GdalDataset rasterize(GdalDataset vectors, String destination, String... options)
      gdal_rasterize of an open vector dataset into a new raster; the result is open (e.g., "-of", "MEM" with destination "").
    • rasterize

      public static void rasterize(GdalDataset vectors, GdalDataset target, String... options)
      gdal_rasterize into an existing raster (opened for update or created): burns the vectors into its bands.