daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Nov 23rd, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os, time
  2. from PyQt4.QtCore import *
  3. from PyQt4.QtXml import *
  4. from PyQt4.QtGui import *
  5. from qgis import *
  6.  
  7. image_path = PATH_TO_IMAGE
  8. export_path = EXPORT_PATH
  9. template = PATH_TO_TEMPLATE
  10.  
  11.  
  12. def exportMap(tempFile, canvas, bbox, outName):
  13.     template_file = file(tempFile)
  14.     template_content = template_file.read()
  15.     template_file.close()
  16.     document = QDomDocument()
  17.     document.setContent(template_content)
  18.     composer = QgsComposition(canvas.mapSettings())
  19.     composer.loadFromTemplate(document)
  20.     map_item = composer.getComposerItemById('main_map')
  21.     map_item.setMapCanvas(canvas)
  22.     bbox.scale(1.2)
  23.     map_item.zoomToExtent(bbox)
  24.     canvas.refresh()
  25.     map_item.updateCachedImage()
  26.     legend_item = composer.getComposerItemById('legend')
  27.     legend_item.updateLegend()
  28.     composer.refreshItems()
  29.     composer.update()
  30.     composer.exportAsPDF(outName)
  31.  
  32. # get map canvas
  33. canvas = iface.mapCanvas()
  34. li = iface.legendInterface()
  35.  
  36. # list all layers
  37. layers = li.layers()
  38. # get specific layer by displayed name
  39. lakes = [l for l in layers if l.name() == 'lakes'][0]
  40. iface.legendInterface().setLayerVisible(lakes, True)
  41. # load raster
  42. image = image_path
  43. raster = QgsRasterLayer(image, QFileInfo(image).baseName())
  44. iface.addRasterLayer(image)
  45. canvas.refresh()
  46. # make sure vector layer is on top
  47. root = QgsProject.instance().layerTreeRoot()
  48. for ch in root.children():
  49.     if ch.layerName() == lakes.name():
  50.         clone = ch.clone()
  51.         root.insertChildNode(0, clone)
  52.         root.removeChildNode(ch)
  53.  
  54. for feat in lakes.getFeatures():
  55.     outname = abbrev[feat.attribute('name')] + '_' + QFileInfo(image).baseName() + '.pdf'
  56.     outfile = os.path.join(export_path, outname)
  57.     # select single feature based on attribute "name"
  58.     expr = QgsExpression(""" "name" = '{0}' """.format(feat.attribute('name')))
  59.     selection = [l.id() for l in lakes.getFeatures(QgsFeatureRequest(expr))]
  60.     lakes.setSelectedFeatures(selection)
  61.     # zoom to selected feature
  62.     canvas.zoomToSelected(lakes)
  63.     # bounding box of selected feature
  64.     bbox = lakes.boundingBoxOfSelected()
  65.     # deselect
  66.     lakes.setSelectedFeatures([])
  67.     # apply local stretch to active raster layer
  68.     iface.setActiveLayer(raster)
  69.     iface.mainWindow().findChild(QAction, 'mActionLocalCumulativeCutStretch').trigger()
  70.     exportMap(template, canvas, bbox, outfile)
  71.  
  72. # remove raster
  73. for ch in root.children():
  74.     if ch.layerName() == raster.name():
  75.         root.removeChildNode(ch)
RAW Paste Data
Top