[Qgis-user] QGis3 Standalone INPUT Layer Parameter Out of The Toolbox Scripts

1520 gis juliermeopensourcedeveloper at gmail.com
Tue Aug 22 19:34:13 PDT 2023


Hi all,

I'm a newbie with PyQt. I'm writing an App and want to retrieve the layers'
names available in QGis's layer tree using the INPUT parameter as the one
available in the Processing Algorithm with the layer dropdown widget and
the widget options [iterate over this layer, advanced options, select
input].
I wonder if it's possible to inherit the widget from
the QgsProcessingAlgorithm class as shown below or whether I should write a
new dropdown widget from scratch to retrieve all the layers available in
the layer tree.

import sys
from PyQt5.QtCore import (QSize,Qt)
import sys
from PyQt5.QtWidgets import (QApplication,
                                QWidget,
                                QPushButton,
                                QMainWindow,
                                QVBoxLayout)

from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink)
from qgis import processing


class MainWindow(QMainWindow,QgsProcessingAlgorithm):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        self.setGeometry(100,100,400,200)


        layout = QVBoxLayout()
        self.button = QPushButton("Calculate Area")
        layout.addWidget(self.button)
        self.result_label = QLabel("QGIS3")
        layout.addWidget(self.result_label)
        self.button.clicked.connect(self.calculate_area)
        central_widget = QWidget()
        central_widget.setLayout(layout)


        self.setCentralWidget(central_widget)
        self.button.clicked.connect(self.calculate_area)
        #self.setFixedSize(QSize(400,300))

    def initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """

        # We add the input vector features source. It can have any kind of
        # geometry.
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry]
            )
        )

    def calculate_area(self):
        print('568,00 km2')
        # Prepare polygon geometry (replace with your own geometry)
        # wkt = "POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))"
        # polygon = QgsGeometry.fromWkt(wkt)

        # # Use the Processing algorithm to calculate the area


        # params = {
        #     "INPUT": self.INPUT,
        #     "OUTPUT": "memory:",
        # }
        # result = processing.run("qgis:exportaddgeometrycolumns", params)
        # area = result["OUTPUT"].getFeatures().__next__().attribute("area")

        # self.result_label.setText(f"Calculated Area: {area:.2f} square
units")

window = MainWindow()
window.show()



-- 
##########################################
*Julierme G Pinheiro*
*SDI Expert & Geoprocessing Specialist*
Phone: +55 61 99994-3569
Website: GIS
<https://www.udemy.com/course/qgis-server-para-iniciantes-instalacao-e-configuracao/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20230822/b888f825/attachment.htm>


More information about the QGIS-User mailing list