From 8dac8f1150910dfc62759330c7e3dcdd7e5b53a2 Mon Sep 17 00:00:00 2001 From: zmiao Date: Mon, 20 Apr 2020 11:33:46 +0300 Subject: [core] Distance expression: add unit tests for distance expression --- src/mbgl/style/expression/distance.cpp | 8 +- test/fixtures/expression_equality/distance.a.json | 5 + test/fixtures/expression_equality/distance.b.json | 5 + test/fixtures/geometry_data/line_string_1.geojson | 259 +++++++++++++++ test/fixtures/geometry_data/line_string_2.geojson | 106 ++++++ .../geometry_data/multi_line_string_1.geojson | 199 +++++++++++ .../geometry_data/multi_line_string_2.geojson | 88 +++++ test/fixtures/geometry_data/multi_point_1.geojson | 91 +++++ test/fixtures/geometry_data/multi_point_2.geojson | 85 +++++ test/style/property_expression.test.cpp | 370 ++++++++++++++++++++- 10 files changed, 1211 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/expression_equality/distance.a.json create mode 100644 test/fixtures/expression_equality/distance.b.json create mode 100644 test/fixtures/geometry_data/line_string_1.geojson create mode 100644 test/fixtures/geometry_data/line_string_2.geojson create mode 100644 test/fixtures/geometry_data/multi_line_string_1.geojson create mode 100644 test/fixtures/geometry_data/multi_line_string_2.geojson create mode 100644 test/fixtures/geometry_data/multi_point_1.geojson create mode 100644 test/fixtures/geometry_data/multi_point_2.geojson diff --git a/src/mbgl/style/expression/distance.cpp b/src/mbgl/style/expression/distance.cpp index 67a460b7d7..c4d0c7b56d 100644 --- a/src/mbgl/style/expression/distance.cpp +++ b/src/mbgl/style/expression/distance.cpp @@ -21,8 +21,8 @@ namespace mbgl { namespace { -const std::size_t MinPointsSize = 500; -const std::size_t MinLinePointsSize = 200; +const std::size_t MinPointsSize = 100; +const std::size_t MinLinePointsSize = 50; using BBox = std::array; const BBox DefaultBBox = BBox{std::numeric_limits::infinity(), @@ -414,9 +414,9 @@ optional parseValue(const style::conversion::Convertible& value, styl } else if (*input == "NauticalMiles") { unit = mapbox::cheap_ruler::CheapRuler::Unit::NauticalMiles; } else if (*input == "Yards") { - unit = mapbox::cheap_ruler::CheapRuler::Unit::Kilometers; + unit = mapbox::cheap_ruler::CheapRuler::Unit::Yards; } else if (*input == "Feet") { - unit = mapbox::cheap_ruler::CheapRuler::Unit::Miles; + unit = mapbox::cheap_ruler::CheapRuler::Unit::Feet; } else if (*input == "Inches") { unit = mapbox::cheap_ruler::CheapRuler::Unit::Inches; } else { diff --git a/test/fixtures/expression_equality/distance.a.json b/test/fixtures/expression_equality/distance.a.json new file mode 100644 index 0000000000..a1a7db1e39 --- /dev/null +++ b/test/fixtures/expression_equality/distance.a.json @@ -0,0 +1,5 @@ +["distance", { + "type": "LineString", + "coordinates": [[0, 0], [0, 5], [5, 5], [5, 0]] + }, "Meters" +] \ No newline at end of file diff --git a/test/fixtures/expression_equality/distance.b.json b/test/fixtures/expression_equality/distance.b.json new file mode 100644 index 0000000000..77f5b4b5ba --- /dev/null +++ b/test/fixtures/expression_equality/distance.b.json @@ -0,0 +1,5 @@ +["distance", { + "type": "LineString", + "coordinates": [[0, 0], [0, 6], [5, 5], [5, 0]] + }, "Meters" +] \ No newline at end of file diff --git a/test/fixtures/geometry_data/line_string_1.geojson b/test/fixtures/geometry_data/line_string_1.geojson new file mode 100644 index 0000000000..eb0b8e3780 --- /dev/null +++ b/test/fixtures/geometry_data/line_string_1.geojson @@ -0,0 +1,259 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": + [ + [ + 24.919674396514893, + 60.16455037398201 + ], + [ + 24.921332001686096, + 60.16460108220136 + ], + [ + 24.92250680923462, + 60.164705167248556 + ], + [ + 24.92311298847198, + 60.16476254732591 + ], + [ + 24.923794269561768, + 60.16481192080062 + ], + [ + 24.924674034118652, + 60.16487063510643 + ], + [ + 24.92523193359375, + 60.1648919857371 + ], + [ + 24.925435781478882, + 60.164910667527536 + ], + [ + 24.92561012506485, + 60.16495069989984 + ], + [ + 24.925816655158997, + 60.165010748366896 + ], + [ + 24.926661550998688, + 60.165288304299814 + ], + [ + 24.92731064558029, + 60.1655044760466 + ], + [ + 24.927439391613003, + 60.165561854728224 + ], + [ + 24.928563237190247, + 60.16595149335769 + ], + [ + 24.929917752742767, + 60.16642652598447 + ], + [ + 24.930156469345093, + 60.16644253820056 + ], + [ + 24.930403232574463, + 60.166394501528906 + ], + [ + 24.93046224117279, + 60.16634913349672 + ], + [ + 24.930633902549744, + 60.166365145750525 + ], + [ + 24.93071973323822, + 60.16642385728104 + ], + [ + 24.93074119091034, + 60.166511924380124 + ], + [ + 24.930797517299652, + 60.16665069629966 + ], + [ + 24.931159615516663, + 60.16733387258923 + ], + [ + 24.93122398853302, + 60.1673819078876 + ], + [ + 24.93122935295105, + 60.167383242200444 + ], + [ + 24.931269586086273, + 60.16739658532588 + ], + [ + 24.93135541677475, + 60.167392582388814 + ], + [ + 24.93148148059845, + 60.16739525101357 + ], + [ + 24.931594133377075, + 60.167403256886566 + ], + [ + 24.931905269622803, + 60.167397919638134 + ], + [ + 24.932076930999752, + 60.167405925510465 + ], + [ + 24.932237863540646, + 60.16741660000386 + ], + [ + 24.93250608444214, + 60.1674673038002 + ], + [ + 24.933310747146603, + 60.167734164596524 + ], + [ + 24.934450685977936, + 60.168130448879936 + ], + [ + 24.934933483600616, + 60.16830123659222 + ], + [ + 24.934713542461395, + 60.16847068914809 + ], + [ + 24.934949576854706, + 60.16855474796568 + ], + [ + 24.935268759727478, + 60.16866682605459 + ], + [ + 24.936030507087704, + 60.16894701960423 + ], + [ + 24.93632823228836, + 60.16903107720314 + ], + [ + 24.93696391582489, + 60.16926456940512 + ], + [ + 24.938240647315975, + 60.16974755943638 + ], + [ + 24.938401579856873, + 60.16979692542083 + ], + [ + 24.938731491565704, + 60.16987297557607 + ], + [ + 24.939565658569332, + 60.17019985581767 + ], + [ + 24.939801692962643, + 60.17026389727989 + ], + [ + 24.939911663532257, + 60.17027590504016 + ], + [ + 24.941115975379944, + 60.170305257324564 + ], + [ + 24.94418442249298, + 60.170386643066905 + ], + [ + 24.94469404220581, + 60.17047469954465 + ], + [ + 24.945133924484253, + 60.17062412817889 + ], + [ + 24.94567573070526, + 60.17080023962529 + ], + [ + 24.946464300155636, + 60.17102971676008 + ], + [ + 24.94716167449951, + 60.17129921692988 + ], + [ + 24.94742453098297, + 60.17141929055101 + ], + [ + 24.947537183761593, + 60.17157405145976 + ], + [ + 24.9476820230484, + 60.171819532785136 + ], + [ + 24.948642253875732, + 60.17268670756399 + ], + [ + 24.949076771736145, + 60.17306558905074 + ], + [ + 24.949543476104736, + 60.1734391299005 + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/geometry_data/line_string_2.geojson b/test/fixtures/geometry_data/line_string_2.geojson new file mode 100644 index 0000000000..718051eaee --- /dev/null +++ b/test/fixtures/geometry_data/line_string_2.geojson @@ -0,0 +1,106 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "LineString", + "coordinates": [ + [ + 24.928536415100098, + 60.1628956417812 + ], + [ + 24.93021011352539, + 60.16348281435769 + ], + [ + 24.93299961090088, + 60.164454295036016 + ], + [ + 24.93443727493286, + 60.16346146281146 + ], + [ + 24.936904907226562, + 60.16434754032213 + ], + [ + 24.93551015853882, + 60.165351020940705 + ], + [ + 24.93443727493286, + 60.16613029857937 + ], + [ + 24.93673324584961, + 60.16695225633493 + ], + [ + 24.937891960144043, + 60.16614097348721 + ], + [ + 24.939265251159668, + 60.16512684175867 + ], + [ + 24.941647052764893, + 60.165948824615676 + ], + [ + 24.94020938873291, + 60.16695225633493 + ], + [ + 24.94175434112549, + 60.16749665859389 + ], + [ + 24.942162036895752, + 60.16758205420823 + ], + [ + 24.943556785583496, + 60.16667471445701 + ], + [ + 24.943921566009518, + 60.166706738639476 + ], + [ + 24.943857192993164, + 60.166365145750525 + ], + [ + 24.944028854370117, + 60.16550047287903 + ], + [ + 24.942784309387207, + 60.1651375169925 + ], + [ + 24.940338134765625, + 60.16435821580911 + ], + [ + 24.938042163848877, + 60.16353619316258 + ], + [ + 24.935553073883057, + 60.16268212187951 + ], + [ + 24.934222698211666, + 60.16212696364248 + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/geometry_data/multi_line_string_1.geojson b/test/fixtures/geometry_data/multi_line_string_1.geojson new file mode 100644 index 0000000000..cde096af19 --- /dev/null +++ b/test/fixtures/geometry_data/multi_line_string_1.geojson @@ -0,0 +1,199 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [ + [ + 24.920597076416016, + 60.18971338103967 + ], + [ + 24.91849422454834, + 60.1880919194332 + ], + [ + 24.920125007629395, + 60.18672641596177 + ], + [ + 24.92398738861084, + 60.183888547404635 + ], + [ + 24.927291870117188, + 60.181733310424505 + ], + [ + 24.925832748413082, + 60.18105043355454 + ], + [ + 24.924373626708984, + 60.18049558564138 + ], + [ + 24.92321491241455, + 60.17921513160081 + ], + [ + 24.922914505004883, + 60.17891635180763 + ] + ], + [ + [ + 24.889440536499023, + 60.161070001844394 + ], + [ + 24.892358779907227, + 60.16132623812907 + ], + [ + 24.901306629180908, + 60.16263941773268 + ], + [ + 24.9035382270813, + 60.162863613884376 + ], + [ + 24.905061721801758, + 60.16350416589006 + ], + [ + 24.905898571014404, + 60.16381376155042 + ], + [ + 24.906907081604004, + 60.16405930086227 + ], + [ + 24.90765810012817, + 60.16415538096314 + ], + [ + 24.9079155921936, + 60.16415538096314 + ], + [ + 24.91274356842041, + 60.16435821580911 + ], + [ + 24.915618896484375, + 60.164443619580226 + ], + [ + 24.916906356811523, + 60.16429416283512 + ], + [ + 24.91969585418701, + 60.164443619580226 + ], + [ + 24.92089748382568, + 60.16442226865825 + ], + [ + 24.920983314514157, + 60.16369632905707 + ], + [ + 24.91892337799072, + 60.163546868913166 + ], + [ + 24.915404319763184, + 60.16339740808949 + ], + [ + 24.913859367370605, + 60.16339740808949 + ] + ], + [ + [ + 24.94943618774414, + 60.183547133154306 + ], + [ + 24.94969367980957, + 60.18105043355454 + ], + [ + 24.94999408721924, + 60.17979134209734 + ], + [ + 24.94999408721924, + 60.17831878406661 + ], + [ + 24.950551986694336, + 60.178233416358864 + ], + [ + 24.95068073272705, + 60.1785748858584 + ], + [ + 24.950509071350098, + 60.17979134209734 + ], + [ + 24.950509071350098, + 60.17998341001618 + ], + [ + 24.95166778564453, + 60.18047424514982 + ], + [ + 24.956259727478027, + 60.18205340406896 + ], + [ + 24.95922088623047, + 60.180623628299564 + ], + [ + 24.956817626953125, + 60.17951390867583 + ], + [ + 24.956603050231934, + 60.17910842484387 + ], + [ + 24.957289695739746, + 60.177955969776264 + ], + [ + 24.9574613571167, + 60.1767181026212 + ], + [ + 24.958319664001465, + 60.176291241060305 + ], + [ + 24.96046543121338, + 60.176227211347694 + ], + [ + 24.96170997619629, + 60.17554421997969 + ] + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/geometry_data/multi_line_string_2.geojson b/test/fixtures/geometry_data/multi_line_string_2.geojson new file mode 100644 index 0000000000..2e6484898b --- /dev/null +++ b/test/fixtures/geometry_data/multi_line_string_2.geojson @@ -0,0 +1,88 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": { + "stroke": "#77ea3a", + "stroke-width": 2, + "stroke-opacity": 1 + }, + "geometry": { + "type": "MultiLineString", + "coordinates": [[ + [ + 24.920940399169922, + 60.17014381943579 + ], + [ + 24.921412467956543, + 60.16785958842209 + ], + [ + 24.923558235168457, + 60.168585436051856 + ], + [ + 24.92694854736328, + 60.16963148236986 + ], + [ + 24.928836822509766, + 60.17014381943579 + ], + [ + 24.931025505065918, + 60.170858943230954 + ] + ], [ + [ + 24.938535690307617, + 60.16965282990714 + ], + [ + 24.943599700927734, + 60.166365145750525 + ], + [ + 24.944028854370117, + 60.165511147991474 + ], + [ + 24.940338134765625, + 60.16435821580911 + ], + [ + 24.938278198242188, + 60.1635895718808 + ], + [ + 24.93424415588379, + 60.1623511632902 + ] + ], [ + [ + 24.930660724639893, + 60.16620502286138 + ], + [ + 24.931282997131348, + 60.165778024675454 + ], + [ + 24.93199110031128, + 60.16524426914001 + ], + [ + 24.933085441589355, + 60.16446497048834 + ], + [ + 24.934523105621338, + 60.16340808388516 + ] + ]] + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/geometry_data/multi_point_1.geojson b/test/fixtures/geometry_data/multi_point_1.geojson new file mode 100644 index 0000000000..92ff62378b --- /dev/null +++ b/test/fixtures/geometry_data/multi_point_1.geojson @@ -0,0 +1,91 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "MultiPoint", + "coordinates": [[ + 24.938492774963375, + 60.16980226227959 + ], [ + 24.938063621520996, + 60.16931126764639 + ], [ + 24.937269687652588, + 60.16984495711831 + ], [ + 24.93675470352173, + 60.16963148236986 + ], [ + 24.937162399291992, + 60.16934328925918 + ], [ + 24.938106536865234, + 60.17026122888812 + ], [ + 24.93896484375, + 60.16964215614023 + ], [ + 24.939308166503903, + 60.170346617317186 + ], [ + 24.937076568603516, + 60.170464026044776 + ], [ + 24.93623971939087, + 60.17009045136419 + ], [ + 24.936411380767822, + 60.169268572114106 + ], [ + 24.938857555389404, + 60.17032527023073 + ], [ + 24.938342571258545, + 60.171392607562005 + ], [ + 24.940145015716553, + 60.1709443301065 + ], [ + 24.93767738342285, + 60.17102971676008 + ], [ + 24.938321113586426, + 60.17082692309542 + ], [ + 24.9408745765686, + 60.170367964389776 + ], [ + 24.940080642700195, + 60.1720009743249 + ], [ + 24.939522743225098, + 60.171371261155215 + ], [ + 24.935853481292725, + 60.17111510319178 + ], [ + 24.936046600341797, + 60.17060278127276 + ], [ + 24.934716224670407, + 60.17005843047963 + ], [ + 24.934866428375244, + 60.16955676588021 + ], [ + 24.93574619293213, + 60.16927924600239 + ], [ + 24.93463039398193, + 60.17089096333529 + ], [ + 24.93673324584961, + 60.17158472459901 + ]] + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/geometry_data/multi_point_2.geojson b/test/fixtures/geometry_data/multi_point_2.geojson new file mode 100644 index 0000000000..396f579fdb --- /dev/null +++ b/test/fixtures/geometry_data/multi_point_2.geojson @@ -0,0 +1,85 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": {}, + "geometry": { + "type": "MultiPoint", + "coordinates": [[ + 24.941625595092773, + 60.16672808807713 + ], [ + 24.940338134765625, + 60.16613029857937 + ], [ + 24.94570255279541, + 60.16561789892518 + ], [ + 24.943084716796875, + 60.16516954267316 + ], [ + 24.947547912597656, + 60.16625839724445 + ], [ + 24.94896411895752, + 60.16777419352904 + ], [ + 24.94548797607422, + 60.16450767226291 + ], [ + 24.94870662689209, + 60.164998738682236 + ], [ + 24.942140579223633, + 60.164721180303474 + ], [ + 24.939351081848145, + 60.16510549128062 + ], [ + 24.943857192993164, + 60.16450767226291 + ], [ + 24.94038105010986, + 60.16435821580911 + ], [ + 24.942870140075684, + 60.163610923343796 + ], [ + 24.9459171295166, + 60.163610923343796 + ], [ + 24.940295219421383, + 60.16324794658606 + ], [ + 24.94295597076416, + 60.16279955799712 + ], [ + 24.9459171295166, + 60.162863613884376 + ], [ + 24.948406219482422, + 60.16367497764958 + ], [ + 24.947547912597656, + 60.165489797763136 + ], [ + 24.937763214111328, + 60.16275685400291 + ], [ + 24.943556785583496, + 60.16589544972972 + ], [ + 24.946088790893555, + 60.16711237558275 + ], [ + 24.942569732666016, + 60.16783823971963 + ], [ + 24.942612648010254, + 60.16196682010054 + ]] + } + } + ] +} \ No newline at end of file diff --git a/test/style/property_expression.test.cpp b/test/style/property_expression.test.cpp index 570ba4a6f3..c584d64741 100644 --- a/test/style/property_expression.test.cpp +++ b/test/style/property_expression.test.cpp @@ -7,6 +7,9 @@ #include #include +#include +#include + #include using namespace mbgl; @@ -395,7 +398,7 @@ TEST(PropertyExpression, WithinExpressionAntiMeridian) { // evaluation test with line geometries { - static const std::string polygon1 = R"data( + const std::string polygon1 = R"data( { "type": "Polygon", "coordinates": [[[-190, 0], [-178, 0], [-178, 10], [-190, 10], [-190, 0]]] @@ -461,3 +464,368 @@ TEST(PropertyExpression, WithinExpressionAntiMeridian) { EXPECT_FALSE(evaluatedResult); } } + +TEST(PropertyExpression, DistanceExpression) { + static const double invalidResult = std::numeric_limits::quiet_NaN(); + static const CanonicalTileID canonicalTileID(15, 18653, 9484); + // Parsing error with invalid geojson source + { + // geoJSON source must be Point or LineString. + const std::string invalidGeoSource = R"({ + "type": "Polygon", + "coordinates": [[[0, 0], [0, 5], [5, 5], [5, 0], [0, 0]]] + })"; + std::stringstream ss; + ss << std::string(R"(["distance", )") << invalidGeoSource << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_FALSE(expression); + } + + // Parsing error with invalid unit + { + const std::string invalidGeoSource = R"({ + "type": "Point", + "coordinates": [0, 0], + })"; + std::stringstream ss; + ss << std::string(R"(["distance", )") << invalidGeoSource << std::string(R"(, "InvalidUnits"])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_FALSE(expression); + } + + // Evaluation test with non-supported geometry type + { + const std::string pointGeoSource = R"({ + "type": "Point", + "coordinates": + [ + 24.938492774963375, + 60.16980226227959 + ] + })"; + std::stringstream ss; + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + Polygon testRing{{{24.937505722045895, 60.16815846879986}, + {24.940595626831055, 60.16815846879986}, + {24.940595626831055, 60.169268572114106}, + {24.937505722045895, 60.169268572114106}, + {24.937505722045895, 60.16815846879986}}}; + auto geoPoly = convertGeometry(testRing, canonicalTileID); + StubGeometryTileFeature polyFeature(FeatureType::Polygon, geoPoly); + + // return evaluation error + auto evaluatedResult = + propExpr.evaluate(EvaluationContext(&polyFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_TRUE(std::isnan(evaluatedResult)); + } + + // Evaluation test with Point to Point distance by using different units + { + const std::string pointGeoSource = R"({ + "type": "Point", + "coordinates": + [ + 24.938492774963375, + 60.16980226227959 + ] + })"; + + Point featurePoint{24.931159615516663, 60.16733387258923}; + auto geoPoint = convertGeometry(featurePoint, canonicalTileID); + StubGeometryTileFeature pointFeature(FeatureType::Point, geoPoint); + + std::stringstream ss; + + // Default Unit: Meters + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(491.307, evaluatedResult, 0.01); + + // Unit: Meters + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "Meters" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(491.307, evaluatedResult, 0.01); + + // Unit: Kilometers + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "Kilometers" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(0.491307, evaluatedResult, 0.00001); + + // Unit: Miles + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "Miles" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(0.305284, evaluatedResult, 0.00001); + + // Unit: NauticalMiles + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "NauticalMiles" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(0.265284, evaluatedResult, 0.00001); + + // Unit: Yards + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "Yards" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(537.299, evaluatedResult, 0.01); + + // Unit: Feet + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "Feet" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(1611.898, evaluatedResult, 0.01); + + // Unit: Inches + ss.str(std::string()); + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"(, "Inches" ])"); + expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + propExpr = std::move(expression); + + evaluatedResult = + propExpr.evaluate(EvaluationContext(&pointFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(19342.781, evaluatedResult, 0.01); + } + + // Evaluation test with Point to MultiPoint distance + { + const std::string pointGeoSource = R"({ + "type": "Point", + "coordinates": + [ + 24.938492774963375, + 60.16980226227959 + ] + })"; + std::stringstream ss; + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + MultiPoint featurePoint{{24.931159615516663, 60.16733387258923}, + {24.936089515686035, 60.169033745694826}}; + auto geoPoint = convertGeometry(featurePoint, canonicalTileID); + StubGeometryTileFeature feature(FeatureType::Point, geoPoint); + + auto evaluatedResult = + propExpr.evaluate(EvaluationContext(&feature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(158.541, evaluatedResult, 0.01); + } + + // Evaluation test with Point to LineString distance + { + const std::string pointGeoSource = R"({ + "type": "Point", + "coordinates": + [ + 24.938492774963375, + 60.16980226227959 + ] + })"; + std::stringstream ss; + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + LineString line{{24.93621826171875, 60.17131789507754}, + {24.941453933715817, 60.170229208170014}, + {24.941110610961914, 60.16850004304534}}; + auto geoLine = convertGeometry(line, canonicalTileID); + StubGeometryTileFeature lineFeature(FeatureType::LineString, geoLine); + + auto evaluatedResult = + propExpr.evaluate(EvaluationContext(&lineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(107.211, evaluatedResult, 0.01); + } + + // Evaluation test with Point to MultiLineString distance + { + const std::string pointGeoSource = R"({ + "type": "Point", + "coordinates": + [ + 24.938492774963375, + 60.16980226227959 + ] + })"; + std::stringstream ss; + ss << std::string(R"(["distance", )") << pointGeoSource << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + MultiLineString lines{ + {{24.93621826171875, 60.17131789507754}, + {24.941453933715817, 60.170229208170014}, + {24.941110610961914, 60.16850004304534}}, + {{24.937773942947388, 60.16940199546824}, {24.933900833129883, 60.17007977773949}}}; + auto geoLine = convertGeometry(lines, canonicalTileID); + StubGeometryTileFeature lineFeature(FeatureType::LineString, geoLine); + + auto evaluatedResult = + propExpr.evaluate(EvaluationContext(&lineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(59.845, evaluatedResult, 0.01); + } + + const auto getFeature = [](const std::string &name, FeatureType type, const CanonicalTileID &canonical) { + const auto geometryInput = mbgl::util::read_file(std::string("test/fixtures/geometry_data/" + name)); + mapbox::geojson::geojson geojson{mapbox::geojson::parse(geometryInput)}; + const auto &geometry = geojson.match( + [](const mapbox::geometry::geometry &geometrySet) { return mbgl::Feature(geometrySet).geometry; }, + [](const mapbox::feature::feature &feature) { return mbgl::Feature(feature).geometry; }, + [](const mapbox::feature::feature_collection &features) { + return mbgl::Feature(features.front()).geometry; + }, + [](const auto &) { return mapbox::geometry::empty(); }); + return StubGeometryTileFeature(type, convertGeometry(geometry, canonical)); + }; + + // Evaluation test with MultiPoint to MultiPoint distance + { + const auto multiPoints1 = mbgl::util::read_file("test/fixtures/geometry_data/multi_point_1.geojson"); + const auto multiPointsFeature = getFeature("multi_point_2.geojson", FeatureType::Point, canonicalTileID); + std::stringstream ss; + ss << std::string(R"(["distance", )") << multiPoints1 << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = propExpr.evaluate( + EvaluationContext(&multiPointsFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(283.60, evaluatedResult, 0.1); + } + + // Evaluation test with MultiPoint to LineString distance + { + const auto multiPoints1 = mbgl::util::read_file("test/fixtures/geometry_data/multi_point_1.geojson"); + const auto multiLineFeature = getFeature("line_string_2.geojson", FeatureType::LineString, canonicalTileID); + std::stringstream ss; + ss << std::string(R"(["distance", )") << multiPoints1 << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = propExpr.evaluate( + EvaluationContext(&multiLineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(258.691, evaluatedResult, 0.01); + + // ----------------- switch feature and argument ----------------- + const auto multiLine2 = mbgl::util::read_file("test/fixtures/geometry_data/line_string_2.geojson"); + const auto multiPointsFeature = getFeature("multi_point_1.geojson", FeatureType::Point, canonicalTileID); + std::stringstream ss1; + ss1 << std::string(R"(["distance", )") << multiLine2 << std::string(R"( ])"); + auto expression1 = createExpression(ss1.str().c_str()); + ASSERT_TRUE(expression1); + PropertyExpression propExpr1(std::move(expression1)); + + auto evaluatedResult1 = propExpr1.evaluate( + EvaluationContext(&multiPointsFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(258.691, evaluatedResult1, 0.01); + } + + // Evaluation test with LineString to LineString distance + { + const auto lineString1 = mbgl::util::read_file("test/fixtures/geometry_data/line_string_1.geojson"); + const auto lineFeature = getFeature("line_string_2.geojson", FeatureType::LineString, canonicalTileID); + std::stringstream ss; + ss << std::string(R"(["distance", )") << lineString1 << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = + propExpr.evaluate(EvaluationContext(&lineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(180.408, evaluatedResult, 0.01); + } + + // Evaluation test with MultiPoints to MutltiLineString distance + { + const auto multiPoints = mbgl::util::read_file("test/fixtures/geometry_data/multi_point_2.geojson"); + const auto multiLineFeature = + getFeature("multi_line_string_1.geojson", FeatureType::LineString, canonicalTileID); + std::stringstream ss; + ss << std::string(R"(["distance", )") << multiPoints << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = propExpr.evaluate( + EvaluationContext(&multiLineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(937.632, evaluatedResult, 0.01); + } + + // Evaluation test with LineString to MutltiLineString distance + { + const auto LineString1 = mbgl::util::read_file("test/fixtures/geometry_data/line_string_1.geojson"); + const auto multiLineFeature = + getFeature("multi_line_string_2.geojson", FeatureType::LineString, canonicalTileID); + std::stringstream ss; + ss << std::string(R"(["distance", )") << LineString1 << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = propExpr.evaluate( + EvaluationContext(&multiLineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(17.511, evaluatedResult, 0.01); + } + + // Evaluation test with MultiLineString to MutltiLineString distance + { + const auto multiLineString1 = mbgl::util::read_file("test/fixtures/geometry_data/multi_line_string_1.geojson"); + const auto multiLineFeature = + getFeature("multi_line_string_2.geojson", FeatureType::LineString, canonicalTileID); + std::stringstream ss; + ss << std::string(R"(["distance", )") << multiLineString1 << std::string(R"( ])"); + auto expression = createExpression(ss.str().c_str()); + ASSERT_TRUE(expression); + PropertyExpression propExpr(std::move(expression)); + + auto evaluatedResult = propExpr.evaluate( + EvaluationContext(&multiLineFeature).withCanonicalTileID(&canonicalTileID), invalidResult); + EXPECT_NEAR(384.109, evaluatedResult, 0.01); + } +} -- cgit v1.2.1