Skip to content

Commit

Permalink
Merge pull request #1030 from DennisOSRM/features/json-generator
Browse files Browse the repository at this point in the history
Distance tables and JSON generator
  • Loading branch information
DennisOSRM committed May 21, 2014
2 parents bddad0c + 35c9021 commit 2822382
Show file tree
Hide file tree
Showing 58 changed files with 3,157 additions and 2,510 deletions.
142 changes: 142 additions & 0 deletions Algorithms/ExtractRouteNames.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
Copyright (c) 2013, Project OSRM, Dennis Luxen, others
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef EXTRACT_ROUTE_NAMES_H
#define EXTRACT_ROUTE_NAMES_H

#include <boost/assert.hpp>

#include <algorithm>
#include <string>
#include <vector>

struct RouteNames
{
std::string shortest_path_name_1;
std::string shortest_path_name_2;
std::string alternative_path_name_1;
std::string alternative_path_name_2;
};

// construct routes names
template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
{
RouteNames operator()(std::vector<SegmentT> &shortest_path_segments,
std::vector<SegmentT> &alternative_path_segments,
const DataFacadeT *facade)
{
RouteNames route_names;

SegmentT shortest_segment_1, shortest_segment_2;
SegmentT alternative_segment_1, alternative_segment_2;

auto length_comperator = [](SegmentT a, SegmentT b)
{ return a.length > b.length; };
auto name_id_comperator = [](SegmentT a, SegmentT b)
{ return a.name_id < b.name_id; };

if (shortest_path_segments.empty())
{
return route_names;
}

std::sort(shortest_path_segments.begin(), shortest_path_segments.end(), length_comperator);
shortest_segment_1 = shortest_path_segments[0];
if (!alternative_path_segments.empty())
{
std::sort(alternative_path_segments.begin(),
alternative_path_segments.end(),
length_comperator);
alternative_segment_1 = alternative_path_segments[0];
}
std::vector<SegmentT> shortest_path_set_difference(shortest_path_segments.size());
std::vector<SegmentT> alternative_path_set_difference(alternative_path_segments.size());
std::set_difference(shortest_path_segments.begin(),
shortest_path_segments.end(),
alternative_path_segments.begin(),
alternative_path_segments.end(),
shortest_path_set_difference.begin(),
name_id_comperator);
int size_of_difference = shortest_path_set_difference.size();
if (size_of_difference)
{
int i = 0;
while (i < size_of_difference &&
shortest_path_set_difference[i].name_id == shortest_path_segments[0].name_id)
{
++i;
}
if (i < size_of_difference)
{
shortest_segment_2 = shortest_path_set_difference[i];
}
}

std::set_difference(alternative_path_segments.begin(),
alternative_path_segments.end(),
shortest_path_segments.begin(),
shortest_path_segments.end(),
alternative_path_set_difference.begin(),
name_id_comperator);
size_of_difference = alternative_path_set_difference.size();
if (size_of_difference)
{
int i = 0;
while (i < size_of_difference &&
alternative_path_set_difference[i].name_id ==
alternative_path_segments[0].name_id)
{
++i;
}
if (i < size_of_difference)
{
alternative_segment_2 = alternative_path_set_difference[i];
}
}
if (shortest_segment_1.position > shortest_segment_2.position)
{
std::swap(shortest_segment_1, shortest_segment_2);
}
if (alternative_segment_1.position > alternative_segment_2.position)
{
std::swap(alternative_segment_1, alternative_segment_2);
}
route_names.shortest_path_name_1 =
facade->GetEscapedNameForNameID(shortest_segment_1.name_id);
route_names.shortest_path_name_2 =
facade->GetEscapedNameForNameID(shortest_segment_2.name_id);

route_names.alternative_path_name_1 =
facade->GetEscapedNameForNameID(alternative_segment_1.name_id);
route_names.alternative_path_name_2 =
facade->GetEscapedNameForNameID(alternative_segment_2.name_id);

return route_names;
}
};

#endif // EXTRACT_ROUTE_NAMES_H
89 changes: 39 additions & 50 deletions Algorithms/PolylineCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "PolylineCompressor.h"
#include "../Util/StringUtil.h"

//TODO: return vector of start indices for each leg

