Skip to content

Commit c4a2e0b

Browse files
authored
gdal_translate: expand output to include pixels partially within -projwin (#12105)
* gdal_translate: expand output to include pixels partially within -projwin * Doc: Describe possible expansion of -projwin * OGCAPI tests: Update expected values after gdal_translate -projwin behavior change Fixes #12100
1 parent e863d2c commit c4a2e0b

File tree

46 files changed

+56290
-48477
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+56290
-48477
lines changed

apps/gdal_translate_lib.cpp

+31-11
Original file line numberDiff line numberDiff line change
@@ -980,24 +980,44 @@ GDALDatasetH GDALTranslate(const char *pszDest, GDALDatasetH hSrcDataset,
980980
}
981981
}
982982

983+
bool bAlignToInputPixels =
984+
psOptions->osResampling.empty() ||
985+
EQUALN(psOptions->osResampling.c_str(), "NEAR", 4);
986+
987+
double dfULX = psOptions->dfULX;
988+
double dfULY = psOptions->dfULY;
989+
983990
psOptions->adfSrcWin[0] =
984-
(psOptions->dfULX - adfGeoTransform[0]) / adfGeoTransform[1];
991+
(dfULX - adfGeoTransform[0]) / adfGeoTransform[1];
985992
psOptions->adfSrcWin[1] =
986-
(psOptions->dfULY - adfGeoTransform[3]) / adfGeoTransform[5];
993+
(dfULY - adfGeoTransform[3]) / adfGeoTransform[5];
994+
995+
// In case of nearest resampling, round to integer pixels (#6610)
996+
if (bAlignToInputPixels)
997+
{
998+
psOptions->adfSrcWin[0] =
999+
std::floor(psOptions->adfSrcWin[0] + 0.001); // xoff
1000+
psOptions->adfSrcWin[1] =
1001+
std::floor(psOptions->adfSrcWin[1] + 0.001); // yoff
1002+
1003+
dfULX = psOptions->adfSrcWin[0] * adfGeoTransform[1] +
1004+
adfGeoTransform[0];
1005+
dfULY = psOptions->adfSrcWin[1] * adfGeoTransform[5] +
1006+
adfGeoTransform[3];
1007+
}
9871008

1009+
// Calculate xsize and ysize based on the (possibly snapped) ULX, ULY
9881010
psOptions->adfSrcWin[2] =
989-
(psOptions->dfLRX - psOptions->dfULX) / adfGeoTransform[1];
1011+
(psOptions->dfLRX - dfULX) / adfGeoTransform[1]; // xsize
9901012
psOptions->adfSrcWin[3] =
991-
(psOptions->dfLRY - psOptions->dfULY) / adfGeoTransform[5];
1013+
(psOptions->dfLRY - dfULY) / adfGeoTransform[5]; // ysize
9921014

993-
// In case of nearest resampling, round to integer pixels (#6610)
994-
if (psOptions->osResampling.empty() ||
995-
EQUALN(psOptions->osResampling.c_str(), "NEAR", 4))
1015+
if (bAlignToInputPixels)
9961016
{
997-
psOptions->adfSrcWin[0] = floor(psOptions->adfSrcWin[0] + 0.001);
998-
psOptions->adfSrcWin[1] = floor(psOptions->adfSrcWin[1] + 0.001);
999-
psOptions->adfSrcWin[2] = floor(psOptions->adfSrcWin[2] + 0.5);
1000-
psOptions->adfSrcWin[3] = floor(psOptions->adfSrcWin[3] + 0.5);
1017+
psOptions->adfSrcWin[2] =
1018+
std::ceil(psOptions->adfSrcWin[2] - 0.001);
1019+
psOptions->adfSrcWin[3] =
1020+
std::ceil(psOptions->adfSrcWin[3] - 0.001);
10011021
}
10021022

