I'm trying to reproject a GeoTIFF file within a PyQGIS standalone script.
Using QGIS 3.14 the steps would be:
resulting in the following process history and the output_raster.tiff being successfully created:
processing.run("gdal:warpreproject", {'INPUT':'C:/Projects/PythonScripts/qgis_python/input/input_raster.tif','SOURCE_CRS':QgsCoordinateReferenceSystem('EPSG:2051'),'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:4326'),'RESAMPLING':0,'NODATA':None,'TARGET_RESOLUTION':None,'OPTIONS':'','DATA_TYPE':0,'TARGET_EXTENT':None,'TARGET_EXTENT_CRS':None,'MULTITHREADING':False,'EXTRA':'','OUTPUT':'C:/Projects/PythonScripts/qgis_python/output/output_raster.tif'})
However
Running the exact same command in a standalone python script does not create the output file even though it runs without any errors.
My guess is that it has something to do with a conflicting GDAL because I receive the following error reprojecting a raster directly with GDAL:
from osgeo import gdal
input_raster = gdal.Open(r'C:/Projects/PythonScripts/qgis_python/input/input_raster.tif')
gdal.Warp(r'C:\Projects\PythonScripts\qgis_python\output\output_raster.tiff', input_raster, format = 'GTiff', dstSRS="EPSG:4326")
Error:
TypeError: in method 'wrapper_GDALWarpDestName', argument 4 of type 'GDALWarpAppOptions *'
and if I import GDAL befor qgis.core
, then It can't find the DLL for qgis._core import *
Full Script
import os
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:\\Program Files\\QGIS 3.14\\apps\\Qt5\\plugins'
os.environ['GDAL_DATA'] = '/home/server/anaconda3/share/gdal'
from qgis.core import (
QgsApplication,
QgsProcessingFeedback,
QgsVectorLayer,
QgsCoordinateReferenceSystem
)
# See https://gis.stackexchange.com/a/155852/4972 for details about the prefix
QgsApplication.setPrefixPath('C:\\Program Files\\QGIS 3.14\\apps\\qgis\\', True)
from PyQt5 import QtGui, QtCore
qgs = QgsApplication([], False)
qgs.initQgis()
import sys
sys.path.append('C:\\Program Files\\QGIS 3.14\\apps\\qgis\\python\\plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
from qgis.analysis import QgsNativeAlgorithms
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
reproject_params = {'INPUT':'input/input_raster.tif',
'SOURCE_CRS':QgsCoordinateReferenceSystem('EPSG:2051'),
'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:4326'),
'RESAMPLING':0,
'NODATA':None,
'TARGET_RESOLUTION':None,
'OPTIONS':'',
'DATA_TYPE':0,
'TARGET_EXTENT':None,
'TARGET_EXTENT_CRS':QgsCoordinateReferenceSystem('EPSG:4326'),
'MULTITHREADING':False,
'EXTRA':'',
'OUTPUT':'output/output_raster.tif'}
processing.run("gdal:warpreproject", reproject_params)