Open misea opened 2 days ago
This is well known: you expect too much from the software, at least at the moment. What happens is that st_transform
assigns NA values to coordinates of polygons not "visible" on the orthographic projection, and the plotting render mechanism simply drops such polygons or raises an error. I don't see how sf::plot()
or st_transform()
could be modified to make this work (but am open to suggestions). A single function could do this, and is available e.g. in pkg https://github.com/paleolimbot/s2plot. A script (modified) from here that could form the basis for such a function is:
library(sf)
old <- options(s2_oriented = TRUE) # don't change orientation from here on
countries <- s2::s2_data_countries() |> st_as_sfc()
globe <- st_as_sfc("POLYGON FULL", crs = st_crs(countries))
oceans <- st_difference(globe, st_union(countries))
visible <- st_buffer(st_as_sfc("POINT(-30 -10)", crs = st_crs(countries)), 9800000) # visible half
visible_ocean <- st_intersection(visible, oceans)
visible_countries <- st_intersection(visible, countries)
st_transform(visible_ocean, "+proj=ortho +lat_0=-10 +lon_0=-30") |>
plot(col = 'lightblue')
st_transform(visible_countries, "+proj=ortho +lat_0=-10 +lon_0=-30") |>
plot(col = NA, add = TRUE)
options(old)
Some further discussion in the context of map plotting and tmap
: https://github.com/r-tmap/tmap/issues/457
I compared this example using terra
and it basically works (probably doesn't draw that one missing geometry?).
library(terra)
f = "https://naciscdn.org/naturalearth/50m/cultural/ne_50m_admin_0_countries.zip"
v = vect(paste0("/vsizip/vsicurl/", f), what = "geoms")
v = project(v, "+proj=ortho +lat_0=0 +lon_0=27.0")
#> 1: Point outside of projection domain (GDAL error 1)
#> ...
all(is.valid(v))
#> [1] TRUE # it seems that all geometries are fixed after transformation
plot(v)
Plotting fails when drawing globe at very specific rotation.
To Reproduce
Created on 2024-11-11 with reprex v2.1.1
Additional context First saw and reported as occurring via coord_sf https://github.com/tidyverse/ggplot2/issues/6180 , but problem is lower down the call chain