Skip to content

QgisApp

Import
from pyqgis_wrapper.application import QgisApp

QgisApp(processing_path, prefix_path='/usr', providers=[], project=QgsProject.instance(), use_canvas=False, canvas_name='PyQGIS Map Viewer')

Custom Qgis Application Allowing an easy use of processing algoritmh within your IDE.

You should still have an environment with qgis library available.

You can add custom or additionnal providers (such as plugins)

You can enable the use of a simple canvas within your IDE to help visualize your layer without the needs to write them or loading them into QGIS.

Access to qgs applications with QgisApp.app Access to qgs project with QgisApp.project Access to qgs processing with QgisApp.processing Access to qgs canvas with QgisApp.canvas

After instanciation you can import processing in your local scope if you want to avoid using QgisApp.processing.

Parameters:

Name Type Description Default
processing_path str

Path of the processing module. An helper function (find_module_path) is available in the misc module to help locating it.

required
prefix_path str

Prefix where QGIS will look for his ressources, defaults to "/usr". On windows it's under : "C:/Program Files/QGIS *

'/usr'
providers typing.List[qgis.core.QgsProcessingProvider]

Additionnal providers to load, defaults to []

[]
project typing.Union[str, qgis.core.QgsProject]

Project to use, defaults to QgsProject.instance()

qgis.core.QgsProject.instance()
canvas_name str

Name of the canvas windows, defaults to "PyQGIS Map Viewer"

'PyQGIS Map Viewer'
Source code in pyqgis_wrapper/application/qgis_app.py
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
def __init__(
    self,
    processing_path: str,
    prefix_path: str = "/usr",
    providers: List[QgsProcessingProvider] = [],
    project: Union[str, QgsProject] = QgsProject.instance(),
    use_canvas: bool = False,
    canvas_name: str = "PyQGIS Map Viewer",
):
    """
    :param processing_path: Path of the processing module.
    An helper function (find_module_path) is available
    in the misc module to help locating it.
    :type processing_path: str
    :param prefix_path: Prefix where QGIS will look for
    his ressources, defaults to "/usr".
    On windows it's under : "C:/Program Files/QGIS *
    :type prefix_path: str, optional
    :param providers: Additionnal providers to load, defaults to []
    :type providers: List[QgsProcessingProvider], optional
    :param project: Project to use, defaults to QgsProject.instance()
    :type project: Union[ str, QgsProject ], optional
    :param canvas_name: Name of the canvas windows, defaults to "PyQGIS Map Viewer"
    :type canvas_name: str, optional
    """

    self.logger = create_child_logger(__name__)
    self._init_qgis(prefix_path, use_canvas)
    self._init_processing(processing_path, providers)
    self.project = project
    if use_canvas:
        self.canvas = canvas_name

canvas property writable

Canvas getter

Returns:

Type Description
QgisCanvas

Canvas object

processing property

processing module getter

app property

Qgis app getter

Returns:

Type Description
QgsApplication

Current SGIS application

project property writable

QgsProject getter

Returns:

Type Description
QgsProject

Instance of a QgsProject

exit property

Close QgisApp.

save_project(file_path=None)

Save a project to his associated file or a new file if provided.

Parameters:

Name Type Description Default
file_path typing.Optional[str]

File to save the project to, defaults to None

None
Source code in pyqgis_wrapper/application/qgis_app.py
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
def save_project(self, file_path: Optional[str] = None):
    """
    Save a project to his associated file or a new file if provided.

    :param file_path: File to save the project to, defaults to None
    :type file_path: Optional[str], optional
    """
    if not self.project.fileName() and not file_path:
        raise IOError(
            "The project is not associated with a file. Please provide a file path"
        )
    if file_path:
        self.project.setFileName(file_path)

    success = self.project.write()
    if success:
        self.logger.info("Project saved.")
    else:
        self.logger.info("Could not save the project")