Class Gdal
java.lang.Object
com.eco.bio7.gdal.Gdal
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 Summary
Modifier and TypeMethodDescriptionstatic GdalDatasetbuildVrt(String destination, GdalDataset[] sources, String... options) gdalbuildvrt of open datasets; the result is open (destination "" for a virtual raster in memory).static voidgdalbuildvrt: a virtual mosaic (or stack with "-separate") of raster files (options of gdalbuildvrt).static voidcolorRelief(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).static GdalDatasetcreate(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.static GdalDatasetcreateVector(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 withGdalDataset.createLayer(String, SpatialReference, GeometryType, String...).static GdalDatasetgdaldem of an open dataset; the result is open (e.g., "-of", "MEM" with destination "").static voidgdaldem: "hillshade", "slope", "aspect", "color-relief", "TRI", "TPI", "roughness" of an elevation model (options of gdaldem, e.g.static List<GdalDriver> drivers()The drivers (formats) of this GDAL.static Stringgdalinfo: the description of a dataset (options of gdalinfo, e.g.static booleanisLoaded()True if GDAL is loaded in this process.static Pathlibrary()The loaded library (null if GDAL is not loaded).static Stringload()Loads the GDAL library which is found first (GdalLibraryFinder).static StringLoads 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.static GdalDatasetOpens a raster or vector dataset for reading (a file, a folder, a URL with /vsicurl/, ...).static GdalDatasetstatic GdalDatasetopenForUpdate(String path) Opens a dataset for reading and writing.static voidrasterize(GdalDataset vectors, GdalDataset target, String... options) gdal_rasterize into an existing raster (opened for update or created): burns the vectors into its bands.static GdalDatasetrasterize(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 "").static voidgdal_rasterize: vectors into a new raster (options of gdal_rasterize: the size "-tr" or "-ts", the values "-burn" or "-a field", "-of", "-ot", ...).static voidsetConfigOption(String key, String value) Sets a configuration option of GDAL (CPLSetConfigOption, e.g., GDAL_NUM_THREADS).static GdalDatasettranslate(GdalDataset source, String destination, String... options) gdal_translate of an open dataset; the result is open (e.g., "-of", "MEM" with destination "").static voidgdal_translate: converts a raster (options of gdal_translate, e.g.static GdalDatasetvectorTranslate(GdalDataset source, String destination, String... options) ogr2ogr of an open dataset; the result is open (e.g., "-f", "Memory" with destination "").static voidvectorTranslate(String source, String destination, String... options) ogr2ogr: converts vectors (options of ogr2ogr, e.g.static Stringversion()The version of the loaded GDAL (e.g., "3.10.2").static StringversionInfo(String request) A value of GDALVersionInfo (e.g., "--version", "BUILD_INFO", "LICENSE").static GdalDatasetwarp(GdalDataset source, String destination, String... options) gdalwarp of an open dataset; the result is open (e.g., "-of", "MEM" with destination "").static voidgdalwarp: reprojects, cuts or resamples a raster (options of gdalwarp, e.g.
-
Method Details
-
isLoaded
public static boolean isLoaded()True if GDAL is loaded in this process. -
library
The loaded library (null if GDAL is not loaded). -
load
Loads the GDAL library which is found first (GdalLibraryFinder).- Returns:
- the version (e.g., "3.10.2")
-
load
-
version
The version of the loaded GDAL (e.g., "3.10.2"). -
versionInfo
-
setConfigOption
-
open
Opens a raster or vector dataset for reading (a file, a folder, a URL with /vsicurl/, ...). -
open
-
openForUpdate
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 (orGdalDataset.flush()). -
createVector
Creates a vector dataset (e.g., driver "GPKG", "ESRI Shapefile", "GeoJSON", "FlatGeobuf", or "MEM" in memory with the name ""): add layers withGdalDataset.createLayer(String, SpatialReference, GeometryType, String...). -
drivers
The drivers (formats) of this GDAL. -
info
-
translate
-
translate
gdal_translate of an open dataset; the result is open (e.g., "-of", "MEM" with destination ""). -
warp
-
warp
gdalwarp of an open dataset; the result is open (e.g., "-of", "MEM" with destination ""). -
vectorTranslate
-
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
-
colorRelief
-
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
-
buildVrt
gdalbuildvrt of open datasets; the result is open (destination "" for a virtual raster in memory). -
rasterize
-
rasterize
gdal_rasterize of an open vector dataset into a new raster; the result is open (e.g., "-of", "MEM" with destination ""). -
rasterize
gdal_rasterize into an existing raster (opened for update or created): burns the vectors into its bands.
-