sf::st_transform()转换正射投影返回空几何体问题咨询
正射投影转换后几何体为空的问题
我按照某流程对rnaturalearth国家数据集进行转换,用于正射投影绘图,但使用st_transform()转换后,俄罗斯大陆的几何体变为空值。
读取并转换问题多边形的代码如下:
polygon <- st_as_sf(data.frame(st_as_sfc(readLines("https://pastebin.com/raw/APH15G6X"), crs = 4326))) st_transform(polygon, "+proj=ortho +lat_0=20 +lon_0=0 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs")
执行后得到的输出:
Simple feature collection with 1 feature and 0 fields (with 1 geometry empty) Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: NA ymin: NA xmax: NA ymax: NA CRS: +proj=ortho +lat_0=20 +lon_0=0 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs geometry 1 MULTIPOLYGON EMPTY
该几何体本身有效,且预期在正射投影后完全可见,无法确定问题原因。
问题原因与解决方法
原因分析
正射投影属于透视投影,仅能展示半球范围内的几何体。俄罗斯大陆跨度过大,部分区域位于投影半球之外,st_transform()默认会直接丢弃包含不可见区域的复杂几何体(如MULTIPOLYGON),导致整个几何体变为空值。
解决步骤
- 拆分复杂几何体
将MULTIPOLYGON拆分为单个POLYGON,这样可以对每个独立多边形单独处理,保留可见部分:
library(sf) # 读取目标几何体 polygon <- st_as_sf(data.frame(st_as_sfc(readLines("https://pastebin.com/raw/APH15G6X"), crs = 4326))) # 拆分MULTIPOLYGON为多个POLYGON polygon_split <- st_cast(polygon, "POLYGON")
- 转换投影并过滤空值
对拆分后的每个多边形执行投影转换,然后过滤掉转换后为空的部分:
# 定义正射投影参数 ortho_crs <- "+proj=ortho +lat_0=20 +lon_0=0 +x_0=0 +y_0=0 +a=6371000 +b=6371000 +units=m +no_defs" # 转换投影并移除空几何体 polygon_ortho <- st_transform(polygon_split, ortho_crs) %>% dplyr::filter(!sf::st_is_empty(.))
- 合并可见多边形(可选)
如果需要将可见部分重新合并为单个MULTIPOLYGON,可使用以下代码:
polygon_ortho_combined <- sf::st_combine(polygon_ortho) %>% sf::st_sf()
通过以上步骤,即可得到正射投影下可见的俄罗斯大陆几何体,避免出现整体为空的情况。
内容的提问来源于stack exchange,提问作者user3170702
相关产品推荐
相关产品推荐

