tests/testthat/test-geojson_sp.R

test_that("geojson_sp works with geo_list inputs", {
  skip_on_cran()

  # numeric vector of length 2, making a point type
  vec <- c(-99.74, 32.45)
  a <- geojson_sp(geojson_list(vec))
  expect_s4_class(a, "SpatialPointsDataFrame")

  # from a list to a data.frame
  mylist <- list(
    list(latitude = 30, longitude = 120, marker = "red"),
    list(latitude = 30, longitude = 130, marker = "blue")
  )
  b <- geojson_sp(supm(geojson_list(mylist)))
  expect_s4_class(b, "SpatialPointsDataFrame")

  # from a list of numeric vectors to a polygon
  # vecs <- list(c(100.0,0.0), c(101.0,0.0), c(101.0,1.0), c(100.0,1.0), c(100.0,0.0))
  # d <- geojson_sp(geojson_list(vecs, geometry="polygon"))
  # expect_s4_class(d, "SpatialPolygonsDataFrame")
})

test_that("geojson_sp works with string inputs", {
  skip_on_cran()

  x <- unclass(geojson_json(c(-99.74, 32.45)))
  a <- geojson_sp(x)
  expect_s4_class(a, "Spatial")
})

test_that("geojson_sp works with json inputs", {
  skip_on_cran()

  # numeric vector of length 2, making a point type
  vec <- c(-99.74, 32.45)
  a <- geojson_sp(geojson_json(vec))
  expect_s4_class(a, "SpatialPointsDataFrame")

  # from a list to a data.frame
  mylist <- list(
    list(latitude = 30, longitude = 120, marker = "red"),
    list(latitude = 30, longitude = 130, marker = "blue")
  )
  b <- geojson_sp(supm(geojson_json(mylist)))
  expect_s4_class(b, "SpatialPointsDataFrame")

  # from a polygon
  poly <- structure('{"type":"FeatureCollection","features":[{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[53, -42],
[57, -42],
[57, -47],
[53, -47],
[53, -42]
]
]
}}]
}', class = c("json", "geo_json"))
  d <- geojson_sp(poly)
  # expect_s4_class(d, "SpatialPolygonsDataFrame")
})

Try the geojsonio package in your browser

Any scripts or data that you put into this service are public.

geojsonio documentation built on Sept. 8, 2023, 5:54 p.m.