Wednesday 26 August 2015

gdal - Converting TIF to EPSG:3857?


I'm trying to convert this TIF to EPSG:3857, but when I run:


gdalwarp -of GTiff -s_srs epsg:4326 -t_srs epsg:3857 -co "tfw=yes" original.tif converted.tif


I just get a black TIF do you know what I am doing wrong?


Command output


> gdalwarp -of GTiff -s_srs epsg:4326 -t_srs epsg:3857 -co "tfw=yes" original.tif converted.tif
Creating output file that is 4088P x 4105L.
Processing input file original.tif.
Using band 2 of source image as alpha.
0...10...20...30...40...50...60...70...80...90...100 - done.

Answer



I think it's the alpha band that's causing problems. To extract the data band:



gdal_translate -of hfa -b 1 original.tif band1.img

Then to warp:


gdalwarp -of GTiff -s_srs epsg:4326 -t_srs epsg:3857 -co "tfw=yes" band1.img band1_warp.tif

Gives an image:


enter image description here


With GDALINFO


> Driver: GTiff/GeoTIFF Files: D:\Testing\Tiff\band1_warp.tif
> D:\Testing\Tiff\band1_warp.tif.ovr

> D:\Testing\Tiff\band1_warp.tfw
> D:\Testing\Tiff\band1_warp.tif.aux.xml Size is 4088, 4105 Coordinate System is: PROJCS["WGS 84 / Pseudo-Mercator",
> GEOGCS["WGS 84",
> DATUM["WGS_1984",
> SPHEROID["WGS 84",6378137,298.257223563,
> AUTHORITY["EPSG","7030"]],
> AUTHORITY["EPSG","6326"]],
> PRIMEM["Greenwich",0],
> UNIT["degree",0.0174532925199433],
> AUTHORITY["EPSG","4326"]],

> PROJECTION["Mercator_1SP"],
> PARAMETER["central_meridian",0],
> PARAMETER["scale_factor",1],
> PARAMETER["false_easting",0],
> PARAMETER["false_northing",0],
> UNIT["metre",1,
> AUTHORITY["EPSG","9001"]],
> EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],
> AUTHORITY["EPSG","3857"]] Origin = (-13520950.132675199000000,4662178.034760829100000) Pixel Size =
> (0.081230547006398,-0.081230547006398) Metadata: AREA_OR_POINT=Area

> DataType=Generic Image Structure Metadata: INTERLEAVE=BAND Corner
> Coordinates: Upper Left (-13520950.133, 4662178.035) (121d27'38.74"W,
> 38d35' 0.40"N) Lower Left (-13520950.133, 4661844.583)
> (121d27'38.74"W, 38d34'51.97"N) Upper Right (-13520618.062,
> 4662178.035) (121d27'28.00"W, 38d35' 0.40"N) Lower Right (-13520618.062, 4661844.583) (121d27'28.00"W, 38d34'51.97"N) Center
> (-13520784.097, 4662011.309) (121d27'33.37"W, 38d34'56.19"N) Band 1
> Block=4088x2 Type=Byte, ColorInterp=Gray Description = Layer_1
> Overviews: 2044x2053, 1022x1027, 511x514, 256x257, 128x129 Metadata:
> DESCRIPTION=Layer_1
> LAYER_TYPE=athematic


Wierdly when warping with an alpha band the entire band becomes 0 (transparent) but when extracting and warping the 2nd band:


gdal_translate -of hfa -b 2 original.tif band2.img
gdalwarp -of GTiff -s_srs epsg:4326 -t_srs epsg:3857 -co "tfw=yes" band2.img band2_warp.tif

it looks fine... then stack the bands back together:


gdal_merge.py -separate -o stacked.tif band1_warp.tif band2_warp.tif

and the image is warped with a correct alpha band.


Further testing indicates that it could be the input tiff that is causing the alpha band to become bad:



gdal_translate -of hfa original.tif original_as.img
gdalwarp -of GTiff -s_srs epsg:4326 -t_srs epsg:3857 -co "tfw=yes" original_as.img warp_from_img.tif

Produces the correct image alpha band: Band 1 Band 1


Band 2 (alpha) Band 2


As warping from the img no longer sees the second band as an alpha band.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...