Пример #1
0
QgsMapLayer *QgsProcessingUtils::loadMapLayerFromString( const QString &string, LayerHint typeHint )
{
  QStringList components = string.split( '|' );
  if ( components.isEmpty() )
    return nullptr;

  QFileInfo fi;
  if ( QFileInfo::exists( string ) )
    fi = QFileInfo( string );
  else if ( QFileInfo::exists( components.at( 0 ) ) )
    fi = QFileInfo( components.at( 0 ) );
  else
    return nullptr;

  // TODO - remove when there is a cleaner way to block the unknown projection dialog!
  ProjectionSettingRestorer restorer;
  ( void )restorer; // no warnings

  QString name = fi.baseName();

  // brute force attempt to load a matching layer
  if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Vector )
  {
    QgsVectorLayer::LayerOptions options;
    options.loadDefaultStyle = false;
    std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( string, name, QStringLiteral( "ogr" ), options ) );
    if ( layer->isValid() )
    {
      return layer.release();
    }
  }
  if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Raster )
  {
    QgsRasterLayer::LayerOptions rasterOptions;
    rasterOptions.loadDefaultStyle = false;
    std::unique_ptr< QgsRasterLayer > rasterLayer( new QgsRasterLayer( string, name, QStringLiteral( "gdal" ), rasterOptions ) );
    if ( rasterLayer->isValid() )
    {
      return rasterLayer.release();
    }
  }
  if ( typeHint == LayerHint::UnknownType || typeHint == LayerHint::Mesh )
  {
    QgsMeshLayer::LayerOptions meshOptions;
    std::unique_ptr< QgsMeshLayer > meshLayer( new QgsMeshLayer( string, name, QStringLiteral( "mdal" ), meshOptions ) );
    if ( meshLayer->isValid() )
    {
      return meshLayer.release();
    }
  }
  return nullptr;
}
Пример #2
0
void QgsZonalStatisticsDialog::on_mRasterLayerComboBox_currentIndexChanged( int index )
{
  Q_UNUSED( index );

  QgsRasterLayer* layer = rasterLayer();
  if ( !layer )
  {
    mBandComboBox->setEnabled( false );
    return;
  }

  mBandComboBox->setEnabled( true );
  mBandComboBox->clear();

  int bandCountInt = layer->bandCount();
  for ( int i = 1; i <= bandCountInt; ++i )
  {
    mBandComboBox->addItem( layer->bandName( i ) );
  }
}
Пример #3
0
void terrama2::services::view::core::drawSeriesList(ViewId viewId, std::shared_ptr< terrama2::services::view::core::ViewLogger > logger, std::vector<std::unordered_map<terrama2::core::DataSetPtr, terrama2::core::DataSetSeries>>& seriesList, uint32_t resolutionWidth, uint32_t resolutionHeigth, uint32_t srid)
{
  std::vector< std::shared_ptr<te::map::MemoryDataSetLayer> > layersList;
  uint32_t layerID = 0;

  if(resolutionWidth == 0 ||  resolutionHeigth == 0)
  {
    QString message = QObject::tr("Invalid resolution for View %1.").arg(viewId);
    logger->error(message.toStdString(), viewId);
    throw Exception() << ErrorDescription(message);
  }

  // Create layers from series
  for(auto& series : seriesList)
  {
    for(auto& serie : series)
    {
      terrama2::core::DataSetPtr dataset = serie.first;
      std::shared_ptr<te::da::DataSet> teDataSet = serie.second.syncDataSet->dataset();
      std::shared_ptr<te::da::DataSetType> teDataSetType = serie.second.teDataSetType;

      if(!teDataSetType->hasRaster() && !teDataSetType->hasGeom())
      {
        QString message = QObject::tr("DataSet %1 has no drawable data.").arg(QString::fromStdString(teDataSetType->getDatasetName()));
        logger->error(message.toStdString(), viewId);
      }

      if(teDataSetType->hasRaster())
      {
        // TODO: A terralib dataset can have more than one raster field in it?
        std::size_t rpos = te::da::GetFirstPropertyPos(teDataSet.get(), te::dt::RASTER_TYPE);

        if(!teDataSet->moveFirst())
        {
          QString message = QObject::tr("Can not access DataSet %1 raster data.").arg(QString::fromStdString(teDataSetType->getDatasetName()));
          logger->error(message.toStdString(), viewId);
        }
        else
        {
          auto raster(teDataSet->getRaster(rpos));

          te::gm::Envelope* extent = raster->getExtent();

          // Creates a DataSetLayer of raster
          std::shared_ptr<te::map::MemoryDataSetLayer> rasterLayer(new te::map::MemoryDataSetLayer(te::common::Convert2String(++layerID), raster->getName(), teDataSet, teDataSetType));
          rasterLayer->setDataSetName(teDataSetType->getDatasetName());
          rasterLayer->setExtent(*extent);
          rasterLayer->setRendererType("ABSTRACT_LAYER_RENDERER");

          // if dataset SRID is not setted, try to use the SRID from layer
          if(dataset->format.find("srid") == dataset->format.end())
            rasterLayer->setSRID(raster->getSRID());
          else
            rasterLayer->setSRID(std::stoi(dataset->format.at("srid")));

          // VINICIUS: Set Style
          MONO_0_Style(rasterLayer);

          layersList.push_back(rasterLayer);
        }
      }

      if(teDataSetType->hasGeom())
      {
        // TODO: A terralib dataset can have more than one geometry field in it?
        auto geomProperty = te::da::GetFirstGeomProperty(teDataSetType.get());

        if(!teDataSet->moveFirst())
        {
          QString message = QObject::tr("Can not access DataSet %1 geometry data.").arg(QString::fromStdString(teDataSetType->getDatasetName()));
          logger->error(message.toStdString(), viewId);
        }
        else
        {
          std::shared_ptr< te::gm::Envelope > extent(teDataSet->getExtent(teDataSetType->getPropertyPosition(geomProperty)));

          // Creates a Layer
          std::shared_ptr< te::map::MemoryDataSetLayer > geomLayer(new te::map::MemoryDataSetLayer(te::common::Convert2String(++layerID), geomProperty->getName(), teDataSet, teDataSetType));
          geomLayer->setDataSetName(teDataSetType->getName());
          geomLayer->setVisibility(te::map::VISIBLE);
          geomLayer->setExtent(*extent);
          geomLayer->setRendererType("ABSTRACT_LAYER_RENDERER");

          // if dataset SRID is not setted, try to use the SRID from layer
          if(dataset->format.find("srid") == dataset->format.end())
            geomLayer->setSRID(geomProperty->getSRID());
          else
            geomLayer->setSRID(std::stoi(dataset->format.at("srid")));

          // VINICIUS: set style
          geomLayer->setStyle(CreateFeatureTypeStyle(geomProperty->getGeometryType()));

          layersList.push_back(geomLayer);
        }
      }
    }
  }

  // Draw layers

  if(layersList.size() > 0)
  {
    te::gm::Envelope extent;

    for(auto& layer : layersList)
    {
      if(!extent.isValid())
        extent = layer->getExtent();
      else
        extent.Union(layer->getExtent());

      // If the SRID was not setted, use the SRID from the first layer
      if(srid == 0)
        srid = layer->getSRID();
    }

    // Creates a canvas
    double llx = extent.m_llx;
    double lly = extent.m_lly;
    double urx = extent.m_urx;
    double ury = extent.m_ury;

    std::unique_ptr<te::qt::widgets::Canvas> canvas(new te::qt::widgets::Canvas(resolutionWidth, resolutionHeigth));
    canvas->calcAspectRatio(llx, lly, urx, ury);
    canvas->setWindow(llx, lly, urx, ury);
    canvas->setBackgroundColor(te::color::RGBAColor(255, 255, 255, TE_OPAQUE));

    bool cancel = false;

    for(auto& layer : layersList)
    {
      layer->draw(canvas.get(), extent, srid, 0, &cancel);
    }

    // Save view

    canvas->save("GeneretadImage", te::map::PNG);

    canvas->clear();
  }
  else
  {
    QString message = QObject::tr("View %1 could not find any data.").arg(viewId);
    logger->error(message.toStdString(), viewId);
    throw Exception() << ErrorDescription(message);
  }
}
Пример #4
0
QString QgsZonalStatisticsDialog::rasterFilePath() const
{
  QgsRasterLayer* layer = rasterLayer();
  return layer ? layer->source() : QString();
}