geotrellis / gdal-warp-bindings

Thread-safe bindings for GDAL's Warp functionality
Apache License 2.0
17 stars 6 forks source link

GDALRasterSource unable to read PAM metadata files #105

Open CloudNiner opened 3 years ago

CloudNiner commented 3 years ago

GDALRasterSource, using these bindings, throws an error when attempting to read metadata for files that use the PAM xml metadata specification.

Open a GeoTrellis 3.5.1+ SBT console:

cd path/to/geotrellis
./sbt "project gdal"
> console

Then try to read metadata of a tiff with external PAM metadata using GDALRasterSource

scala> import geotrellis.raster.gdal.GDALRasterSource
scala> val rs = GDALRasterSource("s3://aviris-data/test/f130329t01p00r06_corr_v1.tiff")
scala> rs.metadata
geotrellis.raster.gdal.MalformedProjectionException: Unable to get the metadata. GDAL Error Code: 1
  at geotrellis.raster.gdal.GDALDataset$.getMetadata$extension3(GDALDataset.scala:73)
  at geotrellis.raster.gdal.GDALDataset$.$anonfun$getMetadata$1(GDALDataset.scala:61)
  at scala.collection.immutable.List.map(List.scala:293)
  at geotrellis.raster.gdal.GDALDataset$.getMetadata$extension1(GDALDataset.scala:61)
  at geotrellis.raster.gdal.GDALMetadata$.apply(GDALMetadata.scala:49)
  at geotrellis.raster.gdal.GDALRasterSource.metadata$lzycompute(GDALRasterSource.scala:59)
  at geotrellis.raster.gdal.GDALRasterSource.metadata(GDALRasterSource.scala:59)
  ... 36 elided

See:

I'm able to read all metadata for this COG with gdalinfo, the Python GDAL bindings, and Rasterio. For example, gdalinfo /vsis3/aviris-data/test/f130329t01p00r06_corr_v1.tiff prints all metadata contained in associated xml file.

pomadchin commented 3 years ago

Probably connected with https://github.com/geotrellis/gdal-warp-bindings/issues/70

pomadchin commented 3 years ago

It looks like the issue happens on this line when we try to access band 0 metadata:

import geotrellis.raster.gdal._
val ds = GDALDataset("/vsis3/aviris-data/test/f130329t01p00r06_corr_v1.tiff")

ds.getAllMetadata(GDALDataset.SOURCE, 0)

//> geotrellis.raster.gdal.MalformedProjectionException: Unable to get the metadata. GDAL Error Code: 1
//   at geotrellis.raster.gdal.GDALDataset$.getMetadata$extension3(GDALDataset.scala:73)
//   at geotrellis.raster.gdal.GDALDataset$.getMetadata$extension2(GDALDataset.scala:63)
//   at geotrellis.raster.gdal.GDALDataset$.$anonfun$getAllMetadata$1(GDALDataset.scala:37)
//   at scala.collection.immutable.List.map(List.scala:293)
//   at geotrellis.raster.gdal.GDALDataset$.getAllMetadata$extension1(GDALDataset.scala:37)
//   ... 36 elided

However the following part still works:

import geotrellis.raster.gdal._

val ds = GDALDataset("/vsis3/aviris-data/test/f130329t01p00r06_corr_v1.tiff")

(1 until ds.bandCount).toList.map(ds.getMetadata(GDALDataset.SOURCE, GDALMetadataDomain.ALL, _))
//>: List[Map[geotrellis.raster.gdal.GDALMetadataDomain,Map[String,String]]] = List(
//   Map( -> Map(wavelength -> 365.9136593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 375.5776593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 385.2466593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 394.9196593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 404.5966593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 414.2786593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 423.9646593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 433.6546593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 443.3496593, wavelength_units -> Nanometers)), 
//   Map( -> Map(wavelength -> 45...