Find features in a sublayer based on attributes and location.
Use case
Sublayers of an ArcGISMapImageLayer
may expose a ServiceFeatureTable
through a table
property. This allows you to perform the same queries available when working with a table from a FeatureLayer
: attribute query, spatial query, statistics query, query for related features, etc. An image layer with a sublayer of counties can be queried by population to only show those above a minimum population.
How to use the sample
Specify a minimum population in the input field (values under 1810000 will produce a selection in all layers) and click the query button to query the sublayers in the current view extent. After a short time, the results for each sublayer will appear as graphics.
How it works
- Create an
ArcGISMapImageLayer
object using the URL of an image service. - After loading the layer, get the sublayer you want to query with
(ArcGISMapImageSublayer) layer::subLayers()::at(index)
. - Load the sublayer, and then get its
ServiceFeatureTable
withsublayer::table()
. - Create
QueryParameters
. You can usequeryParameters::setWhereClause(sqlQueryString)
to query against a table attribute and/or setqueryParameters::setGeometry(extent)
to limit the results to an area of the map. - Call
sublayerTable::queryFeatures(queryParameters)
to get aFeatureQueryResult
with features matching the query. The result is an iterable of features.
Relevant API
- ServiceFeatureTable
- ArcGISMapImageLayer
- ArcGISMapImageLayer::loadTablesAndLayers
- ArcGISMapImageSublayer
- ArcGISMapImageSublayer::table
- QueryParameters
About the data
The ArcGISMapImageLayer
in the map uses the "USA" map service as its data source. This service is hosted by ArcGIS Online, and is composed of four sublayers: "states", "counties", "cities", and "highways". Since the cities
, counties
, and states
tables all have a POP2000
field, they can all execute a query against that attribute and a map extent.
Additional information
An ArcGISMapImageSublayer
must be loaded before accessing its metadata or table. Use ArcGISMapImageLayer::loadTablesAndLayers
to recursively load all sublayers and tables associated with a map image layer. Some sublayers do not have an associated table (group layers, for example) and some may not support specific types of queries. Consult the map service metadata for details.
Tags
Query, Sublayer, MapServer, Table
Sample Code
// [WriteFile Name=QueryMapImageSublayer, Category=Layers]
// [Legal]
// Copyright 2018 Esri.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]
#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD
#include "QueryMapImageSublayer.h"
#include "Basemap.h"
#include "Map.h"
#include "MapQuickView.h"
#include "ArcGISMapImageLayer.h"
#include "ArcGISMapImageSublayer.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "QueryParameters.h"
#include "Graphic.h"
#include "GraphicsOverlay.h"
#include "GraphicListModel.h"
using namespace Esri::ArcGISRuntime;
QueryMapImageSublayer::QueryMapImageSublayer(QQuickItem* parent /* = nullptr */):
QQuickItem(parent)
{
}
void QueryMapImageSublayer::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<QueryMapImageSublayer>("Esri.Samples", 1, 0, "QueryMapImageSublayerSample");
}
void QueryMapImageSublayer::componentComplete()
{
QQuickItem::componentComplete();
// find QML MapView component
m_mapView = findChild<MapQuickView*>("mapView");
// Create a map using the streets vector basemap
m_map = new Map(BasemapStyle::ArcGISStreets, this);
// add map image layer
m_usaImageLayer = new ArcGISMapImageLayer(QUrl("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer"), this);
m_map->operationalLayers()->append(m_usaImageLayer);
// add a graphics overlay
m_selectionOverlay = new GraphicsOverlay(this);
m_mapView->graphicsOverlays()->append(m_selectionOverlay);
// create symbols
createSymbols();
// connect signals
connectSignals();
// set initial viewpoint
Viewpoint vp(Point(-12716000, 4170400, SpatialReference::webMercator()), 6000000 /*scale*/);
m_map->setInitialViewpoint(vp);
// Set map to map view
m_mapView->setMap(m_map);
}
void QueryMapImageSublayer::connectSignals()
{
connect(m_usaImageLayer, &ArcGISMapImageLayer::loadTablesAndLayersCompleted, this, [this](QUuid)
{
ArcGISSublayerListModel* sublayers = m_usaImageLayer->mapImageSublayers();
if (sublayers->size() < 4)
return;
// get the sublayer's tables
m_citiesTable = dynamic_cast<ArcGISMapImageSublayer*>(sublayers->at(0))->table();
m_statesTable = dynamic_cast<ArcGISMapImageSublayer*>(sublayers->at(2))->table();
m_countiesTable = dynamic_cast<ArcGISMapImageSublayer*>(sublayers->at(3))->table();
// connect to city sublayer query signal
connect(m_citiesTable, &ServiceFeatureTable::queryFeaturesCompleted, this, [this](QUuid, FeatureQueryResult* result)
{
addResultsAsGraphics(result, m_citySymbol);
delete result;
});
// connect to county sublayer query signal
connect(m_countiesTable, &ServiceFeatureTable::queryFeaturesCompleted, this, [this](QUuid, FeatureQueryResult* result)
{
addResultsAsGraphics(result, m_countySymbol);
delete result;
});
// connect to states sublayer query signal
connect(m_statesTable, &ServiceFeatureTable::queryFeaturesCompleted, this, [this](QUuid, FeatureQueryResult* result)
{
addResultsAsGraphics(result, m_stateSymbol);
delete result;
});
});
connect(m_usaImageLayer, &ArcGISMapImageLayer::doneLoading, this, [this](Error e)
{
if (!e.isEmpty())
return;
// load the sublayers and tables of the map image layer
m_usaImageLayer->loadTablesAndLayers();
});
}
void QueryMapImageSublayer::createSymbols()
{
// create symbol for city selection
m_citySymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Circle, QColor("red"), 16.0f /*size*/, this);
// create symbol for county selection
SimpleLineSymbol* countyOutline = new SimpleLineSymbol(SimpleLineSymbolStyle::Dash, QColor("cyan"), 2.0f /*width*/, this);
m_countySymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::DiagonalCross, QColor("cyan"), countyOutline, this);
// create symbol for state selection
SimpleLineSymbol* stateOutline = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("darkcyan"), 6.0f /*width*/, this);
m_stateSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Null, QColor("transparent"), stateOutline, this);
}
void QueryMapImageSublayer::addResultsAsGraphics(FeatureQueryResult* result, Symbol* symbol)
{
// get the iterator of features
FeatureIterator iter = result->iterator();
// Create local parent
QObject localParent;
// add a graphic for each feature
while (iter.hasNext())
{
Feature* feat = iter.next(&localParent);
Graphic* graphic = new Graphic(feat->geometry(), symbol, this);
m_selectionOverlay->graphics()->append(graphic);
}
}
void QueryMapImageSublayer::query(const QString& whereClause)
{
if (!m_citiesTable || !m_countiesTable || !m_statesTable || !m_mapView || !m_selectionOverlay)
return;
// clear & delete previous graphics
GraphicListModel* graphics = m_selectionOverlay->graphics();
const int graphicSize = graphics->size();
for (int i = 0; i < graphicSize; i++)
{
delete graphics->at(i);
}
graphics->clear();
// create the parameters
QueryParameters queryParams;
queryParams.setGeometry(m_mapView->currentViewpoint(ViewpointType::BoundingGeometry).targetGeometry().extent());
queryParams.setWhereClause(whereClause);
// query the feature tables
m_citiesTable->queryFeatures(queryParams);
m_countiesTable->queryFeatures(queryParams);
m_statesTable->queryFeatures(queryParams);
}