2

I'm trying to reproject a GeoTIFF file within a PyQGIS standalone script.

Using QGIS 3.14 the steps would be:

enter image description here

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)

Taras
  • 32,823
  • 4
  • 66
  • 137
user19349
  • 327
  • 2
  • 10

1 Answers1

1

If it works in QGIS interface then I recommend you copy the command from here: warp/qgis

You can then use it in a standalone Python script like that:

import os
cmd = 'gdalwarp -s_srs EPSG:2180 -t_srs EPSG:2180 -r near -of GTiff {srcPath} {outPath}'\
    .format(srcPath='/home/lpowalka/Documents/dane/finalResults/fax3.tif', outPath='/home/lpowalka/Documents/dane/finalResults/fax3_warped.tif')
os.system(cmd)

This way you avoid the complexity of deploying a PyQGIS script or using GDAL Python bindings which both essentially lead to executing this command anyway.

Leon Powałka
  • 1,653
  • 5
  • 18
  • Thank you Leon. In my case your answer also only works until I "Import processing". After importing processing it raises the error "ERROR 6: Cannot find coordinate operations from EPSG:2051' toEPSG:4326'", also I would like to chain events which looks simpler to do using PyQGIS and memory layers, therefore need to import processing. – user19349 Oct 03 '20 at 07:22