Display dimension features from a mobile map package.
Use case
Dimensions show specific lengths or distances on a map. A dimension may indicate the length of a side of a building or land parcel, or the distance between two features, such as a fire hydrant and the corner of a building.
How to use the sample
When the sample loads, it will automatically display the map containing dimension features from the mobile map package. The name of the dimension layer containing the dimension features is displayed in the controls box. Control the visibility of the dimension layer with the "Dimension Layer visibility" check box, and apply a definition expression to show dimensions greater than or equal to 450m in length using the "Definition Expression" check box.
Note: the minimum scale range of the sample is set to 1:35000 to maintain readability of the dimension features.
How it works
- Create a
MobileMapPackage
specifying the path to the .mmpk file. - Load the mobile map package.
- After the mmpk successfully loads, get the map from the mmpk
m_map = m_mmpk->maps().at(0)
and add it to the map view:m_mapView->setMap(m_map)
. - Loop through the map's layers to find the
DimensionLayer
and assign it tom_dimensionLayer
. - Set the title of the controls box using
m_dimensionLayer->name()
. - Control the dimension layer's visibility with
m_dimensionLayer->setVisible(boolean)
and set a definition expression withm_dimensionLayer->setDefinitionExpression(String)
.
Relevant API
- DimensionLayer
- MobileMapPackage
About the data
This sample shows a subset of the network of pylons, substations, and power lines around Edinburgh, Scotland within an Edinburgh Pylon Dimensions mobile map package. The data was digitised from satellite imagery and is intended to be used for illustrative purposes only.
Additional information
Dimension layers can be taken offline from a feature service hosted on ArcGIS Enterprise 10.9 or later, using the GeodatabaseSyncTask. Dimension layers are also supported in mobile map packages or mobile geodatabases created in ArcGIS Pro 2.9 or later.
Tags
definition expression, dimension, distance, layer, length, mmpk, mobile map package, utility
Sample Code
// [WriteFile Name=DisplayDimensions, Category=Layers]
// [Legal]
// Copyright 2021 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 "DisplayDimensions.h"
#include "DimensionLayer.h"
#include "Map.h"
#include "MapQuickView.h"
#include "MobileMapPackage.h"
#include <QDir>
#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS
using namespace Esri::ArcGISRuntime;
namespace
{
// Helper method to get cross platform data path.
QString defaultDataPath()
{
QString dataPath;
#ifdef Q_OS_ANDROID
dataPath = "/sdcard";
#elif defined Q_OS_IOS
dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
#else
dataPath = QDir::homePath();
#endif
return dataPath;
}
// Sample MMPK location.
const QString edinburghPylonFilePath {"/ArcGIS/Runtime/Data/mmpk/Edinburgh_Pylon_Dimensions.mmpk"};
}
DisplayDimensions::DisplayDimensions(QObject* parent /* = nullptr */):
QObject(parent)
{
QString mapPackagePath = defaultDataPath() + edinburghPylonFilePath;
m_mmpk = new MobileMapPackage(mapPackagePath, this);
// Make connections between the mmpk's doneLoading and errorOccurred signal and the addMapToMapView and handleError methods respectively.
connect(m_mmpk, &MobileMapPackage::doneLoading, this, &DisplayDimensions::addMapToMapView);
connect(m_mmpk, &MobileMapPackage::errorOccurred, this, &DisplayDimensions::handleError);
// Load the mmpk.
m_mmpk->load();
}
DisplayDimensions::~DisplayDimensions() = default;
void DisplayDimensions::init()
{
// Register the map view for QML
qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
qmlRegisterType<DisplayDimensions>("Esri.Samples", 1, 0, "DisplayDimensionsSample");
}
void DisplayDimensions::addMapToMapView(const Error& error)
{
// If no errors have occurred, the mmpk is loaded, and there is only one map in the mmpk, the map
// in the mmpk can be assigned to the MapView. Any errors will be managed by the handleError method.
if (error.isEmpty() && m_mmpk->loadStatus() == LoadStatus::Loaded && m_mmpk->maps().count() > 0)
{
// Enable the checkboxes.
setDimensionsAvailable(true);
// Assign the map in the mmpk to m_map.
m_map = m_mmpk->maps().at(0);
// Set the minimum scale to prevent zooming out too far.
m_map->setMinScale(35000);
// Check to ensure that m_mapView has been initialised properly.
if (m_mapView != nullptr)
{
// Set m_map as the MapView's map.
m_mapView->setMap(m_map);
}
// From the map's layers, find the Dimension Layer.
findDimensionLayer();
}
else
{
// If the map hasn't loaded or an error has occurred, disable the checkboxes in the UI.
setDimensionsAvailable(false);
}
}
void DisplayDimensions::findDimensionLayer()
{
LayerListModel* layers = m_mapView->map()->operationalLayers();
for (Layer* layer : *layers)
{
if (layer->layerType() == LayerType::DimensionLayer)
{
// The current layer, which is the DimensionLayer, has type Layer*. Ths needs to be converted to a DimensionLayer*.
m_dimensionLayer = static_cast<DimensionLayer*>(layer);
// Use the name of the Dimension Layer to define the title of the UI element.
setDimensionLayerName(m_dimensionLayer->name());
// There is only one Dimension Layer, so we can break out of the loop.
break;
}
}
}
void DisplayDimensions::handleError(const Error& error)
{
if (error.additionalMessage().isEmpty())
setErrorMessage(error.message());
else
setErrorMessage(error.message() + "\n" + error.additionalMessage());
}
MapQuickView* DisplayDimensions::mapView() const
{
return m_mapView;
}
// Set the view (created in QML)
void DisplayDimensions::setMapView(MapQuickView* mapView)
{
if (!mapView || mapView == m_mapView)
return;
m_mapView = mapView;
if (m_map)
m_mapView->setMap(m_map);
emit mapViewChanged();
}
QString DisplayDimensions::errorMessage()
{
return m_errorMessage;
}
void DisplayDimensions::setErrorMessage(const QString& message)
{
m_errorMessage = message;
emit errorMessageChanged();
}
QString DisplayDimensions::dimensionLayerName() const
{
return m_dimensionLayerName;
}
void DisplayDimensions::setDimensionLayerName(const QString& name)
{
m_dimensionLayerName = name;
emit dimensionLayerNameChanged();
}
bool DisplayDimensions::dimensionLayerVisible() const
{
return m_dimensionLayer && m_dimensionLayer->isVisible();
}
void DisplayDimensions::setDimensionLayerVisible(bool visible)
{
m_dimensionLayer->setVisible(visible);
}
bool DisplayDimensions::useDefinitionExpression() const
{
return m_dimensionLayer && !m_dimensionLayer->definitionExpression().isEmpty();
}
void DisplayDimensions::setUseDefinitionExpression(bool applied)
{
if (applied)
m_dimensionLayer->setDefinitionExpression("DIMLENGTH >= 450");
else
m_dimensionLayer->setDefinitionExpression("");
}
bool DisplayDimensions::dimensionsAvailable()
{
return m_dimensionsAvailable;
}
void DisplayDimensions::setDimensionsAvailable(bool status)
{
m_dimensionsAvailable = status;
emit dimensionsAvailableChanged();
}