FlatCityBuf C++ reader 0.8.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;
37};
38
70class FcbWriter {
71 public:
80 FcbWriter(nlohmann::ordered_json cj, FcbWriterOptions options, AttributeSchema attr_schema,
81 std::optional<AttributeSchema> semantic_attr_schema);
82 ~FcbWriter();
83
84 FcbWriter(const FcbWriter&) = delete;
85 FcbWriter& operator=(const FcbWriter&) = delete;
86 FcbWriter(FcbWriter&&) = delete;
88
92 void add_feature(const nlohmann::ordered_json& city_json_feature);
93
105 void write(std::ostream& out);
106
111 std::vector<std::uint8_t> write();
112
113 private:
114 struct FeatureSlot {
115 std::uint64_t offset;
116 std::uint64_t size;
117 };
118
119 nlohmann::ordered_json cj_;
120 FcbWriterOptions options_;
121 AttributeSchema attr_schema_;
122 std::optional<AttributeSchema> semantic_attr_schema_;
123 std::vector<std::string> indexing_attr_;
124
125 double scale_x_;
126 double scale_y_;
127 double translate_x_;
128 double translate_y_;
129
130 // `std::tmpfile()`, not a hand-rolled path under
131 // `std::filesystem::temp_directory_path()`: the C standard guarantees
132 // it names a file "different from any other existing file" and removes
133 // it automatically on close, which is exactly the anonymous,
134 // collision-safe temp file Rust's own `tempfile` crate provides --
135 // reusing that guarantee is safer than reimplementing it. 64-bit
136 // seeking uses `fseeko`/`ftello` (POSIX) or `_fseeki64` (MSVC) instead
137 // of `fseek`'s `long` offset, which is only 32 bits wide on some
138 // platforms -- a real limit for a writer whose whole point is handling
139 // files too large to fit in memory.
140 std::FILE* tmp_ = nullptr;
141 std::uint64_t tmp_write_pos_ = 0;
142 bool written_ = false;
143
144 std::vector<FeatureSlot> feat_offsets_;
145 std::vector<NodeItem> feat_nodes_;
146 std::vector<std::vector<AttributeIndexEntry>> index_entries_by_feature_;
147};
148
149} // namespace fcb
150
151#endif // FCB_WITH_JSON
Streaming writer for one .fcb file, mirroring Rust's own FcbWriter (writer/mod.rs) shape: each add_fe...
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(const FcbWriter &)=delete
FcbWriter(FcbWriter &&)=delete
FcbWriter & operator=(FcbWriter &&)=delete
constexpr std::uint16_t kDefaultNodeSize
Definition layout.hpp:16
std::map< std::string, std::pair< std::uint16_t, ::ColumnType > > AttributeSchema
Attribute schema: name -> (column index, column type).
Definition attribute.hpp:42
std::uint64_t offset
Definition stree.cpp:57
Configuration for FcbWriter.
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