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  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>
18  */
19 
20 #include <osgEarthFeatures/Session>
21 #include <osgEarthFeatures/Script>
22 #include <osgEarthFeatures/ScriptEngine>
23 #include <osgEarthFeatures/FeatureSource>
24 
25 #include <osgEarthSymbology/ResourceCache>
26 #include <osgEarthSymbology/StyleSheet>
27 
28 #include <osgEarth/FileUtils>
29 #include <osgEarth/StringUtils>
30 #include <osgEarth/Registry>
31 
32 #define LC "[Session] "
33 
34 using namespace osgEarth;
35 using namespace osgEarth::Features;
36 
37 //---------------------------------------------------------------------------
38 
Session()39 Session::Session() :
40 osg::Object(),
41 _map(0L),
42 _mapInfo(0L)
43 {
44     init();
45 }
46 
Session(const Map * map)47 Session::Session(const Map* map) :
48 osg::Object(),
49 _map(map),
50 _mapInfo(map)
51 {
52     init();
53 }
54 
Session(const Map * map,StyleSheet * styles)55 Session::Session(const Map* map, StyleSheet* styles) :
56 osg::Object(),
57 _map(map),
58 _mapInfo(map),
59 _styles(styles)
60 {
61     init();
62 }
63 
Session(const Map * map,StyleSheet * styles,FeatureSource * source,const osgDB::Options * dbOptions)64 Session::Session(const Map* map, StyleSheet* styles, FeatureSource* source, const osgDB::Options* dbOptions) :
65 osg::Object(),
66 _map(map),
67 _mapInfo(map),
68 _styles(styles),
69 _featureSource(source),
70 _dbOptions(dbOptions)
71 {
72     init();
73 }
74 
Session(const Session & rhs,const osg::CopyOp & op)75 Session::Session(const Session& rhs, const osg::CopyOp& op) :
76 osg::Object(rhs, op),
77 _map(rhs._map.get()),
78 _mapInfo(rhs._mapInfo)
79 {
80     //nop
81 }
82 
83 
84 void
init()85 Session::init()
86 {
87     setStyles(_styles.get());
88 
89     // A new cache to optimize state changes. Since the cache lives in the Session, any
90     // geometry created under this session takes advantage of it. That's reasonable since
91     // tiles in a particular "layer" will tend to share state.
92     _stateSetCache = new StateSetCache();
93 
94     _name = "Session (unnamed)";
95 }
96 
~Session()97 Session::~Session()
98 {
99     //nop
100 }
101 
102 const osgDB::Options*
getDBOptions() const103 Session::getDBOptions() const
104 {
105     // local options if they were set:
106     if (_dbOptions.valid())
107         return _dbOptions.get();
108 
109     // otherwise get them from the map if possible:
110     osg::ref_ptr<const Map> map;
111     if (_map.lock(map))
112         return map->getReadOptions();
113 
114     return 0L;
115 }
116 
117 void
setResourceCache(ResourceCache * cache)118 Session::setResourceCache(ResourceCache* cache)
119 {
120     _resourceCache = cache;
121 }
122 
123 ResourceCache*
getResourceCache()124 Session::getResourceCache()
125 {
126     return _resourceCache.get();
127 }
128 
129 osg::ref_ptr<const Map>
getMap() const130 Session::getMap() const
131 {
132     osg::ref_ptr<const Map> map;
133     _map.lock(map);
134     return map;
135 }
136 
137 const SpatialReference*
getMapSRS() const138 Session::getMapSRS() const
139 {
140     return _mapInfo.getSRS();
141 }
142 
143 StateSetCache*
getStateSetCache()144 Session::getStateSetCache()
145 {
146     return _stateSetCache.get();
147 }
148 
149 void
setStyles(StyleSheet * value)150 Session::setStyles( StyleSheet* value )
151 {
152     _styles = value ? value : new StyleSheet();
153     initScriptEngine();
154 }
155 
156 void
initScriptEngine()157 Session::initScriptEngine()
158 {
159     _styleScriptEngine = 0L;
160 
161     // Create a script engine for the StyleSheet
162     if (_styles)
163     {
164         if (_styles->script())
165         {
166             _styleScriptEngine = ScriptEngineFactory::createWithProfile(
167                 Script(
168                     _styles->script()->code,
169                     _styles->script()->language,
170                     _styles->script()->name ),
171                 _styles->script()->profile );
172         }
173         else
174         {
175             // If the stylesheet has no script set, create a default JS engine
176             // This enables the use of "inline" scripting in StringExpression
177             // and NumericExpression style values.
178             _styleScriptEngine = ScriptEngineFactory::create("javascript", "", true);
179         }
180     }
181 }
182 
183 ScriptEngine*
getScriptEngine() const184 Session::getScriptEngine() const
185 {
186     return _styleScriptEngine.get();
187 }
188 
189 void
setFeatureSource(FeatureSource * fs)190 Session::setFeatureSource(FeatureSource* fs)
191 {
192     _featureSource = fs;
193 }
194 
195 FeatureSource*
getFeatureSource() const196 Session::getFeatureSource() const
197 {
198     return _featureSource.get();
199 }
200