Symbolize a shapefile

View inC++QMLView on GitHubSample viewer app

Display a shapefile with custom symbology.

screenshot

Use case

Feature layers created from shapefiles do not possess any rendering information, and will be assigned with a default symbology. You can apply custom styling to ensure that the content is visible and usable in the context of a specific map. For example, you could use this to visually differentiate between features originating from two different shapefiles, by applying a blue color to one, and a red color to the other.

How to use the sample

Click the button to apply a new symbology renderer to the feature layer created from the shapefile.

How it works

  1. Create a ShapefileFeatureTable, providing the path to a shapefile.
  2. Create a FeatureLayer and associate it with the ShapeFileFeatureTable.
  3. Create a SimpleRenderer to override the default symbology. The simple renderer takes a symbol and applies that to all features in a layer.
  4. Apply the renderer to the FeatureLayer by setting the renderer.

Relevant API

  • FeatureLayer
  • ShapefileFeatureTable
  • SimpleFillSymbol
  • SimpleLineSymbol
  • SimpleRenderer

Offline Data

To set up the sample's offline data, see the Use offline data in the samples section of the Qt Samples repository overview.

Link Local Location
Aurora Subdivions Shapefile <userhome>/ArcGIS/Runtime/Data/shp/Subdivisions.shp

About the data

This sample displays a shapefile containing subdivisions in Aurora, CO.

Additional information

While shapefiles contain no rendering information, other data sources such as Service Feature Tables or Geodatabase Feature Tables can contain such information. As a result, the rendering properties of the other data sources can be pre-defined by the author.

Tags

package, shape file, shapefile, symbology, visualization

Sample Code

SymbolizeShapefile.cppSymbolizeShapefile.cppSymbolizeShapefile.hSymbolizeShapefile.qml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// [WriteFile Name=SymbolizeShapefile, Category=DisplayInformation]
// [Legal]
// Copyright 2017 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 "SymbolizeShapefile.h"

#include "Map.h"
#include "MapQuickView.h"
#include "ShapefileFeatureTable.h"
#include "FeatureLayer.h"
#include "SimpleRenderer.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"

#include <QUrl>
#include <QDir>
#include <QtCore/qglobal.h>

#ifdef Q_OS_IOS
#include <QStandardPaths>
#endif // Q_OS_IOS

using namespace Esri::ArcGISRuntime;

// helper method to get cross platform data path
namespace
{
  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;
  }
} // namespace

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

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

void SymbolizeShapefile::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);

  // Set an initial viewpoint
  const Point pt(-11662054, 4818336, SpatialReference(3857));
  const Viewpoint vp(pt, 200000);
  m_map->setInitialViewpoint(vp);

  // Create the ShapefileFeatureTable
  const QString dataPath = defaultDataPath() + "/ArcGIS/Runtime/Data/shp";
  ShapefileFeatureTable* shp = new ShapefileFeatureTable(dataPath + "/Subdivisions.shp", this);

  // Create the FeatureLayer
  m_featureLayer = new FeatureLayer(shp, this);
  m_map->operationalLayers()->append(m_featureLayer);

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

void SymbolizeShapefile::updateRenderer()
{
  // Create the Symbol
  SimpleFillSymbol* sfs = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor("yellow"), this);
  SimpleLineSymbol* sls = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("red"), 1.0f, this);
  sfs->setOutline(sls);

  // Create the Renderer
  SimpleRenderer* simpleRenderer = new SimpleRenderer(sfs, this);

  // Set the Renderer on the Layer
  m_featureLayer->setRenderer(simpleRenderer);
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.