Skip to content

Instantly share code, notes, and snippets.

@patperu
Last active August 22, 2017 18:31
Show Gist options
  • Save patperu/6ae344433ec0638a26e686c5d8f559de to your computer and use it in GitHub Desktop.
Save patperu/6ae344433ec0638a26e686c5d8f559de to your computer and use it in GitHub Desktop.
How to convert to a sf-object?
# see also https://github.com/r-spatial/sf/issues/185#issuecomment-304093929
library(sf)
library(purrr)
library(jsonlite)
# devtools::install_github("ropensci/crul")
library(crul)
x <- HttpClient$new(url = "http://dati.mit.gov.it/catalog/api/action/datastore_search")
res <- x$get(query = list(resource_id="484e08bf-3e0d-4b89-81b5-407b80c5f5e8", limit=5))
res <- jsonlite::fromJSON(res$parse())
z <- res$result$records
w <- z$`Multipoint WGS84 EPSG:4386 Coordinates`
w
# [1] "[16.578441,40.669874]"
# [2] "[16.390963,39.752256]"
# [3] "[[15.229943,40.441221],[15.889952,40.516008]]"
# [4] "[[13.565973,37.322554],[13.837775,37.371788],[13.672253,37.317606],[13.685218,37.422153],[13.750323,37.403225]]"
# [5] "[[11.903521,42.324303],[12.024773,42.311299]]"
# Apparently "single points" are not in the correct format
w[1] <- paste0("[", w[1], "]")
w[2] <- paste0("[", w[2], "]")
mm <- function(x) paste0('{"type":"MultiPoint","coordinates":', x, "}")
k <- map(w, mm) %>%
map(read_sf)
k[[1]]
# > k[[1]]
# Simple feature collection with 1 feature and 0 fields
# geometry type: MULTIPOINT
# dimension: XY
# bbox: xmin: 16.57844 ymin: 40.66987 xmax: 16.57844 ymax: 40.66987
# epsg (SRID): 4326
# proj4string: +proj=longlat +datum=WGS84 +no_defs
# geometry
# 1 MULTIPOINT (16.578441 40.66...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment