2

My code selects points in a layer by date and creates a new layer from them. I want to make this a processing tool where the user can choose the date and run the script.

I'm brand new to QGIS, but I've created several tools like this for ArcMap with ArcPy - that process involves adding GetParameterAsText() for user inputs and appending your script to a toolbox.

I have been searching and watching videos, and as yet I haven't found a way to do something similar in QGIS.

Script snippet below (this is not the complete script, but it includes the date that I'd like to replace with user input):

import qgis.utils
import PyQt5
from PyQt5 import QtPrintSupport
from PyQt5.QtPrintSupport import QPrinter
from qgis.utils import iface
from qgis.PyQt.QtCore import (QCoreApplication,
                             QSizeF)
from PyQt5.QtGui import (QColor,
                         QPageLayout,
                         QPainter)
from qgis.core import (QgsProcessing,
                       QgsProcessingAlgorithm,
                       QgsProcessingException,
                       QgsProcessingContext,
                       QgsProcessingParameterString,
                       QgsProcessingOutputVectorLayer,
                       QgsProcessingOutputRasterLayer,
                       QgsProcessingOutputString,
                       QgsProcessingParameterNumber,
                       QgsVectorLayer,
                       QgsVectorFileWriter,
                       QgsRasterLayer,
                       QgsDataSourceUri,
                       QgsProject,
                       Qgis,
                       QgsMapRendererCustomPainterJob)

#select features
temporaryCompaction = QgsProject.instance().mapLayersByName('Temporarycompaction')[0]
iface.setActiveLayer(temporaryCompaction)

iface.mapCanvas().setSelectionColor(QColor('green'))
expression = "to_date(Date) = to_date('2021-09-23', 'yyyy-MM-dd')"
temporaryCompaction.selectByExpression(expression)

#create layer from selected features and import to Layers
fn = r'U:\skelley\public\QGISstuff\PacTrustMergin-2test/newLayer.gpkg'
writer = QgsVectorFileWriter.writeAsVectorFormat(temporaryCompaction, fn, 'utf-8',        
driverName='GPKG', onlySelected=True)
selected_layer = iface.addVectorLayer(fn, '', 'ogr')
del(writer)
1

1 Answer 1

0

There are many tutorials that can help you create a Python Plugin for QGIS, although it sounds like you've found them. I'm not sure what your question is if that's the case.

Start over at the PyQGIS documentation, https://docs.qgis.org/3.22/en/docs/pyqgis_developer_cookbook/plugins/index.html

2
  • Alternative to a processing plugin is a processing script. I particularly like the @alg decorator way of writing scripts.
    – user2856
    Jan 24, 2023 at 4:56
  • I think processing script is term I'm looking for. My head's full of ArcGIS terminology, so all I could think was script + toolbox. I don't want to go the plugin route. Thank you for the the link.
    – Skel80
    Jan 25, 2023 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.