mirror of
https://github.com/qgis/QGIS.git
synced 2025-02-27 00:33:48 -05:00
[processing]added new example script (contributed by Michael Douchin)
This commit is contained in:
parent
7b0af0df3c
commit
7f0bc42ea5
@ -0,0 +1,103 @@
|
||||
##[Example scripts]=group
|
||||
##Create rasters from canvas for each vector feature extent=name
|
||||
##vector_layer=vector
|
||||
##scale=number 250000.0
|
||||
##dpi=number 96
|
||||
##image_width_mm=number 0
|
||||
##image_height_mm=number 0
|
||||
##output_directory=folder
|
||||
##outputDir=output string
|
||||
|
||||
from qgis.core import *
|
||||
from qgis.gui import *
|
||||
from qgis.utils import iface
|
||||
from PyQt4.QtCore import *
|
||||
from PyQt4.QtGui import *
|
||||
import os
|
||||
|
||||
# Loop though grid layerfeatures
|
||||
layer = processing.getObject(vector_layer)
|
||||
provider = layer.dataProvider()
|
||||
|
||||
# Get map renderer
|
||||
mapRenderer = iface.mapCanvas().mapRenderer()
|
||||
|
||||
# Create a new composition
|
||||
c = QgsComposition(mapRenderer)
|
||||
c.setPrintResolution(dpi)
|
||||
|
||||
# Add a composer map object
|
||||
x, y = 0, 0
|
||||
composerMap = QgsComposerMap(c, x, y, image_width_mm, image_height_mm)
|
||||
c.addComposerMap(composerMap)
|
||||
composerMap.setBackgroundEnabled(False)
|
||||
|
||||
# World file
|
||||
c.setWorldFileMap(composerMap)
|
||||
c.setGenerateWorldFile( True )
|
||||
|
||||
# Get vector layer features
|
||||
feats = processing.features(layer)
|
||||
nFeat = len(feats)
|
||||
i = 0
|
||||
progress.setText(u'%s tiles to be generated' % nFeat)
|
||||
|
||||
# Iterate over the features
|
||||
for feat in feats:
|
||||
# Log progression
|
||||
progress.setPercentage(int(100 * i / nFeat))
|
||||
i+=1
|
||||
|
||||
# Get the feature bouding box
|
||||
geom = feat.geometry()
|
||||
rect = geom.boundingBox()
|
||||
|
||||
# Recalculate paper width and heigh if not given
|
||||
if not image_width_mm and scale > 0:
|
||||
image_width_mm = rect.width() * 1000 / scale
|
||||
if not image_height_mm and scale > 0:
|
||||
image_height_mm = rect.height() * 1000 / scale
|
||||
|
||||
# Calculate image size in Pixel
|
||||
inch2mm = 25.4
|
||||
image_width_pixel = int(image_width_mm * dpi / inch2mm)
|
||||
image_height_pixel = int(image_height_mm * dpi / inch2mm)
|
||||
progress.setText(u'Feature %s - Image width : %s * %s mm / %s * %s pixels' % (
|
||||
i,
|
||||
image_width_mm,
|
||||
image_height_mm,
|
||||
image_width_pixel,
|
||||
image_height_pixel
|
||||
)
|
||||
)
|
||||
|
||||
# Set paper and composerMap width and height
|
||||
c.setPaperSize(image_width_mm, image_height_mm)
|
||||
composerMap.setItemPosition(x, y, image_width_mm, image_height_mm)
|
||||
|
||||
# Set the map extent and scale
|
||||
composerMap.setNewExtent(rect)
|
||||
if scale > 0:
|
||||
composerMap.setNewScale(scale)
|
||||
|
||||
# Image destination path
|
||||
outputPath= "tile_%s_%s" % (scale, i)
|
||||
outputImagePath = os.path.join(output_directory, outputPath + '.png')
|
||||
|
||||
# Generate image from composition
|
||||
myImage = c.printPageAsRaster(0)
|
||||
myImage.save(outputImagePath)
|
||||
|
||||
# Generate World file
|
||||
wf = c.computeWorldFileParameters()
|
||||
outputWorldPath = os.path.join(output_directory, outputPath + '.pgw')
|
||||
with open(outputWorldPath, 'w') as f:
|
||||
f.write('%s\n' % wf[0])
|
||||
f.write('%s\n' % wf[1])
|
||||
f.write('%s\n' % wf[3])
|
||||
f.write('%s\n' % wf[4])
|
||||
f.write('%s\n' % wf[2])
|
||||
f.write('%s\n' % wf[5])
|
||||
|
||||
# export chosen output directory as a output variable
|
||||
outputDir = output_directory
|
@ -0,0 +1,42 @@
|
||||
(dp0
|
||||
S'outputDir'
|
||||
p1
|
||||
VThe directory where the rasters will be saved. This output can be used as a parameter for another script or algorithm (for example upcoming gdalbuildvrt )
|
||||
p2
|
||||
sS'ALG_DESC'
|
||||
p3
|
||||
VBuilding a good looking map often implies to create a QGIS project with many layers : roads, styled dem, contour lines, rivers, etc.\u000aFor large areas, QGIS can suffer to render each layers every time the user pan or zoom in or out. \u000a\u000aThis algorithm allows to create a georeferenced raster which mimics the image rendered by QGIS for all displayed layers. This way you can use the exported raster instead of all the layers as a base layer for your projects.\u000a\u000aThe user chooses the input vector layer, a scale and optionnaly the size of the raster in mm. The script iterate over the vector features. \u000a\u000aFor each feature:\u000a\u000a* the extent of the feature is calculated\u000a* the size of the output raster is calculated in mm "paper size", unless width and height are given\u000a* the canvas is rendered for the feature extent, at the given scale, and the result is saved as a png\u000a* a world file containing the georeferencement parameters is also saved ( *.pgw )
|
||||
p4
|
||||
sS'image_height_mm'
|
||||
p5
|
||||
V(optionnal) The height in mm of the output raster. If left to 0, the script will calculate it automatically to render the whole feature extent.
|
||||
p6
|
||||
sS'ALG_CREATOR'
|
||||
p7
|
||||
VMichaël DOUCHIN ( 3liz.com )
|
||||
p8
|
||||
sS'vector_layer'
|
||||
p9
|
||||
VThe vector layer containing the features to use to calculate each raster extent. You can use an existing layer, or a layer created with other algorithm (for example a regular grid of 250km by 250km)
|
||||
p10
|
||||
sS'output_directory'
|
||||
p11
|
||||
VThe directory where the rasters will be saved.
|
||||
p12
|
||||
sS'scale'
|
||||
p13
|
||||
VThe scale at which the canvas will be rendered
|
||||
p14
|
||||
sS'ALG_HELP_CREATOR'
|
||||
p15
|
||||
VMichaël DOUCHIN ( 3liz.com )
|
||||
p16
|
||||
sS'image_width_mm'
|
||||
p17
|
||||
V(optionnal) The width in mm of the output raster. If left to 0, the script will calculate it automatically to render the whole feature extent.
|
||||
p18
|
||||
sS'dpi'
|
||||
p19
|
||||
VThe resolution of the output rasters. "dpi" means "dot per inch"
|
||||
p20
|
||||
s.
|
Loading…
x
Reference in New Issue
Block a user