Geographic Information Systems Asked by Leendert Van Wolfswinkel on March 13, 2021
I want to use the temporal controller to create an animation of a time series of water depth rasters. Using pyqgis, I want to add the layers to my project and set the correct temporal properties for each layer, so I then only have to press play in the Temporal Controller. Adding the raster layers to QGIS and setting the temporal properties works just fine, but these properties are not picked up by the Temporal Controller until after I manually click Properties > Apply for each layer. In most use cases it will concern > 20 raster layers, so this is not very practical.
How do I tell the Temporal Controller to read the updated raster layer temporal properties?
Code snippet below. The input is a folder of .tif files (water depth, each at a specific time). They are named water_depth_{zero_padded_number}.tif.
QGIS version: 3.16.4
wd = 'C:/Users/leendert.vanwolfswin/Documents/temporal_experiments/'
tif_folder = os.path.join(wd, 'waterdepth_batch')
nr_time_steps = 5
time_steps = range(nr_time_steps)
result_layers = {}
start_datetime = QDateTime(2000, 1, 1, 0, 0, 0, Qt.LocalTime)
duration_s = 300
qml = os.path.join(wd, 'waterdepth.qml')
root = QgsProject.instance().layerTreeRoot()
layer_group = root.insertGroup(0, 'Water depth (temporal)')
for time_step in time_steps:
tif_basename = 'water_depth_{}'.format(str(time_step).zfill(4))
tif_fn = os.path.join(tif_folder, '{}.tif'.format(tif_basename))
result_layers[time_step] = QgsRasterLayer(tif_fn, tif_basename)
# add layer to project
result_layers[time_step].loadNamedStyle(qml)
project = QgsProject.instance()
project.addMapLayer(result_layers[time_step], addToLegend=False)
layer_group.insertLayer(-1, result_layers[time_step])
result_layers[time_step].temporalProperties().setMode(QgsRasterLayerTemporalProperties.ModeFixedTemporalRange)
start_time = start_datetime.addSecs(time_step*duration_s+1) # + 1 to avoid temporal overlap between the frames
end_time = start_datetime.addSecs((time_step+1)*duration_s-1) # - 1 to avoid temporal overlap between the frames
time_range = QgsDateTimeRange(start_time, end_time)
result_layers[time_step].temporalProperties().setFixedTemporalRange(time_range)
result_layers[time_step].temporalProperties().setIsActive(True)
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP