File: hellogeometry.cc

package info (click to toggle)
libkml 1.3.0-13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,660 kB
  • sloc: cpp: 48,088; python: 2,008; xml: 1,806; ansic: 1,766; php: 223; java: 195; ruby: 109; perl: 108; sh: 42; makefile: 17
file content (235 lines) | stat: -rw-r--r-- 8,332 bytes parent folder | download | duplicates (6)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2008, Google Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//  2. Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//  3. Neither the name of Google Inc. nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// This file contains some sample code for Geometry elements:
// Point, LineString, LinearRing, Polygon, MultiGeometry

#include <iostream>
#include <sstream>
#include "kml/dom.h"

using std::stringstream;
using kmldom::CoordinatesPtr;
using kmldom::FolderPtr;
using kmldom::InnerBoundaryIsPtr;
using kmldom::KmlPtr;
using kmldom::KmlFactory;
using kmldom::LinearRingPtr;
using kmldom::LineStringPtr;
using kmldom::MultiGeometryPtr;
using kmldom::OuterBoundaryIsPtr;
using kmldom::PlacemarkPtr;
using kmldom::PointPtr;
using kmldom::PolygonPtr;

static void CreateGeometry(bool verbose);
template<int N> static LinearRingPtr CreateBoundary(const double (&corners)[N]);
static PlacemarkPtr CreateLineStringPlacemark();
static PlacemarkPtr CreateMultiGeometryPlacemark();
static PointPtr Create2dPoint(int id, double longitude, double latitude);
static PlacemarkPtr CreatePointPlacemark();
static PlacemarkPtr CreateSimplePolygon();
static PlacemarkPtr CreateTwoHolePolygon();

static PlacemarkPtr CreatePointPlacemark() {
  KmlFactory* factory = KmlFactory::GetFactory();

  CoordinatesPtr coordinates = factory->CreateCoordinates();
  coordinates->add_latlng(36.36, -123.32);

  PointPtr point = factory->CreatePoint();
  point->set_coordinates(coordinates);

  PlacemarkPtr placemark = factory->CreatePlacemark();
  placemark->set_name("Point");
  placemark->set_geometry(point);

  return placemark;
}

static PlacemarkPtr CreateLineStringPlacemark() {
  KmlFactory* factory = KmlFactory::GetFactory();

  CoordinatesPtr coordinates = factory->CreateCoordinates();
  coordinates->add_latlngalt(38.38, -121.12, 123.456);
  coordinates->add_latlngalt(37.37, -122.22, 122.345);
  coordinates->add_latlngalt(36.36, -123.32, 121.234);
  coordinates->add_latlng(36.36, -123.32);

  LineStringPtr linestring = factory->CreateLineString();
  linestring->set_extrude(true);
  linestring->set_tessellate(true);
  linestring->set_altitudemode(kmldom::ALTITUDEMODE_ABSOLUTE);
  linestring->set_coordinates(coordinates);

  PlacemarkPtr placemark = factory->CreatePlacemark();
  placemark->set_geometry(linestring);
  placemark->set_name("LineString");

  return placemark;
}

static PlacemarkPtr CreateSimplePolygon() {
  KmlFactory* factory = KmlFactory::GetFactory();

  CoordinatesPtr coordinates = factory->CreateCoordinates();
  coordinates->add_latlngalt(38.38, -121.12, 123.456);
  coordinates->add_latlngalt(37.37, -122.22, 122.345);
  coordinates->add_latlngalt(36.36, -123.32, 121.234);
  coordinates->add_latlngalt(38.38, -121.12, 123.456);

  LinearRingPtr linearring = factory->CreateLinearRing();
  linearring->set_coordinates(coordinates);

  OuterBoundaryIsPtr outerboundaryis = factory->CreateOuterBoundaryIs();
  outerboundaryis->set_linearring(linearring);

  PolygonPtr polygon = factory->CreatePolygon();
  polygon->set_tessellate(true);
  polygon->set_altitudemode(kmldom::ALTITUDEMODE_RELATIVETOGROUND);
  polygon->set_outerboundaryis(outerboundaryis);

  PlacemarkPtr placemark = factory->CreatePlacemark();
  placemark->set_name("Simple Polygon");
  placemark->set_geometry(polygon);

  return placemark;
}

