Create and save KML file

View inQMLC++
View on GitHub
Sample viewer app

Construct a KML document and save it as a KMZ file.

screenshot

Use case

If you need to create and save data on the fly, you can use KML to create points, lines, and polygons and then serializing them as KML nodes in a KML Document. Once complete, you can share the KML data with others that are using a KML reading application, such as ArcGIS Earth.

How to use the sample

Application opens to a view of the Southwestern United States. Click on the "Save KMZ file" button to save the active KML document as a .kmz file on your system.

How it works

  1. Create a Point, Polyline, and Polygon.
  2. Create a Graphic for each and add it to the GraphicsOverlay
  3. Create a KmlGeometry object using the Geometry from the point, line, and polygon.
  4. Create a KmlPlacemark for each kml geometry.
  5. Set a KmlStyle for each placemark.
  6. Add all three kml placemarks to the KmlDocument.
  7. Save the kml document to a file using the saveAs function.

Relevant API

  • KmlDocument
  • KmlGeometry
  • KmlNode::saveAs
  • KmlPlacemark
  • KmlStyle
  • KmlIconStyle
  • KmlLineStyle
  • KmlPolygonStyle
  • KmlIcon

Tags

Keyhole, KML, KMZ, OGC

Sample Code

CreateAndSaveKmlFile.cppCreateAndSaveKmlFile.cppCreateAndSaveKmlFile.hCreateAndSaveKmlFile.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// [WriteFile Name=CreateAndSaveKmlFile, Category=Layers]
// [Legal]
// Copyright 2019 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 "CreateAndSaveKmlFile.h"

#include "Map.h"
#include "MapQuickView.h"
#include "PolygonBuilder.h"
#include "PolylineBuilder.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "KmlDocument.h"
#include "KmlPlacemark.h"
#include "KmlNodeListModel.h"
#include "KmlDataset.h"
#include "KmlLayer.h"
#include "KmlLineStyle.h"
#include "KmlIcon.h"
#include "KmlIconStyle.h"
#include "KmlPolygonStyle.h"
#include "KmlStyle.h"

using namespace Esri::ArcGISRuntime;

CreateAndSaveKmlFile::CreateAndSaveKmlFile(QObject* parent /* = nullptr */):
  QObject(parent),
  m_map(new Map(BasemapStyle::ArcGISDarkGray, this)),
  m_kmlDocument(new KmlDocument(this)),
  m_point(createPoint()),
  m_polyline(createPolyline()),
  m_polygon(createPolygon()),
  m_kmlStyleWithPointStyle(createKmlStyleWithPointStyle()),
  m_kmlStyleWithLineStyle(createKmlStyleWithLineStyle()),
  m_kmlStyleWithPolygonStyle(createKmlStyleWithPolygonStyle())
{
  // set initial viewpoint
  m_map->setInitialViewpoint(Viewpoint(createEnvelope()));

  connect(m_map, &Map::doneLoading, this, [this](Error e)
  {
    if (!e.isEmpty())
      return;

    m_kmlDataset = new KmlDataset(m_kmlDocument, this);
    m_kmlLayer = new KmlLayer(m_kmlDataset, this);
    addGraphics();
  });
}

CreateAndSaveKmlFile::~CreateAndSaveKmlFile() = default;

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

MapQuickView* CreateAndSaveKmlFile::mapView() const
{
  return m_mapView;
}

// Set the view (created in QML)
void CreateAndSaveKmlFile::setMapView(MapQuickView* mapView)
{
  if (!mapView || mapView == m_mapView)
    return;

  m_mapView = mapView;
  m_mapView->setMap(m_map);

  emit mapViewChanged();
}

Geometry CreateAndSaveKmlFile::createPoint() const
{
  constexpr double x = -117.195800;
  constexpr double y = 34.056295;
  const SpatialReference spatialRef(4326);

  return Point(x, y, spatialRef);
}