void PolylineCompressor::encodeVectorSignedNumber(std::vector<int> &numbers, std::string &output)
const
Expand All @@ -39,9 +42,9 @@ void PolylineCompressor::encodeVectorSignedNumber(std::vector<int> &numbers, std
numbers[i] = ~(numbers[i]);
}
}
for (unsigned i = 0; i < end; ++i)
for (const int number: numbers)
{
encodeNumber(numbers[i], output);
encodeNumber(number, output);
}
}

Expand All @@ -66,37 +69,36 @@ void PolylineCompressor::encodeNumber(int number_to_encode, std::string &output)
}
}

void PolylineCompressor::printEncodedString(const std::vector<SegmentInformation> &polyline,
std::string &output) const
JSON::String PolylineCompressor::printEncodedString(const std::vector<SegmentInformation> &polyline) const
{
std::string output;
std::vector<int> delta_numbers;
output += "\"";
if (!polyline.empty())
{
FixedPointCoordinate last_coordinate = polyline[0].location;
delta_numbers.emplace_back(last_coordinate.lat);
delta_numbers.emplace_back(last_coordinate.lon);
for (unsigned i = 1; i < polyline.size(); ++i)
for (const auto & segment : polyline)
{
if (polyline[i].necessary)
if (segment.necessary)
{
int lat_diff = polyline[i].location.lat - last_coordinate.lat;
int lon_diff = polyline[i].location.lon - last_coordinate.lon;
int lat_diff = segment.location.lat - last_coordinate.lat;
int lon_diff = segment.location.lon - last_coordinate.lon;
delta_numbers.emplace_back(lat_diff);
delta_numbers.emplace_back(lon_diff);
last_coordinate = polyline[i].location;
last_coordinate = segment.location;
}
}
encodeVectorSignedNumber(delta_numbers, output);
}
output += "\"";
JSON::String return_value(output);
return return_value;
}

void PolylineCompressor::printEncodedString(const std::vector<FixedPointCoordinate> &polyline,
std::string &output) const
JSON::String PolylineCompressor::printEncodedString(const std::vector<FixedPointCoordinate> &polyline) const
{
std::string output;
std::vector<int> delta_numbers(2 * polyline.size());
output += "\"";
if (!polyline.empty())
{
delta_numbers[0] = polyline[0].lat;
Expand All @@ -110,53 +112,40 @@ void PolylineCompressor::printEncodedString(const std::vector<FixedPointCoordina
}
encodeVectorSignedNumber(delta_numbers, output);
}
output += "\"";
JSON::String return_value(output);
return return_value;
}

void PolylineCompressor::printUnencodedString(const std::vector<FixedPointCoordinate> &polyline,
std::string &output) const

JSON::Array PolylineCompressor::printUnencodedString(const std::vector<FixedPointCoordinate> &polyline) const
{
output += "[";
std::string tmp;
for (unsigned i = 0; i < polyline.size(); i++)
JSON::Array json_geometry_array;
for( const auto & coordinate : polyline)
{
FixedPointCoordinate::convertInternalLatLonToString(polyline[i].lat, tmp);
output += "[";
output += tmp;
FixedPointCoordinate::convertInternalLatLonToString(polyline[i].lon, tmp);
output += ", ";
std::string tmp, output;
FixedPointCoordinate::convertInternalLatLonToString(coordinate.lat, tmp);
output += (tmp + ",");
FixedPointCoordinate::convertInternalLatLonToString(coordinate.lon, tmp);
output += tmp;
output += "]";
if (i < polyline.size() - 1)
{
output += ",";
}
json_geometry_array.values.push_back(output);
}
output += "]";
return json_geometry_array;
}

void PolylineCompressor::printUnencodedString(const std::vector<SegmentInformation> &polyline,
std::string &output) const
JSON::Array PolylineCompressor::printUnencodedString(const std::vector<SegmentInformation> &polyline) const
{
output += "[";
std::string tmp;
for (unsigned i = 0; i < polyline.size(); i++)
JSON::Array json_geometry_array;
for( const auto & segment : polyline)
{
if (!polyline[i].necessary)
{
continue;
}
FixedPointCoordinate::convertInternalLatLonToString(polyline[i].location.lat, tmp);
output += "[";
output += tmp;
FixedPointCoordinate::convertInternalLatLonToString(polyline[i].location.lon, tmp);
output += ", ";
output += tmp;
output += "]";
if (i < polyline.size() - 1)
if (segment.necessary)
{
output += ",";
std::string tmp, output;
FixedPointCoordinate::convertInternalLatLonToString(segment.location.lat, tmp);
output += (tmp + ",");
FixedPointCoordinate::convertInternalLatLonToString(segment.location.lon, tmp);
output += tmp;
json_geometry_array.values.push_back(output);
}
}
output += "]";
return json_geometry_array;
}
14 changes: 5 additions & 9 deletions Algorithms/PolylineCompressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef POLYLINECOMPRESSOR_H_
#define POLYLINECOMPRESSOR_H_

