Skip to content

Commit

Permalink
Add read-only raster driver, SNAP_TIFF, for Sentinel Application Proc…
Browse files Browse the repository at this point in the history
…essing GeoTIFF files

Such files are formulated in a way that makes it difficult to read them with the
generic GTiff driver:
- single strip of uncompressed Float32 data type (1.7 GB on the sample file I worked on)
- more than 65,535 GCPs in the GeoTIFF TiePoints tag (> 1 million), which make it
  impossible to read with libgeotiff. Furthermore those tiepoints form
  actually a geolocation array, and are thus best represented using the
  GDAL GEOLOCATION formalism
- a unknown TIFF tag of code 65000, which happens to contain XML DIMAP
  metadata of several megabytes. Part of it contains information on the
  nodata value, offset and scale, and thus must be parsed to report
  appropriate band properties

All that combined calls for a specialized driver, instead of cluttering
even more the generic GTiff driver.
To help for that, I've developed a simple TIFF reader in https://github.com/libertiff/libertiff
and included it here.
  • Loading branch information
rouault committed Oct 7, 2024
1 parent 8cbe405 commit c90f550
Show file tree
Hide file tree
Showing 14 changed files with 2,520 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Supported Formats: (ro:read-only, rw:read-write, +:update, v:virtual-I/O s:subda
VRT -raster,multidimensional raster- (rw+v): Virtual Raster (*.vrt)
DERIVED -raster- (ro): Derived datasets using VRT pixel functions
GTI -raster- (rov): GDAL Raster Tile Index (*.gti.gpkg, *.gti.fgb, *.gti)
SNAP_TIFF -raster- (rov): Sentinel Application Processing GeoTIFF (*.tif, *.tiff)
GTiff -raster- (rw+vs): GeoTIFF (*.tif, *.tiff)
COG -raster- (wv): Cloud optimized GeoTIFF generator (*.tif, *.tiff)
NITF -raster- (rw+vs): National Imagery Transmission Format (*.ntf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Supported Formats: (ro:read-only, rw:read-write, +:update, v:virtual-I/O s:subda
VRT -raster,multidimensional raster- (rw+v): Virtual Raster (*.vrt)
DERIVED -raster- (ro): Derived datasets using VRT pixel functions
GTI -raster- (rov): GDAL Raster Tile Index (*.gti.gpkg, *.gti.fgb, *.gti)
SNAP_TIFF -raster- (rov): Sentinel Application Processing GeoTIFF (*.tif, *.tiff)
GTiff -raster- (rw+vs): GeoTIFF (*.tif, *.tiff)
COG -raster- (wv): Cloud optimized GeoTIFF generator (*.tif, *.tiff)
NITF -raster- (rw+vs): National Imagery Transmission Format (*.ntf)
Expand Down
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ repos:
autotest/cpp/data/|
autotest/gdrivers/data/|
swig/|
third_party/|
third_party/fast_float/|
third_party/LercLib/|
autotest/ogr/data/|
alg/internal_libqhull/|
apps/argparse/|
Expand Down
Binary file not shown.
134 changes: 134 additions & 0 deletions autotest/gdrivers/snap_tiff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/env pytest
# -*- coding: utf-8 -*-
###############################################################################
# Project: GDAL/OGR Test Suite
# Purpose: SNAP_TIFF driver testing.
# Author: Even Rouault <even.rouault at spatialys.com>
#
###############################################################################
# Copyright (c) 2024, Even Rouault <even.rouault at spatialys.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################

import pytest

from osgeo import gdal

pytestmark = pytest.mark.require_driver("SNAP_TIFF")


def test_snap_tiff():
ds = gdal.Open(
"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip"
)
assert ds.GetDriver().GetDescription() == "SNAP_TIFF"
assert ds.RasterXSize == 25548
assert ds.RasterYSize == 16716
assert ds.RasterCount == 1
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float32
assert ds.GetGCPCount() == 4
assert ds.GetGCPSpatialRef().GetAuthorityCode(None) == "4326"
gcps = ds.GetGCPs()
assert len(gcps) == 4
assert gcps[0].GCPPixel == 0.5
assert gcps[0].GCPLine == 0.5
assert gcps[0].GCPX == -121.18662152623274
assert gcps[0].GCPY == 39.655540466308594
assert gcps[3].GCPPixel == 25547.5
assert gcps[3].GCPLine == 16715.5
assert gcps[3].GCPX == -124.43485147116212
assert gcps[3].GCPY == 38.550738598352105
assert ds.GetRasterBand(1).GetNoDataValue() == 0
assert ds.GetRasterBand(1).GetDescription() == "Intensity_VV"
assert ds.GetRasterBand(1).GetUnitType() == "intensity"
assert ds.GetRasterBand(1).GetScale() == 1
assert ds.GetRasterBand(1).GetOffset() == 0
assert ds.GetMetadataDomainList() == [
"",
"DERIVED_SUBDATASETS",
"GEOLOCATION",
"SUBDATASETS",
"xml:DIMAP",
]
assert ds.GetMetadata() == {
"IMAGE_DESCRIPTION": "S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr"
}
assert (
ds.GetMetadataItem("IMAGE_DESCRIPTION")
== "S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr"
)
assert ds.GetMetadata("GEOLOCATION") == {
"LINE_OFFSET": "0",
"LINE_STEP": "16.025886864813039",
"PIXEL_OFFSET": "0",
"PIXEL_STEP": "16.02697616060226",
"SRS": 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS '
'84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
"X_BAND": "1",
"X_DATASET": 'SNAP_TIFF:"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip":GEOLOCATION',
"Y_BAND": "2",
"Y_DATASET": 'SNAP_TIFF:"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip":GEOLOCATION',
}
assert ds.GetMetadataItem("LINE_OFFSET", "GEOLOCATION") == "0"
assert ds.GetMetadata("SUBDATASETS") == {
"SUBDATASET_1_DESC": "Main content of "
"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip",
"SUBDATASET_1_NAME": 'SNAP_TIFF:"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip":MAIN',
"SUBDATASET_2_DESC": "Geolocation array of "
"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip",
"SUBDATASET_2_NAME": 'SNAP_TIFF:"/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip":GEOLOCATION',
}
assert ds.GetMetadata("xml:DIMAP")[0].startswith("<?xml")

subds = gdal.Open(ds.GetMetadataItem("SUBDATASET_1_NAME", "SUBDATASETS"))
assert subds.RasterXSize == ds.RasterXSize

subds = gdal.Open(ds.GetMetadataItem("SUBDATASET_2_NAME", "SUBDATASETS"))
assert subds.RasterXSize == 1595
assert subds.RasterYSize == 1044
assert subds.RasterCount == 2
assert subds.GetRasterBand(1).DataType == gdal.GDT_Float64
assert subds.GetMetadata("SUBDATASETS") == {}
subds.GetRasterBand(1).SetNoDataValue(0)
assert subds.GetRasterBand(1).ComputeRasterMinMax() == (
-124.43485147116212,
-121.18662152623274,
)
subds.GetRasterBand(2).SetNoDataValue(0)
assert subds.GetRasterBand(2).ComputeRasterMinMax() == (
38.15253672014443,
40.05228536834884,
)
with gdal.quiet_errors():
subds.Close()

with pytest.raises(Exception):
gdal.Open("SNAP_TIFF:")

with pytest.raises(Exception):
gdal.Open("SNAP_TIFF:data/byte.tif:MAIN")

with pytest.raises(Exception):
gdal.Open("SNAP_TIFF:data/non_existing.tif:MAIN")

with pytest.raises(Exception):
gdal.Open(
"SNAP_TIFF:/vsizip/vsizip/data/snap_tiff/S1A_IW_GRDH_1SDV_20171009T141532_20171009T141557_018737_01F9E2_E974_tnr_empty_truncated.tif.zip.zip:INVALID"
)
1 change: 1 addition & 0 deletions doc/source/drivers/raster/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Raster drivers
sentinel2
sgi
sigdem
snap_tiff
snodas
srp
srtmhgt
Expand Down
39 changes: 39 additions & 0 deletions doc/source/drivers/raster/snap_tiff.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
.. _raster.snap_tiff:

================================================================================
SNAP_TIFF -- Sentinel Application Processing GeoTIFF
================================================================================

.. versionadded:: 3.10

.. shortname:: SNAP_TIFF

.. built_in_by_default::

This driver deals specifically with GeoTIFF files produced by the
Sentinel Application Processing (SNAP) toolbox.

Such files are formulated in a way that makes it difficult to read them with the
generic :ref:`raster.gtiff` driver.

Driver capabilities
-------------------

.. supports_georeferencing::

.. supports_virtualio::

Georeferencing
--------------

SNAP GeoTIFF files contain a geolocation array (stored as a regular grid of
GeoTIFF tie points). It is reported in the ``GEOLOCATION`` metadata domain.

The 4 corners of the geolocation array are also reported as ground control points
for faster (but less accurate) georeferencing.

Metadata
--------

Extensive metadata following DIMAP conventions is reported in the ``xml:DIMAP``
metadata domain.
1 change: 1 addition & 0 deletions frmts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ gdal_optional_format(grib "WMO General Regularly-distributed Information in Bina
gdal_optional_format(bmp "Microsoft Windows Device Independent Bitmap")
gdal_optional_format(tga "TGA")
gdal_optional_format(stacta "STACTA")
gdal_optional_format(snap_tiff "SNAP TIFF")

# optional Formats
gdal_optional_format(bsb "Maptech/NOAA BSB Nautical Chart Format")
Expand Down
1 change: 1 addition & 0 deletions frmts/drivers.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
VRT
Derived
GTI
SNAP_TIFF
GTiff
COG
NITF
Expand Down
4 changes: 4 additions & 0 deletions frmts/gdalallregister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ void CPL_STDCALL GDALAllRegister()
GDALRegister_GTI();
#endif

#ifdef FRMT_snap_tiff
GDALRegister_SNAP_TIFF();
#endif

#ifdef FRMT_gtiff
GDALRegister_GTiff();
GDALRegister_COG();
Expand Down
9 changes: 9 additions & 0 deletions frmts/snap_tiff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
add_gdal_driver(
TARGET gdal_SNAP_TIFF
SOURCES snaptiffdriver.cpp
PLUGIN_CAPABLE
NO_DEPS
STRONG_CXX_FLAGS
)

gdal_standard_includes(gdal_SNAP_TIFF)
Loading

0 comments on commit c90f550

Please sign in to comment.