Sunday 23 April 2017

Making map using PyQGIS?




Im looking at making a standalone script, that on execution will run the QGis MapComposer using a previously generated template and output a PDF/PNG of the map. This process of making a map will be the last step in a operational system that runs twice daily.
I considered using the --snapshot function, but because I'd also like to have a number of images (logos etc) on the layout, the snapshot becomes unworkable.
In essense, what I'd like to do is: From a standalone script, save a PDF or PNG that is based on a qpt-template-file. For each run of the script, I'll have a new .tif file, which I can write into a qgs-project-file, in order to load the right file. With this, what I expect that I'm supposed to do is:



(1) initialize Qgis
(2) open up the qgs-file (in order to specify file, stretch etc)
(3) open up the map composer with the qpt-file (in order to have all my static images, labels etc)
(4) save the map to a PDF / PNG (format is of little concern)
(5) close Qgis


What I've been having trouble with, is steps 2 and 3. Steps 1, 4 and 5 I've grabbed from other scripts and various examples. The pyqgis cookbook doesn't really cover the parts that are troubling me, and API Documentation is significantly out of my league.
Is anyone here willing and able to give me a hand with my problems, either by pointing me at the right functions, or by providing a script doing something along the lines of what I wish to do?



Answer



This bit of code might help you programmatically carry out steps 3 and 4. It will load a composer template from file and export a map to jpeg by creating a atlas. It will require some tweaking but should get you started.


def quick_export(self, ref, stype, scale):


# Add all layers in map canvas to render
myMapRenderer = self.iface.mapCanvas().mapRenderer()

# Load template from file
myComposition = QgsComposition(myMapRenderer)
if str(stype) == "Metalliferous":
template = 'MMR_Template.qpt'
if str(stype) == "Clay":
template = 'Clay_Template.qpt'

if str(stype) == "Combined":
template = 'Combined_Template.qpt'
if str(stype) == "Metalliferous A3":
template = 'MMR_A3_Template.qpt'
if str(stype) == "Limestone":
template = 'Limestone_Template.qpt'
myFile = os.path.join(os.path.dirname(__file__), template)
myTemplateFile = file(myFile, 'rt')
myTemplateContent = myTemplateFile.read()
myTemplateFile.close()

myDocument = QDomDocument()
myDocument.setContent(myTemplateContent)
myComposition.loadFromTemplate(myDocument)

# Get map composition and define scale
myAtlasMap = myComposition.getComposerMapById(0)
myAtlasMap.setNewScale(int(scale))

# Setup Atlas
myAtlas = QgsAtlasComposition(myComposition)

myAtlas.setCoverageLayer(atlas_desktop) # Atlas run from desktop_search
myAtlas.setComposerMap(myAtlasMap)
myAtlas.setFixedScale(True)
myAtlas.fixedScale()
myAtlas.setHideCoverage(False)
myAtlas.setFilterFeatures(True)
myAtlas.setFeatureFilter("reference = '%s'" % (str(ref)))
myAtlas.setFilterFeatures(True)

# Generate atlas

myAtlas.beginRender()
for i in range(0, myAtlas.numFeatures()):
myAtlas.prepareForFeature( i )
jobs = r"\\location\for\Jobs"
job_fol = os.path.join(jobs, str(ref))
if not os.path.exists(job_fol):
os.makedirs(job_fol)
output_jpeg = os.path.join(job_fol, ref + "_plan.jpg")
myImage = myComposition.printPageAsRaster(0)
myImage.save(output_jpeg)

myAtlas.endRender()
if os.path.isfile(output_jpeg) is False:
QMessageBox.warning(self.iface.mainWindow(), "Uh Oh!", "Something went wrong, no image exported!")
return
else:
return

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...