#include "../DataStructures/JSONContainer.h"
#include "../DataStructures/SegmentInformation.h"
#include "../Util/StringUtil.h"

#include <string>
#include <vector>
Expand All @@ -42,17 +42,13 @@ class PolylineCompressor
void encodeNumber(int number_to_encode, std::string &output) const;

public:
void printEncodedString(const std::vector<SegmentInformation> &polyline,
std::string &output) const;
JSON::String printEncodedString(const std::vector<SegmentInformation> &polyline) const;

void printEncodedString(const std::vector<FixedPointCoordinate> &polyline,
std::string &output) const;
JSON::String printEncodedString(const std::vector<FixedPointCoordinate> &polyline) const;

void printUnencodedString(const std::vector<FixedPointCoordinate> &polyline,
std::string &output) const;
JSON::Array printUnencodedString(const std::vector<FixedPointCoordinate> &polyline) const;

void printUnencodedString(const std::vector<SegmentInformation> &polyline,
std::string &output) const;
JSON::Array printUnencodedString(const std::vector<SegmentInformation> &polyline) const;
};

#endif /* POLYLINECOMPRESSOR_H_ */
31 changes: 12 additions & 19 deletions Algorithms/StronglyConnectedComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../DataStructures/TurnInstructions.h"

#include "../Util/SimpleLogger.h"
#include "../Util/StdHashExtensions.h"

#include <osrm/Coordinate.h>

Expand All @@ -60,17 +61,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <unordered_set>
#include <vector>

namespace std
{
template <> struct hash<std::pair<NodeID, NodeID>>
{
size_t operator()(const std::pair<NodeID, NodeID> &pair) const
{
return std::hash<int>()(pair.first) ^ std::hash<int>()(pair.second);
}
};
}

class TarjanSCC
{
private:
Expand Down Expand Up @@ -339,14 +329,17 @@ class TarjanSCC
<< " many components, marking small components";

// TODO/C++11: prime candidate for lambda function
unsigned size_one_counter = 0;
for (unsigned i = 0, end = component_size_vector.size(); i < end; ++i)
{
if (1 == component_size_vector[i])
{
++size_one_counter;
}
}
// unsigned size_one_counter = 0;
// for (unsigned i = 0, end = component_size_vector.size(); i < end; ++i)
// {
// if (1 == component_size_vector[i])
// {
// ++size_one_counter;
// }
// }
unsigned size_one_counter = std::count_if(component_size_vector.begin(),
component_size_vector.end(),
[] (unsigned value) { return 1 == value;});

SimpleLogger().Write() << "identified " << size_one_counter << " SCCs of size 1";

Expand Down
8 changes: 7 additions & 1 deletion Contractor/EdgeBasedGraphFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ void EdgeBasedGraphFactory::CompressGeometry()
continue;
}

// check if v is a via node for a turn restriction, i.e. a 'directed' barrier node
if (m_restriction_map->IsNodeAViaNode(v))
{
continue;
}

const bool reverse_edge_order =
!(m_node_based_graph->GetEdgeData(m_node_based_graph->BeginEdges(v)).forward);
const EdgeID forward_e2 = m_node_based_graph->BeginEdges(v) + reverse_edge_order;
Expand Down Expand Up @@ -722,7 +728,7 @@ TurnInstruction EdgeBasedGraphFactory::AnalyzeTurn(const NodeID u,
if (data1.roundabout && data2.roundabout)
{
// Is a turn possible? If yes, we stay on the roundabout!
if (1 == m_node_based_graph->GetOutDegree(v))
if (1 == m_node_based_graph->GetDirectedOutDegree(v))
{
// No turn possible.
return TurnInstruction::NoTurn;
Expand Down
Loading

0 comments on commit 2822382

Please sign in to comment.