|
| 1 | +#ifndef CUBAO_PLANET_HPP |
| 2 | +#define CUBAO_PLANET_HPP |
| 3 | + |
| 4 | +// https://github.com/microsoft/vscode-cpptools/issues/9692 |
| 5 | +#if __INTELLISENSE__ |
| 6 | +#undef __ARM_NEON |
| 7 | +#undef __ARM_NEON__ |
| 8 | +#endif |
| 9 | + |
| 10 | +#include "geojson_cropping.hpp" |
| 11 | +#include "packedrtree.hpp" |
| 12 | + |
| 13 | +namespace cubao |
| 14 | +{ |
| 15 | +struct Planet |
| 16 | +{ |
| 17 | + using FeatureCollection = mapbox::geojson::feature_collection; |
| 18 | + Planet() = default; |
| 19 | + Planet(const FeatureCollection &features) { this->features(features); } |
| 20 | + |
| 21 | + const FeatureCollection &features() const { return features_; } |
| 22 | + Planet &features(const FeatureCollection &features) |
| 23 | + { |
| 24 | + features_ = features; |
| 25 | + rtree_.reset(); |
| 26 | + return *this; |
| 27 | + } |
| 28 | + |
| 29 | + void build() const { this->rtree(); } |
| 30 | + |
| 31 | + // TODO, query by style expression |
| 32 | + Eigen::VectorXi query(const Eigen::Vector2d &min, |
| 33 | + const Eigen::Vector2d &max) const |
| 34 | + { |
| 35 | + auto &tree = this->rtree(); |
| 36 | + auto hits = tree.search(min[0], min[1], max[0], max[1]); |
| 37 | + Eigen::VectorXi index(hits.size()); |
| 38 | + for (int i = 0, N = hits.size(); i < N; ++i) { |
| 39 | + index[i] = hits[i].offset; |
| 40 | + } |
| 41 | + return index; |
| 42 | + } |
| 43 | + FeatureCollection copy(const Eigen::VectorXi &index) const |
| 44 | + { |
| 45 | + auto fc = FeatureCollection(); |
| 46 | + fc.reserve(index.size()); |
| 47 | + for (int i = 0, N = index.size(); i < N; ++i) { |
| 48 | + fc.push_back(features_[index[i]]); |
| 49 | + } |
| 50 | + return fc; |
| 51 | + } |
| 52 | + |
| 53 | + FeatureCollection crop(const Eigen::Ref<const RowVectorsNx2> &polygon, |
| 54 | + const std::string &clipping_mode = "longest", |
| 55 | + bool strip_properties = false, |
| 56 | + bool is_wgs84 = true) const |
| 57 | + { |
| 58 | + auto bbox = row_vectors_to_bbox(polygon); |
| 59 | + auto hits = |
| 60 | + this->query({bbox.min.x, bbox.min.y}, {bbox.max.x, bbox.max.y}); |
| 61 | + auto fc = FeatureCollection(); |
| 62 | + fc.reserve(hits.size()); |
| 63 | + for (auto idx : hits) { |
| 64 | + auto &feature = features_[idx]; |
| 65 | + if (!feature.geometry.is<mapbox::geojson::line_string>() || |
| 66 | + clipping_mode == "whole") { |
| 67 | + // only check centroid |
| 68 | + auto centroid = geometry_to_centroid(feature.geometry); |
| 69 | + auto mask = point_in_polygon( |
| 70 | + Eigen::Map<const RowVectorsNx2>(¢roid.x, 1, 2), |
| 71 | + polygon); |
| 72 | + if (mask[0]) { |
| 73 | + fc.emplace_back( |
| 74 | + feature.geometry, |
| 75 | + strip_properties |
| 76 | + ? mapbox::feature::property_map{{"index", idx}} |
| 77 | + : feature.properties, |
| 78 | + feature.id); |
| 79 | + } |
| 80 | + continue; |
| 81 | + } |
| 82 | + auto &line_string = |
| 83 | + feature.geometry.get<mapbox::geojson::line_string>(); |
| 84 | + auto polyline = Eigen::Map<const RowVectors>(&line_string[0].x, |
| 85 | + line_string.size(), 3); |
| 86 | + auto segs = polyline_in_polygon(polyline, polygon, is_wgs84); |
| 87 | + if (segs.empty()) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + if (clipping_mode == "first") { |
| 91 | + auto &coords = segs.begin()->second; |
| 92 | + auto geom = mapbox::geojson::line_string(); |
| 93 | + geom.resize(coords.rows()); |
| 94 | + as_row_vectors(geom) = coords; |
| 95 | + fc.emplace_back( |
| 96 | + geom, |
| 97 | + strip_properties |
| 98 | + ? mapbox::feature::property_map{{"index", idx}} |
| 99 | + : feature.properties, |
| 100 | + feature.id); |
| 101 | + continue; |
| 102 | + } |
| 103 | + // longest or all |
| 104 | + std::vector<PolylineChunks::key_type> keys; |
| 105 | + keys.reserve(segs.size()); |
| 106 | + for (auto &pair : segs) { |
| 107 | + keys.push_back(pair.first); |
| 108 | + } |
| 109 | + if (clipping_mode == "longest") { // else assume all |
| 110 | + auto itr = std::max_element( |
| 111 | + keys.begin(), keys.end(), |
| 112 | + [](const auto &k1, const auto &k2) { |
| 113 | + double len1 = std::get<5>(k1) - std::get<2>(k1); |
| 114 | + double len2 = std::get<5>(k2) - std::get<2>(k2); |
| 115 | + return len1 < len2; |
| 116 | + }); |
| 117 | + keys = {*itr}; // pick longest |
| 118 | + } |
| 119 | + for (auto &key : keys) { |
| 120 | + auto &coords = segs[key]; |
| 121 | + auto geom = mapbox::geojson::line_string(); |
| 122 | + geom.resize(coords.rows()); |
| 123 | + as_row_vectors(geom) = coords; |
| 124 | + fc.emplace_back( |
| 125 | + geom, |
| 126 | + strip_properties |
| 127 | + ? mapbox::feature::property_map{{"index", idx}} |
| 128 | + : feature.properties, |
| 129 | + feature.id); |
| 130 | + } |
| 131 | + } |
| 132 | + return fc; |
| 133 | + } |
| 134 | + |
| 135 | + private: |
| 136 | + FeatureCollection features_; |
| 137 | + |
| 138 | + mutable std::optional<FlatGeobuf::PackedRTree> rtree_; |
| 139 | + |
| 140 | + template <typename G> |
| 141 | + static FlatGeobuf::NodeItem envelope_2d(G const &geometry, uint64_t index) |
| 142 | + { |
| 143 | + // mapbox/geometry/envelope.hpp |
| 144 | + using limits = std::numeric_limits<double>; |
| 145 | + constexpr double min_t = |
| 146 | + limits::has_infinity ? -limits::infinity() : limits::min(); |
| 147 | + constexpr double max_t = |
| 148 | + limits::has_infinity ? limits::infinity() : limits::max(); |
| 149 | + double min_x = max_t; |
| 150 | + double min_y = max_t; |
| 151 | + double max_x = min_t; |
| 152 | + double max_y = min_t; |
| 153 | + mapbox::geometry::for_each_point( |
| 154 | + geometry, [&](mapbox::geojson::point const &point) { |
| 155 | + if (min_x > point.x) |
| 156 | + min_x = point.x; |
| 157 | + if (min_y > point.y) |
| 158 | + min_y = point.y; |
| 159 | + if (max_x < point.x) |
| 160 | + max_x = point.x; |
| 161 | + if (max_y < point.y) |
| 162 | + max_y = point.y; |
| 163 | + }); |
| 164 | + return {min_x, min_y, max_x, max_y, index}; |
| 165 | + } |
| 166 | + |
| 167 | + FlatGeobuf::PackedRTree &rtree() const |
| 168 | + { |
| 169 | + if (rtree_) { |
| 170 | + return *rtree_; |
| 171 | + } |
| 172 | + auto nodes = std::vector<FlatGeobuf::NodeItem>{}; |
| 173 | + nodes.reserve(features_.size()); |
| 174 | + uint64_t index{0}; |
| 175 | + for (auto &feature : features_) { |
| 176 | + nodes.emplace_back(envelope_2d(feature.geometry, index++)); |
| 177 | + } |
| 178 | + auto extent = calcExtent(nodes); |
| 179 | + hilbertSort(nodes, extent); |
| 180 | + rtree_ = FlatGeobuf::PackedRTree(nodes, extent); |
| 181 | + return *rtree_; |
| 182 | + } |
| 183 | +}; |
| 184 | +} // namespace cubao |
| 185 | + |
| 186 | +#endif |
0 commit comments