Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用sf::st_cast时,所有数据都会丢失_R_Sf - Fatal编程技术网

R 使用sf::st_cast时,所有数据都会丢失

R 使用sf::st_cast时,所有数据都会丢失,r,sf,R,Sf,在下面的代码中,我首先从两条水平线和两条垂直线创建一个矩形。然后我使用st_polygonize创建一个多边形。最后,我使用st_cast创建一个类的对象 [1] XY“多边形”sfg 这正是我所需要的。然而,当我尝试用波浪做类似的事情时,它失败了。我通过复制波浪线、向上移动并添加边来创建波浪多边形 wave\u gc绘图正常,但执行时: wave\u poly你写的wave\u poly你是对的。谢谢。你已经写了wave\u poly,你是对的。多谢各位。 library(sf) librar

在下面的代码中,我首先从两条水平线和两条垂直线创建一个矩形。然后我使用
st_polygonize
创建一个多边形。最后,我使用
st_cast
创建一个类的对象

[1] XY“多边形”sfg

这正是我所需要的。然而,当我尝试用波浪做类似的事情时,它失败了。我通过复制波浪线、向上移动并添加边来创建波浪多边形

wave\u gc
绘图正常,但执行时:


wave\u poly你写的
wave\u poly你是对的。谢谢。你已经写了
wave\u poly,你是对的。多谢各位。
library(sf)
library(transformr)

# Rectangle #########################
# two vertical lines
v_lines <- st_multilinestring(list(rbind(c(0,0),c(0,1)), rbind(c(3,0),c(3,1))))

# two horizontal lines
h_lines <- st_multilinestring(list(rbind(c(0,0),c(3,0)), rbind(c(0,1),c(3,1))))
all_lines <- c(v_lines, h_lines)

rect_gc  <-  st_polygonize(all_lines)
rect_poly <- st_cast(rect_gc, "POLYGON")

class(rect_gc)
# [1] "XY"                 "GEOMETRYCOLLECTION" "sfg"  
class(rect_poly)
# [1] "XY"      "POLYGON" "sfg"
rect_poly

#################################################################
# Wave
################################################################

w1 <- path_waves(st = TRUE)[[1]]
w2 <- w1 

# Add to second column, y values
w2[,2] <- w2[,2] + 0.1

# Vertical bars
nr <- nrow(w1)
v_bars <- st_multilinestring(list(rbind(c(w1[1, 1], w1[1, 2]), c(w2[1, 1], w2[1, 2])), 
                                  rbind(c(w1[nr, 1], w1[nr, 2]), c(w2[nr, 1], w2[nr, 2]))))
two_waves <- st_multilinestring(list(w1, w2))
full_wave <- c(v_bars, two_waves)

wave_gc = st_polygonize(full_wave)
wave_poly <- class(st_cast(wave_gc, "POLYGON"))

class(wave_gc)
# [1] "XY"                 "GEOMETRYCOLLECTION" "sfg"   

plot(wave_gc)

wave_poly
# [1] "XY"      "POLYGON" "sfg"