Display a WMS (Web Map Service) layer. The WMS layer is displayed over a tiled map service layer and constructed using a list of initial layers to display from the WMS service. This sample also allows changing the visibility of each sub-layer.
WMS and tiled layer (online)
// Copyright 2015 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the Sample code usage restrictions document for further information.
//
#ifndef WMS_LAYER_SAMPLE_H
#define WMS_LAYER_SAMPLE_H
#include "rtsample.h"
#include "WmsDynamicMapServiceLayer.h"
namespace Ui {
class wms_layer_sample;
}
class wms_layer_sample : public RTSample
{
Q_OBJECT
public:
explicit wms_layer_sample(const QString& title, const QString& categoryList, const QString& shortDesc, const QString& longDesc, const QString& thumbnail);
virtual ~wms_layer_sample();
virtual void run();
public slots:
void onMapReady();
void onLayerCreateComplete();
void onItemChanged(QTreeWidgetItem* item, int column);
private:
EsriRuntimeQt::WmsDynamicMapServiceLayer* m_wmsLayer;
};
#endif // WMS_LAYER_SAMPLE_H
// Copyright 2015 ESRI
//
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
//
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
//
// See the Sample code usage restrictions document for further information.
//
#include "wms_layer_sample.h"
#include "ArcGISTiledMapServiceLayer.h"
wms_layer_sample::wms_layer_sample(const QString& title, const QString& categoryList, const QString& shortDesc, const QString& longDesc, const QString& thumbnail) :
RTSample(title, categoryList, shortDesc, longDesc, thumbnail)
{
}
wms_layer_sample::~wms_layer_sample()
{
}
void wms_layer_sample::run()
{
setRunning();
m_map = new EsriRuntimeQt::Map(this);
m_mapGraphicsView = EsriRuntimeQt::MapGraphicsView::create(m_map, this);
connect(m_map, SIGNAL(mapReady()), this, SLOT(onMapReady()));
// display the map
displaySampleWidget(m_mapGraphicsView);
// add a base layer
QString baseLayerUrl("http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
EsriRuntimeQt::ArcGISTiledMapServiceLayer* worldLayer = new EsriRuntimeQt::ArcGISTiledMapServiceLayer(baseLayerUrl, this);
m_map->addLayer(worldLayer);
// set up the WMS layer creation parameters
QString url = "http://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer";
QStringList initialLayers = QStringList() << "0" << "1" << "2";
// create and add the WMS layer
m_wmsLayer = new EsriRuntimeQt::WmsDynamicMapServiceLayer(url, initialLayers, this);
connect(m_wmsLayer, SIGNAL(layerCreateComplete()), this, SLOT(onLayerCreateComplete()));
m_wmsLayer->setOpacity(0.7);
m_map->addLayer(m_wmsLayer);
}
void wms_layer_sample::onMapReady()
{
m_map->setExtent(EsriRuntimeQt::Envelope(-16145281, 2160905, -6537452, 8569385, m_map->spatialReference()));
}
void wms_layer_sample::onLayerCreateComplete()
{
QWidget* widget = new QWidget();
QVBoxLayout* layout = new QVBoxLayout();
layout->setMargin(2);
QTreeWidget* treeWidget = new QTreeWidget();
QFont font;
font.setPixelSize(15);
treeWidget->setFont(font);
treeWidget->setObjectName("treeWidget");
treeWidget->setStyleSheet("QTreeWidget#treeWidget { background: rgba(0,0,0, 192); color: #ddd; }");
treeWidget->setFrameStyle(QFrame::WinPanel|QFrame::Sunken);
treeWidget->setColumnCount(2);
treeWidget->header()->setVisible(false);
// layer is created, get layers and display
QList<int> ids;
QList<EsriRuntimeQt::WmsLayerInfo*> layerInfos = m_wmsLayer->layers();
foreach (EsriRuntimeQt::WmsLayerInfo* lyr, layerInfos)
{
if (!lyr)
continue;
if (!ids.contains(lyr->layerId()))
{
// Get outer layer
QTreeWidgetItem* item = new QTreeWidgetItem(treeWidget);
item->setText(0, lyr->title());
item->setExpanded(true);
ids.append(lyr->layerId());
foreach (EsriRuntimeQt::WmsLayerInfo* subLayer, lyr->subLayers())
{
if (!subLayer)
continue;
// Get sub-layers and display if not already shown
if (!ids.contains(subLayer->layerId()))
{
QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
subItem->setText(0, subLayer->title());
subItem->setFlags(subItem->flags() | Qt::ItemIsUserCheckable);
subItem->setCheckState(1, subLayer->isVisible() ? Qt::Checked : Qt::Unchecked);
ids.append(subLayer->layerId());
}
}
}
}
treeWidget->resizeColumnToContents(0);
treeWidget->resizeColumnToContents(1);
#ifdef ESRIQTSAMPLE_QT5
treeWidget->header()->setSectionResizeMode(0, QHeaderView::Stretch);
treeWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
#else
treeWidget->header()->setResizeMode(0, QHeaderView::Stretch);
treeWidget->header()->setResizeMode(1, QHeaderView::ResizeToContents);
#endif
treeWidget->header()->setStretchLastSection(false);
layout->addWidget(treeWidget);
widget->setLayout(layout);
widget->resize(150, 105);
connect(treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*,int)), this, SLOT(onItemChanged(QTreeWidgetItem*,int)));
// Add tree widget to the map
QGraphicsProxyWidget* proxy = m_mapGraphicsView->scene()->addWidget(widget);
proxy->setPos(10, 10);
proxy->setOpacity(0.85);
}
void wms_layer_sample::onItemChanged(QTreeWidgetItem* item, int column)
{
if (column == 1)
{
QString title = item->text(0);
foreach (EsriRuntimeQt::WmsLayerInfo* lyr, m_wmsLayer->layers())
{
if (!lyr)
continue;
if (title.compare(lyr->title()) == 0)
{
// Change the visibility of the layer
EsriRuntimeQt::WmsLayerInfo* layerInfo = m_wmsLayer->layer(lyr->name());
if (layerInfo)
{
layerInfo->setVisible(item->checkState(column) == Qt::Checked ? true : false);
}
}
foreach (EsriRuntimeQt::WmsLayerInfo* subLayer, lyr->subLayers())
{
if (!subLayer)
continue;
if (title.compare(subLayer->title()) == 0)
{
// Change the visibility of the sub-layer
EsriRuntimeQt::WmsLayerInfo* subLayerInfo = m_wmsLayer->layer(subLayer->name());
if (subLayerInfo)
{
subLayerInfo->setVisible(item->checkState(column) == Qt::Checked ? true : false);
}
}
}
}
m_wmsLayer->refresh();
}
}