k3r0
k3r0

Reputation: 377

Converting geojson into sf for a clotopleth map

I have recently been wrapping my head around the leaflet package and have gotten around the basics of adding markers, reading/plotting shapefiles from local source and displaying the final outputs in Shiny.

I am currently trying to understand a bit more how to access geojson files directly from the web since the maps that I create in Shiny aren't being properly displayed when uploaded to shinyapps.io (I think it's because the local files aren't being uploaded with the app itself, need to read a bit more on that)

That being said I found this link which contains the political map of all countries in the world. The added documentation suggest using the following code to obtain the polygons but the suggested method does not seem to be working for me.

library(jsonlite)
json_file <- 'https://datahub.io/core/geo-countries/datapackage.json'
json_data <- fromJSON(paste(readLines(json_file), collapse=""))

I tried using geojson_sf(json_data) to convert the extracted information into an sf but I get the error "Error in UseMethod". From what I have read it seems that the best format to work with spatial information is sf which would allow me to left_join the desired information and add population data to each country by its name.

Can I get a pointer on how to extract the polygons so I can addPolygons() them into my leaflet map?

Example data:

Countries <- structure(list(Canada = 37590000, UnitedStates = 328200000, Mexico = 127600000), row.names = c(NA, -1L), class = c("tbl_df", "tbl", "data.frame"))

Appreciate any pointers or links to references which can clarify my question. Thanks!

Upvotes: 3

Views: 794

Answers (1)

SymbolixAU
SymbolixAU

Reputation: 26248

A couple of points in your question suggest a misunderstanding of what geojson is, and what the functions you're using are supposed to do:

  1. geojson_sf() is designed to work on raw json/geojson. So it won't work on your already-parsed json_data.
  2. You say you're trying to work with geojson files, but the link you give is not geojson

If you go to the link https://datahub.io/core/geo-countries/datapackage.json you'll see it's JSON describing the data, and it gives a path of the actual geojson file

library(sf)

json_file <- 'https://datahub.io/core/geo-countries/datapackage.json'
json_data <- jsonlite::fromJSON(json_file)

## The actual geojson is contained here
geojson <- json_data$resources$path[3]

geojson
# [1] "https://pkgstore.datahub.io/core/geo-countries/countries/archive/23f420f929e0e09c39d916b8aaa166fb/countries.geojson"

So now you know where the geojson is located, you can read it in a couple of different ways:

  1. Using geojsonsf
sf <- geojsonsf::geojson_sf(geojson)
  1. Using sf
sf <- sf::st_read(geojson)

You can / should use sf in this instance because the data is not large and it can easily handle it.

I built geojsonsf for when you need a bit more speed, and for it to handle the cases where the sf geojson reader struggles on large files

Upvotes: 1

Related Questions