Remove small detached polygons (islands)

Share:

Description

Remove small detached polygons, keeping those with a minimum area and/or a minimum number of vertices. Optionally remove null geomtries.

Usage

1
2
ms_filter_islands(input, min_area = NULL, min_vertices = NULL,
  drop_null_geometries = TRUE, force_FC = TRUE)

Arguments

input

spatial object to filter. One of:

  • geo_json or character polygons;

  • geo_list polygons;

  • SpatialPolygons*

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 TRUE. Ignored for SpatialPolyons*, as it is always 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.

Value

object with only specified features retained, in the same class as the input

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
library(geojsonio)
library(sp)

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("json", "geo_json"))

poly <- geojson_sp(poly)
plot(poly)

out <- ms_filter_islands(poly, min_area = 12391399903)
plot(out)