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)