R中将点转换为网格

发布于 2025-01-16 18:39:02 字数 241 浏览 4 评论 0原文

我有一个纬度和经度,

locs <- structure(list(Latitude = 0.176094639, Longitude = 117.4955225), row.names = 1L, class = "data.frame")

我想将其转换为尺寸为 5 公里 X 5 公里的方形多边形网格并保存形状文件。我似乎无法在 R 中找到一个函数来执行此操作,想知道是否有人做过 类似的工作?

I have a latitude and longitude

locs <- structure(list(Latitude = 0.176094639, Longitude = 117.4955225), row.names = 1L, class = "data.frame")

I want to convert this into a square polygon grid with dimension of 5 km X 5 km and save the shapefile. I can't seem to find a function in R to do this and wondering if anyone has done
similar work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2025-01-23 18:39:03

假设您提供的点是您想要的网格的中心,

  • 将该点设为 sf 对象
  • 将其缓冲 2.5km
  • 获取缓冲区的边界框
  • 制作网格(下面使用 10x10)
  • 写入 shapefile(注释掉)
library(sf)
library(tidyverse) # pipe & plotting

locs <- structure(list(Latitude = 0.176094639, Longitude = 117.4955225), row.names = 1L, class = "data.frame")

# Make the point an sf object
locs_sf <- locs %>%
  st_as_sf(coords = c('Longitude', 'Latitude')) %>% 
  st_set_crs(4326)

# get the bounding box of a 2500m buffered circle around the point
box <- st_buffer(locs_sf, 2500) %>%
  st_bbox() %>%
  st_as_sfc()

# make a 10x10 grid of the bounding box
grid <- st_make_grid(box, n = c(10,10))

# Use st_write() to write your shapefile
#st_write(grid, '/path/to/file.shp')

ggplot() +
  geom_sf(data = locs_sf, color = 'red', size = 5) + 
  geom_sf(data = grid, fill = NA, color = 'black')

reprex 包 (v2.0.1)

Assuming that the point you provided is the center of the grid you want,

  • make the point an sf object
  • buffer it by 2.5km
  • get the bounding box of the buffer
  • make a grid (10x10 used below)
  • write the shapefile(commented out)
library(sf)
library(tidyverse) # pipe & plotting

locs <- structure(list(Latitude = 0.176094639, Longitude = 117.4955225), row.names = 1L, class = "data.frame")

# Make the point an sf object
locs_sf <- locs %>%
  st_as_sf(coords = c('Longitude', 'Latitude')) %>% 
  st_set_crs(4326)

# get the bounding box of a 2500m buffered circle around the point
box <- st_buffer(locs_sf, 2500) %>%
  st_bbox() %>%
  st_as_sfc()

# make a 10x10 grid of the bounding box
grid <- st_make_grid(box, n = c(10,10))

# Use st_write() to write your shapefile
#st_write(grid, '/path/to/file.shp')

ggplot() +
  geom_sf(data = locs_sf, color = 'red', size = 5) + 
  geom_sf(data = grid, fill = NA, color = 'black')

Created on 2022-03-24 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文