10031023
/*if( !bQuiet )

autotest/gcore/vrt_read.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,7 @@ def test_vrt_protocol():
16291629
assert ds.GetGeoTransform()[0] == 440840.0
16301630
assert ds.GetGeoTransform()[3] == 3751140.0
16311631
assert ds.GetRasterBand(1).XSize == 18
1632-
assert ds.GetRasterBand(1).YSize == 16
1632+
assert ds.GetRasterBand(1).YSize == 17
16331633

16341634
with pytest.raises(Exception):
16351635
gdal.Open("vrt://data/float32.tif?tr=120")
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

autotest/gdrivers/data/ogcapi/request.http_data

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
HTTP/1.1 200 OK
2-
Date: Tue, 20 Jun 2023 10:38:14 GMT
2+
Date: Tue, 08 Apr 2025 23:33:09 GMT
33
Server: Apache/2.4.52 (Ubuntu)
4-
Expires: Tue, 04 Jun 2024 18:43:20 GMT
4+
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, acc$
5+
Expires: Wed, 08 Apr 2026 23:33:09 GMT
56
Access-Control-Allow-Origin: *
67
Vary: Accept,Accept-Encoding,Prefer
7-
Content-Length: 4397
8-
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, acc$
9-
Age: 1266893
8+
Content-Type: application/json
9+
Content-Length: 4603
1010
Keep-Alive: timeout=5, max=100
1111
Connection: Keep-Alive
12-
Content-Type: application/json
1312

1413
{
1514
"links" : [
@@ -102,6 +101,11 @@ Content-Type: application/json
102101
"type" : "text/html",
103102
"title" : "The list of supported tiling schemes (as HTML)",
104103
"href" : "http://127.0.0.1:8080/fakeogcapi/tileMatrixSets?f=html"
104+
},
105+
{
106+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/dggrs-list",
107+
"title" : "Discrete Global Grid Reference Systems",
108+
"href" : "http://127.0.0.1:8080/fakeogcapi/dggs"
105109
}
106110
]
107111
}

autotest/gdrivers/data/ogcapi/request_api_f_json.http_data

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
HTTP/1.1 200 OK
2-
Date: Fri, 12 Jul 2024 14:16:47 GMT
2+
Date: Tue, 08 Apr 2025 23:32:22 GMT
33
Server: Apache/2.4.52 (Ubuntu)
4-
Expires: Sat, 12 Jul 2025 13:56:54 GMT
4+
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, acc$
5+
Expires: Wed, 08 Apr 2026 23:32:22 GMT
56
Access-Control-Allow-Origin: *
67
Vary: Accept,Accept-Encoding,Prefer
8+
Content-Type: application/json
79
Content-Length: 204806
8-
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, acc$
9-
Age: 1192
1010
Keep-Alive: timeout=5, max=100
1111
Connection: Keep-Alive
12-
Content-Type: application/json
1312

1413
{
1514
"openapi" : "3.0.0",

autotest/gdrivers/data/ogcapi/request_collections_HRDEM-RedRiver_DTM_2m.http_data

+49-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
HTTP/1.1 200 OK
2-
Date: Thu, 20 Jul 2023 17:27:38 GMT
2+
Date: Tue, 08 Apr 2025 23:33:17 GMT
33
Server: Apache/2.4.52 (Ubuntu)
44
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, acc$
5-
Expires: Fri, 19 Jul 2024 17:27:38 GMT
5+
Expires: Wed, 08 Apr 2026 23:33:17 GMT
66
Access-Control-Allow-Origin: *
77
Vary: Accept,Accept-Encoding,Prefer
88
Content-Type: application/json
9-
Content-Length: 6766
9+
Content-Length: 8324
1010
Keep-Alive: timeout=5, max=100
1111
Connection: Keep-Alive
1212

@@ -37,10 +37,46 @@ Connection: Keep-Alive
3737
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m?f=html"
3838
},
3939
{
40-
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/dggs-list",
40+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/dggrs-list",
4141
"title" : "Discrete Global Grid Systems for 2m",
4242
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/dggs"
4343
},
44+
{
45+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/schema",
46+
"type" : "application/json",
47+
"title" : "Schema (as JSON)",
48+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/schema?f=json"
49+
},
50+
{
51+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/schema",
52+
"type" : "text/plain",
53+
"title" : "Schema (as ECON)",
54+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/schema?f=econ"
55+
},
56+
{
57+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/schema",
58+
"type" : "text/html",
59+
"title" : "Schema (as HTML)",
60+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/schema?f=html"
61+
},
62+
{
63+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/queryables",
64+
"type" : "application/json",
65+
"title" : "Queryables (as JSON)",
66+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/queryables?f=json"
67+
},
68+
{
69+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/queryables",
70+
"type" : "text/plain",
71+
"title" : "Queryables (as ECON)",
72+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/queryables?f=econ"
73+
},
74+
{
75+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/queryables",
76+
"type" : "text/html",
77+
"title" : "Queryables (as HTML)",
78+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/queryables?f=html"
79+
},
4480
{
4581
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/coverage",
4682
"type" : "image/png",
@@ -69,19 +105,19 @@ Connection: Keep-Alive
69105
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/tilesets-coverage",
70106
"type" : "application/json",
71107
"title" : "Coverage tilesets available for this dataset (as JSON)",
72-
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/coverage/tiles?f=json"
108+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/tiles?f=json"
73109
},
74110
{
75111
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/tilesets-coverage",
76112
"type" : "text/plain",
77113
"title" : "Coverage tilesets available for this dataset (as ECON)",
78-
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/coverage/tiles?f=econ"
114+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/tiles?f=econ"
79115
},
80116
{
81117
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/tilesets-coverage",
82118
"type" : "text/html",
83119
"title" : "Coverage tilesets available for this dataset (as HTML)",
84-
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/coverage/tiles?f=html"
120+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m/tiles?f=html"
85121
},
86122
{
87123
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/map",
@@ -141,15 +177,15 @@ Connection: Keep-Alive
141177
"title" : "2m",
142178
"extent" : {
143179
"spatial" : {
144-
"bbox" : [ [ -97.9354967393826, 48.9029217657901, -96.1602100081477, 50.3818852403202 ] ],
180+
"bbox" : [ [ -97.9354967393826, 48.90292176579, -96.1602100081477, 50.3818852403202 ] ],
145181
"grid" : [
146182
{
147183
"cellsCount" : 82735,
148-
"resolution" : 0.0000214576721
184+
"resolution" : 0.00002145767211914
149185
},
150186
{
151-
"cellsCount" : 68925,
152-
"resolution" : 0.0000214576721
187+
"cellsCount" : 68926,
188+
"resolution" : 0.00002145767211914
153189
}
154190
]
155191
}
@@ -164,6 +200,6 @@ Connection: Keep-Alive
164200
"id" : "HRDEM-RedRiver:DTM:2m",
165201
"dataType" : "coverage",
166202
"attribution" : "<a href='https://open.canada.ca/data/en/dataset/957782bf-847c-4644-a757-e383c0057995'>HRDEM (Natural Resources Canada)</a>",
167-
"minScaleDenominator" : 8530.9183353991357,
168-
"minCellSize" : 0.0000214576721
203+
"minScaleDenominator" : 8530.91833539914,
204+
"minCellSize" : 0.00002145767211914
169205
}

autotest/gdrivers/data/ogcapi/request_collections_HRDEM-RedRiver_DTM_2m_map_tiles_WorldMercatorWGS84Quad_f_json.http_data

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
HTTP/1.1 200 OK
2-
Date: Thu, 20 Jul 2023 17:27:39 GMT
2+
Date: Tue, 08 Apr 2025 23:33:18 GMT
33
Server: Apache/2.4.52 (Ubuntu)
44
Access-Control-Allow-Headers: x-requested-with, Content-Type, origin, authorization, acc$
5-
Expires: Fri, 19 Jul 2024 17:27:39 GMT
5+
Expires: Wed, 08 Apr 2026 23:33:18 GMT
66
Access-Control-Allow-Origin: *
77
Vary: Accept,Accept-Encoding,Prefer
88
Content-Type: application/json
9-
Content-Length: 5587
9+
Content-Length: 6303
1010
Keep-Alive: timeout=5, max=100
1111
Connection: Keep-Alive
1212

@@ -98,10 +98,33 @@ Connection: Keep-Alive
9898
"templated" : true
9999
}
100100
],
101+
"layers" : [
102+
{
103+
"id" : "_2m",
104+
"dataType" : "coverage",
105+
"minScaleDenominator" : 8530.91833539914,
106+
"minCellSize" : 2.38865713391176,
107+
"maxTileMatrix" : "16",
108+
"links" : [
109+
{
110+
"rel" : "http://www.opengis.net/def/rel/ogc/1.0/geodata",
111+
"href" : "http://127.0.0.1:8080/fakeogcapi/collections/HRDEM-RedRiver:DTM:2m"
112+
}
113+
],
114+
"propertiesSchema" : {
115+
"$schema" : "https://json-schema.org/draft/2020-12/schema",
116+
"type" : "object",
117+
"properties" : { "v" : {
118+
"type" : "number",
119+
"x-ogc-propertySeq" : 1
120+
} }
121+
}
122+
}
123+
],
101124
"centerPoint" : {
102-
"coordinates" : [ -97.0478533737652, 49.6424035030551 ],
125+
"coordinates" : [ -97.0478533737652, 49.642403503055 ],
103126
"tileMatrix" : "11",
104-
"scaleDenominator" : 272989.3867327723419,
127+
"scaleDenominator" : 272989.386732772,
105128
"cellSize" : 76.4370282851763,
106129
"crs" : "http://www.opengis.net/def/crs/OGC/1.3/CRS84"
107130
}

0 commit comments

Comments
 (0)