1 #include "extractor/location_dependent_data.hpp"
2 
3 #include "../common/range_tools.hpp"
4 
5 #include <boost/filesystem.hpp>
6 #include <boost/test/unit_test.hpp>
7 
8 #include <fstream>
9 #include <vector>
10 
11 BOOST_AUTO_TEST_SUITE(location_dependent_data_tests)
12 
13 using namespace osrm;
14 using namespace osrm::extractor;
15 using point_t = LocationDependentData::point_t;
16 
17 struct LocationDataFixture
18 {
LocationDataFixtureLocationDataFixture19     LocationDataFixture(const std::string &json) : temporary_file(boost::filesystem::unique_path())
20     {
21         std::ofstream file(temporary_file.string());
22         file << json;
23     }
~LocationDataFixtureLocationDataFixture24     ~LocationDataFixture() { remove(temporary_file); }
25 
26     boost::filesystem::path temporary_file;
27 };
28 
BOOST_AUTO_TEST_CASE(polygon_tests)29 BOOST_AUTO_TEST_CASE(polygon_tests)
30 {
31     LocationDataFixture fixture(R"json({
32 "type": "FeatureCollection",
33 "features": [
34 {
35     "type": "Feature",
36     "properties": {
37       "answer": 42
38     },
39     "geometry": { "type": "Polygon", "coordinates": [
40         [ [3, 0], [1, 1], [0, 3], [-1, 1], [-3, 0], [-1, -1], [0, -3], [1, -1], [3, 0] ],
41         [ [1, 0], [0, 1], [-1, 0], [0, -1], [1, 0] ]
42     ] }
43 },
44 {
45     "type": "Feature",
46     "properties": {
47       "answer": true
48     },
49     "geometry": { "type": "Polygon", "coordinates": [ [ [0, 10], [3, 5], [1, 5], [10, 0], [-1, 5], [-3, 5], [0, 10] ] ] }
50 }
51 ]})json");
52 
53     LocationDependentData data({fixture.temporary_file});
54 
55     BOOST_CHECK(data.GetPropertyIndexes(point_t(0, 0)).empty());
56     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(1, 1)), 0);
57     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(0, 1)), 0);
58     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(0.5, -0.5)), 0);
59     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(0, -3)), 0);
60     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-0.75, 0.75)), 0);
61     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(2, 0)), 0);
62     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(1, 7)), 1);
63     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-2, 6)), 1);
64 
65     BOOST_CHECK_EQUAL(data.FindByKey({}, "answer").which(), 0);
66     BOOST_CHECK_EQUAL(data.FindByKey({0}, "foo").which(), 0);
67     BOOST_CHECK_EQUAL(boost::get<double>(data.FindByKey({0}, "answer")), 42);
68     BOOST_CHECK_EQUAL(boost::get<bool>(data.FindByKey({1}, "answer")), true);
69 }
70 
BOOST_AUTO_TEST_CASE(multy_polygon_tests)71 BOOST_AUTO_TEST_CASE(multy_polygon_tests)
72 {
73     LocationDataFixture fixture(R"json({
74 "type": "FeatureCollection",
75 "features": [
76 {
77     "type": "Feature",
78     "properties": {
79       "answer": 42
80     },
81     "geometry": { "type": "MultiPolygon", "coordinates": [
82     [ [ [1, 0], [0, 1], [-1, 0], [0, -1], [1, 0] ] ],
83     [ [ [6, 0], [5, 1], [4, 0], [5, -1], [6, 0] ] ],
84     [ [ [-4, 0], [-5, 1], [-6, 0], [-5, -1], [-4, 0] ] ]
85  ] }
86 }
87 ]})json");
88 
89     LocationDependentData data({fixture.temporary_file});
90 
91     BOOST_CHECK(data.GetPropertyIndexes(point_t(0, 2)).empty());
92     BOOST_CHECK(data.GetPropertyIndexes(point_t(0, -3)).empty());
93     BOOST_CHECK(!data.GetPropertyIndexes(point_t(0, 0)).empty());
94     BOOST_CHECK(!data.GetPropertyIndexes(point_t(5, 0)).empty());
95     BOOST_CHECK(!data.GetPropertyIndexes(point_t(-5, 0)).empty());
96 }
97 
BOOST_AUTO_TEST_CASE(polygon_merging_tests)98 BOOST_AUTO_TEST_CASE(polygon_merging_tests)
99 {
100     LocationDataFixture fixture(R"json({
101 "type": "FeatureCollection",
102 "features": [
103 {
104     "type": "Feature",
105     "properties": { "answer": "a" },
106     "geometry": { "type": "Polygon", "coordinates": [ [ [3, 3], [-3, 3], [-3, -3], [3, -3], [3, 3] ] ] }
107 },
108 {
109     "type": "Feature",
110     "properties": { "answer": "b" },
111     "geometry": { "type": "Polygon", "coordinates": [ [ [7, 3], [1, 3], [1, -3], [7, -3], [7, 3] ] ] }
112 },
113 {
114     "type": "Feature",
115     "properties": { "answer": "c" },
116     "geometry": { "type": "Polygon", "coordinates": [ [ [8, 3], [2, 3], [2, -3], [8, -3], [8, 3] ] ] }
117 }
118 ]})json");
119 
120     LocationDependentData data({fixture.temporary_file});
121 
122     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-3, 3)), 0);
123     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-3, 1)), 0);
124     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(-3, -3)), 0);
125     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(0, 3)), 0);
126     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(1, 0)), 0, 1);
127     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(2, -3)), 0, 1, 2);
128     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(3, 0)), 0, 1, 2);
129     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(4, 3)), 1, 2);
130     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(6, 1)), 1, 2);
131     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(7, 0)), 1, 2);
132     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(8, 3)), 2);
133     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(8, -1)), 2);
134     CHECK_EQUAL_RANGE(data.GetPropertyIndexes(point_t(8, -3)), 2);
135 
136     BOOST_CHECK_EQUAL(boost::get<std::string>(data.FindByKey({0}, "answer")), "a");
137     BOOST_CHECK_EQUAL(boost::get<std::string>(data.FindByKey({1}, "answer")), "b");
138     BOOST_CHECK_EQUAL(boost::get<std::string>(data.FindByKey({2}, "answer")), "c");
139     BOOST_CHECK_EQUAL(boost::get<std::string>(data.FindByKey({0, 1, 2}, "answer")), "a");
140     BOOST_CHECK_EQUAL(boost::get<std::string>(data.FindByKey({1, 2}, "answer")), "b");
141     BOOST_CHECK_EQUAL(boost::get<std::string>(data.FindByKey({2, 1, 0}, "answer")), "c");
142 }
143 
BOOST_AUTO_TEST_CASE(staircase_polygon)144 BOOST_AUTO_TEST_CASE(staircase_polygon)
145 {
146     LocationDataFixture fixture(R"json({
147 "type": "FeatureCollection",
148 "features": [
149 {
150     "type": "Feature",
151     "properties": { "answer": "a" },
152     "geometry": { "type": "Polygon", "coordinates": [ [ [0, 0], [3, 0], [3, 3], [2, 3], [2, 2], [1, 2], [1, 1], [0, 1], [0, 0] ] ] }
153 }
154 ]})json");
155 
156     LocationDependentData data({fixture.temporary_file});
157 
158     // all corners
159     BOOST_CHECK(!data.GetPropertyIndexes(point_t(0, 0)).empty());
160     BOOST_CHECK(!data.GetPropertyIndexes(point_t(0, 1)).empty());
161     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1, 1)).empty());
162     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1, 2)).empty());
163     BOOST_CHECK(!data.GetPropertyIndexes(point_t(2, 2)).empty());
164     BOOST_CHECK(!data.GetPropertyIndexes(point_t(2, 3)).empty());
165     BOOST_CHECK(!data.GetPropertyIndexes(point_t(3, 3)).empty());
166     BOOST_CHECK(!data.GetPropertyIndexes(point_t(3, 0)).empty());
167 
168     // at x = 1
169     BOOST_CHECK(data.GetPropertyIndexes(point_t(1, -0.5)).empty());
170     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1, 0)).empty());
171     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1, 0.5)).empty());
172     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1, 1.5)).empty());
173     BOOST_CHECK(data.GetPropertyIndexes(point_t(1, 2.5)).empty());
174     BOOST_CHECK(data.GetPropertyIndexes(point_t(3.5, 2)).empty());
175 
176     // at y = 2
177     BOOST_CHECK(data.GetPropertyIndexes(point_t(0.5, 2)).empty());
178     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1, 2)).empty());
179     BOOST_CHECK(!data.GetPropertyIndexes(point_t(1.5, 2)).empty());
180     BOOST_CHECK(!data.GetPropertyIndexes(point_t(2, 2)).empty());
181     BOOST_CHECK(!data.GetPropertyIndexes(point_t(2.5, 2)).empty());
182     BOOST_CHECK(!data.GetPropertyIndexes(point_t(3, 2)).empty());
183     BOOST_CHECK(data.GetPropertyIndexes(point_t(3.5, 2)).empty());
184 }
185 
186 BOOST_AUTO_TEST_SUITE_END()
187