[QGIS-Developer] Selecting active mesh layer dataset

Brian Haynes bhaynes at herricktechlabs.com
Mon Jan 24 11:02:56 PST 2022


Good afternoon,

I am developing a QGIS application in C++ and am looking to overlay weather data stored in NetCDF files as mesh layers.  Each file has several variables that can be displayed; however it only displays one variable or dataset (always the first one in alphabetical order) and I'm not sure how to make it so I can choose which variable I want displayed.  For an entire layer I know I can call the layer tree node like this:


QString layerName = "Temperature";
QgsLayerTree *tree = project->instance()->layerTreeRoot(); //project is QgsProject object

for ( QgsMapLayer *layer: qmc->layers() ) // qmc is QgsMapCanvas object

    QgsLayerTreeNode *node = tree->findLayer(layer->id());

    if ( layer->name() == layerName )
    {
       node->setItemVisibilityChecked(true);

    } else
    {
       node->setItemVisibilityChecked(false);
    }

}

but its parallel for Mesh Layer subsets doesn't work:


QgsMeshLayer *mesh = new QgsMeshLayer("/path/to/file.nc","MyData","mdal");
QString dataName = "Temperature";

for ( int &i: mesh->datasetGroupsIndexes() )
{
   QgsMeshDatasetIndex did = i;
   QgsMeshDatasetGroupTreeItem *child = tree->childFromDatasetGroupIndex(i);

   if ( child->name() == dataName )
   {
      child->setIsEnabled(true);

   } else
   {
      child->setIsEnabled(false);
   }
}

I've also tried to set the layer rendering for datasets, but it seems to pertain to the entire file (mesh->datasetGroupCount > mesh->datasetCount; the renderer only pertains to the dataset (count = 1).  This would be helpful to figure out as setting the renderer for different types of data (vectors for wind, contours for pressure, etc) is the end goal of this application, and would be an upgrade from having to create multiple shapefiles/rasters for this data.  There doesn't seem to be many examples out there for mesh layer use outside of the QGIS console, not even for PyQGIS developers.

Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20220124/98dc8ed9/attachment.html>


More information about the QGIS-Developer mailing list