Skip to content

Commit

Permalink
invalid tag exception?
Browse files Browse the repository at this point in the history
  • Loading branch information
zhixiong-tang committed Nov 13, 2023
1 parent 7bd8a01 commit 6d3d435
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
22 changes: 20 additions & 2 deletions src/geobuf/geobuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ std::string Encoder::encode(const mapbox::geojson::geojson &geojson)
geojson.match(
[&](const mapbox::geojson::feature_collection &features) {
offsets.clear();
offsets.reserve(features.size() + 1);
offsets.reserve(features.size() + 2);
protozero::pbf_writer pbf_fc{pbf, 4};
writeFeatureCollection(features, pbf_fc);
},
Expand Down Expand Up @@ -392,10 +392,11 @@ void Encoder::writeFeatureCollection(
protozero::pbf_writer pbf_f{pbf, 1};
writeFeature(feature, pbf_f);
}
offsets.push_back(data.size());
if (!geojson.custom_properties.empty()) {
offsets.push_back(data.size());
writeProps(geojson.custom_properties, pbf, 15);
}
offsets.push_back(data.size());
}

void Encoder::writeFeature(const mapbox::geojson::feature &feature, Pbf &pbf)
Expand Down Expand Up @@ -673,6 +674,23 @@ void Decoder::decode_header(const uint8_t *data, std::size_t size)
}
}

mapbox::geojson::feature Decoder::decode_feature(const uint8_t *data,
std::size_t size)
{
auto pbf =
protozero::pbf_reader{reinterpret_cast<const char *>(data), size};
if (!pbf.next() || pbf.tag() != 1) {
return {};
}
protozero::pbf_reader pbf_f = pbf.get_message();
return readFeature(pbf_f);
}
mapbox::feature::property_map Decoder::decode_non_features(const uint8_t *data,
std::size_t size)
{
return {};
}

bool Decoder::decode(const std::string &input_path,
const std::string &output_path, //
bool indent, bool sort_keys)
Expand Down
4 changes: 4 additions & 0 deletions src/geobuf/geobuf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ struct Decoder
const std::string &indent = "");
mapbox::geojson::geojson decode(const std::string &pbf_bytes);
void decode_header(const uint8_t *data, std::size_t size);
mapbox::geojson::feature decode_feature(const uint8_t *data,
std::size_t size);
mapbox::feature::property_map decode_non_features(const uint8_t *data,
std::size_t size);

bool decode(const std::string &input_path, const std::string &output_path,
bool indent = false, bool sort_keys = false);
Expand Down
20 changes: 16 additions & 4 deletions src/geobuf/geobuf_plus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,27 @@ struct GeobufPlus
return {};
}

mapbox::geojson::feature decode_one_feature(const char *ptr) const
mapbox::geojson::feature decode_feature(int index)
{
return mapbox::geojson::feature{};
return decode_feature(mmap.data() + offsets[index],
offsets[index + 1] - offsets[index]);
}

mapbox::geojson::feature decode_feature(const uint8_t *data, size_t size)
{
return decoder.decode_feature(data, size);
}

mapbox::geojson::feature_collection
decode(const void *data, const std::vector<int> &offsets) const
decode_feature(const uint8_t *data,
const std::vector<std::array<int, 2>> &index)
{
return {};
mapbox::geojson::feature_collection fc;
fc.reserve(index.size());
for (auto &pair : index) {
fc.push_back(decode_feature(data + pair[0], pair[1]));
}
return fc;
}

mapbox::feature::property_map
Expand Down
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ PYBIND11_MODULE(_pybind11_geobuf, m)
.def("mmap_init", &GeobufPlus::mmap_init, "index_path"_a,
"geobuf_path"_a)
.def("init_index", &GeobufPlus::init_index, "index_bytes"_a)
.def("decode_feature",
py::overload_cast<int>(&GeobufPlus::decode_feature), "index"_a)
//
.def_static("encode",
py::overload_cast<const std::string &, const std::string &,
Expand Down

0 comments on commit 6d3d435

Please sign in to comment.