我有五个经度和纬度形成这样的形状.
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))
Run Code Online (Sandbox Code Playgroud)
如何使用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 ...
Run Code Online (Sandbox Code Playgroud)
Gil*_*les 10
等同于@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...
Run Code Online (Sandbox Code Playgroud)
编辑以回答评论中的问题
请参阅主要的sf插图以获得清晰详细的说明sf,sfc并将sfg对象概括为:
用于表示简单功能的三个类是:
- sf,包含要素属性和要素几何的表(data.frame)
- sfc,list-column,包含每个要素(记录)的几何形状
- sfg,单个简单要素的要素几何.
该st_sfc函数仅构建几何列(这是一个多边形列表 - 此处只有一个多边形)."c" sfc代表"列".该函数st_sf构建一个完整的sf对象(也有一个data.frame类),该对象是一个带有几何列的数据框.在给定的示例中,没有数据附加到多边形(没有属性).您可以通过构建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,...
Run Code Online (Sandbox Code Playgroud)
然后,您可以从空间对象中提取每个元素并检查它们的类:
完整的sf对象:带有sfc几何列的data.frame
class(poly)
## "sf" "data.frame"
Run Code Online (Sandbox Code Playgroud)
第三列作为列表提取:sfc对象
class(poly[[3]])
## "sfc_POLYGON" "sfc"
Run Code Online (Sandbox Code Playgroud)
几何列的第一个元素:sfg多边形对象
class(poly[[3]][[1]])
## "XY" "POLYGON" "sfg"
Run Code Online (Sandbox Code Playgroud)
我看到搜索结果中出现了这个问题,所以我想我会提供一种更灵活的方法,用于sf根据一系列lat和lon坐标创建多边形。
st_as_sf有一个参数coords,它将参数作为数据框中的坐标列给定的点并将这些列转换为sf POINT几何。然后,由于sf与之配合良好dplyr,我们可以st_combine将点转换为MULTIPOINT并将st_cast其转换为POLYGON。与使用的“手动”构造相比st_polygon,它的优点是我们不必仔细考虑关闭环或将嵌套列表的正确级别传递给构造函数,并且如果在其中有多个多边形我们可以用来group_by一次创建所有多边形的坐标列表。
注意:从技术上讲,您可以do_union=FALSE在内部执行此操作summarise,但我认为此语法更加清晰,与常规语法更相似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)
Run Code Online (Sandbox Code Playgroud)

由reprex软件包(v0.2.0)于2018-10-05创建。
| 归档时间: |
|
| 查看次数: |
2601 次 |
| 最近记录: |