-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_encoding.hpp
125 lines (94 loc) · 3.93 KB
/
json_encoding.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include <protozero/pbf_writer.hpp>
#include <protozero/pbf_reader.hpp>
#define RAPIDJSON_HAS_STDSTRING 1
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include "rapidjson/document.h"
#include <osmium/osm/types.hpp>
namespace jsonencoding {
/*
JSON Object Encoding
====================
These functions convert osmium objects to json objects (rapidjson::Document)
These are primarily used to add json strings to rocksdb so will likely be deprecated shortly.
*/
/*
Record Node Locations with basic properties
*/
bool encode_location_json(const osmium::Node& node, rapidjson::Document& doc){
rapidjson::Document::AllocatorType& a = doc.GetAllocator();
rapidjson::Value thisNode(rapidjson::kObjectType);
if( !node.deleted() ){
try{
rapidjson::Value coordinates(rapidjson::kArrayType);
coordinates.PushBack(node.location().lon(), a);
coordinates.PushBack(node.location().lat(), a);
thisNode.AddMember("p", coordinates, a); //p for point
} catch (const osmium::invalid_location& ex) {
//Catch invlid locations, not sure why this would happen... but it could
//std::cerr<< ex.what() << std::endl;
}
}
rapidjson::Value changesetKey;
changesetKey.SetString(std::to_string(node.changeset()), a);// = changesetStr; //(rapidjson::StringRef(changesetStr));
if (doc.HasMember(changesetKey)){
if (unsigned(doc[changesetKey]["i"].GetInt()) > node.version() ){
return true;
}else{
doc.RemoveMember(changesetKey);
}
}
thisNode.AddMember("t", uint32_t(node.timestamp()), a);
thisNode.AddMember("c", node.changeset(), a);
thisNode.AddMember("i", node.version(), a); //i for iteration (version)
thisNode.AddMember("h", std::string{node.user()}, a); //handle
thisNode.AddMember("u", node.uid(), a);
doc.AddMember(changesetKey, thisNode, a);
return true;
}
/*
Extract only primary properties
*/
rapidjson::Document extract_primary_properties(const osmium::OSMObject& object){
rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType& a = doc.GetAllocator();
// doc.AddMember("t", object.timestamp().to_iso(), a);
doc.AddMember("t", uint32_t(object.timestamp()), a);
doc.AddMember("c", object.changeset(), a);
doc.AddMember("i", object.version(), a); //i for iteration (version)
doc.AddMember("h", std::string{object.user()}, a); //handle
doc.AddMember("u", object.uid(), a);
return doc;
}
/*
Extract main OSM properties from the object
*/
rapidjson::Document extract_osm_properties(const osmium::OSMObject& object){
rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType& a = doc.GetAllocator();
doc.AddMember("t", uint32_t(object.timestamp()), a);
doc.AddMember("v", object.visible(), a);
doc.AddMember("h", std::string{object.user()}, a); //handle
doc.AddMember("u", object.uid(), a);
doc.AddMember("c", object.changeset(), a); //
doc.AddMember("i", object.version(), a); //i for iteration (version)
//Extra
if (object.deleted()){
doc.AddMember("d", object.deleted(), a);
}
//Tags
const osmium::TagList& tags = object.tags();
rapidjson::Value object_tags(rapidjson::kObjectType);
for (const osmium::Tag& tag : tags) {
rapidjson::Value key(rapidjson::StringRef(tag.key()));
rapidjson::Value value(rapidjson::StringRef(tag.value()));
object_tags.AddMember(key, value, a);
}
//a for attributes
doc.AddMember("a", object_tags, a);
return doc;
}
}