_qgis
automaps._qgis.export
#
This module handles the export of QGIS layouts.
export_layout(layout, filepath, file_format)
#
Exports layout as pdf, png or svg to the desired filepath.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layout |
QgsLayout |
The layout to export. |
required |
filepath |
str |
The output filepath, including to file extension. |
required |
file_format |
str |
Can be one of 'pdf', 'png', 'svg'. |
required |
Exceptions:
Type | Description |
---|---|
ValueError |
if unsupported file format is requested. |
Source code in automaps/_qgis/export.py
def export_layout(layout: QgsLayout, filepath: str, file_format: str):
"""Exports layout as pdf, png or svg to the desired filepath.
Args:
layout (QgsLayout): The layout to export.
filepath (str): The output filepath, including to file extension.
file_format (str): Can be one of 'pdf', 'png', 'svg'.
Raises:
ValueError: if unsupported file format is requested."""
exporter = QgsLayoutExporter(layout)
if file_format.lower() == "pdf":
pdf_settings = QgsLayoutExporter.PdfExportSettings()
pdf_settings.rasterizeWholeImage = True
exporter.exportToPdf(filepath, pdf_settings)
elif file_format.lower() == "png":
# Don't show the following error message
# ERROR 6: The PNG driver does not support update access to existing datasets.
# https://gis.stackexchange.com/questions/360254/pyqgis-exporting-print-layout-error-6-the-png-driver-does-not-support-update
from osgeo import gdal
gdal.PushErrorHandler("CPLQuietErrorHandler")
image_settings = QgsLayoutExporter.ImageExportSettings()
image_settings.cropToContents = True
exporter.exportToImage(filepath, image_settings)
# Remove transparent margins
# See https://docs.qgis.org/3.16/en/docs/user_manual/print_composer/create_output.html#export-as-image
# "When exporting with the Crop to content option, the resulting image may
# therefore extend beyond the paper extent."
with Image.open(filepath) as im:
im = im.crop(im.getbbox())
im.save(filepath)
elif file_format.lower() == "svg":
svg_settings = QgsLayoutExporter.SvgExportSettings()
svg_settings.forceVectorOutput = True
svg_settings.simplifyGeometries = True
svg_settings.exportLabelsToSeparateLayers = True
svg_settings.exportAsLayers = True
exporter.exportToSvg(filepath, svg_settings)
else:
raise ValueError(f"Unsupported export file format: {file_format}")
automaps._qgis.layout
#
This module handles QGIS print layouts.
get_layout_by_name(project, name)
#
Finds a QGIS print layout by name in a given QGIS project.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project |
QgsProject |
The QGIS project. |
required |
name |
str |
The name of the print layout. |
required |
Returns:
Type | Description |
---|---|
QgsPrintLayout |
The desired print layout. |
Source code in automaps/_qgis/layout.py
def get_layout_by_name(project: QgsProject, name: str) -> QgsPrintLayout:
"""Finds a QGIS print layout by name in a given QGIS project.
Args:
project (QgsProject): The QGIS project.
name (str): The name of the print layout.
Returns:
QgsPrintLayout: The desired print layout.
"""
layout_manager = project.layoutManager()
layout = layout_manager.layoutByName(name)
return layout
automaps._qgis.project
#
This module handles QGIS projects.
get_project()
#
Loads a QGIS project from the path defined in the config file into memory.
Returns:
Type | Description |
---|---|
QgsProject |
The desired QGIS project. |
Config Values: FILEPATH_QGIS_PROJECT: The path to the QGIS project file.
Source code in automaps/_qgis/project.py
def get_project() -> QgsProject:
"""Loads a QGIS project from the path defined in the config file into memory.
Returns:
QgsProject: The desired QGIS project.
Config Values:
FILEPATH_QGIS_PROJECT: The path to the QGIS project file.
"""
project = QgsProject()
project.read(automapsconf.FILEPATH_QGIS_PROJECT)
return project
set_project_variable(project, var_name, var_value)
#
Sets a user defined variable in a given QGIS project.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project |
QgsProject |
The QGIS project to modify. |
required |
var_name |
str |
The variable name. |
required |
var_value |
Any |
The variable value. |
required |
Source code in automaps/_qgis/project.py
def set_project_variable(project: QgsProject, var_name: str, var_value: Any):
"""Sets a user defined variable in a given QGIS project.
Args:
project (QgsProject): The QGIS project to modify.
var_name (str): The variable name.
var_value (Any): The variable value.
"""
QgsExpressionContextUtils.setProjectVariable(project, var_name, var_value)