5

I have a CF-conform NetCDF-file which can be loaded into Quantum GIS (Version 1.8) by using the "Add Raster Layer"-button and subsequently choosing the file and the raster layers of interest. This works without problems. However, when trying to load the same file by following these steps from the python cookbook, the layer remains invalid. I tried it using the console:

>>> fileName = "/path/to/file/myfile.nc"
>>> fileInfo = QtCore.QFileInfo(fileName)
>>> baseName = fileInfo.baseName()
>>> rlayer = QgsRasterLayer(fileName, baseName)
>>> rlayer.isValid()
False

When trying to add the layer to the map registry, it doesn't show up:

>>> QgsMapLayerRegistry.instance().addMapLayer(rlayer)
<qgis.core.QgsRasterLayer object at 0x3715f80>

I tried the same procedure with a jpeg with world file and it works.

The cookbook mentions that GDAL might not support some formats. I don't know how to check this in QGIS, but since it works manually, I don't think that this is the problem.

Maybe, I have to specify the raster layer of interest somewhere in the code?

Any help is highly appreciated.

  • The problem is that netcdf can have several subdataset.See the scripts of the netcdfbrowser plugin. – gene Sep 10 '13 at 16:44
4

Thanks to gene mentioning the netcdfbrowser plugin (see the addLayer()-method in netcdfbrowserdialog.py) I managed to get it working. In case of NetCDF-files the file name is not sufficient to load a raster. We also need to include the variable of interest. The following works:

>>> uri='NETCDF:"/path/to/file/myfile.nc":myvariable'
>>> rlayer = QgsRasterLayer(uri,'mylayername')
>>> rlayer.isValid()
True
>>> QgsMapLayerRegistry.instance().addMapLayer(rlayer)
<qgis.core.QgsRasterLayer object at 0x38f5560>

The layer shows up.

0

You can also use Crayfish Plugin: First you need to install the plugin. And then:

    import qgis.utils
    cf = qgis.utils.plugins["crayfish"]
    cf.loadMesh('some/file.nc', '.nc')
    cf.dock.currentLayerChanged()

Currently, you need to select the CRS interactively.

  • As commented on one of your other answers - While we do not forbid advertising products which solve the specific problem described in a thread, it is generally recommended to provide specific details and - if appropriate - code/screenshots to back up your claims rather than a couple of links to some product website – Midavalo Apr 15 '16 at 8:29
  • Please use the edit button beneath your answer to update it with this information and to add a disclosure statement as per gis.stackexchange.com/help/behavior – PolyGeo Apr 16 '16 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.