Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#5367 closed defect (fixed)

Truncation of projwin envelope when exceeding source envelope

Reported by: Chau Owned by: warmerdam
Priority: normal Milestone: 1.11.0
Component: GDAL_Raster Version: svn-trunk
Severity: major Keywords:
Cc:

Description

I have an image with resolution 1000x1000 pixels. I use the following script to extract a new image with a slightly larger resolution (1008x1008) using gdal_translate:

gdalinfo 1000px-Smiley.svg.png
gdal_translate -of GTiff -a_ullr 100 200 200 100 -co compress=lzw 1000px-Smiley.svg.png result1.tif

gdalinfo result1.tif
gdal_translate -projwin 99.6 200.4 200.4 99.6 result1.tif result2.tif -co compress=lzw

gdalinfo result2.tif

The result is the following image:

Driver: GTiff/GeoTIFF
Files: result2.tif
Size is 1008, 1008
Coordinate System is `'
Origin = (99.700000000000003,200.300000000000010)
Pixel Size = (0.100000000000000,-0.100000000000000)
Image Structure Metadata:
  COMPRESSION=LZW
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (      99.700,     200.300)
Lower Left  (      99.700,      99.500)
Upper Right (     200.500,     200.300)
Lower Right (     200.500,      99.500)
Center      (     150.100,     149.900)
Band 1 Block=1008x2 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA
Band 2 Block=1008x2 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA
Band 3 Block=1008x2 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA
Band 4 Block=1008x2 Type=Byte, ColorInterp=Alpha

The envelope is translated by 0.1 unit compared to the -projwin envelope assigned to gdal_translate.

In the gdal_translate.cpp I found these lines:

anSrcWin[0] = (int)
           	 ((dfULX - adfGeoTransform[0]) / adfGeoTransform[1] + 0.001);
anSrcWin[1] = (int) 
           	 ((dfULY - adfGeoTransform[3]) / adfGeoTransform[5] + 0.001);

In my case the easting coordinates are handled like this:

anSrcWin[0] = (int)((99.6 – 100) / 0.1 + 0.001
            = (int)(-3.999)
            = -3

I would expect it to be -4 instead of the truncated value -3.

Change History (3)

comment:1 Changed 4 years ago by Chau

Version: 1.10.1svn-trunk

comment:2 Changed 4 years ago by Even Rouault

Milestone: 2.0
Resolution: fixed
Status: newclosed

trunk r26894 "gdal_translate: use floor() to compute image coordinates from world coordinates when specifying -projwin (usefull when extracting from left or top of upper-left corner, which generate negative image coordinates) (#5367)"

comment:3 Changed 3 years ago by Even Rouault

Milestone: 2.01.11.0
Note: See TracTickets for help on using tickets.