通过R中的sf将经纬度序列转换为多边形
Convert sequence of longitude and latitude to polygon via sf in R
我有五个经纬度组成这样的形状
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 ...
我不知道 "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)
相当于@Yo B. answer 但 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
、sfc
和 sfg
对象的清晰详细说明,请参阅 main sf vignette,总结为:
The three classes used to represent simple features are:
- sf, the table (data.frame) with feature attributes and feature geometries, which contains
- sfc, the list-column with the geometries for each feature (record), which is composed of
- sfg, the feature geometry of an individual simple feature.
st_sfc
函数仅构建几何列(这是一个多边形列表 - 这里只有一个多边形)。 sfc
中的“c”代表“列”。函数 st_sf
构建一个完整的 sf
对象(它还有一个 data.frame
class),它是一个带有几何列的数据框。在给定的示例中,没有数据附加到多边形(没有属性)。您可以通过构建 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,...
然后您可以从空间对象中提取每个元素并检查它们 class :
完整的 sf 对象:data.frame 带有 sfc 几何列
class(poly)
## "sf" "data.frame"
提取为列表的第三列:sfc 对象
class(poly[[3]])
## "sfc_POLYGON" "sfc"
几何列的第一个元素:sfg 多边形对象
class(poly[[3]][[1]])
## "XY" "POLYGON" "sfg"
我看到这个问题出现在搜索结果中,所以我想我会提供一种更灵活的方法来在 sf
中从一系列 lat
和 [=13] 中创建多边形=] 坐标.
st_as_sf
有一个参数 coords
,它将把给定的点作为数据框中的坐标列,并将这些列转换为 sf
POINT
几何图形。然后,因为 sf
与 dplyr
配合得很好,我们可以将 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 package (v0.2.0) 创建于 2018-10-05。
library(sfheaders)
在 20191004 的 CRAN 上可以采用 data.frame 并将其转换为 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...
我有五个经纬度组成这样的形状
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 ...
我不知道 "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)
相当于@Yo B. answer 但 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
、sfc
和 sfg
对象的清晰详细说明,请参阅 main sf vignette,总结为:
The three classes used to represent simple features are:
- sf, the table (data.frame) with feature attributes and feature geometries, which contains
- sfc, the list-column with the geometries for each feature (record), which is composed of
- sfg, the feature geometry of an individual simple feature.
st_sfc
函数仅构建几何列(这是一个多边形列表 - 这里只有一个多边形)。 sfc
中的“c”代表“列”。函数 st_sf
构建一个完整的 sf
对象(它还有一个 data.frame
class),它是一个带有几何列的数据框。在给定的示例中,没有数据附加到多边形(没有属性)。您可以通过构建 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,...
然后您可以从空间对象中提取每个元素并检查它们 class :
完整的 sf 对象:data.frame 带有 sfc 几何列
class(poly)
## "sf" "data.frame"
提取为列表的第三列:sfc 对象
class(poly[[3]])
## "sfc_POLYGON" "sfc"
几何列的第一个元素:sfg 多边形对象
class(poly[[3]][[1]])
## "XY" "POLYGON" "sfg"
我看到这个问题出现在搜索结果中,所以我想我会提供一种更灵活的方法来在 sf
中从一系列 lat
和 [=13] 中创建多边形=] 坐标.
st_as_sf
有一个参数 coords
,它将把给定的点作为数据框中的坐标列,并将这些列转换为 sf
POINT
几何图形。然后,因为 sf
与 dplyr
配合得很好,我们可以将 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 package (v0.2.0) 创建于 2018-10-05。
library(sfheaders)
在 20191004 的 CRAN 上可以采用 data.frame 并将其转换为 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...