1 // A group of 2D Canvas elements which get automatically transformed according
2 // to the map parameters.
4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
11 // This library 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 GNU
14 // Library General Public License for more details.
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "CanvasMap.hxx"
21 #include "map/geo_node_pair.hxx"
22 #include "map/projection.hxx"
26 #include <boost/algorithm/string/predicate.hpp>
28 #define LOG_GEO_RET(msg) \
34 msg << " (" << child->getStringValue()\
35 << ", " << child->getPath() << ")"\
45 const std::string GEO = "-geo";
47 //----------------------------------------------------------------------------
48 Map::Map( const CanvasWeakPtr& canvas,
49 SGPropertyNode_ptr node,
50 const Style& parent_style ):
51 Group(canvas, node, parent_style),
52 // TODO make projection configurable
53 _projection(new SansonFlamsteedProjection),
54 _projection_dirty(true)
59 //----------------------------------------------------------------------------
65 //----------------------------------------------------------------------------
66 void Map::update(double dt)
68 for( GeoNodes::iterator it = _geo_nodes.begin();
69 it != _geo_nodes.end();
72 GeoNodePair* geo_node = it->second.get();
73 if( !geo_node->isComplete()
74 || (!geo_node->isDirty() && !_projection_dirty) )
77 GeoCoord lat = parseGeoCoord(geo_node->getLat());
78 if( lat.type != GeoCoord::LATITUDE )
81 GeoCoord lon = parseGeoCoord(geo_node->getLon());
82 if( lon.type != GeoCoord::LONGITUDE )
85 Projection::ScreenPosition pos =
86 _projection->worldToScreen(lat.value, lon.value);
88 geo_node->setScreenPos(pos.x, pos.y);
91 geo_node->setDirty(false);
93 _projection_dirty = false;
98 //----------------------------------------------------------------------------
99 void Map::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
101 if( !boost::ends_with(child->getNameString(), GEO) )
102 return Element::childAdded(parent, child);
104 _geo_nodes[child].reset(new GeoNodePair());
107 //----------------------------------------------------------------------------
108 void Map::childRemoved(SGPropertyNode* parent, SGPropertyNode* child)
110 if( !boost::ends_with(child->getNameString(), GEO) )
111 return Element::childRemoved(parent, child);
113 // TODO remove from other node
114 _geo_nodes.erase(child);
117 //----------------------------------------------------------------------------
118 void Map::valueChanged(SGPropertyNode * child)
120 const std::string& name = child->getNameString();
122 if( !boost::ends_with(name, GEO) )
123 return Group::valueChanged(child);
125 GeoNodes::iterator it_geo_node = _geo_nodes.find(child);
126 if( it_geo_node == _geo_nodes.end() )
127 LOG_GEO_RET("geo node not found!")
128 GeoNodePair* geo_node = it_geo_node->second.get();
130 geo_node->setDirty();
132 if( geo_node->getStatus() & GeoNodePair::INCOMPLETE )
134 // Detect lat, lon tuples...
135 GeoCoord coord = parseGeoCoord(child->getStringValue());
136 int index_other = -1;
140 case GeoCoord::LATITUDE:
141 index_other = child->getIndex() + 1;
142 geo_node->setNodeLat(child);
144 case GeoCoord::LONGITUDE:
145 index_other = child->getIndex() - 1;
146 geo_node->setNodeLon(child);
149 LOG_GEO_RET("Invalid geo coord")
152 SGPropertyNode *other = child->getParent()->getChild(name, index_other);
156 GeoCoord coord_other = parseGeoCoord(other->getStringValue());
157 if( coord_other.type == GeoCoord::INVALID
158 || coord_other.type == coord.type )
161 GeoNodes::iterator it_geo_node_other = _geo_nodes.find(other);
162 if( it_geo_node_other == _geo_nodes.end() )
163 LOG_GEO_RET("other geo node not found!")
164 GeoNodePair* geo_node_other = it_geo_node_other->second.get();
166 // Let use both nodes use the same GeoNodePair instance
167 if( geo_node_other != geo_node )
168 it_geo_node_other->second = it_geo_node->second;
170 if( coord_other.type == GeoCoord::LATITUDE )
171 geo_node->setNodeLat(other);
173 geo_node->setNodeLon(other);
175 // Set name for resulting screen coordinate nodes
176 geo_node->setTargetName( name.substr(0, name.length() - GEO.length()) );
180 //----------------------------------------------------------------------------
181 void Map::childChanged(SGPropertyNode * child)
183 if( child->getParent() != _node )
184 return Group::childChanged(child);
186 if( child->getNameString() == "ref-lat"
187 || child->getNameString() == "ref-lon" )
188 _projection->setWorldPosition( _node->getDoubleValue("ref-lat"),
189 _node->getDoubleValue("ref-lon") );
190 else if( child->getNameString() == "hdg" )
191 _projection->setOrientation(child->getFloatValue());
192 else if( child->getNameString() == "range" )
193 _projection->setRange(child->getDoubleValue());
195 return Group::childChanged(child);
197 _projection_dirty = true;
200 //----------------------------------------------------------------------------
201 Map::GeoCoord Map::parseGeoCoord(const std::string& val) const
204 if( val.length() < 2 )
207 if( val[0] == 'N' || val[0] == 'S' )
208 coord.type = GeoCoord::LATITUDE;
209 else if( val[0] == 'E' || val[0] == 'W' )
210 coord.type = GeoCoord::LONGITUDE;
215 coord.value = strtod(&val[1], &end);
217 if( end != &val[val.length()] )
219 coord.type = GeoCoord::INVALID;
223 if( val[0] == 'S' || val[0] == 'W' )
229 } // namespace canvas
230 } // namespace simgear