Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (58 sloc) 2.43 KB
#----------------------------------
#Oto Kalab
#kalab.oto@gmail.com
#GISMentors/OpenGeoLabs
#http://gismentors.cz/mentors/kalab/
#http://prg.osu.cz/cs/tym/oto-kalab/
#----------------------------------
##Points to raster statistics=name
##Raster=group
##Resolution=number
##Extent=extent
##Point_layer=vector
##Attribute_values=field Point_layer
##Statistic=selection sum;mean;min;max;median
##Raster_output=output raster
##Raster_output_count=output raster
from qgis.core import QgsVectorLayer
if Statistic == 0:
stat = "sum"
elif Statistic == 1:
stat = "mean"
elif Statistic == 2:
stat = "min"
elif Statistic == 3:
stat = "max"
elif Statistic == 4:
stat = "median"
#get epsg from point layer
PointLayer = QgsVectorLayer(Point_layer, "layer_name", "ogr")
epsg = PointLayer.crs().authid()
#generate vector grid with point stats
grid=processing.runalg("qgis:creategrid",1,Extent,Resolution,Resolution,epsg,None)
joined=processing.runalg("qgis:joinattributesbylocation", grid['OUTPUT'],PointLayer,['intersects'],0.0,1,stat,0,None)
#get extent from vector grid
joinedLayer = QgsVectorLayer(joined['OUTPUT'], "layer_name", "ogr")
extent2 = joinedLayer.extent()
xmin = extent2.xMinimum()
xmax = extent2.xMaximum()
ymin = extent2.yMinimum()
ymax = extent2.yMaximum()
#get created attribute (joined i.e."sumid")
attr = Attribute_values#chosen attribute
attr = str(stat)+str(attr)#"sum"+"id"
#generate stats raster
try:
processing.runalg("gdalogr:rasterize", joined['OUTPUT'],attr,1,Resolution,Resolution,"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),False,5,"",4,75.0,6.0,1.0,False,0,"",Raster_output)
except:
pass
progress.setInfo("gdalogr:rasterize for point statistics fialed, running GRASS v.to.rast")
processing.runalg("grass7:v.to.rast.attribute",joined['OUTPUT'],0,attr,"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),0,-1,0.0001,Raster_output)
#generate countpoints raster
countpoints = processing.runalg("qgis:countpointsinpolygon",joined['OUTPUT'],Point_layer,"NUMPOINTS",None)
try:
processing.runalg("gdalogr:rasterize", countpoints['OUTPUT'],"NUMPOINTS",1,Resolution,Resolution,"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),False,5,"",4,75.0,6.0,1.0,False,0,"",Raster_output_count)
except:
pass
progress.setInfo("gdalogr:rasterize for counted points fialed, running GRASS v.to.rast")
processing.runalg("grass7:v.to.rast.attribute",countpoints['OUTPUT'],0,"NUMPOINTS","%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),0,-1,0.0001,Raster_output_count)