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 shows basic use of KML Engine KmlFile::CreateFromImport().
27 // KmlFile's GetObjectById() provides an efficient and convenient means to find
28 // an element by its XML ID.
29 
30 #include <iostream>
31 #include "kml/dom.h"
32 #include "kml/engine.h"
33 
34 using kmldom::FolderPtr;
35 using kmldom::KmlFactory;
36 using kmldom::KmlPtr;
37 using kmldom::PlacemarkPtr;
38 using kmlengine::KmlFile;
39 using kmlengine::KmlFilePtr;
40 
HelloKmlFileCreateFromImport()41 static void HelloKmlFileCreateFromImport() {
42   // This is the same KML as in the kmlfile.cc example.
43   KmlFactory* kml_factory = KmlFactory::GetFactory();
44   FolderPtr folder = kml_factory->CreateFolder();
45   folder->set_id("f0");
46   folder->set_name("Folder 0");
47   PlacemarkPtr placemark = kml_factory->CreatePlacemark();
48   placemark->set_id("pm0");
49   placemark->set_name("Placemark 0");
50   folder->add_feature(placemark);
51   placemark = kml_factory->CreatePlacemark();
52   placemark->set_id("pm1");
53   placemark->set_name("Placemark 1");
54   folder->add_feature(placemark);
55   KmlPtr kml = kml_factory->CreateKml();
56   kml->set_feature(folder);
57 
58   // Importing to a KmlFile creates an internal database of object id mappings.
59   KmlFilePtr kml_file = KmlFile::CreateFromImport(kml);
60 
61   // Access the 3 Features by their XML Id.  Note that GetObjectById() returns
62   // a ObjectPtr, hence the use of the cast to access <name>.
63   FolderPtr folder0 = AsFolder(kml_file->GetObjectById("f0"));
64   std::cout << "Folder 0 name: " << folder0->get_name() << std::endl;
65   PlacemarkPtr placemark0 = AsPlacemark(kml_file->GetObjectById("pm0"));
66   std::cout << "Placemark 0 name: " << placemark0->get_name() << std::endl;
67   PlacemarkPtr placemark1 = AsPlacemark(kml_file->GetObjectById("pm1"));
68   std::cout << "Placemark 1 name: " << placemark1->get_name() << std::endl;
69 
70   // KmlFile's serialize defaults to the OGC KML 2.2 XML namespace and adds
71   // an XML header specifying UTF-8 encoding.
72   std::string xml;
73   kml_file->SerializeToString(&xml);
74   std::cout << xml;
75 }
76 
main(int argc,char ** argv)77 int main(int argc, char** argv) {
78   HelloKmlFileCreateFromImport();
79   return 0;
80 }
81