18

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?

asked Mar 26, 2017 at 20:10
4
  • Instead of reinventing the wheel how about subprocess.popen('GDAL_Warp ... docs.python.org/2/library/subprocess.html gdal.org/gdalwarp.html after you've obtained the input coordinate system gdal.org/… assuming it's defined, if the input raster spatial reference isn't defined you'll have to do some tricky assumptions based on your knowledge of your data. Note with GDAL_Warp if the existing spatial reference is defined one need only supply the -t_srs EPSG:4326 Commented Mar 26, 2017 at 21:45
  • 4
    Or just use gdal.Warp(output, input, dstSRS='EPSG:4326') if you have gdal >= 2.0 Commented Mar 26, 2017 at 21:52
  • 2
    @MichaelMiles-Stimson the command is gdalwarp, not GDAL_Warp. Commented Mar 26, 2017 at 21:53
  • Oh it's sooo monday morning @Luke! You are correct. gdal.warp( sounds like an answer to me... I use subprocess.popen because I want to do more than one concurrently usually. Commented Mar 26, 2017 at 21:55

2 Answers 2

40

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
answered Mar 27, 2017 at 6:13
2
  • 2
    The other answers are complicated because gdal.Warp is new at version 2.0. Commented Mar 27, 2017 at 21:27
  • Definetly a much cleaner solution compared to alternatives such as rasterio. Thanks for sharing Commented Jan 14, 2022 at 9:32
1

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>",
)

Source.

answered Sep 4 at 14:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.