|
1 | | -# context("sf") |
2 | | -# |
3 | | -# |
4 | | -# test_that("sf objects encoded", { |
5 | | -# |
6 | | -# df <- data.frame( |
7 | | -# myId = c(1,1,1,1,1,1,1,1,2,2,2,2) |
8 | | -# , lineId = c(1,1,1,1,2,2,2,2,1,1,1,2) |
9 | | -# , lon = c(-80.190, -66.118, -64.757, -80.190, -70.579, -67.514, -66.668, -70.579, -70, -49, -51, -70) |
10 | | -# , lat = c(26.774, 18.466, 32.321, 26.774, 28.745, 29.570, 27.339, 28.745, 22, 23, 22, 22) |
11 | | -# ) |
12 | | -# |
13 | | -# p1 <- as.matrix(df[4:1, c("lon", "lat")]) |
14 | | -# p2 <- as.matrix(df[8:5, c("lon", "lat")]) |
15 | | -# p3 <- as.matrix(df[9:12, c("lon", "lat")]) |
16 | | -# |
17 | | -# sfAttrs <- function(sf, geom) { |
18 | | -# attr(sf, "class") <- c("XY", geom, "sfg") |
19 | | -# sf <- list(sf) |
20 | | -# attr(sf, "class") <- c("sfc_POINT", "sfc") |
21 | | -# attr(sf, "precision") <- 0 |
22 | | -# bbox <- c(xmin = 0, ymin = 0, xmax = 0, ymax = 0) |
23 | | -# attr(sf, "bbox") <- bbox |
24 | | -# attr(sf, "precision") <- 0 |
25 | | -# attr(sf, "n_empty") <- 0 |
26 | | -# return(sf) |
27 | | -# } |
28 | | -# |
29 | | -# constructSf <- function(sf) { |
30 | | -# sf <- data.frame(geometry = sf) |
31 | | -# attr(sf, "class") <- c("sf", "data.frame") |
32 | | -# attr(sf, "sf_column") <- "geometry" |
33 | | -# attr(sf, "agr") <- factor() |
34 | | -# return(sf) |
35 | | -# } |
36 | | -# |
37 | | -# pt <- matrix(c(df[1, "lon"], df[1, "lat"]), nrow = 1) |
38 | | -# mp <- as.matrix(df[1:2, c("lon", "lat")]) |
39 | | -# ln <- p3 |
40 | | -# |
41 | | -# dfpt <- data.frame(geometry = as.list(pt)) |
42 | | -# attr(dfpt, "class") <- c("sf", "data.frame") |
43 | | -# |
44 | | -# data.frame(list(mp)) |
45 | | -# data.frame(list(ln)) |
46 | | -# data.frame(geometry = list(mp, ln)) |
47 | | -# |
48 | | -# pt <- sfAttrs(pt, "POINT") |
49 | | -# pt <- constructSf(pt) |
50 | | -# |
51 | | -# mp <- sfAttrs(mp, "MULTIPOINT") |
52 | | -# #mp <- constructSf(mp) |
53 | | -# ln <- sfAttrs(ln, "LINESTRING") |
54 | | -# #ln <- constructSf(ln) |
55 | | -# ml <- list(p1, p2) |
56 | | -# ml <- sfAttrs(ml, "MULTILINESTRING") |
57 | | -# #ml <- constructSf(ml) |
58 | | -# po <- list(p1, p2) |
59 | | -# po <- sfAttrs(po, "POLYGON") |
60 | | -# #po <- constructSf(po) |
61 | | -# mpl <- list( list(p1, p2), list(p3) ) |
62 | | -# mpl <- sfAttrs(mpl, "MULTIPOLYGON") |
63 | | -# #mpl <- constructSf(mpl) |
64 | | -# |
65 | | -# rbind(pt, mp) |
66 | | -# |
67 | | -# # |
68 | | -# sf <- rbind( |
69 | | -# sf::st_sf(geometry = point), |
70 | | -# sf::st_sf(geometry = multipolygon), |
71 | | -# sf::st_sf(geometry = multilinestring), |
72 | | -# sf::st_sf(geometry = linestring), |
73 | | -# sf::st_sf(geometry = point), |
74 | | -# sf::st_sf(geometry = multipoint) |
75 | | -# ) |
76 | | -# |
77 | | -# expect_true(inherits(googleway:::normalise_sf(sf), "sfencoded")) |
78 | | -# |
79 | | -# enc <- googlePolylines::encode(sf) |
80 | | -# expect_true(googleway:::findEncodedColumn(enc, NULL) == "geometry") |
81 | | -# expect_true(googleway:::findEncodedColumn(enc, "geometry") == "geometry") |
82 | | -# |
83 | | -# df <- data.frame(polyline = "abc") |
84 | | -# expect_true(googleway:::findEncodedColumn(df, 'polyline') == "polyline") |
85 | | -# |
86 | | -# }) |
87 | | -# |
88 | | -# test_that("correct sf rows are returned", { |
89 | | -# |
90 | | -# df <- data.frame(myId = c(1,1,1,1,1,1,1,1,2,2,2,2), |
91 | | -# lineId = c(1,1,1,1,2,2,2,2,1,1,1,2), |
92 | | -# lon = c(-80.190, -66.118, -64.757, -80.190, -70.579, -67.514, -66.668, -70.579, -70, -49, -51, -70), |
93 | | -# lat = c(26.774, 18.466, 32.321, 26.774, 28.745, 29.570, 27.339, 28.745, 22, 23, 22, 22)) |
94 | | -# |
95 | | -# p1 <- as.matrix(df[4:1, c("lon", "lat")]) |
96 | | -# p2 <- as.matrix(df[8:5, c("lon", "lat")]) |
97 | | -# p3 <- as.matrix(df[9:12, c("lon", "lat")]) |
98 | | -# |
99 | | -# point <- sf::st_sfc(sf::st_point(x = c(df[1,"lon"], df[1,"lat"]))) |
100 | | -# multipoint <- sf::st_sfc(sf::st_multipoint(x = as.matrix(df[1:2, c("lon", "lat")]))) |
101 | | -# polygon <- sf::st_sfc(sf::st_polygon(x = list(p1, p2))) |
102 | | -# linestring <- sf::st_sfc(sf::st_linestring(p3)) |
103 | | -# multilinestring <- sf::st_sfc(sf::st_multilinestring(list(p1, p2))) |
104 | | -# multipolygon <- sf::st_sfc(sf::st_multipolygon(x = list(list(p1, p2), list(p3)))) |
105 | | -# |
106 | | -# sf <- rbind( |
107 | | -# sf::st_sf(geometry = polygon), |
108 | | -# sf::st_sf(geometry = multipolygon), |
109 | | -# sf::st_sf(geometry = multilinestring), |
110 | | -# sf::st_sf(geometry = linestring), |
111 | | -# sf::st_sf(geometry = point), |
112 | | -# sf::st_sf(geometry = multipoint) |
113 | | -# ) |
114 | | -# |
115 | | -# expect_true(nrow(googleway:::normaliseSfData(sf, "POLYGON")) == 2) |
116 | | -# expect_true(nrow(googleway:::normaliseSfData(sf, "LINE")) == 2) |
117 | | -# expect_true(nrow(googleway:::normaliseSfData(sf, "POINT")) == 2) |
118 | | -# |
119 | | -# enc <- googlePolylines::encode(sf) |
120 | | -# |
121 | | -# expect_identical(enc, googleway:::normalise_sf(sf)) |
122 | | -# expect_identical(enc, googleway:::normalise_sf(enc)) |
123 | | -# expect_error(googleway:::normalise_sf(""), "Expecting an sf or sfencoded object to add_sf") |
124 | | -# |
125 | | -# expect_true(nrow(googleway:::normaliseSfData(enc, "POLYGON")) == 2) |
126 | | -# expect_true(nrow(googleway:::normaliseSfData(enc, "LINE")) == 2) |
127 | | -# expect_true(nrow(googleway:::normaliseSfData(enc, "POINT")) == 2) |
128 | | -# |
129 | | -# }) |
130 | | -# |
| 1 | +context("sf") |
| 2 | + |
| 3 | + |
| 4 | +test_that("sf objects encoded", { |
| 5 | + |
| 6 | + sf <- structure(list(geometry = structure(list(structure(list(structure(c(-80.19, |
| 7 | +-64.757, -66.118, -80.19, 26.774, 32.321, 18.466, 26.774), .Dim = c(4L, |
| 8 | +2L), .Dimnames = list(c("4", "3", "2", "1"), c("lon", "lat"))), |
| 9 | +structure(c(-70.579, -66.668, -67.514, -70.579, 28.745, 27.339, |
| 10 | +29.57, 28.745), .Dim = c(4L, 2L), .Dimnames = list(c("8", |
| 11 | +"7", "6", "5"), c("lon", "lat")))), class = c("XY", "POLYGON", |
| 12 | +"sfg")), structure(list(list(structure(c(-80.19, -64.757, -66.118, |
| 13 | +-80.19, 26.774, 32.321, 18.466, 26.774), .Dim = c(4L, 2L), .Dimnames = list( |
| 14 | +c("4", "3", "2", "1"), c("lon", "lat"))), structure(c(-70.579, |
| 15 | +-66.668, -67.514, -70.579, 28.745, 27.339, 29.57, 28.745), .Dim = c(4L, |
| 16 | +2L), .Dimnames = list(c("8", "7", "6", "5"), c("lon", "lat")))), |
| 17 | +list(structure(c(-70, -49, -51, -70, 22, 23, 22, 22), .Dim = c(4L, |
| 18 | +2L), .Dimnames = list(c("9", "10", "11", "12"), c("lon", |
| 19 | +"lat"))))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list( |
| 20 | +structure(c(-80.19, -64.757, -66.118, -80.19, 26.774, 32.321, |
| 21 | +18.466, 26.774), .Dim = c(4L, 2L), .Dimnames = list(c("4", |
| 22 | +"3", "2", "1"), c("lon", "lat"))), structure(c(-70.579, -66.668, |
| 23 | +-67.514, -70.579, 28.745, 27.339, 29.57, 28.745), .Dim = c(4L, |
| 24 | +2L), .Dimnames = list(c("8", "7", "6", "5"), c("lon", "lat" |
| 25 | +)))), class = c("XY", "MULTILINESTRING", "sfg")), structure(c(-70, |
| 26 | +-49, -51, -70, 22, 23, 22, 22), .Dim = c(4L, 2L), .Dimnames = list( |
| 27 | +c("9", "10", "11", "12"), c("lon", "lat")), class = c("XY", |
| 28 | +"LINESTRING", "sfg")), structure(c(-80.19, 26.774), class = c("XY", |
| 29 | +"POINT", "sfg")), structure(c(-80.19, -66.118, 26.774, 18.466 |
| 30 | +), .Dim = c(2L, 2L), .Dimnames = list(c("1", "2"), c("lon", "lat" |
| 31 | +)), class = c("XY", "MULTIPOINT", "sfg"))), class = c("sfc_GEOMETRY", |
| 32 | +"sfc"), precision = 0, bbox = structure(c(xmin = -80.19, ymin = 18.466, |
| 33 | +xmax = -49, ymax = 32.321), class = "bbox"), crs = structure(list( |
| 34 | +epsg = NA_integer_, proj4string = NA_character_), class = "crs"), n_empty = 0L, classes = c("POLYGON", |
| 35 | +"MULTIPOLYGON", "MULTILINESTRING", "LINESTRING", "POINT", "MULTIPOINT" |
| 36 | +))), row.names = c(NA, 6L), sf_column = "geometry", agr = structure(integer(0), class = "factor", .Label = c("constant", |
| 37 | +"aggregate", "identity"), .Names = character(0)), class = c("sf", |
| 38 | +"data.frame")) |
| 39 | + |
| 40 | + expect_true(inherits(googleway:::normalise_sf(sf), "sfencoded")) |
| 41 | + |
| 42 | + enc <- googlePolylines::encode(sf) |
| 43 | + expect_true(googleway:::findEncodedColumn(enc, NULL) == "geometry") |
| 44 | + expect_true(googleway:::findEncodedColumn(enc, "geometry") == "geometry") |
| 45 | + |
| 46 | + df <- data.frame(polyline = "abc") |
| 47 | + expect_true(googleway:::findEncodedColumn(df, 'polyline') == "polyline") |
| 48 | + |
| 49 | +}) |
| 50 | + |
| 51 | +test_that("correct sf rows are returned", { |
| 52 | + |
| 53 | + sf <- structure(list(geometry = structure(list(structure(list(structure(c(-80.19, |
| 54 | +-64.757, -66.118, -80.19, 26.774, 32.321, 18.466, 26.774), .Dim = c(4L, |
| 55 | +2L), .Dimnames = list(c("4", "3", "2", "1"), c("lon", "lat"))), |
| 56 | +structure(c(-70.579, -66.668, -67.514, -70.579, 28.745, 27.339, |
| 57 | +29.57, 28.745), .Dim = c(4L, 2L), .Dimnames = list(c("8", |
| 58 | +"7", "6", "5"), c("lon", "lat")))), class = c("XY", "POLYGON", |
| 59 | +"sfg")), structure(list(list(structure(c(-80.19, -64.757, -66.118, |
| 60 | +-80.19, 26.774, 32.321, 18.466, 26.774), .Dim = c(4L, 2L), .Dimnames = list( |
| 61 | +c("4", "3", "2", "1"), c("lon", "lat"))), structure(c(-70.579, |
| 62 | +-66.668, -67.514, -70.579, 28.745, 27.339, 29.57, 28.745), .Dim = c(4L, |
| 63 | +2L), .Dimnames = list(c("8", "7", "6", "5"), c("lon", "lat")))), |
| 64 | +list(structure(c(-70, -49, -51, -70, 22, 23, 22, 22), .Dim = c(4L, |
| 65 | +2L), .Dimnames = list(c("9", "10", "11", "12"), c("lon", |
| 66 | +"lat"))))), class = c("XY", "MULTIPOLYGON", "sfg")), structure(list( |
| 67 | +structure(c(-80.19, -64.757, -66.118, -80.19, 26.774, 32.321, |
| 68 | +18.466, 26.774), .Dim = c(4L, 2L), .Dimnames = list(c("4", |
| 69 | +"3", "2", "1"), c("lon", "lat"))), structure(c(-70.579, -66.668, |
| 70 | +-67.514, -70.579, 28.745, 27.339, 29.57, 28.745), .Dim = c(4L, |
| 71 | +2L), .Dimnames = list(c("8", "7", "6", "5"), c("lon", "lat" |
| 72 | +)))), class = c("XY", "MULTILINESTRING", "sfg")), structure(c(-70, |
| 73 | +-49, -51, -70, 22, 23, 22, 22), .Dim = c(4L, 2L), .Dimnames = list( |
| 74 | +c("9", "10", "11", "12"), c("lon", "lat")), class = c("XY", |
| 75 | +"LINESTRING", "sfg")), structure(c(-80.19, 26.774), class = c("XY", |
| 76 | +"POINT", "sfg")), structure(c(-80.19, -66.118, 26.774, 18.466 |
| 77 | +), .Dim = c(2L, 2L), .Dimnames = list(c("1", "2"), c("lon", "lat" |
| 78 | +)), class = c("XY", "MULTIPOINT", "sfg"))), class = c("sfc_GEOMETRY", |
| 79 | +"sfc"), precision = 0, bbox = structure(c(xmin = -80.19, ymin = 18.466, |
| 80 | +xmax = -49, ymax = 32.321), class = "bbox"), crs = structure(list( |
| 81 | +epsg = NA_integer_, proj4string = NA_character_), class = "crs"), n_empty = 0L, classes = c("POLYGON", |
| 82 | +"MULTIPOLYGON", "MULTILINESTRING", "LINESTRING", "POINT", "MULTIPOINT" |
| 83 | +))), row.names = c(NA, 6L), sf_column = "geometry", agr = structure(integer(0), class = "factor", .Label = c("constant", |
| 84 | +"aggregate", "identity"), .Names = character(0)), class = c("sf", |
| 85 | +"data.frame")) |
| 86 | + |
| 87 | + expect_true(nrow(googleway:::normaliseSfData(sf, "POLYGON")) == 2) |
| 88 | + expect_true(nrow(googleway:::normaliseSfData(sf, "LINE")) == 2) |
| 89 | + expect_true(nrow(googleway:::normaliseSfData(sf, "POINT")) == 2) |
| 90 | + |
| 91 | + enc <- googlePolylines::encode(sf) |
| 92 | + |
| 93 | + expect_identical(enc, googleway:::normalise_sf(sf)) |
| 94 | + expect_identical(enc, googleway:::normalise_sf(enc)) |
| 95 | + expect_error(googleway:::normalise_sf(""), "Expecting an sf or sfencoded object to add_sf") |
| 96 | + |
| 97 | + expect_true(nrow(googleway:::normaliseSfData(enc, "POLYGON")) == 2) |
| 98 | + expect_true(nrow(googleway:::normaliseSfData(enc, "LINE")) == 2) |
| 99 | + expect_true(nrow(googleway:::normaliseSfData(enc, "POINT")) == 2) |
| 100 | + |
| 101 | +}) |
| 102 | + |
0 commit comments