Skip to content

Instantly share code, notes, and snippets.

@RCura
Created November 13, 2012 17:53
Show Gist options
  • Save RCura/1bd9f30b0c5943664bff to your computer and use it in GitHub Desktop.
Save RCura/1bd9f30b0c5943664bff to your computer and use it in GitHub Desktop.
Stats zonales
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 13 16:30:23 2012
@author: robin
"""
from qgis.core import *
from qgis.analysis import *
import time
# On définit où se trouve qgis, et on lance une session
QgsApplication.setPrefixPath('/usr/', True)
QgsApplication.initQgis()
# On charge la couche vecteur (UMZ)
myVectorLayer = QgsVectorLayer('/media/robin/DATA/Donnees_M2/UMZ_2000.shp', 'UMZ_2000', 'ogr')
print('Nom de la couche : ' + myVectorLayer.name())
print(str(myVectorLayer.featureCount()) + ' features')
print('Type de geom : ' + str(myVectorLayer.geometryType()))
print('SRS de la couche : ' + str(myVectorLayer.crs().epsg()))
var = raw_input("Continuer ? (O/N) : \n")
if var != "O":
print('Echec')
else:
startTime = time.time()
# On définit le chemin du Raster
myRasterLayer = '/media/robin/DATA/Donnees_M2/popu01clc00v5/popgrid.tif'
# On choisit le préfixe utilisé par les colonnes de stats zonales
myPrefix = 'ZS_'
# On crée un objet de type ZonalStats
myZS = QgsZonalStatistics(myVectorLayer, myRasterLayer, myPrefix)
# On lance le calcul
myZS.calculateStatistics(None)
endTime = time.time()
print("Fini en %s secondes") % (endTime - startTime)
# On ferme qgis
QgsApplication.exitQgis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment