R/explode.R

Defines functions explode_sf ms_explode.sfc ms_explode.sf explode_sp ms_explode.SpatialLines ms_explode.SpatialPolygons ms_explode.geo_list ms_explode.geo_json ms_explode.character ms_explode

Documented in ms_explode

#' Convert multipart lines or polygons to singlepart
#'
#' For objects of class \code{Spatial} (e.g., \code{SpatialPolygonsDataFrame}),
#' you may find it faster to use \code{sp::disaggregate}.
#'
#' There is currently no method for SpatialMultiPoints
#'
#' @param input One of:
#' \itemize{
#'  \item \code{geo_json} or \code{character} multipart lines, or polygons;
#'  \item \code{geo_list} multipart lines, or polygons;
#'  \item multipart \code{SpatialPolygons}, \code{SpatialLines};
#'  \item \code{sf} or \code{sfc} multipart lines, or polygons object
#' }
#' @inheritParams apply_mapshaper_commands
#'
#' @return same class as input
#'
#' @examples
#' library(geojsonio)
#' library(sp)
#'
#' poly <- structure("{\"type\":\"FeatureCollection\",\"crs\":
#'           {\"type\":\"name\",\"properties\":{\"name\":
#'           \"urn:ogc:def:crs:OGC:1.3:CRS84\"}},\"features\":
#'           [\n{\"type\":\"Feature\",\"geometry\":{\"type\":
#'           \"MultiPolygon\",\"coordinates\":[[[[102,2],[102,3],
#'           [103,3],[103,2],[102,2]]],[[[100,0],[100,1],[101,1],
#'           [101,0],[100,0]]]]},\"properties\":{\"rmapshaperid\":0}}\n]}",
#'           class = c("json", "geo_json"))
#'
#' poly <- geojson_sp(poly)
#' plot(poly)
#' length(poly)
#' poly@data
#'
#' # Explode the polygon
#' out <- ms_explode(poly)
#' plot(out)
#' length(out)
#' out@data
#'
#' @export
ms_explode <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  UseMethod("ms_explode")
}

#' @export
ms_explode.character <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  input <- check_character_input(input)

  apply_mapshaper_commands(data = input, command = "-explode", force_FC = force_FC, sys = sys, sys_mem = sys_mem)

}

#' @export
ms_explode.geo_json <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  apply_mapshaper_commands(data = input, command = "-explode", force_FC = force_FC, sys = sys, sys_mem = sys_mem)
}

#' @export
ms_explode.geo_list <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  geojson <- geo_list_to_json(input)

  ret <- apply_mapshaper_commands(data = geojson, command = "-explode", force_FC = force_FC, sys = sys, sys_mem = sys_mem)

  geojsonio::geojson_list(ret)
}

## The method using mapshaper's explode works, but is waaaay slower than
## sp::disaggregate due to converstion to/from geojson

#' @export
ms_explode.SpatialPolygons <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  explode_sp(input, sys = sys, sys_mem = sys_mem)
}

#' @export
ms_explode.SpatialLines <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  explode_sp(input, sys = sys, sys_mem = sys_mem)
}

# #' @describeIn ms_explode Method for SpatialPoints
# #' @export
# ms_explode.SpatialPoints <- function(input, force_FC = TRUE) {
#   explode_sp(input, force_FC)
# }

explode_sp <- function(input, sys, sys_mem) {
 ms_sp(input = input, call = "-explode", sys = sys, sys_mem = sys_mem)
}

#' @export
ms_explode.sf <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  explode_sf(input = input, sys = sys, sys_mem = sys_mem)
}

#' @export
ms_explode.sfc <- function(input, force_FC = TRUE, sys = FALSE, sys_mem = 8) {
  explode_sf(input = input, sys = sys, sys_mem = sys_mem)
}

explode_sf <- function(input, sys, sys_mem) {
  ms_sf(input = input, call = "-explode", sys = sys, sys_mem = sys_mem)
}

Try the rmapshaper package in your browser

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

rmapshaper documentation built on May 11, 2022, 1:09 a.m.