|
| 1 | +#!/usr/bin/env pytest |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +############################################################################### |
| 4 | +# Project: GDAL/OGR Test Suite |
| 5 | +# Purpose: 'gdal raster inde' testing |
| 6 | +# Author: Even Rouault <even dot rouault @ spatialys.com> |
| 7 | +# |
| 8 | +############################################################################### |
| 9 | +# Copyright (c) 2025, Even Rouault <even dot rouault at spatialys.com> |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: MIT |
| 12 | +############################################################################### |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from osgeo import gdal, ogr |
| 17 | + |
| 18 | +pytestmark = pytest.mark.require_driver("GTI") |
| 19 | + |
| 20 | + |
| 21 | +def get_alg(): |
| 22 | + return gdal.GetGlobalAlgorithmRegistry()["driver"]["gti"]["create"] |
| 23 | + |
| 24 | + |
| 25 | +def test_gdalalg_driver_gti_create_xml_filename(tmp_vsimem): |
| 26 | + |
| 27 | + xml_filename = tmp_vsimem / "out.xml" |
| 28 | + |
| 29 | + alg = get_alg() |
| 30 | + alg["input"] = "../gcore/data/byte.tif" |
| 31 | + alg["output"] = "" |
| 32 | + alg["output-format"] = "MEM" |
| 33 | + alg["layer"] = "my_layer" |
| 34 | + alg["xml-filename"] = xml_filename |
| 35 | + alg["fetch-metadata"] = "AREA_OR_POINT,area_or_point,String" |
| 36 | + assert alg.Run() |
| 37 | + |
| 38 | + ds = alg["output"].GetDataset() |
| 39 | + lyr = ds.GetLayerByName("my_layer") |
| 40 | + assert lyr.GetSpatialRef().GetAuthorityCode(None) == "26711" |
| 41 | + assert lyr.GetLayerDefn().GetFieldCount() == 2 |
| 42 | + assert lyr.GetLayerDefn().GetFieldDefn(0).GetName() == "location" |
| 43 | + assert lyr.GetLayerDefn().GetFieldDefn(0).GetType() == ogr.OFTString |
| 44 | + assert lyr.GetLayerDefn().GetFieldDefn(1).GetName() == "area_or_point" |
| 45 | + assert lyr.GetLayerDefn().GetFieldDefn(1).GetType() == ogr.OFTString |
| 46 | + f = lyr.GetNextFeature() |
| 47 | + assert f["location"] == "../gcore/data/byte.tif" |
| 48 | + assert f["area_or_point"] == "Area" |
| 49 | + assert ( |
| 50 | + f.GetGeometryRef().ExportToWkt() |
| 51 | + == "POLYGON ((440720 3751320,441920 3751320,441920 3750120,440720 3750120,440720 3751320))" |
| 52 | + ) |
| 53 | + assert lyr.GetMetadata_Dict() == {} |
| 54 | + |
| 55 | + with gdal.VSIFile(xml_filename, "rb") as f: |
| 56 | + assert ( |
| 57 | + f.read() |
| 58 | + == b"<GDALTileIndexDataset>\n <IndexDataset></IndexDataset>\n <IndexLayer>my_layer</IndexLayer>\n <LocationField>location</LocationField>\n</GDALTileIndexDataset>\n" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def test_gdalalg_driver_gti_create(): |
| 63 | + |
| 64 | + alg = get_alg() |
| 65 | + alg["input"] = "../gcore/data/byte.tif" |
| 66 | + alg["output"] = "" |
| 67 | + alg["output-format"] = "MEM" |
| 68 | + alg["layer"] = "my_layer" |
| 69 | + alg["resolution"] = [10, 11] |
| 70 | + alg["datatype"] = "UInt16" |
| 71 | + alg["bbox"] = [1, 2, 3, 4] |
| 72 | + alg["band-count"] = 2 |
| 73 | + alg["nodata"] = [5, 6] |
| 74 | + alg["color-interpretation"] = ["red", "green"] |
| 75 | + alg["mask"] = True |
| 76 | + assert alg.Run() |
| 77 | + |
| 78 | + ds = alg["output"].GetDataset() |
| 79 | + lyr = ds.GetLayerByName("my_layer") |
| 80 | + assert lyr.GetMetadata_Dict() == { |
| 81 | + "BAND_COUNT": "2", |
| 82 | + "COLOR_INTERPRETATION": "red,green", |
| 83 | + "DATA_TYPE": "UInt16", |
| 84 | + "LOCATION_FIELD": "location", |
| 85 | + "MASK_BAND": "YES", |
| 86 | + "MAXX": "3", |
| 87 | + "MAXY": "4", |
| 88 | + "MINX": "1", |
| 89 | + "MINY": "2", |
| 90 | + "NODATA": "5,6", |
| 91 | + "RESX": "10", |
| 92 | + "RESY": "11", |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +def test_gdalalg_driver_gti_create_wrong_nodata(): |
| 97 | + |
| 98 | + alg = get_alg() |
| 99 | + alg["input"] = "../gcore/data/byte.tif" |
| 100 | + alg["output"] = "" |
| 101 | + alg["output-format"] = "MEM" |
| 102 | + alg["layer"] = "my_layer" |
| 103 | + alg["band-count"] = 3 |
| 104 | + alg["nodata"] = [5, 6] |
| 105 | + with pytest.raises( |
| 106 | + Exception, match="2 nodata values whereas one or 3 were expected" |
| 107 | + ): |
| 108 | + alg.Run() |
| 109 | + |
| 110 | + |
| 111 | +def test_gdalalg_driver_gti_create_wrong_color_interpretation(): |
| 112 | + |
| 113 | + alg = get_alg() |
| 114 | + alg["input"] = "../gcore/data/byte.tif" |
| 115 | + alg["output"] = "" |
| 116 | + alg["output-format"] = "MEM" |
| 117 | + alg["layer"] = "my_layer" |
| 118 | + alg["band-count"] = 3 |
| 119 | + alg["color-interpretation"] = ["red", "green"] |
| 120 | + with pytest.raises( |
| 121 | + Exception, match="2 color interpretations whereas one or 3 were expected" |
| 122 | + ): |
| 123 | + alg.Run() |
| 124 | + |
| 125 | + |
| 126 | +def test_gdalalg_driver_gti_create_wrong_fetch_metadata(): |
| 127 | + |
| 128 | + alg = get_alg() |
| 129 | + with pytest.raises( |
| 130 | + Exception, |
| 131 | + match="'foo' is not of the form <gdal-metadata-name>,<field-name>,<field-type>", |
| 132 | + ): |
| 133 | + alg["fetch-metadata"] = "foo" |
| 134 | + with pytest.raises( |
| 135 | + Exception, |
| 136 | + match="'foo,bar,baz' has an invalid field type 'baz'. It should be one of 'String', 'Integer', 'Integer64', 'Real', 'Date', 'DateTime'", |
| 137 | + ): |
| 138 | + alg["fetch-metadata"] = "foo,bar,baz" |
0 commit comments