如何使用 pyqgis 更改 QgsMeshLayer 中的提供者时间设置?

问题描述 投票:0回答:1

我正在与

QgsMeshLayer
合作,我想将提供商时间设置更改为秒(默认情况下为小时)。

这是我尝试过的,但提供商时间设置仍然设置为小时。

from qgis.core import (
    QgsProject,
    QgsMeshLayer,
    QgsCoordinateReferenceSystem,
    QgsLayerMetadata,
)
from PyQt5.QtCore import QDateTime

# Load the mesh layer
mesh_layer = QgsMeshLayer(
    path,
    "Mesh Layer",
    "mdal",
)


if isinstance(mesh_layer, QgsMeshLayer):
    # Access temporal properties
    temporal_properties = mesh_layer.temporalProperties()

    # Set reference time
    reference_time = QDateTime.fromSecsSinceEpoch(0)  # Epoch time: "1970-01-01T00:00:00Z"
    capabilities = mesh_layer.dataProvider().temporalCapabilities()
    if capabilities:
        print("Temporal capabilities retrieved successfully.")
        temporal_properties.setReferenceTime(reference_time, capabilities)
        print(f"Reference time set to: {temporal_properties.referenceTime().toString()}")
    
    # Access and modify metadata to enforce seconds
    metadata = mesh_layer.metadata()
    metadata.setAbstract("Time units: seconds")
    mesh_layer.setMetadata(metadata)

    # Refresh the mesh layer
    mesh_layer.triggerRepaint()
    QgsProject.instance().addMapLayer(mesh_layer)
    print("Mesh layer updated: Time units set to seconds and layer refreshed.")
else:
    print("Layer is not a valid mesh layer.")

enter image description here

settings layer qgis pyqgis
1个回答
0
投票

您可以通过

QgsMeshLayerDataProvider
更改时间单位,它有一个
setTemporalUnit
方法。 文档

layer_name = 'TIN Mesh'
mesh_layer = QgsProject.instance().mapLayersByName('TIN Mesh')[0]
mesh_layer.dataProvider().setTemporalUnit(Qgis.TemporalUnit.Seconds)
© www.soinside.com 2019 - 2024. All rights reserved.