summaryrefslogtreecommitdiff
path: root/src/mbgl/style/expression/within.cpp
blob: 1b1b8b11b988f7fa58e2c52d1479ee938c1ed961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <mapbox/geojson.hpp>
#include <mapbox/geometry.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/expression/within.hpp>
#include <mbgl/tile/geometry_tile_data.hpp>
#include <mbgl/util/string.hpp>

namespace {

double isLeft(mbgl::Point<double> P0, mbgl::Point<double> P1, mbgl::Point<double> P2) {
    return ((P1.x - P0.x) * (P2.y - P0.y) - (P2.x - P0.x) * (P1.y - P0.y));
}

// winding number algorithm for checking if point inside a ploygon or not.
// http://geomalgorithms.com/a03-_inclusion.html#wn_PnPoly()
bool pointWithinPoly(mbgl::Point<double> point, const mapbox::geometry::polygon<double>& polys) {
    //  wn = the winding number (=0 only when point is outside)
    int wn = 0;
    for (auto poly : polys) {
        auto size = poly.size();
        auto i = size;
        // loop through every edge of the polygon
        for (i = 0; i < size - 1; ++i) {
            if (poly[i].y <= point.y) {
                if (poly[i + 1].y > point.y) { // upward horizontal crossing from point to edge E(poly[i], poly[i+1])
                    if (isLeft(poly[i], poly[i + 1], point) > 0) {
                        ++wn;
                    }
                }
            } else {
                if (poly[i + 1].y <= point.y) { // downward crossing
                    if (isLeft(poly[i], poly[i + 1], point) < 0) {
                        --wn;
                    }
                }
            }
        }
        if (wn != 0) {
            return true;
        }
    }
    return wn != 0;
}

bool pointsWithinPoly(const mbgl::GeometryTileFeature& feature,
                      const mbgl::CanonicalTileID& canonical,
                      const mbgl::GeoJSON& geoJson) {
    bool result = false;
    geoJson.match(
        [&](const mapbox::geometry::geometry<double>& geometrySet) {
            geometrySet.match(
                [&](const mapbox::geometry::polygon<double>& polygons) {
                    convertGeometry(feature, canonical)
                        .match(
                            [&](const mapbox::geometry::point<double>& point) {
                                result = pointWithinPoly(point, polygons);
                            },
                            [&](const mapbox::geometry::multi_point<double>& points) {
                                for (const auto& p : points) {
                                    result = pointWithinPoly(p, polygons);
                                    if (!result) {
                                        return;
                                    }
                                }
                            },
                            [&](const auto&) { return; });
                },
                [&](const auto&) { return; });
        },
        [&](const auto&) { return; });

    return result;
}

mbgl::optional<mbgl::GeoJSON> parseValue(const mbgl::style::conversion::Convertible& value_,
                                         mbgl::style::expression::ParsingContext& ctx) {
    if (isObject(value_)) {
        const auto geometryType = objectMember(value_, "type");
        if (geometryType) {
            const auto type = toString(*geometryType);
            if (type && *type == "Polygon") {
                mbgl::style::conversion::Error error;
                auto geojson = toGeoJSON(value_, error);
                if (geojson && error.message.empty()) {
                    return geojson;
                }
                ctx.error(error.message);
            }
        }
    }
    ctx.error("'Within' expression requires valid geojson source that contains polygon geometry type.");
    return mbgl::optional<mbgl::GeoJSON>();
}

} // namespace

namespace mbgl {
namespace style {
namespace expression {

using namespace mbgl::style::conversion;

EvaluationResult Within::evaluate(const EvaluationContext& params) const {
    if (!params.feature || !params.canonical) {
        return false;
    }
    auto geometryType = params.feature->getType();
    // Currently only support Point/Points in polygon
    if (geometryType == FeatureType::Point) {
        return pointsWithinPoly(*params.feature, *params.canonical, geoJSONSource);
    }
    return false;
}

ParseResult Within::parse(const Convertible& value, ParsingContext& ctx) {
    if (isArray(value)) {
        // object value, quoted with ["Within", value]
        if (arrayLength(value) != 2) {
            ctx.error("'Within' expression requires exactly one argument, but found " +
                      util::toString(arrayLength(value) - 1) + " instead.");
            return ParseResult();
        }

        auto parsedValue = parseValue(arrayMember(value, 1), ctx);
        if (!parsedValue) {
            return ParseResult();
        }
        return ParseResult(std::make_unique<Within>(*parsedValue));
    }
    return ParseResult();
}

mbgl::Value Within::serialize() const {
    return std::vector<mbgl::Value>{{getOperator()}, {mapbox::geojson::stringify(geoJSONSource)}};
}

} // namespace expression
} // namespace style
} // namespace mbgl