使用R中的sf将经度和纬度序列转换为多边形

18

我有五个经纬度,它们形成了这样的一个形状。

df <- c(order=1:5,
        lon=c(119.4,119.4,119.4,119.5,119.5), 
        lat=c(-5.192,-5.192,-5.187,-5.187,-5.191))

我怎样才能使用sf包将它们轻松地转换为sf多边形数据框架呢?

## Simple feature collection with 1 feature and 0 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## geometry
## 1 POLYGON ((119.4 ...
4个回答

21

我发现这个问题在搜索结果中出现,所以我想提供一种更灵活的方法,从一系列latlon坐标创建多边形的sf

st_as_sf有一个参数coords,它将以坐标列给出的点作为输入,并将这些列转换为sf POINT几何。然后,因为sfdplyr配合得很好,我们可以使用st_combine将点组合成MULTIPOINT并使用st_cast将其转换为POLYGON。与使用st_polygon手动构建相比,这具有优势,我们无需仔细考虑封闭环或传递给构造函数的正确嵌套列表级别,并且如果在一组坐标中有多个多边形,则可以使用group_by一次性创建所有多边形。

N.B. 严格来说,您可以在summarise中使用do_union=FALSE来做到这一点,但我认为这种语法更清晰,更类似于正常的summarise

df <- data.frame(
  lon = c(119.4, 119.4, 119.4, 119.5, 119.5),
  lat = c(-5.192, -5.192, -5.187, -5.187, -5.191)
)
library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
polygon <- df %>%
  st_as_sf(coords = c("lon", "lat"), crs = 4326) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON")
polygon
#> Simple feature collection with 1 feature and 0 fields
#> geometry type:  POLYGON
#> dimension:      XY
#> bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#>                         geometry
#> 1 POLYGON ((119.4 -5.192, 119...

plot(polygon)

该内容由 reprex软件包 (v0.2.0) 在2018年10月5日创建。


当我提供经纬度列表时,我似乎无法使用 group_by 进行分组。例如,对于两个多边形,我提供了以下数据框:df <- tibble(AC_NO = c(100, 101), lon = list(c(76.4, 76.38, 76.4, 76.38), c(76.4, 76.38, 76.4, 76.38)), lat = list(c(9.58, 9.58, 9.56, 9.56), c(9.55, 9.55, 9.53, 9.53)))然后:df %>% group_by(AC_NO) %>% st_as_sf( 等等。我收到一个错误,指出 "list" 无法强制转换为类型 "double"。有什么想法我做错了什么吗? - Fons MA
看起来你有列表列 - 展开可能会解决它。我意识到我上面不准确地使用了“列表”一词,现在已经更改了。 - Calum You

14

与@Yo B.的答案等效,但使用sf

library(sf)
df <- data.frame(lon=c(119.4,119.4,119.4,119.5,119.5), 
                 lat=c(-5.192,-5.192,-5.187,-5.187,-5.191))

# You need first to close your polygon 
# (first and last points must be identical)
df <- rbind(df, df[1,])

poly <- st_sf(st_sfc(st_polygon(list(as.matrix(df)))), crs = 4326)
poly

## Simple feature collection with 1 feature and 0 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   st_sfc.st_polygon.list.as.matrix.df....
## 1          POLYGON ((119.4 -5.192, 119...

编辑以回答评论中的问题

请参阅主要sf示例,了解对sfsfcsfg对象的清晰详细的解释,总结如下:

用于表示简单要素的三种类别分别是:

  • sf:包含要素属性和要素几何的表格(data.frame)。
  • sfc:用于每个要素(记录)的几何图形的列表列,由以下内容组成:
  • sfg:单个简单要素的要素几何。

st_sfc函数仅构建几何列(这是多边形的列表 - 这里仅有一个多边形)。在sfc中,"c"代表"column"。函数st_sf构建完整的sf对象(还具有data.frame类),这是一个带有几何列的数据框。在给定的示例中,未附加多边形的数据(无属性)。您可以通过构建数据框来附加数据:

poly <- st_sf(data.frame(landuse = "Forest", 
                         size = 23 , 
                         st_sfc(st_polygon(list(as.matrix(df))))), 
              crs = 4326)
poly
## ## Simple feature collection with 1 feature and 2 fields
## geometry type:  POLYGON
## dimension:      XYZ
## bbox:           xmin: 1 ymin: 119.4 xmax: 5 ymax: 119.5
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## landuse size                       geometry
## 1  Forest   23 POLYGON Z ((1 119.4 -5.192,...

您可以从空间对象中提取每个元素,并检查它们的类:

完整的 sf 对象:一个带有 sfc 几何列的数据框

class(poly)
## "sf"         "data.frame"

第三列被提取为一个列表:sfc对象

class(poly[[3]])
## "sfc_POLYGON" "sfc"    

几何列的第一个元素:一个sfg多边形对象

class(poly[[3]][[1]])
## "XY"      "POLYGON" "sfg"  

我能够在没有 st_sf() 的情况下完成相同的事情。例如: poly <- st_sfc(st_polygon(list(as.matrix(df))), crs = 4326) 有人能解释一下其中的区别吗?(我不会) - D. Woods

3

library(sfheaders) 于20191004在CRAN上发布,可以将数据框转换为sf对象。

library(sf)
library(sfheaders)

df <- data.frame(
  lon = c(119.4, 119.4, 119.4, 119.5, 119.5),
  lat = c(-5.192, -5.192, -5.187, -5.187, -5.191)
)

sfheaders::sf_polygon(
  obj = df
)
## given only two columns of data are in df there's no need to specify lon & lat arguments

# Simple feature collection with 1 feature and 1 field
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 119.4 ymin: -5.192 xmax: 119.5 ymax: -5.187
# epsg (SRID):    NA
# proj4string:    
#   id                       geometry
# 1  1 POLYGON ((119.4 -5.192, 119...

如何将这个列字符串转换为多边形? "list(c(-72.6689780388482, -72.6697951984133, -72.670538307013, -72.6696918495337, -72.6689780388482, -37.8155440677891, -37.8164165487739, -37.815944377963, -37.8151004670485, -37.8155440677891))" - Cristián Vargas Acevedo

0

我不知道 "sf" 是什么意思,但是如果你的意思是 "sp",这里是 SPDF 的完整构造。

df <- data.frame(lon=c(119.4,119.4,119.4,119.5,119.5), 
             lat=c(-5.192,-5.192,-5.187,-5.187,-5.191))

require(sp)
spdf <- SpatialPolygonsDataFrame(
  SpatialPolygons(
    Srl=list(
      Polygons(srl=list(
        Polygon(coords=df)
      ), ID=1)
    )
  ),
  data=data.frame(a=1)
)

plot(spdf)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接