FlatCityBuf C++ reader 0.10.0
Native C++17 reader for FlatCityBuf, the cloud-optimized CityJSON format
Loading...
Searching...
No Matches
fcb_writer.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef FCB_WITH_JSON
4
5# include <fcb/layout.hpp>
9
10# include <nlohmann/json.hpp>
11
12# include <array>
13# include <cstdint>
14# include <cstdio>
15# include <optional>
16# include <ostream>
17# include <string>
18# include <vector>
19
20namespace fcb {
21
30 bool write_index = true;
35 std::vector<std::pair<std::string, std::optional<std::uint16_t>>> attribute_indices;
36 std::optional<std::array<double, 6>> geographical_extent;
44 std::optional<std::uint16_t> id_index = kDefaultBranchingFactor;
45};
46
78class FcbWriter {
79 public:
88 FcbWriter(nlohmann::ordered_json cj, FcbWriterOptions options, AttributeSchema attr_schema,
89 std::optional<AttributeSchema> semantic_attr_schema);
90 ~FcbWriter();
91
92 FcbWriter(const FcbWriter&) = delete;
93 FcbWriter& operator=(const FcbWriter&) = delete;
94 FcbWriter(FcbWriter&&) = delete;
96
100 void add_feature(const nlohmann::ordered_json& city_json_feature);
101
113 void write(std::ostream& out);
114
119 std::vector<std::uint8_t> write();
120
121 private:
122 struct FeatureSlot {
123 std::uint64_t offset;
124 std::uint64_t size;
125 };
126
127 nlohmann::ordered_json cj_;
128 FcbWriterOptions options_;
129 AttributeSchema attr_schema_;
130 std::optional<AttributeSchema> semantic_attr_schema_;
131 std::vector<std::string> indexing_attr_;
133 std::optional<std::uint16_t> id_column_;
134
135 double scale_x_;
136 double scale_y_;
137 double translate_x_;
138 double translate_y_;
139
140 // `std::tmpfile()`, not a hand-rolled path under
141 // `std::filesystem::temp_directory_path()`: the C standard guarantees
142 // it names a file "different from any other existing file" and removes
143 // it automatically on close, which is exactly the anonymous,
144 // collision-safe temp file Rust's own `tempfile` crate provides --
145 // reusing that guarantee is safer than reimplementing it. 64-bit
146 // seeking uses `fseeko`/`ftello` (POSIX) or `_fseeki64` (MSVC) instead
147 // of `fseek`'s `long` offset, which is only 32 bits wide on some
148 // platforms -- a real limit for a writer whose whole point is handling
149 // files too large to fit in memory.
150 std::FILE* tmp_ = nullptr;
151 std::uint64_t tmp_write_pos_ = 0;
152 bool written_ = false;
153
154 std::vector<FeatureSlot> feat_offsets_;
155 std::vector<NodeItem> feat_nodes_;
156 std::vector<std::vector<AttributeIndexEntry>> index_entries_by_feature_;
157};
158
159} // namespace fcb
160
161#endif // FCB_WITH_JSON
void add_feature(const nlohmann::ordered_json &city_json_feature)
Encodes and spools one CityJSONFeature line.
std::vector< std::uint8_t > write()
Convenience wrapper around write(std::ostream&) that returns the complete file as one buffer.
FcbWriter & operator=(const FcbWriter &)=delete
FcbWriter(nlohmann::ordered_json cj, FcbWriterOptions options, AttributeSchema attr_schema, std::optional< AttributeSchema > semantic_attr_schema)
cj is the CityJSONSeq's metadata line (first line: type/ version/transform/metadata/etc).
FcbWriter(const FcbWriter &)=delete
FcbWriter(FcbWriter &&)=delete
FcbWriter & operator=(FcbWriter &&)=delete
constexpr std::uint16_t kDefaultNodeSize
Definition layout.hpp:16
constexpr std::uint16_t kDefaultBranchingFactor
Mirrors static_btree::DEFAULT_BRANCHING_FACTOR (static_btree/mod.rs:19), used whenever a caller reque...
std::map< std::string, std::pair< std::uint16_t, ::ColumnType > > AttributeSchema
Attribute schema: name -> (column index, column type).
Definition attribute.hpp:43
Configuration for FcbWriter.
std::optional< std::uint16_t > id_index
The branching factor of the index over City Object ids, or std::nullopt to write none.
bool write_index
false forces index_node_size to 0 in the written header (no R-tree at all), regardless of index_node_...
std::uint16_t index_node_size
std::vector< std::pair< std::string, std::optional< std::uint16_t > > > attribute_indices
(attribute name, branching factor); std::nullopt branching factor means build_static_btree's own defa...
std::optional< std::array< double, 6 > > geographical_extent