forked from organicmaps/organicmaps
Passing mapping from osm id to feature ids to json deserializer for transit.
This commit is contained in:
parent
ea1f72e0ba
commit
0483b54b87
5 changed files with 54 additions and 20 deletions
|
@ -7,6 +7,7 @@
|
|||
#include "base/assert.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using namespace routing;
|
||||
|
@ -21,7 +22,7 @@ void TestDeserializerFromJson(string const & jsonBuffer, string const & name, ve
|
|||
my::Json root(jsonBuffer.c_str());
|
||||
CHECK(root.get() != nullptr, ("Cannot parse the json."));
|
||||
|
||||
DeserializerFromJson deserializer(root.get());
|
||||
DeserializerFromJson deserializer(root.get(), make_shared<OsmIdToFeatureIdsMap>());
|
||||
|
||||
vector<Obj> objects;
|
||||
deserializer(objects, name.c_str());
|
||||
|
|
|
@ -308,7 +308,7 @@ int main(int argc, char ** argv)
|
|||
routing::BuildRoadAltitudes(datFile, FLAGS_srtm_path);
|
||||
|
||||
if (!FLAGS_transit_path.empty())
|
||||
routing::transit::BuildTransit(datFile, FLAGS_transit_path);
|
||||
routing::transit::BuildTransit(datFile, osmToFeatureFilename, FLAGS_transit_path);
|
||||
|
||||
if (FLAGS_make_routing_index)
|
||||
{
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
#include "platform/local_country_file_utils.hpp"
|
||||
#include "platform/platform.hpp"
|
||||
|
||||
#include "base/assert.hpp"
|
||||
#include "base/checked_cast.hpp"
|
||||
#include "base/logging.hpp"
|
||||
#include "base/macros.hpp"
|
||||
|
@ -83,17 +84,18 @@ string GetFileName(string const & filePath)
|
|||
}
|
||||
|
||||
template <class Item>
|
||||
void DeserializeFromJson(my::Json const & root, string const & key, vector<Item> & items)
|
||||
void DeserializeFromJson(my::Json const & root, string const & key,
|
||||
shared_ptr<OsmIdToFeatureIdsMap> const & osmIdToFeatureIdsMap, vector<Item> & items)
|
||||
{
|
||||
items.clear();
|
||||
DeserializerFromJson deserializer(root.get());
|
||||
DeserializerFromJson deserializer(root.get(), osmIdToFeatureIdsMap);
|
||||
deserializer(items, key.c_str());
|
||||
}
|
||||
|
||||
void DeserializeGatesFromJson(my::Json const & root, string const & mwmDir, string const & countryId,
|
||||
vector<Gate> & gates)
|
||||
shared_ptr<OsmIdToFeatureIdsMap> const & osmIdToFeatureIdsMap, vector<Gate> & gates)
|
||||
{
|
||||
DeserializeFromJson(root, "gates", gates);
|
||||
DeserializeFromJson(root, "gates", osmIdToFeatureIdsMap, gates);
|
||||
|
||||
// Creating IndexRouter.
|
||||
SingleMwmIndex index(my::JoinFoldersToPath(mwmDir, countryId + DATA_FILE_EXTENSION));
|
||||
|
@ -154,10 +156,11 @@ bool IsValid(vector<Item> const & items)
|
|||
|
||||
/// \brief Reads from |root| (json) and serializes an array to |serializer|.
|
||||
template <class Item>
|
||||
void SerializeObject(my::Json const & root, string const & key, Serializer<FileWriter> & serializer)
|
||||
void SerializeObject(my::Json const & root, string const & key,
|
||||
shared_ptr<OsmIdToFeatureIdsMap> const & osmIdToFeatureIdsMap, Serializer<FileWriter> & serializer)
|
||||
{
|
||||
vector<Item> items;
|
||||
DeserializeFromJson(root, key, items);
|
||||
DeserializeFromJson(root, key, osmIdToFeatureIdsMap, items);
|
||||
CHECK(IsValid(items), ("key:", key, "items:", items));
|
||||
serializer(items);
|
||||
}
|
||||
|
@ -179,6 +182,15 @@ void CalculateEdgeWeight(vector<Stop> const & stops, vector<transit::Edge> & edg
|
|||
e.SetWeight(lengthInMeters / kTransitAverageSpeedMPS);
|
||||
}
|
||||
}
|
||||
|
||||
void FillOsmIdToFeatureIdMap(string const & osmIdsToFeatureIdPath, OsmIdToFeatureIdsMap & map)
|
||||
{
|
||||
CHECK(ForEachOsmId2FeatureId(osmIdsToFeatureIdPath,
|
||||
[&map](osm::Id const & osmId, uint32_t featureId) {
|
||||
map[osmId].push_back(featureId);
|
||||
}),
|
||||
());
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace routing
|
||||
|
@ -198,8 +210,14 @@ void DeserializerFromJson::operator()(m2::PointD & p, char const * name)
|
|||
FromJSONObject(pointItem, "x", p.x);
|
||||
FromJSONObject(pointItem, "y", p.y);
|
||||
}
|
||||
DeserializerFromJson::DeserializerFromJson(json_struct_t * node,
|
||||
shared_ptr<OsmIdToFeatureIdsMap> const & osmIdToFeatureIds)
|
||||
: m_node(node), m_osmIdToFeatureIds(osmIdToFeatureIds)
|
||||
{
|
||||
}
|
||||
|
||||
void BuildTransit(string const & mwmPath, string const & transitDir)
|
||||
void BuildTransit(string const & mwmPath, string const & osmIdsToFeatureIdPath,
|
||||
string const & transitDir)
|
||||
{
|
||||
LOG(LERROR, ("This method is under construction and should not be used for building production mwm "
|
||||
"sections."));
|
||||
|
@ -241,10 +259,13 @@ void BuildTransit(string const & mwmPath, string const & transitDir)
|
|||
my::Json root(jsonBuffer.c_str());
|
||||
CHECK(root.get() != nullptr, ("Cannot parse the json file:", graphFullPath));
|
||||
|
||||
auto mapping = make_shared<OsmIdToFeatureIdsMap>();
|
||||
FillOsmIdToFeatureIdMap(osmIdsToFeatureIdPath, *mapping);
|
||||
|
||||
// Note. |gates| has to be deserialized from json before starting writing transit section to mwm since
|
||||
// the mwm is used to filled |gates|.
|
||||
vector<Gate> gates;
|
||||
DeserializeGatesFromJson(root, my::GetDirectory(mwmPath), countryId, gates);
|
||||
DeserializeGatesFromJson(root, my::GetDirectory(mwmPath), countryId, mapping, gates);
|
||||
CHECK(IsValid(gates), (gates));
|
||||
|
||||
FilesContainerW cont(mwmPath, FileWriter::OP_WRITE_EXISTING);
|
||||
|
@ -257,7 +278,7 @@ void BuildTransit(string const & mwmPath, string const & transitDir)
|
|||
header.Visit(serializer);
|
||||
|
||||
vector<Stop> stops;
|
||||
DeserializeFromJson(root, "stops", stops);
|
||||
DeserializeFromJson(root, "stops", mapping, stops);
|
||||
CHECK(IsValid(stops), ("stops:", stops));
|
||||
sort(stops.begin(), stops.end(), LessById);
|
||||
serializer(stops);
|
||||
|
@ -267,22 +288,22 @@ void BuildTransit(string const & mwmPath, string const & transitDir)
|
|||
header.m_edgesOffset = base::checked_cast<uint32_t>(w.Pos() - startOffset);
|
||||
|
||||
vector<Edge> edges;
|
||||
DeserializeFromJson(root, "edges", edges);
|
||||
DeserializeFromJson(root, "edges", mapping, edges);
|
||||
CalculateEdgeWeight(stops, edges);
|
||||
CHECK(IsValid(stops), ("edges:", edges));
|
||||
serializer(edges);
|
||||
header.m_transfersOffset = base::checked_cast<uint32_t>(w.Pos() - startOffset);
|
||||
|
||||
SerializeObject<Transfer>(root, "transfers", serializer);
|
||||
SerializeObject<Transfer>(root, "transfers", mapping, serializer);
|
||||
header.m_linesOffset = base::checked_cast<uint32_t>(w.Pos() - startOffset);
|
||||
|
||||
SerializeObject<Line>(root, "lines", serializer);
|
||||
SerializeObject<Line>(root, "lines", mapping, serializer);
|
||||
header.m_shapesOffset = base::checked_cast<uint32_t>(w.Pos() - startOffset);
|
||||
|
||||
SerializeObject<Shape>(root, "shapes", serializer);
|
||||
SerializeObject<Shape>(root, "shapes", mapping, serializer);
|
||||
header.m_networksOffset = base::checked_cast<uint32_t>(w.Pos() - startOffset);
|
||||
|
||||
SerializeObject<Network>(root, "networks", serializer);
|
||||
SerializeObject<Network>(root, "networks", mapping, serializer);
|
||||
header.m_endOffset = base::checked_cast<uint32_t>(w.Pos() - startOffset);
|
||||
|
||||
// Rewriting header info.
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include "generator/osm_id.hpp"
|
||||
|
||||
#include "routing_common/transit_types.hpp"
|
||||
|
||||
#include "geometry/point2d.hpp"
|
||||
|
||||
#include "base/macros.hpp"
|
||||
|
@ -7,6 +11,8 @@
|
|||
#include "3party/jansson/myjansson.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
@ -15,10 +21,12 @@ namespace routing
|
|||
{
|
||||
namespace transit
|
||||
{
|
||||
using OsmIdToFeatureIdsMap = std::map<osm::Id, std::vector<FeatureId>>;
|
||||
|
||||
class DeserializerFromJson
|
||||
{
|
||||
public:
|
||||
DeserializerFromJson(json_struct_t * node) : m_node(node) {}
|
||||
DeserializerFromJson(json_struct_t* node, std::shared_ptr<OsmIdToFeatureIdsMap> const & osmIdToFeatureIds);
|
||||
|
||||
template<typename T>
|
||||
typename std::enable_if<std::is_integral<T>::value || std::is_enum<T>::value || std::is_same<T, double>::value>::type
|
||||
|
@ -43,7 +51,7 @@ public:
|
|||
vs.resize(sz);
|
||||
for (size_t i = 0; i < sz; ++i)
|
||||
{
|
||||
DeserializerFromJson arrayItem(json_array_get(arr, i));
|
||||
DeserializerFromJson arrayItem(json_array_get(arr, i), m_osmIdToFeatureIds);
|
||||
arrayItem(vs[i]);
|
||||
}
|
||||
}
|
||||
|
@ -77,6 +85,7 @@ private:
|
|||
}
|
||||
|
||||
json_struct_t * m_node;
|
||||
std::shared_ptr<OsmIdToFeatureIdsMap> m_osmIdToFeatureIds;
|
||||
};
|
||||
|
||||
/// \brief Builds the transit section in the mwm.
|
||||
|
@ -86,6 +95,7 @@ private:
|
|||
/// \note An mwm pointed by |mwmPath| should contain:
|
||||
/// * feature geometry
|
||||
/// * index graph (ROUTING_FILE_TAG)
|
||||
void BuildTransit(std::string const & mwmPath, std::string const & transitDir);
|
||||
void BuildTransit(std::string const & mwmPath, std::string const & osmIdsToFeatureIdPath,
|
||||
std::string const & transitDir);
|
||||
} // namespace transit
|
||||
} // namespace routing
|
||||
|
|
|
@ -40,7 +40,9 @@ Anchor constexpr kInvalidAnchor = std::numeric_limits<Anchor>::max();
|
|||
template<class Source> friend class Deserializer; \
|
||||
friend class DeserializerFromJson; \
|
||||
friend class routing::TransitGraphLoader; \
|
||||
friend void BuildTransit(std::string const & mwmPath, std::string const & transitDir); \
|
||||
friend void BuildTransit(std::string const & mwmPath, \
|
||||
std::string const & osmIdsToFeatureIdPath, \
|
||||
std::string const & transitDir); \
|
||||
template<class Obj> friend void TestSerialization(Obj const & obj); \
|
||||
|
||||
struct TransitHeader
|
||||
|
|
Loading…
Add table
Reference in a new issue