有没有办法绘制具有指定点顺序的多边形? (我现在在 R 中使用 sf 包)
Is there a way to draw a polygon with specified point order ? (I now use the sf package in R)
我运行以下环境下的代码:
R: 4.1.0
包:
旧金山:1.0.0
dplyr : 1.0.7
看过之后
https://gis.stackexchange.com/questions/280671/r-create-multipolygon-from-overlapping-polygons-using-sf-package ,我尝试使用 sf 包,但 运行 遇到了一些问题。
我遇到的问题是以下代码:
library(sf)
library(dplyr)
poly <- data.frame(
lon = c(0, 1, 1, 0, 0.5),
lat = c(0, 0, 1, 1, 0.5),
var = c(1, 1, 1, 1, 1)
) %>%
st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326) %>%
group_by(var) %>%
summarise() %>% st_cast("POLYGON")
plot(poly)
代码的输出图是the following
,我认为它会按照原始数据框的顺序创建多边形。即 (0,0) -> (1,0) -> (1,1) -> (0,1) -> (0.5,0.5) -> (0,0),它应该看起来像 this picture.
我想知道是否有办法指定点连接的顺序?我在这个例子中使用了 set_convex_hull() 但输出只是一个正方形,这是有道理的,因为凸包的定义。我浏览了 sf 文档网站 https://r-spatial.github.io/sf/articles/sf1.html 但仍然找不到解决方案。我觉得我在研究这个包的使用时一定错过了一些关键词或方向。如果有人有任何建议,将不胜感激。
我一直觉得从 data.frames
构建多边形很棘手,所以我编写了 sfheaders
库来简化此过程
library(sf)
library(sfheaders)
poly <- data.frame(
lon = c(0, 1, 1, 0, 0.5),
lat = c(0, 0, 1, 1, 0.5),
var = c(1, 1, 1, 1, 1)
)
sf <- sfheaders::sf_polygon(
obj = poly
, x = "lon"
, y = "lat"
, keep = T ## To keep the 'var' variable
)
sf
# Simple feature collection with 1 feature and 1 field
# Geometry type: POLYGON
# Dimension: XY
# Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1
# CRS: NA
# var geometry
# 1 1 POLYGON ((0 0, 1 0, 1 1, 0 ...
这使用并保持输入中坐标的顺序 data.frame。
plot( sf )
如果您想进行任何计算/几何运算,您需要在此对象上设置 CRS
sf::st_crs(sf) <- 4326
我运行以下环境下的代码:
R: 4.1.0
包: 旧金山:1.0.0 dplyr : 1.0.7
看过之后 https://gis.stackexchange.com/questions/280671/r-create-multipolygon-from-overlapping-polygons-using-sf-package ,我尝试使用 sf 包,但 运行 遇到了一些问题。
我遇到的问题是以下代码:
library(sf)
library(dplyr)
poly <- data.frame(
lon = c(0, 1, 1, 0, 0.5),
lat = c(0, 0, 1, 1, 0.5),
var = c(1, 1, 1, 1, 1)
) %>%
st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326) %>%
group_by(var) %>%
summarise() %>% st_cast("POLYGON")
plot(poly)
代码的输出图是the following
,我认为它会按照原始数据框的顺序创建多边形。即 (0,0) -> (1,0) -> (1,1) -> (0,1) -> (0.5,0.5) -> (0,0),它应该看起来像 this picture.
我想知道是否有办法指定点连接的顺序?我在这个例子中使用了 set_convex_hull() 但输出只是一个正方形,这是有道理的,因为凸包的定义。我浏览了 sf 文档网站 https://r-spatial.github.io/sf/articles/sf1.html 但仍然找不到解决方案。我觉得我在研究这个包的使用时一定错过了一些关键词或方向。如果有人有任何建议,将不胜感激。
我一直觉得从 data.frames
构建多边形很棘手,所以我编写了 sfheaders
库来简化此过程
library(sf)
library(sfheaders)
poly <- data.frame(
lon = c(0, 1, 1, 0, 0.5),
lat = c(0, 0, 1, 1, 0.5),
var = c(1, 1, 1, 1, 1)
)
sf <- sfheaders::sf_polygon(
obj = poly
, x = "lon"
, y = "lat"
, keep = T ## To keep the 'var' variable
)
sf
# Simple feature collection with 1 feature and 1 field
# Geometry type: POLYGON
# Dimension: XY
# Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1
# CRS: NA
# var geometry
# 1 1 POLYGON ((0 0, 1 0, 1 1, 0 ...
这使用并保持输入中坐标的顺序 data.frame。
plot( sf )
如果您想进行任何计算/几何运算,您需要在此对象上设置 CRS
sf::st_crs(sf) <- 4326