// Corners presumed to be the corners of the ring.
template<int N>
static LinearRingPtr CreateBoundary(const double (&corners)[N]) {
  KmlFactory* factory = KmlFactory::GetFactory();
  CoordinatesPtr coordinates = factory->CreateCoordinates();
  for (int i = 0; i < N; i+=2) {
    coordinates->add_latlng(corners[i], corners[i+1]);
  }
  // Last must be the same as first in a LinearRing.
  coordinates->add_latlng(corners[0], corners[1]);

  LinearRingPtr linearring = factory->CreateLinearRing();
  linearring->set_coordinates(coordinates);
  return linearring;
}

static PlacemarkPtr CreateTwoHolePolygon() {
  KmlFactory* factory = KmlFactory::GetFactory();
  PlacemarkPtr placemark = factory->CreatePlacemark();
  placemark->set_name("Polygon with 2 holes");

  PolygonPtr polygon = factory->CreatePolygon();

  const double outer_corners[] = {
    37.80198570954779,-122.4319382787491,
    37.80166118304026,-122.4318730681021,
    37.8017138829201,-122.4314979385389,
    37.80202995372478,-122.4315644851293
  };

  const double inner_west_corners[] = {
    37.80193182195953,-122.4318860160652,
    37.80172085332186,-122.4318421173957,
    37.80173660441158,-122.4317194993788,
    37.8019475540673,-122.4317632243884
  };

  const double inner_east_corners[] = {
    37.80195363606838,-122.4317133795781,
    37.80174288373532,-122.4316718851426,
    37.8017586463012,-122.4315492963515,
    37.80196848593953,-122.4315926416269
  };
  OuterBoundaryIsPtr outerboundaryis = factory->CreateOuterBoundaryIs();
  outerboundaryis->set_linearring(CreateBoundary(outer_corners));
  polygon->set_outerboundaryis(outerboundaryis);

  InnerBoundaryIsPtr innerboundaryis = factory->CreateInnerBoundaryIs();
  innerboundaryis->set_linearring(CreateBoundary(inner_west_corners));
  polygon->add_innerboundaryis(innerboundaryis);

  innerboundaryis = factory->CreateInnerBoundaryIs();
  innerboundaryis->set_linearring(CreateBoundary(inner_east_corners));
  polygon->add_innerboundaryis(innerboundaryis);

  placemark->set_geometry(polygon);

  return placemark;
}

static PointPtr Create2dPoint(int id, double longitude, double latitude) {
  KmlFactory* factory = KmlFactory::GetFactory();

  CoordinatesPtr coordinates = factory->CreateCoordinates();
  coordinates->add_latlng(longitude, latitude);

  PointPtr point = factory->CreatePoint();
  stringstream ss;
  ss << "pt_" << id;
  point->set_id(ss.str());
  point->set_coordinates(coordinates);
  return point;
}

static PlacemarkPtr CreateMultiGeometryPlacemark() {
  KmlFactory* factory = KmlFactory::GetFactory();

  PlacemarkPtr placemark = factory->CreatePlacemark();
  placemark->set_name("MultiGeometry");
  placemark->set_id("pmmg");
  MultiGeometryPtr multigeometry = factory->CreateMultiGeometry();
  placemark->set_geometry(multigeometry);

  const int num_points = 3;
  for (int i = 0; i < num_points; ++i) {
    multigeometry->add_geometry(Create2dPoint(-120 + i, i, 37 + i));
  }

  return placemark;
}

static void CreateGeometry(bool verbose) {
  KmlFactory* factory = KmlFactory::GetFactory();

  FolderPtr folder = factory->CreateFolder();
  folder->add_feature(CreatePointPlacemark());
  folder->add_feature(CreateLineStringPlacemark());
  folder->add_feature(CreateSimplePolygon());
  folder->add_feature(CreateTwoHolePolygon());
  folder->add_feature(CreateMultiGeometryPlacemark());

  KmlPtr kml = factory->CreateKml();
  kml->set_feature(folder);

  if (verbose) {
    std::cout << kmldom::SerializePretty(kml);
  }
}

int main(int argc, char** argv) {
  // Silent by default.
  bool verbose = argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v';
  CreateGeometry(verbose);
  return 0;  // All is well if we got this far.
}