OmniSciDB  04ee39c94c
GeoTypesTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <Shared/geo_types.h>
18 #include "Tests/TestHelpers.h"
19 
20 #include <gtest/gtest.h>
21 
22 using namespace Geo_namespace;
23 
24 namespace {
25 
26 template <class T>
27 void compare_arrays(const std::vector<T>& a, const std::vector<T>& b) {
28  ASSERT_EQ(a.size(), b.size());
29  for (size_t i = 0; i < a.size(); i++) {
30  ASSERT_NEAR(a[i], b[i], 1e-7);
31  }
32 }
33 
34 } // namespace
35 
37  const std::vector<double> coords{1.0, 1.0};
38  const std::string wkt{"POINT (1 1)"};
39 };
40 
41 TEST(GeoPoint, EmptyWKT) {
42  const auto gdal_wkt_pt = GeoPoint("POINT EMPTY");
43  const auto wkt_str = gdal_wkt_pt.getWktString();
44  ASSERT_EQ(wkt_str, "POINT EMPTY");
45 }
46 
47 TEST(GeoPoint, EmptyCoords) {
48  EXPECT_THROW(GeoPoint(std::vector<double>()), GeoTypesError);
49 }
50 
51 TEST(GeoPoint, ImportWKT) {
52  const auto sample_pt = SamplePointData();
53  const auto gdal_pt = GeoPoint(sample_pt.wkt);
54  const auto wkt_str = gdal_pt.getWktString();
55  ASSERT_EQ(wkt_str, sample_pt.wkt);
56 }
57 
58 TEST(GeoPoint, ExportWKT) {
59  const auto sample_pt = SamplePointData();
60  const auto gdal_pt = GeoPoint(sample_pt.coords);
61  const auto wkt_str = gdal_pt.getWktString();
62  ASSERT_EQ(wkt_str, sample_pt.wkt);
63 }
64 
65 TEST(GeoPoint, ExportColumns) {
66  const auto sample_pt = SamplePointData();
67  const auto gdal_pt = GeoPoint(sample_pt.wkt);
68  std::vector<double> coords;
69  gdal_pt.getColumns(coords);
70  compare_arrays(coords, sample_pt.coords);
71 }
72 
73 TEST(GeoPoint, EqualsOperator) {
74  const auto sample_pt = SamplePointData();
75  ASSERT_TRUE(GeoPoint(sample_pt.coords) == GeoPoint(sample_pt.wkt));
76 }
77 
78 TEST(GeoPoint, OGRError) {
79  EXPECT_THROW(GeoPoint("POINT (0)"), GeoTypesError);
80 }
81 
82 TEST(GeoPoint, BadWktType) {
83  try {
84  auto pt = GeoPoint("LINESTRING (1 1)");
85  } catch (const GeoTypesError& e) {
86  ASSERT_STREQ("GeoPoint Error: Unexpected geometry type from WKT string: Line String",
87  e.what());
88  } catch (...) {
89  FAIL();
90  }
91 }
92 
94  const std::vector<double> coords{1.0, 2.0, 3.0, 4.0, 5.1, 5.2};
95  const std::vector<double> bounds{1.0, 2.0, 5.1, 5.2};
96  const std::string wkt{"LINESTRING (1 2,3 4,5.1 5.2)"};
97 };
98 
99 TEST(GeoLineString, EmptyWKT) {
100  const auto gdal_wkt_linestr = GeoLineString("LINESTRING EMPTY");
101  const auto wkt_str = gdal_wkt_linestr.getWktString();
102  ASSERT_EQ(wkt_str, "LINESTRING EMPTY");
103 }
104 
105 TEST(GeoLineString, EmptyCoords) {
106  const auto gdal_linestr = GeoLineString(std::vector<double>());
107  const auto wkt_str = gdal_linestr.getWktString();
108  ASSERT_EQ(wkt_str, "LINESTRING EMPTY");
109 }
110 
111 TEST(GeoLineString, ImportWKT) {
112  const auto sample_linestr = SampleLineStringData();
113  const auto gdal_linestr = GeoLineString(sample_linestr.wkt);
114  const auto wkt_str = gdal_linestr.getWktString();
115  ASSERT_EQ(wkt_str, sample_linestr.wkt);
116 }
117 
118 TEST(GeoLineString, ExportWKT) {
119  const auto sample_linestr = SampleLineStringData();
120  const auto gdal_linestr = GeoLineString(sample_linestr.coords);
121  const auto wkt_str = gdal_linestr.getWktString();
122  ASSERT_EQ(wkt_str, sample_linestr.wkt);
123 }
124 
125 TEST(GeoLineString, ExportColumns) {
126  const auto sample_linestr = SampleLineStringData();
127  const auto gdal_linestr = GeoLineString(sample_linestr.wkt);
128  std::vector<double> coords;
129  std::vector<double> bounds;
130  gdal_linestr.getColumns(coords, bounds);
131  compare_arrays(coords, sample_linestr.coords);
132  compare_arrays(bounds, sample_linestr.bounds);
133 }
134 
135 TEST(GeoLineString, EqualsOperator) {
136  const auto sample_linestr = SampleLineStringData();
137  ASSERT_TRUE(GeoLineString(sample_linestr.coords) == GeoLineString(sample_linestr.wkt));
138 }
139 
140 TEST(GeoLineString, OGRError) {
141  EXPECT_THROW(GeoLineString("LINESTRING (0)"), GeoTypesError);
142 }
143 
144 TEST(GeoLineString, BadWktType) {
145  try {
146  auto pt = GeoLineString("POINT (1 1)");
147  } catch (const GeoTypesError& e) {
148  ASSERT_STREQ("GeoLineString Error: Unexpected geometry type from WKT string: Point",
149  e.what());
150  } catch (...) {
151  FAIL();
152  }
153 }
154 
156  const std::vector<double>
157  coords{35, 10, 45, 45, 15, 40, 10, 20, 20, 30, 35, 35, 30, 20};
158  const std::vector<int32_t> ring_sizes{4, 3};
159  const std::vector<double> bounds{10, 10, 45, 45};
160  const std::string wkt{
161  "POLYGON ((35 10,45 45,15 40,10 20,35 10),(20 30,35 35,30 20,20 30))"};
162 };
163 
164 TEST(GeoPolygon, EmptyWKT) {
165  const auto gdal_wkt_poly = GeoPolygon("POLYGON EMPTY");
166  const auto wkt_str = gdal_wkt_poly.getWktString();
167  ASSERT_EQ(wkt_str, "POLYGON EMPTY");
168 }
169 
170 TEST(GeoPolygon, EmptyCoords) {
171  const auto gdal_poly = GeoPolygon(std::vector<double>(), std::vector<int32_t>());
172  const auto wkt_str = gdal_poly.getWktString();
173  ASSERT_EQ(wkt_str, "POLYGON EMPTY");
174 }
175 
176 TEST(GeoPolygon, ImportWKT) {
177  const auto sample_poly = SamplePolygonData();
178  const auto gdal_poly = GeoPolygon(sample_poly.wkt);
179  const auto wkt_str = gdal_poly.getWktString();
180  ASSERT_EQ(wkt_str, sample_poly.wkt);
181 }
182 
183 TEST(GeoPolygon, ExportWKT) {
184  const auto sample_poly = SamplePolygonData();
185  const auto gdal_poly = GeoPolygon(sample_poly.coords, sample_poly.ring_sizes);
186  const auto wkt_str = gdal_poly.getWktString();
187  ASSERT_EQ(wkt_str, sample_poly.wkt);
188 }
189 
190 TEST(GeoPolygon, ExportColumns) {
191  const auto sample_poly = SamplePolygonData();
192  const auto gdal_poly = GeoPolygon(sample_poly.coords, sample_poly.ring_sizes);
193  std::vector<double> coords;
194  std::vector<int32_t> ring_sizes;
195  std::vector<double> bounds;
196  gdal_poly.getColumns(coords, ring_sizes, bounds);
197  compare_arrays(coords, sample_poly.coords);
198  compare_arrays(ring_sizes, sample_poly.ring_sizes);
199  compare_arrays(bounds, sample_poly.bounds);
200 }
201 
202 TEST(GeoPolygon, EqualsOperator) {
203  const auto sample_poly = SamplePolygonData();
204  ASSERT_TRUE(GeoPolygon(sample_poly.coords, sample_poly.ring_sizes) ==
205  GeoPolygon(sample_poly.wkt));
206 }
207 
208 TEST(GeoPolygon, OGRError) {
209  EXPECT_THROW(GeoPolygon("POYLGON ((0))"), GeoTypesError);
210 }
211 
212 TEST(GeoPolygon, BadWktType) {
213  try {
214  auto pt = GeoPolygon("POINT (1 1)");
215  } catch (const GeoTypesError& e) {
216  ASSERT_STREQ("GeoPolygon Error: Unexpected geometry type from WKT string: Point",
217  e.what());
218  } catch (...) {
219  FAIL();
220  }
221 }
222 
224  const std::vector<double> coords{40, 40, 20, 45, 45, 30, 20, 35, 10, 30, 10,
225  10, 30, 5, 45, 20, 30, 20, 20, 15, 20, 25};
226  const std::vector<int32_t> ring_sizes{3, 5, 3};
227  const std::vector<int32_t> poly_rings{1, 2};
228  const std::vector<double> bounds{10, 5, 45, 45};
229  const std::string wkt{
230  "MULTIPOLYGON (((40 40,20 45,45 30,40 40)),((20 35,10 30,10 10,30 5,45 20,20 "
231  "35),(30 20,20 15,20 25,30 20)))"};
232 };
233 
234 TEST(GeoMultiPolygon, EmptyWKT) {
235  const auto gdal_wkt_mpoly = GeoMultiPolygon("MULTIPOLYGON EMPTY");
236  const auto wkt_str = gdal_wkt_mpoly.getWktString();
237  ASSERT_EQ(wkt_str, "MULTIPOLYGON EMPTY");
238 }
239 
240 TEST(GeoMultiPolygon, EmptyCoords) {
241  const auto gdal_mpoly = GeoMultiPolygon(
242  std::vector<double>(), std::vector<int32_t>(), std::vector<int32_t>());
243  const auto wkt_str = gdal_mpoly.getWktString();
244  ASSERT_EQ(wkt_str, "MULTIPOLYGON EMPTY");
245 }
246 
247 TEST(GeoMultiPolygon, ImportWKT) {
248  const auto sample_mpoly = SampleMultiPolygonData();
249  const auto gdal_mpoly = GeoMultiPolygon(sample_mpoly.wkt);
250  const auto wkt_str = gdal_mpoly.getWktString();
251  ASSERT_EQ(wkt_str, sample_mpoly.wkt);
252 }
253 
254 TEST(GeoMultiPolygon, ExportWKT) {
255  const auto sample_mpoly = SampleMultiPolygonData();
256  const auto gdal_mpoly = GeoMultiPolygon(
257  sample_mpoly.coords, sample_mpoly.ring_sizes, sample_mpoly.poly_rings);
258  const auto wkt_str = gdal_mpoly.getWktString();
259  ASSERT_EQ(wkt_str, sample_mpoly.wkt);
260 }
261 
262 TEST(GeoMultiPolygon, ExportColumns) {
263  const auto sample_mpoly = SampleMultiPolygonData();
264  const auto gdal_mpoly = GeoMultiPolygon(sample_mpoly.wkt);
265  std::vector<double> coords;
266  std::vector<int32_t> ring_sizes;
267  std::vector<int32_t> poly_rings;
268  std::vector<double> bounds;
269  gdal_mpoly.getColumns(coords, ring_sizes, poly_rings, bounds);
270  compare_arrays(coords, sample_mpoly.coords);
271  compare_arrays(ring_sizes, sample_mpoly.ring_sizes);
272  compare_arrays(poly_rings, sample_mpoly.poly_rings);
273  compare_arrays(bounds, sample_mpoly.bounds);
274 }
275 
276 TEST(GeoMultiPolygon, EqualsOperator) {
277  const auto sample_mpoly = SampleMultiPolygonData();
278  ASSERT_TRUE(GeoMultiPolygon(sample_mpoly.coords,
279  sample_mpoly.ring_sizes,
280  sample_mpoly.poly_rings) ==
281  GeoMultiPolygon(sample_mpoly.wkt));
282 }
283 
284 TEST(GeoMultiPolygon, OGRError) {
285  EXPECT_THROW(GeoMultiPolygon("MULTIPOYLGON ((0))"), GeoTypesError);
286 }
287 
288 TEST(GeoMultiPolygon, BadWktType) {
289  try {
290  auto pt = GeoMultiPolygon("POINT (1 1)");
291  } catch (const GeoTypesError& e) {
292  ASSERT_STREQ("GeoMultiPolygon Error: Unexpected geometry type from WKT string: Point",
293  e.what());
294  } catch (...) {
295  FAIL();
296  }
297 }
298 
299 TEST(GeoMisc, Inequality) {
300  const auto sample_pt = SamplePointData();
301  const auto sample_linestring = SampleLineStringData();
302  ASSERT_FALSE(GeoPoint(sample_pt.wkt) == GeoLineString(sample_linestring.wkt));
303 
304  const auto sample_mpoly = SampleMultiPolygonData();
305  ASSERT_FALSE(GeoLineString(sample_linestring.coords) ==
306  GeoMultiPolygon(sample_mpoly.wkt));
307 }
308 
309 int main(int argc, char** argv) {
311  testing::InitGoogleTest(&argc, argv);
312 
313  int err{0};
314  try {
315  err = RUN_ALL_TESTS();
316  } catch (const std::exception& e) {
317  LOG(ERROR) << e.what();
318  }
319  return err;
320 }
int main(int argc, char **argv)
#define LOG(tag)
Definition: Logger.h:182
void compare_arrays(const std::vector< T > &a, const std::vector< T > &b)
TEST(GeoPoint, EmptyWKT)
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194