osrm-backend/DataStructures/NodeInformationHelpDesk.h

181 lines
5.8 KiB
C
Raw Normal View History

2010-07-09 05:05:40 -04:00
/*
open source routing machine
Copyright (C) Dennis Luxen, others 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
2010-07-26 04:17:52 -04:00
*/
2010-07-09 05:05:40 -04:00
#ifndef NODEINFORMATIONHELPDESK_H_
2010-07-09 05:05:40 -04:00
#define NODEINFORMATIONHELPDESK_H_
#include "NodeCoords.h"
#include "PhantomNodes.h"
#include "StaticRTree.h"
#include "../Contractor/EdgeBasedGraphFactory.h"
2013-08-05 11:28:57 -04:00
#include "../Util/OSRMException.h"
#include "../typedefs.h"
#include <boost/assert.hpp>
#include <boost/noncopyable.hpp>
2010-07-09 05:05:40 -04:00
#include <iostream>
2013-07-22 10:32:19 -04:00
#include <fstream>
#include <string>
2010-07-09 05:05:40 -04:00
#include <vector>
typedef EdgeBasedGraphFactory::EdgeBasedNode RTreeLeaf;
2010-07-09 05:05:40 -04:00
2013-01-06 13:38:03 -05:00
class NodeInformationHelpDesk : boost::noncopyable{
2010-07-09 05:05:40 -04:00
public:
NodeInformationHelpDesk(
2013-07-22 10:32:19 -04:00
const std::string & ramIndexInput,
const std::string & fileIndexInput,
const std::string & nodes_filename,
const std::string & edges_filename,
const unsigned number_of_nodes,
2013-07-22 10:32:19 -04:00
const unsigned check_sum
) : number_of_nodes(number_of_nodes), check_sum(check_sum)
{
read_only_rtree = new StaticRTree<RTreeLeaf>(
ramIndexInput,
fileIndexInput
);
BOOST_ASSERT_MSG(
0 == coordinateVector.size(),
"Coordinate vector not empty"
);
2013-07-22 10:32:19 -04:00
LoadNodesAndEdges(nodes_filename, edges_filename);
2012-04-02 07:44:44 -04:00
}
//Todo: Shared memory mechanism
~NodeInformationHelpDesk() {
delete read_only_rtree;
}
inline int getLatitudeOfNode(const unsigned id) const {
2013-01-06 13:38:03 -05:00
const NodeID node = origEdgeData_viaNode.at(id);
return coordinateVector.at(node).lat;
}
inline int getLongitudeOfNode(const unsigned id) const {
2013-01-06 13:38:03 -05:00
const NodeID node = origEdgeData_viaNode.at(id);
return coordinateVector.at(node).lon;
}
inline unsigned getNameIndexFromEdgeID(const unsigned id) const {
2013-01-06 13:38:03 -05:00
return origEdgeData_nameID.at(id);
}
2010-07-09 05:05:40 -04:00
inline TurnInstruction getTurnInstructionFromEdgeID(const unsigned id) const {
2013-01-06 13:38:03 -05:00
return origEdgeData_turnInstruction.at(id);
}
2010-07-09 05:05:40 -04:00
inline NodeID getNumberOfNodes() const {
return number_of_nodes;
}
2012-07-13 11:01:21 -04:00
inline NodeID getNumberOfNodes2() const {
return coordinateVector.size();
}
2010-07-09 05:05:40 -04:00
inline bool FindNearestNodeCoordForLatLon(
const _Coordinate& input_coordinate,
_Coordinate& result,
const unsigned zoom_level = 18
) const {
PhantomNode resulting_phantom_node;
2013-06-27 16:09:21 -04:00
bool foundNode = FindPhantomNodeForCoordinate(
input_coordinate,
resulting_phantom_node, zoom_level
);
result = resulting_phantom_node.location;
return foundNode;
}
inline bool FindPhantomNodeForCoordinate(
const _Coordinate & input_coordinate,
PhantomNode & resulting_phantom_node,
const unsigned zoom_level
) const {
return read_only_rtree->FindPhantomNodeForCoordinate(
input_coordinate,
resulting_phantom_node,
zoom_level
);
}
inline unsigned GetCheckSum() const {
2013-07-22 10:32:19 -04:00
return check_sum;
}
private:
2013-07-22 10:32:19 -04:00
void LoadNodesAndEdges(
const std::string & nodes_file,
const std::string & edges_file
) {
2013-08-05 11:28:57 -04:00
std::ifstream nodes_input_stream(nodes_file.c_str(), std::ios::binary);
if(!nodes_input_stream) {
throw OSRMException("nodes file not found");
}
std::ifstream edges_input_stream(edges_file.c_str(), std::ios::binary);
if(!edges_input_stream) {
throw OSRMException("edges file not found");
}
2013-07-22 10:32:19 -04:00
DEBUG("Loading node data");
NodeInfo b;
while(!nodes_input_stream.eof()) {
nodes_input_stream.read((char *)&b, sizeof(NodeInfo));
coordinateVector.push_back(_Coordinate(b.lat, b.lon));
}
std::vector<_Coordinate>(coordinateVector).swap(coordinateVector);
nodes_input_stream.close();
DEBUG("Loading edge data");
unsigned numberOfOrigEdges(0);
edges_input_stream.read((char*)&numberOfOrigEdges, sizeof(unsigned));
origEdgeData_viaNode.resize(numberOfOrigEdges);
origEdgeData_nameID.resize(numberOfOrigEdges);
origEdgeData_turnInstruction.resize(numberOfOrigEdges);
OriginalEdgeData deserialized_originalEdgeData;
for(unsigned i = 0; i < numberOfOrigEdges; ++i) {
edges_input_stream.read(
(char*)&(deserialized_originalEdgeData),
sizeof(OriginalEdgeData)
);
origEdgeData_viaNode[i] = deserialized_originalEdgeData.viaNode;
origEdgeData_nameID[i] = deserialized_originalEdgeData.nameID;
origEdgeData_turnInstruction[i] = deserialized_originalEdgeData.turnInstruction;
}
edges_input_stream.close();
DEBUG("Loaded " << numberOfOrigEdges << " orig edges");
DEBUG("Opening NN indices");
}
2011-12-17 16:19:08 -05:00
std::vector<_Coordinate> coordinateVector;
2013-01-06 13:38:03 -05:00
std::vector<NodeID> origEdgeData_viaNode;
std::vector<unsigned> origEdgeData_nameID;
std::vector<TurnInstruction> origEdgeData_turnInstruction;
StaticRTree<EdgeBasedGraphFactory::EdgeBasedNode> * read_only_rtree;
const unsigned number_of_nodes;
2013-07-22 10:32:19 -04:00
const unsigned check_sum;
};
2010-07-09 05:05:40 -04:00
#endif /*NODEINFORMATIONHELPDESK_H_*/