[GIS] Generate HTML/XML file in QGIS

htmlqgisxml

I'm looking for best practices (plugins, procedures…) in QGIS to generate an HTML document (which later on can be finalized in MS Word) in which the result of spatial queries can be documented.

An example: I have a layer with parcels of a municipality. I would like to generate a document, according to a pre-defined HTML layout, where the results of overlays of this parcel with other geographic layers (e.g. legal boundaries) are included. E.g. in my document, there is a question "is the parcel located within an agricultural area?" and behind the question there are two checkboxes (yes and no) which should be checked depending on the result of a spatial overlay between the selected parcel and the layer with agricultural areas.

In addition to the above-mentioned spatial query, it would be also useful to include in the HTML-document the information from some attribute fields from the layer with agricultural areas (e.g. name of the agricultural area) in case the parcel overlaps with an agricultural area?

Are there any easy-to-use plugins or tools for this in QGIS?

Best Answer

I recently did something similar, but I was embedding the html in a pdf. I made a Python script that the user can run from the processing toolbox. Check out this tutorial for a guide on how to set one up.

The basic idea is to perform some analysis and use the print composer to automatically generate the output.

Here is a Python snippet from my own code to get you started on the html side. Since my output was a pdf I've omitted the rendering part. This should give you an idea of how to save the composer as an html document though.

#Replace capitalised words with your data 

layer = QgsVectorLayer("C:/YOUR SHAPEFILE.shp", "A NAME YOU LIKE", "ogr")

#set up composer
c = QgsComposition(mr) 
c.setPlotStyle(QgsComposition.Print) 
c.setPaperSize(210, 297) 
dpi = c.printResolution() 
c.setPrintResolution(dpi)

#Add the information 
infoBox = QgsComposerLabel(c)
infoBox.setFont(QFont("Arial",12))
infoBox.setItemPosition(25, 15)
infoBox.setHtmlState(2)
message = '''<H1>TITLE</H1>
<TABLE>
<TR><TD VALIGN="top">YOUR ATTRIBUTE: <TD VALIGN="top">{value}
<TABLE>'''

newMessage = message.format(value=layer.attribute("YOUR ATTRIBUTE"))

infoBox.setText(newMessage)
infoBox.adjustSizeToText()
infoBox.setMargin(-65)
c.addItem(infoBox)