Skip to content

Instantly share code, notes, and snippets.

View benwirf's full-sized avatar

Ben Wirf benwirf

  • Darwin, NT Australia
View GitHub Profile
layer = QgsProject.instance().mapLayersByName('LIMITES_ADMINISTRATIVES_EXPRESS.LATEST')[0]
url = 'https://www.geoportail.gouv.fr/depot/layers/LIMITES_ADMINISTRATIVES_EXPRESS.LATEST/legendes/LIMITES_ADMINISTRATIVES_EXPRESS.LATEST-legend.png'
layer.setLegendPlaceholderImage(url)
props = QgsRasterLayerProperties(layer, self.iface.mapCanvas())# Note use of self.iface object if this is in a plugin
props.apply()
class SnappingMapTool(QgsMapToolEmitPoint):
def __init__(self, canvas):
self.canvas = canvas
self.project = QgsProject.instance()
QgsMapToolEmitPoint.__init__(self, self.canvas)
self.layer = iface.activeLayer()
# set up snapping settings
self.snap_config = QgsSnappingConfig(self.project)
self.snap_config.setEnabled(True)
@benwirf
benwirf / field_expression_widget_mlcb.py
Created September 19, 2022 08:42
Field expression widget with map layer combo box
class TestDialog(QDialog):
def __init__(self, iface):
QDialog.__init__(self)
self.iface = iface
self.mlcb = QgsMapLayerComboBox(self)
self.exp_widget = QgsFieldExpressionWidget(self)
self.line_edit = QLineEdit(self)
self.btn = QPushButton('Run', self)
self.list_widget = QListWidget(self)
class TestDialog(QDialog):
def __init__(self, iface):
QDialog.__init__(self)
self.iface = iface
self.exp_widget = QgsFieldExpressionWidget(self)
self.line_edit = QLineEdit(self)
self.btn = QPushButton('Run', self)
self.list_widget = QListWidget(self)
#-----------------------------------------------------------
# Copyright (C) June 2022 Ben Wirf
#-----------------------------------------------------------
# Licensed under the terms of GNU GPL 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#---------------------------------------------------------------------
#-----------------------------------------------------------
# Copyright (C) June 2022 Ben Wirf
#-----------------------------------------------------------
# Licensed under the terms of GNU GPL 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#---------------------------------------------------------------------
@benwirf
benwirf / Export_layout_legend_to_spreadsheet.py
Last active June 9, 2022 03:54
Export a QGIS layout legend schema with icons to an .xlsx spreadsheet
#-----------------------------------------------------------
# Copyright (C) 2022 Ben Wirf
#-----------------------------------------------------------
# Licensed under the terms of GNU GPL 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#---------------------------------------------------------------------
@benwirf
benwirf / Points_in_polygons_model.py
Created December 16, 2021 04:25
An exported model script modified to add a points within polygons query add update output layer attributes with 1 (within) or 0 (not within)
"""
Model exported as python.
Name : Point_within_polygon
Group : Examples
With QGIS : 32003
***************************************************************************
Point_inside_polygon.py
---------------------
Date : December 2021
Copyright : (C) 2021 by Ben Wirf
@benwirf
benwirf / qgis_get_raster_names_under_click_point.py
Last active October 21, 2021 07:42
Example of QGIS map tool which prints names of raster layers whose extents contain a mouse click point
# Run this code in QGIS Python console
class MyPointTool(QgsMapToolEmitPoint):
def __init__(self, canvas):
self.canvas = canvas
QgsMapToolEmitPoint.__init__(self, self.canvas)
def canvasReleaseEvent(self, e):
pnt = e.mapPoint()
r_layers = [l for l in QgsProject.instance().mapLayers().values() if l.type() == QgsMapLayerType.RasterLayer]
@benwirf
benwirf / model_script_post_processor.py
Last active March 30, 2023 23:08
QGIS processing model exported as Python script with layer post processor to rename final output layer
"""
Model exported as python.
Name : clipped_points_buffered
Group : Models
With QGIS : 31415
"""
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback