2

I want to project a raster from Sinusoidal to UTM. I used the following command in Python:

gdalwarp -overwrite -t_srs EPSG:32643 -of GTiff D:/IMAGE.tif D:/IMAGE2.tif

I'm getting the following error:

SyntaxError: invalid syntax

Anyone who can help?

  • The syntax error is in your Python code, not the gdalwarp invocation. You need to share more code around that. This is probably rather a generic programming question for stackoverflow – bugmenot123 Jan 8 '17 at 9:40
  • There is no code around. I was simply using this command to reproject the raster. – Ashutosh Jan 8 '17 at 9:56
  • If you use the command in Python, then there is Python code around it. The error is in that code. – bugmenot123 Jan 9 '17 at 10:49
3

In Python you should use os module and an adequate string combination, for gdalwarp parameters, to get a good performance. For example, I use next adapted code in my QGIS plugin and it works perfectly. In this case, it is employed for transforming UTM 12 N projection (EPSG:32612) in my raster to UTM Utah North projection (EPSG: 32142).

import os

layer = iface.activeLayer()

provider = layer.dataProvider()

source_path = " -of GTiff " + provider.dataSourceUri()
target_path = "/home/zeito/pyqgis_data/utah_dem_UtmNorth2.tif"

CRS = layer.crs()
source_epsg = CRS.postgisSrid()
new_espg = 32142
target_epsg = " -t_srs EPSG:" + str(new_espg)

cmd = "gdalwarp -overwrite -s_srs EPSG:" + \
                        str(source_epsg) + \
                        target_epsg + \
                        source_path + \
                        " " + \
                        target_path

print cmd

os.system(cmd)

To test the adequate command execution, comment last line:

os.system(cmd)

and observe and correct gdalwarp syntax until you can get expected result.

Editing Note:

In your case, I think that next code could work:

import os

source_path = " -of GTiff D:/IMAGE.tif " 
target_path = "D:/IMAGE2.tif "

source_epsg = 6974  #Sinusoidal Projection
new_espg = 32643
target_epsg = " -t_srs EPSG:" + str(new_espg)

cmd = "gdalwarp -overwrite -s_srs EPSG:" + \
                        str(source_epsg) + \
                        target_epsg + \
                        source_path + \
                        " " + \
                        target_path

print cmd

os.system(cmd)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.