Skip to content

Instantly share code, notes, and snippets.

View RichardQZeng's full-sized avatar

Richard Zeng RichardQZeng

View GitHub Profile
@RichardQZeng
RichardQZeng / qgsrubberband_polygon_moving
Created October 22, 2024 20:55
Move polygon in QGIS canvas using QgsRubberband
class mMapTool(QgsMapTool):
def __init__(self, mainLayer, canvas):
self.mlayer = mainLayer
self.canvas = canvas
QgsMapTool.__init__(self, self.canvas)
self.rubberBand = QgsRubberBand(self.canvas, Qgis.GeometryType.Polygon)
mFillColor = QColor(0, 0, 0, 88);
self.rubberBand.setColor(mFillColor)
# initialise a class attribute to store the offsets between the feature vertices and the mouse position
@RichardQZeng
RichardQZeng / cfrw.py
Created July 31, 2024 19:07 — forked from sgillies/cfrw.py
Rasterio concurrency example
"""Concurrent read-process-write example"""
import concurrent.futures
from itertools import islice
from time import sleep
import rasterio
CHUNK = 100
@RichardQZeng
RichardQZeng / python_dual_log_setup.py
Created July 31, 2024 17:04
Setup python logging using console and rotating file at the same time. Color coded format is applied.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# -
# Python dual-logging setup (console and log file), -
# supporting different log levels and colorized output -
# -
# Created by Fonic <https://github.com/fonic> -
# Date: 04/05/20 - 02/07/23 -
@RichardQZeng
RichardQZeng / tqdm_in_imap_unordered.py
Created July 31, 2024 17:03
Use tqdm in multiprocessing
from multiprocessing import Pool
import time
from tqdm import tqdm
def _foo(my_number):
square = my_number * my_number
time.sleep(1)
return square
@RichardQZeng
RichardQZeng / pyqgis_treeview_canvas
Created June 13, 2024 19:01
PyQGIS application example with QgsTreeView and Canvas.
import sys
from qgis.core import QgsApplication, QgsProject, QgsRasterLayer, QgsLayerTreeModel
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge, QgsLayerTreeView
from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget
from PyQt5.QtCore import Qt
class myWindow(QMainWindow):
def __init__(self):
@RichardQZeng
RichardQZeng / pyqgis_example
Created June 13, 2024 17:38
PyQGIS independent application showing layers saved in QGIS project
import sys
from qgis.core import QgsApplication, QgsProject, QgsRasterLayer
from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge
from PyQt5.QtWidgets import QApplication, QMainWindow
class myWindow(QMainWindow):
def __init__(self, path, layer_name):
QMainWindow.__init__(self)
@RichardQZeng
RichardQZeng / pyqt5_stock_icons.py
Created June 6, 2024 21:33
Show PyQt5 Stock Icons
# refer to: https://www.pythonguis.com/faq/built-in-qicons-pyqt/
import sys
from PyQt5.QtWidgets import QApplication, QGridLayout, QPushButton, QStyle, QWidget
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
@RichardQZeng
RichardQZeng / qslider_click_to_move.py
Created June 6, 2024 20:45
QSlider click and slide to mouse position
# refer to: https://stackoverflow.com/a/52690011/1561540
from PyQt5 import QtCore, QtWidgets
class Slider(QtWidgets.QSlider):
def mousePressEvent(self, event):
super(Slider, self).mousePressEvent(event)
if event.button() == QtCore.Qt.LeftButton:
val = self.pixelPosToRangeValue(event.pos())
@RichardQZeng
RichardQZeng / pyqt5_treeview_example.py
Created June 6, 2024 18:43
PyQt5 QTreeWidget Example
import sys
from PyQt5.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem
# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
data = {"Project A": ["file_a.py", "file_a.txt", "something.xls"],
"Project B": ["file_b.csv", "photo.jpg"],
@RichardQZeng
RichardQZeng / pyqt5_threads_with_gui.py
Created June 6, 2024 18:42
PyQt5 start multiple threads
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import traceback, sys
class WorkerSignals(QObject):
'''