1 2test_that("s2_closest|farthest_feature() works", { 3 cities <- s2_data_cities("London") 4 countries <- s2_data_countries() 5 6 # with zero length y, results will all be empty 7 expect_identical(s2_closest_feature(cities, character(0)), rep_len(NA_integer_, length(cities))) 8 9 # should correctly identify that London is closest to United Kingdom 10 country_match <- s2_closest_feature(cities, countries) 11 expect_identical(s2_data_tbl_countries$name[country_match], "United Kingdom") 12 13 country_match_farthest <- s2_farthest_feature(cities, countries) 14 expect_identical(s2_data_tbl_countries$name[country_match_farthest], "New Zealand") 15}) 16 17test_that("s2_closest_edges() works", { 18 expect_identical( 19 s2_closest_edges( 20 "POINT (0 0)", 21 c("POINT (0 0)", "POINT (0 1)", "POINT (0 2)", "POINT (0 3)"), 22 k = 1 23 ), 24 list(1L) 25 ) 26 27 expect_identical( 28 s2_closest_edges( 29 "POINT (0 0)", 30 c("POINT (0 0)", "POINT (0 1)", "POINT (0 2)", "POINT (0 3)"), 31 k = 2, 32 min_distance = 0 33 ), 34 list(2L) 35 ) 36 37 expect_identical( 38 s2_closest_edges( 39 "POINT (0 0)", 40 c("POINT (0 0)", "POINT (0 1)", "POINT (0 2)", "POINT (0 3)"), 41 k = 5 42 ) %>% lapply(sort), 43 list(1:4) 44 ) 45}) 46 47test_that("matrix predicates work", { 48 expect_identical( 49 s2_contains_matrix( 50 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", 51 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 52 ), 53 list(2L) 54 ) 55 56 expect_identical( 57 s2_within_matrix( 58 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 59 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))" 60 ), 61 list(integer(0), 1L, integer(0)) 62 ) 63 64 expect_identical( 65 s2_covers_matrix( 66 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", 67 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 68 ), 69 list(2L) 70 ) 71 72 expect_identical( 73 s2_covered_by_matrix( 74 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 75 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))" 76 ), 77 list(integer(0), 1L, integer(0)) 78 ) 79 80 expect_identical( 81 s2_intersects_matrix( 82 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 83 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))" 84 ), 85 list(integer(0), 1L, integer(0)) 86 ) 87 88 expect_identical( 89 s2_disjoint_matrix( 90 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 91 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))" 92 ), 93 list(1L, integer(0), 1L) 94 ) 95 96 expect_identical( 97 s2_equals_matrix( 98 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)", 99 "POLYGON ((1 0, 1 1, 0 1, 0 0, 1 0))"), 100 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))" 101 ), 102 list(integer(0), integer(0), integer(0), 1L) 103 ) 104 105 expect_identical( 106 s2_touches_matrix( 107 c("POINT (0.5 0.5)", "POINT (0 0)", "POINT (-0.5 -0.5)"), 108 "POLYGON ((0 0, 0 1, 1 1, 0 0))" 109 ), 110 list(integer(0), 1L, integer(0)) 111 ) 112 113 expect_identical( 114 s2_dwithin_matrix( 115 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 116 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", 117 0 118 ), 119 list(integer(0), 1L, integer(0)) 120 ) 121 expect_identical( 122 s2_dwithin_matrix( 123 c("POINT (-1 0.5)", "POINT (0.5 0.5)", "POINT (2 0.5)"), 124 "POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0))", 125 s2_earth_radius_meters() 126 ), 127 list(1L, 1L, 1L) 128 ) 129}) 130 131test_that("s2_(max_)?distance_matrix() works", { 132 x <- c("POINT (0 0)", "POINT (0 90)") 133 y <- c("POINT (180 0)", "POINT (0 -90)", "POINT (0 0)") 134 135 expect_equal( 136 s2_distance_matrix(x, x, radius = 180 / pi), 137 matrix(c(0, 90, 90, 0), ncol = 2) 138 ) 139 expect_equal( 140 s2_distance_matrix(x, y, radius = 180 / pi), 141 matrix(c(180, 90, 90, 180, 0, 90), ncol = 3) 142 ) 143 144 # max distance is the same for points 145 expect_equal( 146 s2_max_distance_matrix(x, x, radius = 180 / pi), 147 matrix(c(0, 90, 90, 0), ncol = 2) 148 ) 149 150 expect_equal( 151 s2_max_distance_matrix(x, y, radius = 180 / pi), 152 matrix(c(180, 90, 90, 180, 0, 90), ncol = 3) 153 ) 154 155 # NA handling for both rows and cols 156 y[2] <- NA 157 expect_true(all(is.na(s2_distance_matrix(x, y)[, 2]))) 158 expect_true(all(is.na(s2_max_distance_matrix(x, y)[, 2]))) 159 160 x[2] <- NA 161 expect_true(all(is.na(s2_distance_matrix(x, y)[2, ]))) 162 expect_true(all(is.na(s2_max_distance_matrix(x, y)[2, ]))) 163}) 164 165test_that("s2_may_intersect_matrix() works", { 166 countries <- s2_data_countries() 167 timezones <- s2_data_timezones() 168 169 maybe_intersects <- s2_may_intersect_matrix(countries, timezones) 170 intersects <- s2_intersects_matrix_brute_force(countries, timezones) 171 for (i in seq_along(countries)) { 172 expect_identical(setdiff(intersects[[!!i]], maybe_intersects[[!!i]]), integer(0)) 173 } 174}) 175 176test_that("indexed matrix predicates return the same thing as brute-force comparisons", { 177 countries <- s2_data_countries() 178 timezones <- s2_data_timezones() 179 180 # contains 181 expect_identical( 182 s2_contains_matrix(countries, countries), 183 s2_contains_matrix_brute_force(countries, countries) 184 ) 185 expect_identical( 186 s2_contains_matrix(timezones, countries), 187 s2_contains_matrix_brute_force(timezones, countries) 188 ) 189 190 # within 191 expect_identical( 192 s2_within_matrix(countries, countries), 193 s2_within_matrix_brute_force(countries, countries) 194 ) 195 expect_identical( 196 s2_within_matrix(timezones, countries), 197 s2_within_matrix_brute_force(timezones, countries) 198 ) 199 200 # covers 201 expect_identical( 202 s2_covers_matrix(countries, countries), 203 s2_covers_matrix_brute_force(countries, countries) 204 ) 205 expect_identical( 206 s2_covers_matrix(timezones, countries), 207 s2_covers_matrix_brute_force(timezones, countries) 208 ) 209 210 # covered by 211 expect_identical( 212 s2_covered_by_matrix(countries, countries), 213 s2_covered_by_matrix_brute_force(countries, countries) 214 ) 215 expect_identical( 216 s2_covered_by_matrix(timezones, countries), 217 s2_covered_by_matrix_brute_force(timezones, countries) 218 ) 219 220 # intersects 221 expect_identical( 222 s2_intersects_matrix(countries, countries), 223 s2_intersects_matrix_brute_force(countries, countries) 224 ) 225 expect_identical( 226 s2_intersects_matrix(timezones, countries), 227 s2_intersects_matrix_brute_force(timezones, countries) 228 ) 229 230 # disjoint 231 expect_identical( 232 s2_disjoint_matrix(countries, countries), 233 s2_disjoint_matrix_brute_force(countries, countries) 234 ) 235 expect_identical( 236 s2_disjoint_matrix(timezones, countries), 237 s2_disjoint_matrix_brute_force(timezones, countries) 238 ) 239 240 # equals 241 expect_identical( 242 s2_equals_matrix(countries, countries), 243 s2_equals_matrix_brute_force(countries, countries) 244 ) 245 expect_identical( 246 s2_equals_matrix(timezones, countries), 247 s2_equals_matrix_brute_force(timezones, countries) 248 ) 249}) 250