Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

ms_dissolve

Aggregate shapes in a polygon or point layer.


Description

Aggregates using specified field, or all shapes if no field is given. For point layers, replaces a group of points with their centroid.

Usage

ms_dissolve(
  input,
  field = NULL,
  sum_fields = NULL,
  copy_fields = NULL,
  weight = NULL,
  snap = TRUE,
  force_FC = TRUE,
  sys = FALSE
)

Arguments

input

spatial object to dissolve. One of:

  • geo_json or character points or polygons;

  • geo_list points or polygons;

  • SpatialPolygons, or SpatialPoints

field

the field to dissolve on

sum_fields

fields to sum

copy_fields

fields to copy. The first instance of each field will be copied to the aggregated feature.

weight

Name of an attribute field for generating weighted centroids (points only).

snap

Snap together vertices within a small distance threshold to fix small coordinate misalignment in adjacent polygons. Default TRUE.

force_FC

should the output be forced to be a FeatureCollection even if there are no attributes? Default TRUE. FeatureCollections are more compatible with rgdal::readOGR and geojsonio::geojson_sp. If FALSE and there are no attributes associated with the geometries, a GeometryCollection will be output. Ignored for Spatial objects, as the output is always the same class as the input.

sys

Should the system mapshaper be used instead of the bundled mapshaper? Gives better performance on large files. Requires the mapshaper node package to be installed and on the PATH.

Value

the same class as the input

Examples

library(geojsonio)
library(sp)

poly <- structure('{"type":"FeatureCollection",
  "features":[
  {"type":"Feature",
  "properties":{"a": 1, "b": 2},
  "geometry":{"type":"Polygon","coordinates":[[
  [102,2],[102,3],[103,3],[103,2],[102,2]
  ]]}}
  ,{"type":"Feature",
  "properties":{"a": 5, "b": 3},
  "geometry":{"type":"Polygon","coordinates":[[
  [100,0],[100,1],[101,1],[101,0],[100,0]
  ]]}}]}', class = c("json", "geo_json"))
poly <- geojson_sp(poly)
plot(poly)
length(poly)
poly@data

# Dissolve the polygon
out <- ms_dissolve(poly)
plot(out)
length(out)
out@data

# Dissolve and summing columns
out <- ms_dissolve(poly, sum_fields = c("a", "b"))
plot(out)
out@data

rmapshaper

Client for 'mapshaper' for 'Geospatial' Operations

v0.4.4
MIT + file LICENSE
Authors
Andy Teucher [aut, cre], Kenton Russell [aut] (JavaScript support), Matthew Bloch [cph] (mapshaper Javascript library)
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.