ArcGIS Runtime SDK for Qt

Raster function (file)

Loading

Code

#include "RasterFunctionFile.h"

#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "Raster.h"
#include "RasterLayer.h"
#include "RasterFunction.h"
#include "Envelope.h"
#include <QQmlProperty>
#include <QFileInfo>

using namespace Esri::ArcGISRuntime;

RasterFunctionFile::RasterFunctionFile(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void RasterFunctionFile::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<RasterFunctionFile>("Esri.Samples", 1, 0, "RasterFunctionFileSample");
}

void RasterFunctionFile::componentComplete()
{
  QQuickItem::componentComplete();

  // get data path
  m_dataPath = QUrl(QQmlProperty::read(this, "dataPath").toString()).toLocalFile();
  m_rasterPath = m_dataPath + "Shasta_Elevation.tif";

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // Create a map using the imagery basemap
  m_map = new Map(Basemap::imagery(this), this);

  // Add the Raster Layer
  m_raster = new Raster(m_rasterPath, this);
  RasterLayer* rasterLayer = new RasterLayer(m_raster, this);
  rasterLayer->setOpacity(0.5);

  connect(rasterLayer, &RasterLayer::doneLoading, this, &RasterFunctionFile::readyChanged);

  m_map->operationalLayers()->append(rasterLayer);

  // Set initial Extent
  Envelope env = Envelope::fromJson("{\"spatialReference\": {\"latestWkid\": 3857, \"wkid\": 102100 },\"xmax\": -13591503.517810356,\"xmin\": -13606233.44023646,\"ymax\": 4982810.138527592,\"ymin\": 4971762.696708013}");
  m_map->setInitialViewpoint(Viewpoint(env));

  // Set map to map view
  m_mapView->setMap(m_map);
}

void RasterFunctionFile::applyRasterFunction()
{
  // create raster function
  RasterFunction* rasterFunction = createRasterFunction();

  // check for valid raster function
  if (!rasterFunction)
    return;

  // create the raster from the raster function
  Raster* raster = new Raster(rasterFunction, this);

  // create raster layer from raster
  RasterLayer* rasterLayer = new RasterLayer(raster, this);
  rasterLayer->setOpacity(0.5);

  // add raster to map
  m_map->operationalLayers()->clear();
  m_map->operationalLayers()->append(rasterLayer);
}

RasterFunction* RasterFunctionFile::createRasterFunction()
{
  // Check if the raster function json exists
  QFileInfo colorJson(m_dataPath + "/color.json");
  if (!colorJson.exists())
    return nullptr;

  // create a RasterFunction
  RasterFunction* rasterFunction = new RasterFunction(m_dataPath + "/color.json", this);

  // check for valid raster function
  if (!rasterFunction)
    return nullptr;

  // set the number of rasters required - 2 in this case
  rasterFunction->arguments()->setRaster("raster", m_raster);
  rasterFunction->arguments()->setRaster("raster", m_raster);

  return rasterFunction;
}

bool RasterFunctionFile::ready() const
{
  return m_raster && m_raster->loadStatus() == LoadStatus::Loaded;
}


In this topic
  1. Code