Geometry CreateAndSaveKmlFile::createPolyline() const
{
  const SpatialReference spatialRef(4326);
  PolylineBuilder polylineBuilder(spatialRef);

  polylineBuilder.addPoint(-119.992, 41.989);
  polylineBuilder.addPoint(-119.994, 38.994);
  polylineBuilder.addPoint(-114.620, 35.0);

  return polylineBuilder.toGeometry();
}

Geometry CreateAndSaveKmlFile::createPolygon() const
{
  const SpatialReference spatialRef(4326);
  PolygonBuilder polygonBuilder(spatialRef);

  polygonBuilder.addPoint(-109.048, 40.998);
  polygonBuilder.addPoint(-102.047, 40.998);
  polygonBuilder.addPoint(-102.037, 36.989);
  polygonBuilder.addPoint(-109.048, 36.998);

  return polygonBuilder.toGeometry();
}

Geometry CreateAndSaveKmlFile::createEnvelope() const
{
  constexpr double xMin = -123.0;
  constexpr double yMin = 33.5;
  constexpr double xMax = -101.0;
  constexpr double yMax = 42.0;

  const SpatialReference spatialRef(4326);

  return Envelope(xMin, yMin, xMax, yMax, spatialRef);
}

KmlStyle* CreateAndSaveKmlFile::createKmlStyleWithPointStyle()
{
  // Create Icon Style
  KmlIcon* icon = new KmlIcon(QUrl("https://static.arcgis.com/images/Symbols/Shapes/BlueStarLargeB.png"), this);
  KmlIconStyle* iconStyle = new KmlIconStyle(icon, 1.0, this);

  // Create KmlStyle
  KmlStyle* kmlStyle = new KmlStyle(this);
  kmlStyle->setIconStyle(iconStyle);

  return kmlStyle;
}

KmlStyle* CreateAndSaveKmlFile::createKmlStyleWithLineStyle()
{
  // Create Line Style
  KmlLineStyle* lineStyle = new KmlLineStyle(QColor(Qt::red), 2, this);

  // Create KmlStyle
  KmlStyle* kmlStyle = new KmlStyle(this);
  kmlStyle->setLineStyle(lineStyle);

  return kmlStyle;
}

KmlStyle* CreateAndSaveKmlFile::createKmlStyleWithPolygonStyle()
{
  // Create Polygon Style
  KmlPolygonStyle* polygonStyle = new KmlPolygonStyle(QColor(Qt::yellow), this);

  // Create KmlStyle
  KmlStyle* kmlStyle = new KmlStyle(this);
  kmlStyle->setPolygonStyle(polygonStyle);

  return kmlStyle;
}

void CreateAndSaveKmlFile::addGraphics()
{
  addToKmlDocument(m_point, m_kmlStyleWithPointStyle);
  addToKmlDocument(m_polyline, m_kmlStyleWithLineStyle);
  addToKmlDocument(m_polygon, m_kmlStyleWithPolygonStyle);

  m_mapView->map()->operationalLayers()->append(m_kmlLayer);
}

void CreateAndSaveKmlFile::addToKmlDocument(const Geometry& geometry, KmlStyle* kmlStyle)
{
  const KmlGeometry kmlGeometry = KmlGeometry(geometry, KmlAltitudeMode::ClampToGround);
  KmlPlacemark* placemark = new KmlPlacemark(kmlGeometry, this);
  placemark->setStyle(kmlStyle);
  m_kmlDocument->childNodesListModel()->append(placemark);
}

void CreateAndSaveKmlFile::saveKml(const QUrl& url)
{
  m_busy = true;
  emit busyChanged();
  connect(m_kmlDocument, &KmlDocument::saveAsCompleted, this, [this]()
  {
    m_busy = false;
    emit kmlSaveCompleted();
    emit busyChanged();
  });

  connect(m_kmlDocument, &KmlDocument::errorOccurred, this, [](Error e)
  {
    if (!e.isEmpty())
    {
      qDebug() << QString("Error: %1 - %2").arg(e.message(), e.additionalMessage());
    }
  });
  // Write the KML document to the chosen path.
  m_kmlDocument->saveAs(url.toLocalFile());
}

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