3

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, but I haven't found a way to do something similar in QGIS yet.

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)
Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Jan 24, 2023 at 2:18
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

answered Jan 24, 2023 at 4:15
2
  • Alternative to a processing plugin is a processing script. I particularly like the @alg decorator way of writing scripts. Commented 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. Commented Jan 25, 2023 at 1:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.