1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 //  1. Redistributions of source code must retain the above copyright notice,
7 //     this list of conditions and the following disclaimer.
8 //  2. Redistributions in binary form must reproduce the above copyright notice,
9 //     this list of conditions and the following disclaimer in the documentation
10 //     and/or other materials provided with the distribution.
11 //  3. Neither the name of Google Inc. nor the names of its contributors may be
12 //     used to endorse or promote products derived from this software without
13 //     specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // This program makes some basic use of the KML DOM API.
27 
28 #include <assert.h>
29 #include <iostream>
30 #include "kml/dom.h"
31 
32 using std::cout;
33 using std::endl;
34 using kmldom::CoordinatesPtr;
35 using kmldom::KmlFactory;
36 using kmldom::PlacemarkPtr;
37 using kmldom::PointPtr;
38 
HelloKml(bool verbose)39 void HelloKml(bool verbose) {
40   KmlFactory* factory(KmlFactory::GetFactory());
41   // <coordinates>
42   CoordinatesPtr coordinates(factory->CreateCoordinates());
43   coordinates->add_latlngalt(37.123, -122.456, 314.159);
44   // <Point><coordinates>...
45   PointPtr point(factory->CreatePoint());
46   point->set_coordinates(coordinates);
47   // <Point><altitudeMode>...<coordinates>...
48   point->set_altitudemode(kmldom::ALTITUDEMODE_RELATIVETOGROUND);
49   assert(point->get_altitudemode() == kmldom::ALTITUDEMODE_RELATIVETOGROUND);
50   // <Placemark><Point><coordinates>...
51   PlacemarkPtr placemark(factory->CreatePlacemark());
52   placemark->set_geometry(point);
53 
54   // A Placemark is (duh) a Placemark
55   assert (placemark->Type() == kmldom::Type_Placemark);
56   // It's also a Feature.
57   assert(placemark->IsA(kmldom::Type_Feature));
58   placemark->set_name("point placemark");
59   if (verbose) {
60     cout << "Placemark's name is " << placemark->get_name() << endl;
61   }
62   // We know it has some geometry.
63   assert(placemark->has_geometry());
64   // And we can test to see if that geometry is a Point.
65   assert(placemark->get_geometry()->IsA(kmldom::Type_Point));
66   // If it is, we can make a point from it. (Yes, API should hide casting.)
67   const PointPtr pt = kmldom::AsPoint(placemark->get_geometry());
68   assert(pt->get_altitudemode() == kmldom::ALTITUDEMODE_RELATIVETOGROUND);
69   if (verbose) {
70     cout.precision(6);
71     cout << placemark->get_name() << " is located at: ";
72     cout << pt->get_coordinates()->get_coordinates_array_at(0).get_latitude()
73          << ", ";
74     cout << pt->get_coordinates()->get_coordinates_array_at(0).get_longitude()
75          << endl;
76   }
77 
78   // All storage is freed by smart pointers as they go out of scope.
79 }
80 
main(int argc,char ** argv)81 int main(int argc, char** argv) {
82   HelloKml(argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v');
83   return 0;
84 }
85