From 5ff3f2fe5d6e44aeca507bc25c09bdc8fd70a856 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Wed, 18 Dec 2024 17:57:28 +0100 Subject: [PATCH] GDALRasterizeLayers(): do not emit warning about missing SRS if the target raster dataset has no SRS Related to https://lists.osgeo.org/pipermail/gdal-dev/2024-December/059948.html --- alg/gdalrasterize.cpp | 16 +++++++++++----- autotest/alg/rasterize.py | 13 +++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/alg/gdalrasterize.cpp b/alg/gdalrasterize.cpp index 458a8217bf02..46f6470413a9 100644 --- a/alg/gdalrasterize.cpp +++ b/alg/gdalrasterize.cpp @@ -1645,11 +1645,17 @@ CPLErr GDALRasterizeLayers(GDALDatasetH hDS, int nBandCount, int *panBandList, OGRSpatialReference *poSRS = poLayer->GetSpatialRef(); if (!poSRS) { - CPLError(CE_Warning, CPLE_AppDefined, - "Failed to fetch spatial reference on layer %s " - "to build transformer, assuming matching coordinate " - "systems.", - poLayer->GetLayerDefn()->GetName()); + if (poDS->GetSpatialRef() != nullptr || + poDS->GetGCPSpatialRef() != nullptr || + poDS->GetMetadata("RPC") != nullptr) + { + CPLError( + CE_Warning, CPLE_AppDefined, + "Failed to fetch spatial reference on layer %s " + "to build transformer, assuming matching coordinate " + "systems.", + poLayer->GetLayerDefn()->GetName()); + } } else { diff --git a/autotest/alg/rasterize.py b/autotest/alg/rasterize.py index 6a666c797370..f45e62cb715d 100755 --- a/autotest/alg/rasterize.py +++ b/autotest/alg/rasterize.py @@ -111,6 +111,10 @@ def test_rasterize_2(): burn_values=[200, 220, 240], options=["ALL_TOUCHED=TRUE"], ) + assert ( + "Failed to fetch spatial reference on layer cutline to build transformer, assuming matching coordinate systems" + in gdal.GetLastErrorMsg() + ) assert err == 0, "got non-zero result code from RasterizeLayer" @@ -130,20 +134,15 @@ def test_rasterize_2(): def test_rasterize_3(): - # Setup working spatial reference - sr_wkt = 'LOCAL_CS["arbitrary"]' - sr = osr.SpatialReference(sr_wkt) - # Create a memory raster to rasterize into. target_ds = gdal.GetDriverByName("MEM").Create("", 100, 100, 3, gdal.GDT_Byte) target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1)) - target_ds.SetProjection(sr_wkt) # Create a memory layer to rasterize from. rast_ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("wrk") - rast_mem_lyr = rast_ogr_ds.CreateLayer("poly", srs=sr) + rast_mem_lyr = rast_ogr_ds.CreateLayer("poly") # Add polygons and linestrings. wkt_geom = [ @@ -160,6 +159,7 @@ def test_rasterize_3(): # Run the algorithm. + gdal.ErrorReset() err = gdal.RasterizeLayer( target_ds, [3, 2, 1], @@ -167,6 +167,7 @@ def test_rasterize_3(): burn_values=[10, 10, 55], options=["BURN_VALUE_FROM=Z"], ) + assert gdal.GetLastErrorMsg() == "" assert err == 0, "got non-zero result code from RasterizeLayer"