R/maps.R

Defines functions st_as_sfc.map st_as_sf.map

Documented in st_as_sfc.map st_as_sf.map

#' @export
#' @name st_as_sf
st_as_sf.map = function(x, ...) {
	if (!requireNamespace("maptools", quietly = TRUE))
		stop("package maptools required, please install it first")
	if (!requireNamespace("sp", quietly = TRUE))
		stop("package sp required, please install it first")

	ID0 = vapply(strsplit(x$names, ":"), function(y) y[1], "")
	m.sp = maptools::map2SpatialPolygons(x, IDs = ID0, 
		proj4string = sp::CRS("+proj=longlat +datum=WGS84 +no_defs"))
	m = st_as_sf(m.sp)
	m$ID = vapply(m.sp@polygons, function(x) slot(x, "ID"), "")
	m
}

#' @export
#' @name st_as_sfc
st_as_sfc.map = function(x, ...) {
	st_geometry(st_as_sf(x, ...))
}

Try the sf package in your browser

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

sf documentation built on July 24, 2019, 5:05 p.m.