[QGIS-Developer] Can't get values from QgsMeshLayer

Enrico Ferreguti enricofer at gmail.com
Wed Feb 3 06:58:00 PST 2021


I'm trying to write a simple function for retrieving wind speed and
direction at a specified moment from a given grib file readable as mesh
layer the prototype script is the following:

mesh = QgsMeshLayer("[path to the attached
grib]/20210202_211414_ICON_EU_P06_.grb2","test","mdal")
#mesh = iface.activeLayer()
p1 = QgsPointXY(1.60301, 38.38453)

ds_group_index = 1

for g in mesh.datasetGroupsIndexes():
    print ("MESH GROUP:", g,
mesh.datasetGroupMetadata(mesh.datasetIndexAtRelativeTime(h0,g)).name())

for i in range(mesh.dataProvider().datasetCount(ds_group_index)):
    meta =
mesh.dataProvider().datasetMetadata(QgsMeshDatasetIndex(ds_group_index, i))
    t = meta.time()
    dataset = QgsMeshDatasetIndex(ds_group_index, i)
    value = mesh.datasetValue(dataset, p1)
    print ("VALUES P1",t,value.scalar(),value.x(),value.y())

When I run the script I can get dataset names from metadata but I'getting
Nan as values
I verified that the grib is loadable in mapcanvas and the sample point is
within grib extent.
I read the note in api (
https://qgis.org/api/classQgsMeshLayer.html#a76a7ec072b4acaf45c1a10f5872c4948)
"It uses previously cached and indexed triangular mesh and so if the layer
has not been rendered previously (e.g. when used in a script) it returns
NaN value" so I tried to process an already loaded mesh (decomment line 2)
without luck.

Is there anybody who can help me finding what I'm doing wrong? Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20210203/cc8bd064/attachment-0001.html>


More information about the QGIS-Developer mailing list