Click here to Skip to main content
15,444,993 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I am trying to add a raster layer, I get this error from QgsRasterLayer::IsValid()!!! Can anybody tell me why this happens!? I am beginner in qgis, so could you please explain in detail. the first func "onAddLayer" used for Add button clicked and second func is defined for adding a raster layer.

Note: I am using QGis 2.18 and gdal 2.3.2 in fedora.

What I have tried:

void  MainWindow::onAddLayer()
{
  const auto  path = QFileDialog::getOpenFileName(this, "SELECT GIS MAP LAYER", QDir(
                                                    _lastMapDir).path());

  if (path.isEmpty() || path.isNull())
  {
    return;
  }

  addRasterLayer(path);
  _lastMapDir = QDir(path).canonicalPath();
}

void  MainWindow::addRasterLayer(const QFileInfo &info)
{
  LOG(QString("adding map layer %1").arg(info.filePath()));
  QString  errMsg;

//  if (!QgsRasterLayer::isValidRasterFileName(info.filePath(), errMsg))
//  {
//    LOG(errMsg);

//    return;
//  }

  QScopedPointer<QgsRasterLayer>  layer(new QgsRasterLayer(info.filePath(), info.baseName()));

  if (!layer->isValid())
  {
    LOG(QString("failed to add layer (%1)").arg(layer->error().message(
                                                  QgsErrorMessage::Format::Text)));

    return;
  }

  save("last_layer_path", info.filePath(), "gis");
  _rasterProvider = layer->dataProvider();
  _mapCanvas->freeze(true);
  auto  item = QgsMapCanvasLayer(layer.take(), true);
  _mapCanvas->setExtent(item.layer()->extent());
  QgsMapLayerRegistry::instance()->addMapLayer(item.layer(), true);
  _mapCanvas->setLayerSet(QList<QgsMapCanvasLayer>() << item);
  _mapCanvas->freeze(false);
  _mapCanvas->setVisible(true);
  _mapCanvas->refresh();
  statusBar()->showMessage(QString("%1 LOADED").arg(info.fileName()), 3000);
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900