坐标中的多边形

皇家TS

我有一个data.frame带有lats和lngs的对象,它们定义矩形框的边界,像这样

  geohash north_lat south_lat  east_lng  west_lng
1   gbsuv  48.69141  48.64746 -4.306641 -4.350586
2   gbsuy  48.69141  48.64746 -4.262695 -4.306641

将其转换为sf包含POLYGONs列的对象的最简单方法是什么?

SymbolixAU

创建多边形的关键是必须按顺序形成一个封闭区域(即,最后一个点与第一个点相同)。

因此,您的数据将需要一些操作来创建坐标,并将其排序。在我的示例中,我使用了lapply

然后可以从SF示例中获取其余信息

lst <- lapply(1:nrow(df), function(x){
  ## create a matrix of coordinates that also 'close' the polygon
  res <- matrix(c(df[x, 'north_lat'], df[x, 'west_lng'],
           df[x, 'north_lat'], df[x, 'east_lng'],
           df[x, 'south_lat'], df[x, 'east_lng'],
           df[x, 'south_lat'], df[x, 'west_lng'],
           df[x, 'north_lat'], df[x, 'west_lng'])  ## need to close the polygon
         , ncol =2, byrow = T
  )
  ## create polygon objects
  st_polygon(list(res))

})

## st_sfc : creates simple features collection
## st_sf : creates simple feature object
sfdf <- st_sf(geohash = df[, 'geohash'], st_sfc(lst))

sfdf
# Simple feature collection with 2 features and 1 field
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 48.64746 ymin: -4.350586 xmax: 48.69141 ymax: -4.262695
# epsg (SRID):    NA
# proj4string:    NA
# geohash                    st_sfc.lst.
# 1   gbsuv POLYGON((48.69141 -4.350586...
# 2   gbsuy POLYGON((48.69141 -4.306641...

plot(sfdf)

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章