R/data_norway_map.R

Defines functions gen_norway_map_counties gen_norway_map_municips

#' Maps of Norwegian Counties.
#'
#' We conveniently package map datasets for Norwegian counties
#' (taken from Geonorge) that can be used in ggplot2 without needing any geo
#' libraries. This data is licensed under Creative Commons 0.
#'
#' @format
#' \describe{
#' \item{long}{Location code.}
#' \item{lat}{Location name.}
#' \item{order}{The order that this line should be plotted in.}
#' \item{hole}{Geodata. Not used.}
#' \item{piece}{Geodata. Not used.}
#' \item{group}{Needs to be used as 'group' aesthetic in ggplot2.}
#' \item{id}{Non-informative id code.}
#' \item{location_code}{Location code (county code).}
#' }
#' @source \url{https://kartkatalog.geonorge.no/metadata/uuid/cb02ab77-d3e6-4500-8a92-ea67367e7734}
#' @examples
#' library(ggplot2)
#' q <- ggplot(mapping = aes(x = long, y = lat, group = group, fill = location_code))
#' q <- q + geom_polygon(data = fhidata::norway_map_counties, color = "black")
#' q <- q + theme_void()
#' q <- q + coord_quickmap()
#' q
"norway_map_counties"

#' Maps of Norwegian Municipalities
#'
#' We conveniently package map datasets for Norwegian municipalities
#' (taken from Geonorge) that can be used in ggplot2 without needing any geo
#' libraries. This data is licensed under Creative Commons 0.
#'
#' @format
#' \describe{
#' \item{long}{Location code.}
#' \item{lat}{Location name.}
#' \item{order}{The order that this line should be plotted in.}
#' \item{hole}{Geodata. Not used.}
#' \item{piece}{Geodata. Not used.}
#' \item{group}{Needs to be used as 'group' aesthetic in ggplot2.}
#' \item{id}{Non-informative id code.}
#' \item{location_code}{Location code (municipality code).}
#' }
#' @source \url{https://kartkatalog.geonorge.no/metadata/uuid/cb02ab77-d3e6-4500-8a92-ea67367e7734}
#' @examples
#' library(ggplot2)
#' q <- ggplot(mapping = aes(x = long, y = lat, group = group))
#' q <- q + geom_polygon(data = fhidata::norway_map_municips, color = "black")
#' q <- q + theme_void()
#' q <- q + coord_quickmap()
#' q
"norway_map_municips"

gen_norway_map_counties <- function() {
  id <- NULL
  location_code <- NULL
  long <- NULL
  lat <- NULL

  require_namespace(c("geojsonio", "broom", "rmapshaper", "sp"))

  spdf <- geojsonio::geojson_read(
    system.file("extdata", "Fylker19.geojson", package = "fhidata"),
    what = "sp"
  )

  spdf_simple <- rmapshaper::ms_simplify(spdf, keep = 0.1)

  spdf_fortified <- broom::tidy(spdf_simple, region = "fylkesnummer")

  setDT(spdf_fortified)
  spdf_fortified[, location_code := sprintf("county%s", id)]

  # convert from UTM to latlong
  utm <- spdf_fortified[, c("long", "lat")]
  sputm <- sp::SpatialPoints(utm, proj4string = sp::CRS("+proj=utm +zone=33 +datum=WGS84"))
  spgeo <- sp::spTransform(sputm, sp::CRS("+proj=longlat +datum=WGS84"))
  spgeo <- as.data.table(spgeo)

  spdf_fortified[, long := spgeo$long]
  spdf_fortified[, lat := spgeo$lat]

  return(invisible(spdf_fortified))
}

gen_norway_map_municips <- function() {
  id <- NULL
  location_code <- NULL
  long <- NULL
  lat <- NULL

  require_namespace(c("geojsonio", "broom", "rmapshaper", "sp"))

  spdf <- geojsonio::geojson_read(
    system.file("extdata", "Kommuner19.geojson", package = "fhidata"),
    what = "sp"
  )

  spdf_simple <- rmapshaper::ms_simplify(rgeos::gBuffer(spdf, byid = TRUE, width = 0), keep = 0.1)

  spdf_fortified <- broom::tidy(spdf_simple, region = "kommunenummer")

  setDT(spdf_fortified)
  spdf_fortified[, location_code := sprintf("municip%s", formatC(as.numeric(id), width = 4, flag = "0"))]

  # convert from UTM to latlong
  utm <- spdf_fortified[, c("long", "lat")]
  sputm <- sp::SpatialPoints(utm, proj4string = sp::CRS("+proj=utm +zone=33 +datum=WGS84"))
  spgeo <- sp::spTransform(sputm, sp::CRS("+proj=longlat +datum=WGS84"))
  spgeo <- as.data.table(spgeo)

  spdf_fortified[, long := spgeo$long]
  spdf_fortified[, lat := spgeo$lat]

  return(invisible(spdf_fortified))
}

Try the fhidata package in your browser

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

fhidata documentation built on Aug. 29, 2019, 5:09 p.m.