summaryrefslogtreecommitdiff
path: root/test/test_geo_json.cc
blob: e466e75db665e3e864ae0c94bb6a7e0f63d5e961 (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
#include "common.hh"

#include "file_test.hh"
#include "geo_json.hh"
#include "logger.hh"

#include <gtest/gtest.h>

namespace {

class GeoJsonTest : public FileTest {
public:
  std::unique_ptr<GeoJson> load(std::string_view data) {
    write(data);
    close();
    return GeoJson::create(logger_, path());
  }

private:
  std::shared_ptr<Logger> logger_{Logger::create_null()};
};

}  // namespace

TEST_F(GeoJsonTest, empty) {
  auto geo_json = load("");

  auto opt = geo_json->get_data(0.0, 0.0, "prop0");
  EXPECT_FALSE(opt.has_value());
}

TEST_F(GeoJsonTest, sanity) {
  auto geo_json = load(
      "{"
      "  \"type\": \"FeatureCollection\","
      "  \"features\": ["
      "  {"
      "    \"type\": \"Feature\","
      "    \"geometry\": {"
      "      \"type\": \"Polygon\","
      "      \"coordinates\": ["
      "        ["
      "          [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],"
      "          [100.0, 1.0], [100.0, 0.0]"
      "        ]"
      "      ]"
      "    },"
      "    \"properties\": {"
      "      \"prop0\": \"value0\","
      "      \"prop1\": { \"this\": \"that\" }"
      "    }"
      "  }"
      "  ]"
      "}");

  auto opt = geo_json->get_data(0.0, 0.0, "prop0");
  EXPECT_FALSE(opt.has_value());

  opt = geo_json->get_data(0.5, 100.5, "prop0");
  EXPECT_TRUE(opt.has_value());
  if (opt.has_value()) {
    EXPECT_EQ("value0", opt.value());
  }

  opt = geo_json->get_data(0.0, 100.0, "prop0");
  EXPECT_TRUE(opt.has_value());
  if (opt.has_value()) {
    EXPECT_EQ("value0", opt.value());
  }

  opt = geo_json->get_data(0.5, 100.5, "prop1");
  EXPECT_FALSE(opt.has_value());
}

TEST_F(GeoJsonTest, hole) {
  auto geo_json = load(
      "{"
      "  \"type\": \"FeatureCollection\","
      "  \"features\": ["
      "  {"
      "    \"type\": \"Feature\","
      "    \"geometry\": {"
      "      \"type\": \"Polygon\","
      "      \"coordinates\": ["
      "        ["
      "          [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],"
      "          [100.0, 1.0], [100.0, 0.0]"
      "        ],"
      "        ["
      "          [100.25, 0.25], [100.75, 0.25], [100.75, 0.75],"
      "          [100.25, 0.75], [100.25, 0.25]"
      "        ]"
      "      ]"
      "    },"
      "    \"properties\": {"
      "      \"prop0\": \"value0\""
      "    }"
      "  }"
      "  ]"
      "}");

  auto opt = geo_json->get_data(0.5, 100.5, "prop0");
  EXPECT_FALSE(opt.has_value());

  opt = geo_json->get_data(0.0, 100.0, "prop0");
  EXPECT_TRUE(opt.has_value());
  if (opt.has_value()) {
    EXPECT_EQ("value0", opt.value());
  }

  opt = geo_json->get_data(0.1, 100.20, "prop0");
  EXPECT_TRUE(opt.has_value());
  if (opt.has_value()) {
    EXPECT_EQ("value0", opt.value());
  }
}

TEST_F(GeoJsonTest, bad) {
  auto geo_json = load(std::string(1000, '{'));

  auto opt = geo_json->get_data(0.0, 0.0, "prop0");
  EXPECT_FALSE(opt.has_value());
}