Load raster into QGis with a python script
This week I have started to study PyQGis[2], then I have tried to write a python script to load several raster into the QGis layer .
My filenames contain date as a part of name, so, instead to load all files in the folder, I have built the file name as a join of two string, the path of the folder and date (extracted from a fixed range -> 01-01-2006 to 01-31-2006). Then I have extracted the maximum and minimum value from this raster, and the value in some fixed point of a shape file.
import datetime
import time
startdate = datetime.date(2006,1,1)
enddate = datetime.date(2006,1,31)
minValue=0
maxValue=0
#load my point
myShapeFile = QgsVectorLayer("/myPath/stazioni.shp", \
"stazioni", "ogr")
myPoint = myShapeFile.getFeatures()
#iterate to load all raster
for i in range((enddate-startdate).days + 1):
currentdate = startdate + datetime.timedelta(days=i)
tt=time.strftime("%m-%d", currentdate.timetuple())
fileName=""
fileName=fileName.join(["/myFolderPath/temperature",tt,".asc"])
qgis.utils.iface.addRasterLayer(fileName)
layer1 = qgis.utils.iface.mapCanvas().layer(0)
#iterate over all feature to extract the value
for feature in myPoint:
ident = layer1.dataProvider().identify(feature.geometry().asPoint(),QgsRaster.IdentifyFormatValue)
if ident.isValid():
print ident.results()
maxValue1=layer1.maximumValue(1)
if maxValue < maxValue1 :
maxValue=maxValue1
minValue1=layer1.minimumValue(1)
if minValue > minValue1 :
minValue=minValue1
print(layer1.name())
print("Max")
print(maxValue)
print("Min")
print(minValue)
allLayers = canvas.layers()
for i in allLayers:
References:
- QGis, http://www.qgis.org
- PyQGis Developer CookBook, http://www.qgis.org/en/docs/pyqgis_developer_cookbook/index.html
Comments
Post a Comment