forked from cyang-kth/fmm
-
Notifications
You must be signed in to change notification settings - Fork 0
Home
TANG ZhiXiong edited this page Jun 5, 2023
·
1 revision
Welcome to the fmm wiki!
#include "util/cubao_types.hpp"
// json load/dump
bool load(const std::string &path);
bool dump(const std::string &path) const;
bool loads(const std::string &json);
std::string dumps() const;
bool from_json(const RapidjsonValue &json);
RapidjsonValue to_json(RapidjsonAllocator &allocator) const;
RapidjsonValue to_json() const
{
RapidjsonAllocator allocator;
return to_json(allocator);
}
#include "util/cubao_helpers.hpp"
bool Network::load(const std::string &path)
{
return from_json(cubao::load_json(path));
}
bool Network::dump(const std::string &path) const
{
return cubao::dump_json(path, to_json(), true);
}
bool Network::loads(const std::string &json)
{
return from_json(cubao::loads(json));
}
std::string Network::dumps() const { return cubao::dumps(to_json()); }
bool Network::from_json(const RapidjsonValue &json)
{
if (!json.IsObject()) {
return false;
}
using namespace cubao;
srid = json["srid"].GetInt();
for (auto &e : json["edges"].GetArray()) {
auto id = e["id"].GetInt64();
auto source = e["source"].GetInt64();
auto target = e["target"].GetInt64();
FMM::CORE::LineString geom;
for (auto &xy : e["coordinates"].GetArray()) {
geom.add_point(xy[0].GetDouble(), xy[1].GetDouble());
}
add_edge((EdgeID)id, (NodeID)source, (NodeID)target, geom);
}
return true;
}
RapidjsonValue Network::to_json(RapidjsonAllocator &allocator) const
{
using namespace cubao;
RapidjsonValue edges(rapidjson::kArrayType);
for (auto &e : this->edges) {
RapidjsonValue edge(rapidjson::kObjectType);
edge.AddMember("id", RapidjsonValue((int64_t)e.id), allocator);
edge.AddMember("source",
RapidjsonValue((uint32_t)get_node_id(e.source)),
allocator);
edge.AddMember("target",
RapidjsonValue((uint32_t)get_node_id(e.target)),
allocator);
RapidjsonValue coordinates(rapidjson::kArrayType);
auto &G = e.geom;
int N = G.get_num_points();
for (int i = 0; i < N; ++i) {
RapidjsonValue xy(rapidjson::kArrayType);
xy.Reserve(2, allocator);
xy.PushBack(RapidjsonValue(G.get_x(i)), allocator);
xy.PushBack(RapidjsonValue(G.get_y(i)), allocator);
coordinates.PushBack(xy, allocator);
}
edge.AddMember("coordinates", coordinates, allocator);
edges.PushBack(edge, allocator);
}
RapidjsonValue json(rapidjson::kObjectType);
json.AddMember("srid", RapidjsonValue(srid), allocator);
json.AddMember("edges", edges, allocator);
return json;
}
other steps:
- update fmm.i
- make build
- python3 test.py