Skip to content

Instantly share code, notes, and snippets.

@gounux
Created March 1, 2024 08:50
Show Gist options
  • Save gounux/60180462b1457389222bf72dc5208709 to your computer and use it in GitHub Desktop.
Save gounux/60180462b1457389222bf72dc5208709 to your computer and use it in GitHub Desktop.
Sweet QGIS moves
from datetime import datetime, timedelta
from time import sleep
from typing import Tuple
import numpy as np
def coords8(size: int, nb_points: int = 100) -> Tuple[int, int]:
t = np.linspace(0, 2 * np.pi, nb_points)
x = size * np.sin(t)
y = size * np.cos(t) / 2
return x, y
class SweetMovesTask(QgsTask):
def __init__(
self,
description: str,
iface: QgisInterface,
duration: float = 3,
refresh: float = 0.1,
size: int = 500,
nb_points: int = 20,
):
super().__init__(description, QgsTask.CanCancel)
self.iface = iface
self.duration = duration
self.refresh = refresh
self.size = size
self.nb_points = nb_points
def run(self) -> bool:
start_time = datetime.now()
stop_time = start_time + timedelta(seconds=self.duration)
canvas = iface.mapCanvas()
center = canvas.center()
cx, cy = center.x(), center.y()
coords = coords8(self.size, self.nb_points)
i = 0
up = True
while datetime.now() < stop_time:
self.setProgress(
((datetime.now() - start_time).total_seconds()) / self.duration * 100
)
if self.isCanceled():
return False
nx, ny = coords[0][i], coords[1][i]
dx, dy = nx - cx, ny - cy
rect = canvas.sceneRect()
rect.moveTo(dx, dy)
canvas.setSceneRect(rect)
sleep(self.refresh)
cx = nx
cy = ny
if up:
i += 1
if i >= self.nb_points - 1:
up = False
else:
i -= 1
if i <= 0:
up = True
return True
task = SweetMovesTask(
"Sweet moves",
iface,
duration=5,
refresh=0.05,
size=333,
nb_points=20,
)
QgsApplication.taskManager().addTask(task)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment