Learn R Programming

rmapshaper (version 0.4.4)

ms_points: Create points from a polygon layer

Description

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.

Usage

ms_points(
  input,
  location = NULL,
  x = NULL,
  y = NULL,
  force_FC = TRUE,
  sys = FALSE
)

Arguments

input

input polygons object to convert to points. One of:

  • geo_json or character polygons;

  • geo_list polygons;

  • SpatialPolygons*;

  • sf or sfc polygons object

location

either "centroid" or "inner". If "centroid", creates points at the centroid of the largest ring of each polygon feature. if "inner", creates points in the interior of the largest ring of each polygon feature. Inner points are located away from polygon boundaries. Must be NULL if x and y are specified. If left as NULL (default), will use centroids.

x

name of field containing x coordinate values. Must be NULL if location is specified.

y

name of field containing y coordinate values. Must be NULL if location is specified.

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 a SpatialPoints* is always the output.

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

points in the same class as the input.

Examples

Run this code
# NOT RUN {
library(geojsonio)
library(sp)

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

poly <- geojson_sp(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)

# }

Run the code above in your browser using DataLab