I want to do zonal analysis from QGIS standalone script.For this, I am using following code to run through command line

Following code is save in .bat file.

import sys
from qgis.core import *
from qgis.utils import *
from qgis.gui import *
from PyQt4.QtGui import *
from PyQt4.QtCore import *
 # Initialize QGIS Application
QgsApplication.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True)
app = QgsApplication([], True)
print 'Hello QGIS1!'
# Add the path to Processing framework

# Import and initialize Processing framework
from processing.core.Processing import Processing
import processing

#zonal Analysis

 from qgis.analysis import QgsZonalStatistics

 #specify polygon shapefile vector
 polygonLayer = QgsVectorLayer('F:/test/road_15715.shp','zonepolygons', "ogr") 

# specify raster filename
 rasterFilePath = 'F:/test/abcd.tif'

# usage - QgsZonalStatistics (QgsVectorLayer *polygonLayer, const QString  &rasterFile, const QString &attributePrefix="", int rasterBand=1)

  zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath,'vi_', 1)

  print 'Zonal Analysis Done!'

While using code

    zoneStat = QgsZonalStatistics (polygonLayer, rasterFilePath,'vi_', 1)

it is working with QGIS Python console but while using in a standalone script, zonal analysis is not done.

Your Answer

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

Browse other questions tagged or ask your own question.