Created
March 1, 2022 19:59
-
-
Save timlinux/45dba41d8bb305ec0dd4cb100aa4f9d1 to your computer and use it in GitHub Desktop.
Debuggin parallel renderer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def render_image_to_file(self, name): | |
size = self.iface.mapCanvas().size() | |
image = QImage(size, QImage.Format_RGB32) | |
painter = QPainter(image) | |
settings = self.iface.mapCanvas().mapSettings() | |
self.iface.mapCanvas().refresh() | |
# You can fine tune the settings here for different | |
# dpi, extent, antialiasing... | |
# Just make sure the size of the target image matches | |
job = QgsMapRendererCustomPainterJob(settings, painter) | |
job.renderSynchronously() | |
painter.end() | |
image.save(name) | |
def free_render_lock(self): | |
print('Freeing render lock.') | |
self.current_render_thread_count -= 1 | |
print(' Now %d threads used ' % self.current_render_thread_count) | |
def render_image_as_task(self,name,current_point_id,current_frame): | |
# Wait for the render queue to empty first then allow another batch to run | |
# Block until there is space in the render thread pool | |
if self.current_render_thread_count > self.render_thread_pool_size: | |
print('Waiting for render lock.') | |
while self.current_render_thread_count > 0: | |
time.sleep(1.0) | |
print(' Now %d threads used ' % self.current_render_thread_count) | |
#size = self.iface.mapCanvas().size() | |
settings = self.iface.mapCanvas().mapSettings() | |
# The next part sets project variables that you can use in your | |
# cartography etc. to see the progress. Here is an example | |
# of a QGS expression you can use in the map decoration copyright | |
# widget to show current script progress | |
# [%'Frame ' || to_string(coalesce(@current_frame, 0)) || '/' || | |
# to_string(coalesce(@frames_per_point, 0)) || ' for point ' || | |
# to_string(coalesce(@current_point_id,0))%] | |
task_scope = QgsExpressionContextScope() | |
task_scope.setVariable('current_point_id', current_point_id) | |
task_scope.setVariable('frames_per_point', self.frames_per_point) | |
task_scope.setVariable('current_frame', current_frame) | |
context = settings.expressionContext() | |
context.appendScope(task_scope) | |
settings.setExpressionContext(context) | |
# Set the output file name for the render task | |
mapRendererTask = QgsMapRendererTask( settings, name, "PNG" ) | |
# We need to clone the annotations because otherwise SIP will | |
# pass ownership and then cause a crash when the render task is destroyed | |
annotations = QgsProject.instance().annotationManager().annotations() | |
annotations_list = [a.clone() for a in annotations] | |
if (len(annotations_list) > 0): | |
mapRendererTask.addAnnotations([a.clone() for a in annotations]) | |
# Add decorations to the render job | |
decorations = self.iface.activeDecorations() | |
mapRendererTask.addDecorations(decorations) | |
# Allow other tasks waiting in the queue to go on and render | |
mapRendererTask.renderingComplete.connect(self.free_render_lock) | |
# Does not work | |
#QObject.connect(mapRendererTask,SIGNAL("renderingComplete()"),free_render_lock) | |
# Ready to start rendering, claim a space in the pool | |
self.current_render_thread_count += 1 | |
print(' Now %d threads used ' % self.current_render_thread_count) | |
# Start the rendering task on the queue | |
QgsApplication.taskManager().addTask(mapRendererTask) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment