Apply a local projection to every feature in a longlat data set, and plot.
https://gis.stackexchange.com/questions/335570/row-wise-reprojection-of-spdf-in-r
NOTE: there's a grander problem here in that the centroid used is in longlat, so it's not really the centre in the final map - but probably good enough. There are trig methods to get an angular centre that I don't bother with here.
local_proj <- function(lonlat) {
sprintf("+proj=laea +lon_0=%f +lat_0=%f +datum=WGS84", lonlat[1], lonlat[2])
}
local_reproj <- function(x) {
cc <- sf::st_coordinates(st_centroid(x))
sf::st_transform(x, local_proj(cc))
}
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.4.2, PROJ 5.2.0
w <- rnaturalearth::ne_countries(returnclass = "sf")
## easiest way to row-wise is split
listof <- lapply(split(w, 1:nrow(w)), local_reproj)
par(mfrow = n2mfrow(length(listof)), mar = rep(0, 4))
## use walk to ignore all the side effect cruft
purrr::walk(listof, ~plot(st_geometry(.x)))
Created on 2019-09-17 by the reprex package (v0.3.0)
better version