|
| 1 | +#include "lib.rs.h" |
| 2 | + |
| 3 | +#include <fstream> |
| 4 | +#include <iostream> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +int main(int argc, char* argv[]) { |
| 8 | + if (argc < 3) { |
| 9 | + std::cerr << "Usage: " << argv[0] << " <input.city.jsonl> <output.fcb>" << std::endl; |
| 10 | + return 1; |
| 11 | + } |
| 12 | + |
| 13 | + std::string jsonl_path = argv[1]; |
| 14 | + std::string fcb_path = argv[2]; |
| 15 | + |
| 16 | + try { |
| 17 | + // === Step 1: Encode CityJSONSeq -> FCB === |
| 18 | + std::cout << "=== Encoding CityJSONSeq -> FCB ===" << std::endl; |
| 19 | + |
| 20 | + std::ifstream infile(jsonl_path); |
| 21 | + if (!infile.is_open()) { |
| 22 | + std::cerr << "Failed to open: " << jsonl_path << std::endl; |
| 23 | + return 1; |
| 24 | + } |
| 25 | + |
| 26 | + std::string line; |
| 27 | + // First line is CityJSON metadata |
| 28 | + if (!std::getline(infile, line)) { |
| 29 | + std::cerr << "Empty input file" << std::endl; |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + auto writer = fcb::fcb_writer_new(line); |
| 34 | + std::cout << "Created writer with metadata" << std::endl; |
| 35 | + |
| 36 | + size_t write_count = 0; |
| 37 | + while (std::getline(infile, line)) { |
| 38 | + if (line.empty()) |
| 39 | + continue; |
| 40 | + fcb::fcb_writer_add_feature(*writer, line); |
| 41 | + write_count++; |
| 42 | + } |
| 43 | + infile.close(); |
| 44 | + |
| 45 | + std::cout << "Added " << write_count << " features" << std::endl; |
| 46 | + |
| 47 | + fcb::fcb_writer_write(std::move(writer), fcb_path); |
| 48 | + std::cout << "Wrote FCB file: " << fcb_path << std::endl; |
| 49 | + |
| 50 | + // === Step 2: Decode FCB -> read back === |
| 51 | + std::cout << "\n=== Decoding FCB ===" << std::endl; |
| 52 | + |
| 53 | + auto reader = fcb::fcb_reader_open(fcb_path); |
| 54 | + auto meta = fcb::fcb_reader_metadata(*reader); |
| 55 | + |
| 56 | + std::cout << "Features count: " << meta.features_count << std::endl; |
| 57 | + std::cout << "Has spatial index: " << (meta.has_spatial_index ? "yes" : "no") << std::endl; |
| 58 | + |
| 59 | + auto iter = fcb::fcb_reader_select_all(std::move(reader)); |
| 60 | + |
| 61 | + size_t read_count = 0; |
| 62 | + while (fcb::fcb_iterator_next(*iter)) { |
| 63 | + auto feature = fcb::fcb_iterator_current(*iter); |
| 64 | + read_count++; |
| 65 | + |
| 66 | + // Print first 3 features briefly |
| 67 | + if (read_count <= 3) { |
| 68 | + std::cout << " Feature " << read_count << ": ID=" << std::string(feature.id) |
| 69 | + << std::endl; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + std::cout << "Total features read back: " << read_count << std::endl; |
| 74 | + |
| 75 | + // === Step 3: Verify === |
| 76 | + std::cout << "\n=== Roundtrip Verification ===" << std::endl; |
| 77 | + if (read_count == write_count) { |
| 78 | + std::cout << "PASS: wrote " << write_count << " features, read back " << read_count |
| 79 | + << std::endl; |
| 80 | + } else { |
| 81 | + std::cerr << "FAIL: wrote " << write_count << " but read back " << read_count |
| 82 | + << std::endl; |
| 83 | + return 1; |
| 84 | + } |
| 85 | + |
| 86 | + } catch (const std::exception& e) { |
| 87 | + std::cerr << "Error: " << e.what() << std::endl; |
| 88 | + return 1; |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments