1 /* -*-c++-*- */
2 /* osgEarth - Geospatial SDK for OpenSceneGraph
3 * Copyright 2019 Pelican Mapping
4 * http://osgearth.org
5 *
6 * osgEarth is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
16 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
17 * IN THE SOFTWARE.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21 */
22 
23 #include <osg/Notify>
24 #include <osgGA/StateSetManipulator>
25 #include <osgViewer/Viewer>
26 #include <osgViewer/ViewerEventHandlers>
27 
28 #include <osgEarth/MapNode>
29 #include <osgEarthUtil/EarthManipulator>
30 #include <osgEarthUtil/MouseCoordsTool>
31 #include <osgEarthUtil/MGRSFormatter>
32 #include <osgEarthUtil/LatLongFormatter>
33 
34 #include <osgEarthUtil/GeodeticGraticule>
35 #include <osgEarthUtil/MGRSGraticule>
36 #include <osgEarthUtil/UTMGraticule>
37 #include <osgEarthUtil/GARSGraticule>
38 
39 using namespace osgEarth::Util;
40 using namespace osgEarth::Annotation;
41 
42 int
usage(const std::string & msg)43 usage( const std::string& msg )
44 {
45     OE_NOTICE
46         << msg << std::endl
47         << "USAGE: osgearth_graticule [options] file.earth" << std::endl
48         << "   --geodetic            : display a Lat/Long graticule" << std::endl
49         << "   --utm                 : display a UTM graticule" << std::endl
50         << "   --mgrs                : display an MGRS graticule" << std::endl
51         << "   --gars                : display a GARS graticule" << std::endl;
52     return -1;
53 }
54 
55 //------------------------------------------------------------------------
56 
57 int
main(int argc,char ** argv)58 main(int argc, char** argv)
59 {
60     osg::ArgumentParser arguments(&argc,argv);
61     osgViewer::Viewer viewer(arguments);
62 
63     // parse command line:
64     bool isUTM = arguments.read("--utm");
65     bool isMGRS = arguments.read("--mgrs");
66     bool isGeodetic = arguments.read("--geodetic");
67     bool isGARS = arguments.read("--gars");
68 
69     // load the .earth file from the command line.
70     MapNode* mapNode = MapNode::load( arguments );
71     if ( !mapNode )
72         return usage( "Failed to load a map from the .earth file" );
73 
74     // install our manipulator:
75     viewer.setCameraManipulator( new EarthManipulator() );
76 
77     // root scene graph:
78     osg::Group* root = new osg::Group();
79     root->addChild( mapNode );
80 
81     Formatter* formatter = 0L;
82     if ( isUTM )
83     {
84         UTMGraticule* gr = new UTMGraticule();
85         mapNode->getMap()->addLayer(gr);
86         formatter = new MGRSFormatter();
87     }
88     else if ( isMGRS )
89     {
90         MGRSGraticule* gr = new MGRSGraticule();
91         mapNode->getMap()->addLayer(gr);
92         formatter = new MGRSFormatter();
93     }
94     else if ( isGARS )
95     {
96         GARSGraticule* gr = new GARSGraticule();
97         mapNode->getMap()->addLayer(gr);
98         formatter = new LatLongFormatter();
99     }
100     else // if ( isGeodetic )
101     {
102         GeodeticGraticule* gr = new GeodeticGraticule();
103         mapNode->getMap()->addLayer(gr);
104         formatter = new LatLongFormatter();
105     }
106 
107     // mouse coordinate readout:
108     ControlCanvas* canvas = new ControlCanvas();
109     root->addChild( canvas );
110     VBox* vbox = new VBox();
111     canvas->addControl( vbox );
112 
113     LabelControl* readout = new LabelControl();
114     vbox->addControl( readout );
115 
116     MouseCoordsTool* tool = new MouseCoordsTool( mapNode );
117     tool->addCallback( new MouseCoordsLabelCallback(readout, formatter) );
118     viewer.addEventHandler( tool );
119 
120 
121     // disable the small-feature culling
122     viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);
123 
124     // set a near/far ratio that is smaller than the default. This allows us to get
125     // closer to the ground without near clipping. If you need more, use --logdepth
126     viewer.getCamera()->setNearFarRatio(0.0001);
127 
128 
129     // finalize setup and run.
130     viewer.setSceneData( root );
131     viewer.addEventHandler(new osgViewer::StatsHandler());
132     viewer.addEventHandler(new osgViewer::WindowSizeHandler());
133     viewer.addEventHandler(new osgViewer::ThreadingHandler());
134     viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
135     return viewer.run();
136 }
137