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 generates GroundOverlays with Regions.  The two overlays have
27 // exclusive Lods on their Regions, hence they swap as the viewpoint changes.
28 
29 #include <iostream>
30 #include <string>
31 #include "kml/dom.h"
32 
33 using kmldom::FolderPtr;
34 using kmldom::GroundOverlayPtr;
35 using kmldom::KmlPtr;
36 using kmldom::KmlFactory;
37 using kmldom::LatLonAltBoxPtr;
38 using kmldom::LatLonBoxPtr;
39 using kmldom::LodPtr;
40 using kmldom::RegionPtr;
41 
42 // Declare functions defined in this file.
43 static GroundOverlayPtr CreateGroundOverlay(double north, double south,
44                                             double east, double west,
45                                             const std::string& name,
46                                             const std::string& color);
47 static RegionPtr CreateRegion(double north, double south,
48                               double east, double west,
49                               double minlodpixels, double maxlodpixels);
50 void SwapOverlays();
51 
52 // This creates a Region at the given bounding box with the given Lod range.
CreateRegion(double north,double south,double east,double west,double minlodpixels,double maxlodpixels)53 static RegionPtr CreateRegion(double north, double south,
54                               double east, double west,
55                               double minlodpixels, double maxlodpixels) {
56   KmlFactory* factory = KmlFactory::GetFactory();
57   RegionPtr region = factory->CreateRegion();
58   LatLonAltBoxPtr latlonaltbox = factory->CreateLatLonAltBox();
59   latlonaltbox->set_north(north);
60   latlonaltbox->set_south(south);
61   latlonaltbox->set_east(east);
62   latlonaltbox->set_west(west);
63   LodPtr lod = factory->CreateLod();
64   lod->set_minlodpixels(minlodpixels);
65   lod->set_maxlodpixels(maxlodpixels);
66   region->set_latlonaltbox(latlonaltbox);
67   region->set_lod(lod);
68   return region;
69 }
70 
71 // This creates a GroundOverlay at the given bounding box.
72 // Since there is no Icon (image) a polygon of the given color is drawn.
CreateGroundOverlay(double north,double south,double east,double west,const std::string & name,const std::string & color)73 static GroundOverlayPtr CreateGroundOverlay(double north, double south,
74                                             double east, double west,
75                                             const std::string& name,
76                                             const std::string& color) {
77   KmlFactory* factory = KmlFactory::GetFactory();
78   GroundOverlayPtr groundoverlay = factory->CreateGroundOverlay();
79   groundoverlay->set_name(name);
80   LatLonBoxPtr latlonbox = factory->CreateLatLonBox();
81   latlonbox->set_north(north);
82   latlonbox->set_south(south);
83   latlonbox->set_east(east);
84   latlonbox->set_west(west);
85   groundoverlay->set_latlonbox(latlonbox);
86   groundoverlay->set_color(color);
87   return groundoverlay;
88 }
89 
90 // This uses Regions to make a clean swap between overlays.
91 // Set minLodPixels of one Region to the same as maxLodPixels of the other.
SwapOverlays()92 void SwapOverlays() {
93   KmlFactory* factory = KmlFactory::GetFactory();
94   double north = 10;
95   double south = -10;
96   double east = 10;
97   double west = -10;
98   double lod_a = 128;  // 128 x 128 pixels
99   double lod_b = 512;
100   double lod_c = 1024;
101   std::string solid_red("ff0000ff");  // aabbggrr
102   std::string solid_blue("ffff0000");
103 
104   // Create a solid red GroundOverlay.
105   GroundOverlayPtr red =
106     CreateGroundOverlay(north, south, east, west, "Red", solid_red);
107   // Give it a Region with lod range a - b
108   red->set_region(CreateRegion(north, south, east, west, lod_a, lod_b));
109   // Create a solid blue GroundOverlay.
110   GroundOverlayPtr blue =
111     CreateGroundOverlay(north, south, east, west, "Blue", solid_blue);
112   // Give it a Region with lod range b - c
113   blue->set_region(CreateRegion(north, south, east, west, lod_b, lod_c));
114 
115   FolderPtr folder = factory->CreateFolder();
116   folder->set_name("Swap Overlays");
117   folder->add_feature(red);
118   folder->add_feature(blue);
119 
120   KmlPtr kml = factory->CreateKml();
121   kml->set_feature(folder);
122 
123   std::cout << kmldom::SerializePretty(kml);
124 }
125 
126 
main(int argc,char ** argv)127 int main(int argc, char** argv) {
128   SwapOverlays();
129 }
130