12std::string as_str_or_empty(
const nlohmann::ordered_json& obj,
const std::string& key) {
13 auto it = obj.find(key);
14 if (it == obj.end() || !it->is_string())
16 return it->get<std::string>();
25std::string require_string_field(
const nlohmann::ordered_json& obj,
const std::string& key) {
26 auto it = obj.find(key);
27 if (it == obj.end() || !it->is_string())
29 "pointOfContact." + key +
" is required and must be a string");
30 return it->get<std::string>();
43std::optional<std::string> optional_string_field(
const nlohmann::ordered_json& obj,
44 const std::string& key) {
45 auto it = obj.find(key);
46 if (it == obj.end() || !it->is_string())
48 return it->get<std::string>();
56std::optional<std::string> address_member(
const nlohmann::ordered_json& address,
57 const std::string& key) {
58 auto it = address.find(key);
59 if (it == address.end() || it->is_null())
62 return it->get<std::string>();
66std::optional<std::string> address_either(
const nlohmann::ordered_json& address,
67 const std::string& a,
const std::string& b) {
68 if (
auto v = address_member(address, a))
70 return address_member(address, b);
73std::int32_t parse_i32_whole(std::string_view s) {
82 if (s.front() ==
'+') {
84 if (s.empty() || !(s.front() >=
'0' && s.front() <=
'9'))
87 std::int32_t value = 0;
88 auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), value);
89 if (ec != std::errc() || ptr != s.data() + s.size())
97 const auto& scale = transform.at(
"scale");
98 const auto& translate = transform.at(
"translate");
100 ::Vector(scale.at(0).get<
double>(), scale.at(1).get<
double>(), scale.at(2).get<
double>()),
101 ::Vector(translate.at(0).get<
double>(), translate.at(1).get<
double>(),
102 translate.at(2).get<
double>()));
106 static constexpr std::string_view kPrefixes[] = {
"http://www.opengis.net/def/crs/",
107 "https://www.opengis.net/def/crs/"};
108 for (
const auto& prefix : kPrefixes) {
109 if (url.compare(0, prefix.size(), prefix) != 0)
112 const std::string rest = url.substr(prefix.size());
113 std::vector<std::string> segments;
114 std::size_t start = 0;
116 const std::size_t pos = rest.find(
'/', start);
117 const std::size_t end = pos == std::string::npos ? rest.size() : pos;
118 segments.push_back(rest.substr(start, end - start));
119 if (pos == std::string::npos)
126 out.
version = segments.size() > 1 ? parse_i32_whole(segments[1]) : 0;
127 out.
code = segments.size() > 2 ? parse_i32_whole(segments[2]) : 0;
135 auto authority = fbb.CreateString(ref_sys.
authority);
136 return CreateReferenceSystem(fbb, authority, ref_sys.
version, ref_sys.
code, 0);
139::flatbuffers::Offset<::Extension>
to_extension(::flatbuffers::FlatBufferBuilder& fbb,
140 const std::string& name,
const std::string& url,
141 const std::string& version,
142 const nlohmann::ordered_json& members) {
143 auto name_off = fbb.CreateString(name);
144 auto url_off = fbb.CreateString(url);
145 auto version_off = fbb.CreateString(version);
148 std::optional<::flatbuffers::Offset<::flatbuffers::String>> members_off;
149 if (members.is_object() && !members.empty())
150 members_off = fbb.CreateString(members.dump());
151 return CreateExtension(fbb, name_off, 0, url_off, version_off, 0, 0, 0, 0, 0,
152 members_off.value_or(0));
155::flatbuffers::Offset<::flatbuffers::Vector<const ::DoubleVertex*>>
157 const nlohmann::ordered_json& vertices_templates) {
158 std::vector<::DoubleVertex> verts;
159 if (vertices_templates.is_array()) {
160 for (
const auto& v : vertices_templates) {
163 std::vector<double> coords;
164 for (
const auto& c : v)
166 coords.push_back(c.get<
double>());
167 if (coords.size() == 3)
168 verts.emplace_back(coords[0], coords[1], coords[2]);
171 return fbb.CreateVectorOfStructs(verts);
175 const nlohmann::ordered_json& poc) {
177 out.
contact_name = fbb.CreateString(require_string_field(poc,
"contactName"));
179 if (
auto v = optional_string_field(poc,
"contactType"))
181 if (
auto v = optional_string_field(poc,
"role"))
182 out.
role = fbb.CreateString(*v);
183 if (
auto v = optional_string_field(poc,
"phone"))
184 out.
phone = fbb.CreateString(*v);
185 out.
email = fbb.CreateString(require_string_field(poc,
"emailAddress"));
186 if (
auto v = optional_string_field(poc,
"website"))
187 out.
website = fbb.CreateString(*v);
194 if (
auto addr_it = poc.find(
"address"); addr_it != poc.end() && addr_it->is_object()) {
195 const nlohmann::ordered_json& address = *addr_it;
196 if (
auto v = address_member(address,
"thoroughfareNumber"))
198 if (
auto v = address_member(address,
"thoroughfareName"))
200 if (
auto v = address_member(address,
"locality"))
202 if (
auto v = address_either(address,
"postcode",
"postalCode"))
204 if (
auto v = address_member(address,
"country"))
210::flatbuffers::Offset<::Header>
211to_fcb_header(::flatbuffers::FlatBufferBuilder& fbb,
const nlohmann::ordered_json& cj,
214 const std::vector<AttributeIndexInfo>* attribute_indices_info) {
215 auto version = fbb.CreateString(cj.at(
"version").get<std::string>());
216 ::Transform transform =
to_transform(cj.at(
"transform"));
220 std::optional<::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::Column>>>>
222 if (semantic_attr_schema !=
nullptr)
223 semantic_columns =
to_columns(fbb, *semantic_attr_schema);
227 std::optional<::flatbuffers::Offset<::flatbuffers::Vector<const ::AttributeIndex*>>>
229 if (attribute_indices_info !=
nullptr) {
230 std::vector<::AttributeIndex> entries;
231 entries.reserve(attribute_indices_info->size());
232 for (
const auto& info : *attribute_indices_info)
233 entries.emplace_back(info.index, info.length, info.branching_factor,
234 info.num_unique_items);
235 attribute_index = fbb.CreateVectorOfStructs(entries);
238 std::optional<::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::Extension>>>>
240 if (
auto it = cj.find(
"extensions"); it != cj.end() && it->is_object()) {
241 std::vector<::flatbuffers::Offset<::Extension>> ext_offs;
242 for (
const auto& [name, ext] : it->items()) {
244 nlohmann::ordered_json members = nlohmann::ordered_json::object();
245 if (ext.is_object()) {
246 for (
const auto& [key, val] : ext.items())
247 if (key !=
"url" && key !=
"version")
250 ext_offs.push_back(
to_extension(fbb, name, as_str_or_empty(ext,
"url"),
251 as_str_or_empty(ext,
"version"), members));
253 extensions = fbb.CreateVector(ext_offs);
256 std::optional<::GeographicalExtent> geographical_extent;
260 std::optional<::flatbuffers::Offset<::Appearance>> appearance;
261 if (
auto it = cj.find(
"appearance"); it != cj.end() && it->is_object())
264 std::optional<::flatbuffers::Offset<::flatbuffers::Vector<const ::DoubleVertex*>>>
266 std::optional<::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::Geometry>>>>
268 if (
auto gm_it = cj.find(
"geometry-templates"); gm_it != cj.end() && gm_it->is_object()) {
271 std::vector<::flatbuffers::Offset<::Geometry>> geom_offs;
272 for (
const auto& g : gm_it->at(
"templates"))
273 geom_offs.push_back(
to_geometry(fbb, g, semantic_attr_schema));
274 templates = fbb.CreateVector(geom_offs);
277 std::optional<::flatbuffers::Offset<::ReferenceSystem>> reference_system;
278 std::optional<::flatbuffers::Offset<::flatbuffers::String>> identifier;
279 std::optional<::flatbuffers::Offset<::flatbuffers::String>> reference_date;
280 std::optional<::flatbuffers::Offset<::flatbuffers::String>> title;
283 if (
auto meta_it = cj.find(
"metadata"); meta_it != cj.end() && meta_it->is_object()) {
284 const nlohmann::ordered_json& meta = *meta_it;
286 if (
auto rs_it = meta.find(
"referenceSystem"); rs_it != meta.end() && rs_it->is_string()) {
291 if (!geographical_extent) {
292 if (
auto ge_it = meta.find(
"geographicalExtent");
293 ge_it != meta.end() && ge_it->is_array() && ge_it->size() == 6) {
294 std::array<double, 6> extent{};
295 for (std::size_t i = 0; i < 6; ++i)
296 extent[i] = ge_it->at(i).get<
double>();
301 if (
auto v = optional_string_field(meta,
"identifier"))
302 identifier = fbb.CreateString(*v);
303 if (
auto v = optional_string_field(meta,
"referenceDate"))
304 reference_date = fbb.CreateString(*v);
305 if (
auto v = optional_string_field(meta,
"title"))
306 title = fbb.CreateString(*v);
308 if (
auto poc_it = meta.find(
"pointOfContact"); poc_it != meta.end() && poc_it->is_object())
326 fbb, &transform, appearance.value_or(0), columns, semantic_columns.value_or(0),
327 features_count, index_node_size, attribute_index.value_or(0),
328 geographical_extent ? &*geographical_extent :
nullptr, reference_system.value_or(0),
329 identifier.value_or(0), reference_date.value_or(0), title.value_or(0),
330 templates.value_or(0), templates_vertices.value_or(0), extensions.value_or(0),
Every failure the library reports is one of these.
::flatbuffers::Offset<::flatbuffers::Vector<::flatbuffers::Offset<::Column > > > to_columns(::flatbuffers::FlatBufferBuilder &fbb, const AttributeSchema &schema, std::optional< std::uint16_t > id_column=std::nullopt)
Builds the Column vector for Header.columns or CityObject.columns, in ascending column-index order.
::flatbuffers::Offset<::Extension > to_extension(::flatbuffers::FlatBufferBuilder &fbb, const std::string &name, const std::string &url, const std::string &version, const nlohmann::ordered_json &members)
Builds one extensions entry: name, url, version, and members – every other member of the entry,...
::Transform to_transform(const nlohmann::ordered_json &transform)
Builds the Transform struct (scale + translate) from CityJSON's top-level transform member.
std::map< std::string, std::pair< std::uint16_t, ::ColumnType > > AttributeSchema
Attribute schema: name -> (column index, column type).
::flatbuffers::Offset<::flatbuffers::Vector< const ::DoubleVertex * > > to_templates_vertices(::flatbuffers::FlatBufferBuilder &fbb, const nlohmann::ordered_json &vertices_templates)
Builds Header.templates_vertices from CityJSON's geometry-templates.vertices-templates (f64 precision...
PocOffsets to_point_of_contact(::flatbuffers::FlatBufferBuilder &fbb, const nlohmann::ordered_json &poc)
::flatbuffers::Offset<::Appearance > to_appearance(::flatbuffers::FlatBufferBuilder &fbb, const nlohmann::ordered_json &appearance)
Builds the Appearance table (materials, textures, UV vertices, default themes) from a CityJSON appear...
::flatbuffers::Offset<::Geometry > to_geometry(::flatbuffers::FlatBufferBuilder &fbb, const nlohmann::ordered_json &geometry, const AttributeSchema *semantic_attr_schema)
Builds one Geometry table – boundaries, and whatever semantics, material and texture it carries – fro...
::GeographicalExtent to_geographical_extent(const std::array< double, 6 > &extent)
Builds a GeographicalExtent struct from a 6-element [minx,miny,minz,maxx,maxy,maxz] array.
std::optional< std::uint16_t > id_column_index(const std::optional< std::uint16_t > &id_index, const AttributeSchema &schema)
The index of the City Object id column: the next free one when id_index asks for an id index and sche...
::flatbuffers::Offset<::ReferenceSystem > to_reference_system(::flatbuffers::FlatBufferBuilder &fbb, const ParsedReferenceSystem &ref_sys)
Builds the ReferenceSystem table from a parsed metadata.referenceSystem URL.
std::optional< ParsedReferenceSystem > parse_reference_system(const std::string &url)
Parses a referenceSystem URL (https://www.opengis.net/def/crs/{authority}/{version}...
::flatbuffers::Offset<::Header > to_fcb_header(::flatbuffers::FlatBufferBuilder &fbb, const nlohmann::ordered_json &cj, const HeaderWriterOptions &options, const AttributeSchema &attr_schema, const AttributeSchema *semantic_attr_schema, const std::vector< AttributeIndexInfo > *attribute_indices_info)
Builds the whole Header table from the CityJSON metadata line (the first line of a CityJSONSeq: type/...
A metadata.referenceSystem URL, parsed into its OGC three-element form.
Builds the pointOfContact fields of a header from CityJSON metadata.pointOfContact.
std::optional<::flatbuffers::Offset<::flatbuffers::String > > contact_name
std::optional<::flatbuffers::Offset<::flatbuffers::String > > role
std::optional<::flatbuffers::Offset<::flatbuffers::String > > phone
std::optional<::flatbuffers::Offset<::flatbuffers::String > > website
std::optional<::flatbuffers::Offset<::flatbuffers::String > > address_postcode
std::optional<::flatbuffers::Offset<::flatbuffers::String > > email
std::optional<::flatbuffers::Offset<::flatbuffers::String > > address_thoroughfare_number
std::optional<::flatbuffers::Offset<::flatbuffers::String > > address_country
std::optional<::flatbuffers::Offset<::flatbuffers::String > > address_thoroughfare_name
std::optional<::flatbuffers::Offset<::flatbuffers::String > > contact_type
std::optional<::flatbuffers::Offset<::flatbuffers::String > > address_locality