I'm trying to perform a rather simple task.. but can't find a simple solution.. I'm trying to re-project a raster layer using GDAL in python. I've read this solution , and this , and this, and others. It seems that all of them assume some parameters like pixel size or xy origins but I need to get all of the parameters from the raster itself, it suppose to be as generic as possible, the only constant thing is the output coordinate system which is wgs84 (EPSG=4326).
I've also saw different tools to perform this task like gdalwrap or getransform, so I'm a bit confused.
To sum it all up, I need to re-project the input_raster
from whatever coordinate system it is on to output_raster
that will be in wgs84 coordinate system.
input_raster = r"C:\Users\B3.tif"
output_raster = r"C:\Users\B3_proj.tif"
Could you help me in writing a python code using gdal to perform this task?
2 Answers 2
So, thanks to @Luke I used the simple line: gdal.Warp(output_raster,input_raster,dstSRS='EPSG:4326')
and it works, this was exactly what I was looking for, simple code with few line to execute simple task.. I don't understand why all of the other answers I found online are much more complicated..
The code I wrote is:
from osgeo import gdal
filename = r"C:\path\to\input\raster
input_raster = gdal.Open(filename)
output_raster = r"C:\path\to\output\raster
warp = gdal.Warp(output_raster,input_raster,dstSRS='EPSG:4326')
warp = None # Closes the files
-
2The other answers are complicated because
gdal.Warp
is new at version 2.0.user2856– user28562017年03月27日 21:27:49 +00:00Commented Mar 27, 2017 at 21:27 -
Definetly a much cleaner solution compared to alternatives such as
rasterio
. Thanks for sharingGCGM– GCGM2022年01月14日 09:32:14 +00:00Commented Jan 14, 2022 at 9:32
You can do:
from osgeo import gdal
gdal.UseExceptions()
gdal.Run(
"raster",
"reproject",
input="src.tiff",
output="dst.jpeg",
dst_crs="EPSG:<dst-epsg-code>",
)
gdal.Warp(output, input, dstSRS='EPSG:4326')
if you have gdal >= 2.0gdalwarp
, not GDAL_Warp.