Title: | Client for 'mapshaper' for 'Geospatial' Operations |
---|---|
Description: | Edit and simplify 'geojson', 'Spatial', and 'sf' objects. This is wrapper around the 'mapshaper' 'JavaScript' library by Matthew Bloch <https://github.com/mbloch/mapshaper/> to perform topologically-aware polygon simplification, as well as other operations such as clipping, erasing, dissolving, and converting 'multi-part' to 'single-part' geometries. |
Authors: | Andy Teucher [aut, cre] , Kenton Russell [aut] (JavaScript support), Matthew Bloch [cph] (mapshaper Javascript library) |
Maintainer: | Andy Teucher <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.5.0.9000 |
Built: | 2024-11-04 04:34:34 UTC |
Source: | https://github.com/ateucher/rmapshaper |
Apply a mapshaper command string to a geojson object
apply_mapshaper_commands( data, command, force_FC = TRUE, sys = FALSE, sys_mem = getOption("mapshaper.sys_mem", default = 8), quiet = getOption("mapshaper.sys_quiet", default = FALSE), gj2008 = FALSE )
apply_mapshaper_commands( data, command, force_FC = TRUE, sys = FALSE, sys_mem = getOption("mapshaper.sys_mem", default = 8), quiet = getOption("mapshaper.sys_quiet", default = FALSE), gj2008 = FALSE )
data |
character containing geojson or path to geojson file. If a file
path, |
command |
valid mapshaper command string |
force_FC |
should the output be forced to be a FeatureCollection (or sf
object or Spatial*DataFrame) even if there are no attributes? Default
|
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. |
sys_mem |
How much memory (in GB) should be allocated if using the
system mapshaper ( |
quiet |
If |
gj2008 |
Generate output that is consistent with the pre-RFC 7946
GeoJSON spec (dating to 2008). Polygon rings are CW and holes are CCW,
which is the opposite of the default RFC 7946-compatible output. This should
be rarely needed, though may be useful when preparing data for D3-based
data visualizations (such as |
geojson
nc <- sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf")) rmapshaper::apply_mapshaper_commands(geojsonsf::sf_geojson(nc), "-clean")
nc <- sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf")) rmapshaper::apply_mapshaper_commands(geojsonsf::sf_geojson(nc), "-clean")
Check the system mapshaper
check_sys_mapshaper(command = "mapshaper-xl", verbose = TRUE)
check_sys_mapshaper(command = "mapshaper-xl", verbose = TRUE)
command |
either "mapshaper-xl" (default) or "mapshaper" |
verbose |
Print a message stating mapshaper's current version? Default |
character path to mapshaper executable if appropriate version is installed, otherwise throws an error
geo_json
FeatureCollection with null geometriesDrop features from a geo_json
FeatureCollection with null geometries
drop_null_geometries(x)
drop_null_geometries(x)
x |
a |
a geo_json
FeatureCollection with Features with null geometries
removed
Removes portions of the target layer that fall outside the clipping layer or bounding box.
ms_clip(target, clip = NULL, bbox = NULL, remove_slivers = FALSE, ...)
ms_clip(target, clip = NULL, bbox = NULL, remove_slivers = FALSE, ...)
target |
the target layer from which to remove portions. One of:
|
clip |
the clipping layer (polygon). One of:
|
bbox |
supply a bounding box instead of a clipping layer to extract from
the target layer. Supply as a numeric vector: |
remove_slivers |
Remove tiny sliver polygons created by clipping. (Default |
... |
Arguments passed on to
|
clipped target in the same class as the input target
if (rmapshaper:::v8_version() >= "6") { library(geojsonsf, quietly = TRUE) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\",\"coordinates\": [[[52.8658,-44.7219],[53.7702,-40.4873],[55.3204,-37.5579], [56.2757,-37.917],[56.184,-40.6443],[61.0835,-40.7529], [58.0202,-43.634],[61.6699,-45.0678],[62.737,-46.2841], [55.7763,-46.2637],[54.9742,-49.1184],[52.799,-45.9386], [52.0329,-49.5677],[50.1747,-52.1814],[49.0098,-52.3641], [52.7068,-45.7639],[43.2278,-47.1908],[48.4755,-45.1388], [50.327,-43.5207],[48.0804,-41.2784],[49.6307,-40.6159], [52.8658,-44.7219]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) plot(poly) clip_poly <- structure('{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [51, -40], [55, -40], [55, -45], [51, -45], [51, -40] ] ] } }', class = c("geojson", "json")) clip_poly <- geojson_sf(clip_poly) plot(clip_poly) out <- ms_clip(poly, clip_poly) plot(out, add = TRUE) }
if (rmapshaper:::v8_version() >= "6") { library(geojsonsf, quietly = TRUE) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\",\"coordinates\": [[[52.8658,-44.7219],[53.7702,-40.4873],[55.3204,-37.5579], [56.2757,-37.917],[56.184,-40.6443],[61.0835,-40.7529], [58.0202,-43.634],[61.6699,-45.0678],[62.737,-46.2841], [55.7763,-46.2637],[54.9742,-49.1184],[52.799,-45.9386], [52.0329,-49.5677],[50.1747,-52.1814],[49.0098,-52.3641], [52.7068,-45.7639],[43.2278,-47.1908],[48.4755,-45.1388], [50.327,-43.5207],[48.0804,-41.2784],[49.6307,-40.6159], [52.8658,-44.7219]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) plot(poly) clip_poly <- structure('{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [51, -40], [55, -40], [55, -45], [51, -45], [51, -40] ] ] } }', class = c("geojson", "json")) clip_poly <- geojson_sf(clip_poly) plot(clip_poly) out <- ms_clip(poly, clip_poly) plot(out, add = TRUE) }
Aggregates using specified field, or all shapes if no field is given. For point layers, replaces a group of points with their centroid.
ms_dissolve( input, field = NULL, sum_fields = NULL, copy_fields = NULL, weight = NULL, snap = TRUE, ... )
ms_dissolve( input, field = NULL, sum_fields = NULL, copy_fields = NULL, weight = NULL, snap = TRUE, ... )
input |
spatial object to dissolve. One of:
|
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 |
... |
Arguments passed on to
|
the same class as the input
library(geojsonsf) library(sf) 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("geojson", "json")) poly <- geojson_sf(poly) plot(poly) length(poly) poly # Dissolve the polygon out <- ms_dissolve(poly) plot(out) length(out) out # Dissolve and summing columns out <- ms_dissolve(poly, sum_fields = c("a", "b")) plot(out) out
library(geojsonsf) library(sf) 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("geojson", "json")) poly <- geojson_sf(poly) plot(poly) length(poly) poly # Dissolve the polygon out <- ms_dissolve(poly) plot(out) length(out) out # Dissolve and summing columns out <- ms_dissolve(poly, sum_fields = c("a", "b")) plot(out) out
Removes portions of the target layer that fall inside the erasing layer or bounding box.
ms_erase(target, erase = NULL, bbox = NULL, remove_slivers = FALSE, ...)
ms_erase(target, erase = NULL, bbox = NULL, remove_slivers = FALSE, ...)
target |
the target layer from which to remove portions. One of:
|
erase |
the erase layer (polygon). One of:
|
bbox |
supply a bounding box instead of an erasing layer to remove from
the target layer. Supply as a numeric vector: |
remove_slivers |
Remove tiny sliver polygons created by erasing. (Default |
... |
Arguments passed on to
|
erased target in the same format as the input target
if (rmapshaper:::v8_version() >= "6") { library(geojsonsf, quietly = TRUE) library(sf) points <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Point\",\"coordinates\": [52.8658,-44.7219]}},{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Point\",\"coordinates\": [53.7702,-40.4873]}},{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Point\",\"coordinates\":[55.3204,-37.5579]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[56.2757,-37.917]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[56.184,-40.6443]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[61.0835,-40.7529]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[58.0202,-43.634]}}]}", class = c("geojson", "json")) points <- geojson_sf(points) plot(points) erase_poly <- structure('{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [51, -40], [55, -40], [55, -45], [51, -45], [51, -40] ] ] } }', class = c("geojson", "json")) erase_poly <- geojson_sf(erase_poly) out <- ms_erase(points, erase_poly) plot(out, add = TRUE) }
if (rmapshaper:::v8_version() >= "6") { library(geojsonsf, quietly = TRUE) library(sf) points <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Point\",\"coordinates\": [52.8658,-44.7219]}},{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Point\",\"coordinates\": [53.7702,-40.4873]}},{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Point\",\"coordinates\":[55.3204,-37.5579]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[56.2757,-37.917]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[56.184,-40.6443]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[61.0835,-40.7529]}}, {\"type\":\"Feature\",\"properties\":{},\"geometry\": {\"type\":\"Point\",\"coordinates\":[58.0202,-43.634]}}]}", class = c("geojson", "json")) points <- geojson_sf(points) plot(points) erase_poly <- structure('{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [ [ [51, -40], [55, -40], [55, -45], [51, -45], [51, -40] ] ] } }', class = c("geojson", "json")) erase_poly <- geojson_sf(erase_poly) out <- ms_erase(points, erase_poly) plot(out, add = TRUE) }
For objects of class Spatial
(e.g., SpatialPolygonsDataFrame
),
you may find it faster to use sp::disaggregate
.
ms_explode(input, ...)
ms_explode(input, ...)
input |
One of:
|
... |
Arguments passed on to
|
There is currently no method for SpatialMultiPoints
same class as input
library(geojsonsf) library(sf) poly <- "{\"type\":\"FeatureCollection\",\"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\":{\"a\":0}}\n]}" poly <- geojson_sf(poly) plot(poly) length(poly) poly # Explode the polygon out <- ms_explode(poly) plot(out) length(out) out
library(geojsonsf) library(sf) poly <- "{\"type\":\"FeatureCollection\",\"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\":{\"a\":0}}\n]}" poly <- geojson_sf(poly) plot(poly) length(poly) poly # Explode the polygon out <- ms_explode(poly) plot(out) length(out) out
Removes all fields except those listed in the fields
parameter
ms_filter_fields(input, fields, ...)
ms_filter_fields(input, fields, ...)
input |
spatial object to filter fields on. One of:
|
fields |
character vector of fields to retain. |
... |
Arguments passed on to
|
object with only specified attributes retained, in the same class as the input
library(geojsonsf) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"properties\":{\"a\": 1, \"b\":2, \"c\": 3}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) poly # Filter (keep) fields a and b, drop c out <- ms_filter_fields(poly, c("a", "b")) out
library(geojsonsf) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\", \"properties\":{\"a\": 1, \"b\":2, \"c\": 3}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) poly # Filter (keep) fields a and b, drop c out <- ms_filter_fields(poly, c("a", "b")) out
Remove small detached polygons, keeping those with a minimum area and/or a minimum number of vertices. Optionally remove null geometries.
ms_filter_islands( input, min_area = NULL, min_vertices = NULL, drop_null_geometries = TRUE, ... )
ms_filter_islands( input, min_area = NULL, min_vertices = NULL, drop_null_geometries = TRUE, ... )
input |
spatial object to filter. One of:
|
min_area |
minimum area of polygons to retain. Area is calculated using planar geometry, except for the area of unprojected polygons, which is calculated using spherical geometry in units of square meters. |
min_vertices |
minimum number of vertices in polygons to retain. |
drop_null_geometries |
should features with empty geometries be dropped?
Default |
... |
Arguments passed on to
|
object with only specified features retained, in the same class as the input
library(geojsonsf) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}, {\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,2],[98,4],[101.5,4],[100,2]]]}}, {\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,0],[100,1],[101,1],[101,0],[100,0]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) plot(poly) out <- ms_filter_islands(poly, min_area = 12391399903) plot(out)
library(geojsonsf) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}, {\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,2],[98,4],[101.5,4],[100,2]]]}}, {\"type\":\"Feature\",\"properties\":{}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,0],[100,1],[101,1],[101,0],[100,0]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) plot(poly) out <- ms_filter_islands(poly, min_area = 12391399903) plot(out)
Create a line layer consisting of shared boundaries with no attribute data
ms_innerlines(input, ...)
ms_innerlines(input, ...)
input |
input polygons object to convert to inner lines. One of:
|
... |
Arguments passed on to
|
lines in the same class as the input layer, but without attributes
library(geojsonsf) library(sf) poly <- structure('{"type":"FeatureCollection", "features":[ {"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [102,2],[102,3],[103,3],[103,2],[102,2] ]]}} ,{"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [103,3],[104,3],[104,2],[103,2],[103,3] ]]}}, {"type":"Feature", "properties":{"foo": "b"}, "geometry":{"type":"Polygon","coordinates":[[ [102,1],[102,2],[103,2],[103,1],[102,1] ]]}}, {"type":"Feature", "properties":{"foo": "b"}, "geometry":{"type":"Polygon","coordinates":[[ [103,1],[103,2],[104,2],[104,1],[103,1] ]]}}]}', class = c("geojson", "json")) poly <- geojson_sf(poly) plot(poly) out <- ms_innerlines(poly) plot(out)
library(geojsonsf) library(sf) poly <- structure('{"type":"FeatureCollection", "features":[ {"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [102,2],[102,3],[103,3],[103,2],[102,2] ]]}} ,{"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [103,3],[104,3],[104,2],[103,2],[103,3] ]]}}, {"type":"Feature", "properties":{"foo": "b"}, "geometry":{"type":"Polygon","coordinates":[[ [102,1],[102,2],[103,2],[103,1],[102,1] ]]}}, {"type":"Feature", "properties":{"foo": "b"}, "geometry":{"type":"Polygon","coordinates":[[ [103,1],[103,2],[104,2],[104,1],[103,1] ]]}}]}', class = c("geojson", "json")) poly <- geojson_sf(poly) plot(poly) out <- ms_innerlines(poly) plot(out)
Convert polygons to topological boundaries (lines)
ms_lines(input, fields = NULL, ...)
ms_lines(input, fields = NULL, ...)
input |
input polygons object to convert to inner lines. One of:
|
fields |
character vector of field names. If left as |
... |
Arguments passed on to
|
topological boundaries as lines, in the same class as the input
library(geojsonsf) library(sf) poly <- structure('{"type":"FeatureCollection", "features":[ {"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [102,2],[102,3],[103,3],[103,2],[102,2] ]]}} ,{"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [103,3],[104,3],[104,2],[103,2],[103,3] ]]}}, {"type":"Feature", "properties":{"foo": "b"}, "geometry":{"type":"Polygon","coordinates":[[ [102.5,1],[102.5,2],[103.5,2],[103.5,1],[102.5,1] ]]}}]}', class = c("geojson", "json")) poly <- geojson_sf(poly) summary(poly) plot(poly) out <- ms_lines(poly) summary(out) plot(out)
library(geojsonsf) library(sf) poly <- structure('{"type":"FeatureCollection", "features":[ {"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [102,2],[102,3],[103,3],[103,2],[102,2] ]]}} ,{"type":"Feature", "properties":{"foo": "a"}, "geometry":{"type":"Polygon","coordinates":[[ [103,3],[104,3],[104,2],[103,2],[103,3] ]]}}, {"type":"Feature", "properties":{"foo": "b"}, "geometry":{"type":"Polygon","coordinates":[[ [102.5,1],[102.5,2],[103.5,2],[103.5,1],[102.5,1] ]]}}]}', class = c("geojson", "json")) poly <- geojson_sf(poly) summary(poly) plot(poly) out <- ms_lines(poly) summary(out) plot(out)
Can be generated from the polygons by specifying location
to be
"centroid"
or "inner"
, OR by specifying fields in the
attributes of the layer containing x
and y
coordinates.
ms_points(input, location = NULL, x = NULL, y = NULL, ...)
ms_points(input, location = NULL, x = NULL, y = NULL, ...)
input |
input polygons object to convert to points. One of:
|
location |
either |
x |
name of field containing x coordinate values. Must be |
y |
name of field containing y coordinate values. Must be |
... |
Arguments passed on to
|
points in the same class as the input.
library(geojsonsf) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\": {\"x_pos\": 1, \"y_pos\": 2}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}, {\"type\":\"Feature\",\"properties\":{\"x_pos\": 3, \"y_pos\": 4}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,2],[98,4],[101.5,4],[100,2]]]}}, {\"type\":\"Feature\",\"properties\":{\"x_pos\": 5, \"y_pos\": 6}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,0],[100,1],[101,1],[101,0],[100,0]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) summary(poly) plot(poly) # Convert to points using centroids out <- ms_points(poly, location = "centroid") summary(out) plot(out) # Can also specify locations using attributes in the data out <- ms_points(poly, x = "x_pos", y = "y_pos") summary(out) plot(out)
library(geojsonsf) library(sf) poly <- structure("{\"type\":\"FeatureCollection\", \"features\":[{\"type\":\"Feature\",\"properties\": {\"x_pos\": 1, \"y_pos\": 2}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[102,2],[102,4],[104,4],[104,2],[102,2]]]}}, {\"type\":\"Feature\",\"properties\":{\"x_pos\": 3, \"y_pos\": 4}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,2],[98,4],[101.5,4],[100,2]]]}}, {\"type\":\"Feature\",\"properties\":{\"x_pos\": 5, \"y_pos\": 6}, \"geometry\":{\"type\":\"Polygon\", \"coordinates\":[[[100,0],[100,1],[101,1],[101,0],[100,0]]]}}]}", class = c("geojson", "json")) poly <- geojson_sf(poly) summary(poly) plot(poly) # Convert to points using centroids out <- ms_points(poly, location = "centroid") summary(out) plot(out) # Can also specify locations using attributes in the data out <- ms_points(poly, x = "x_pos", y = "y_pos") summary(out) plot(out)
Uses mapshaper to simplify polygons.
ms_simplify( input, keep = 0.05, method = NULL, weighting = 0.7, keep_shapes = FALSE, no_repair = FALSE, snap = TRUE, explode = FALSE, drop_null_geometries = TRUE, snap_interval = NULL, ... )
ms_simplify( input, keep = 0.05, method = NULL, weighting = 0.7, keep_shapes = FALSE, no_repair = FALSE, snap = TRUE, explode = FALSE, drop_null_geometries = TRUE, snap_interval = NULL, ... )
input |
spatial object to simplify. One of:
|
keep |
proportion of points to retain (0-1; default 0.05) |
method |
simplification method to use: |
weighting |
Coefficient for weighting Visvalingam simplification (default is 0.7). Higher values produce smoother output. weighting=0 is equivalent to unweighted Visvalingam simplification. |
keep_shapes |
Prevent small polygon features from disappearing at high
simplification (default |
no_repair |
disable intersection repair after simplification (default
|
snap |
Snap together vertices within a small distance threshold to fix
small coordinate misalignment in adjacent polygons. Default |
explode |
Should multipart polygons be converted to singlepart polygons?
This prevents small shapes from disappearing during simplification if
|
drop_null_geometries |
should Features with null geometries be dropped?
Ignored for |
snap_interval |
Specify snapping distance in source units, must be a
numeric. Default |
... |
Arguments passed on to
|
a simplified representation of the geometry in the same class as the input
# With a simple geojson object poly <- structure('{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [[ [-70.603637, -33.399918], [-70.614624, -33.395332], [-70.639343, -33.392466], [-70.659942, -33.394759], [-70.683975, -33.404504], [-70.697021, -33.419406], [-70.701141, -33.434306], [-70.700454, -33.446339], [-70.694274, -33.458369], [-70.682601, -33.465816], [-70.668869, -33.472117], [-70.646209, -33.473835], [-70.624923, -33.472117], [-70.609817, -33.468107], [-70.595397, -33.458369], [-70.587158, -33.442901], [-70.587158, -33.426283], [-70.590591, -33.414248], [-70.594711, -33.406224], [-70.603637, -33.399918] ]] } }', class = c("geojson", "json")) ms_simplify(poly, keep = 0.1) # With an sf object poly_sf <- geojsonsf::geojson_sf(poly) ms_simplify(poly_sf, keep = 0.5)
# With a simple geojson object poly <- structure('{ "type": "Feature", "properties": {}, "geometry": { "type": "Polygon", "coordinates": [[ [-70.603637, -33.399918], [-70.614624, -33.395332], [-70.639343, -33.392466], [-70.659942, -33.394759], [-70.683975, -33.404504], [-70.697021, -33.419406], [-70.701141, -33.434306], [-70.700454, -33.446339], [-70.694274, -33.458369], [-70.682601, -33.465816], [-70.668869, -33.472117], [-70.646209, -33.473835], [-70.624923, -33.472117], [-70.609817, -33.468107], [-70.595397, -33.458369], [-70.587158, -33.442901], [-70.587158, -33.426283], [-70.590591, -33.414248], [-70.594711, -33.406224], [-70.603637, -33.399918] ]] } }', class = c("geojson", "json")) ms_simplify(poly, keep = 0.1) # With an sf object poly_sf <- geojsonsf::geojson_sf(poly) ms_simplify(poly_sf, keep = 0.5)