英文:
simple feature - create new points with certain properties
问题
我有一个simple features
,其中包含一个简单的点,我想在该点周围创建6个均匀分布且距离原始点100米的新点。在R(sf)中是否容易解决这个问题,你是否有可能提供一个类似这样的实现示例?
英文:
I've a simple features
with a simple point, arround this point I like to create 6 new points equal distributed and 100 meter away from the orginal point. Is this easily solvable in R (sf) and do you have perhaps an example how I could implement something like this?
答案1
得分: 1
开始时将点缓冲100米,将其转换为线串,然后对缓冲的线串进行采样:
library(sf)
#> 链接到 GEOS 3.8.0、GDAL 3.0.4、PROJ 6.3.1;sf_use_s2() 为 TRUE
library(tidyverse)
set.seed(41)
point <- st_sfc(st_point(c(0, 51.5))) %>% st_set_crs(4326) # 伦敦附近的一个点
buffer <- st_buffer(point, dist = 100) %>%
st_cast('LINESTRING')
six_points <- st_sample(buffer, size = 6, type = 'regular')
#> 尽管坐标是经度/纬度,st_sample 假设它们是平面的
#距离:
six_points %>% st_as_sf() %>% st_cast('POINT') %>% st_distance(point)
#> 单位:[m]
#> [,1]
#> [1,] 100.5366
#> [2,] 100.2739
#> [3,] 100.7187
#> [4,] 100.5599
#> [5,] 100.2410
#> [6,] 100.6761
差不多了。如果需要厘米级精度,选择更好的坐标系会有帮助。
ggplot() +
geom_sf(data = point, col = 'red') +
geom_sf(data = buffer) +
geom_sf(data = six_points, color = 'turquoise', size = 4) +
theme_void()
在地图上:
library(mapview)
mapview(point) +
buffer +
six_points
<sup>由 reprex 包 (v2.0.1) 于 2023-03-31 创建</sup>
英文:
Start by buffering the point by 100 meters, cast it to a linestring, and then sample the buffered linestring:
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tidyverse)
set.seed(41)
point <- st_sfc(st_point(c(0, 51.5))) %>% st_set_crs(4326) # a point around London
buffer <- st_buffer(point, dist = 100) %>%
st_cast('LINESTRING')
six_points <- st_sample(buffer, size = 6, type = 'regular')
#> although coordinates are longitude/latitude, st_sample assumes that they are
#> planar
#distances:
six_points %>% st_as_sf() %>% st_cast('POINT') %>% st_distance(point)
#> Units: [m]
#> [,1]
#> [1,] 100.5366
#> [2,] 100.2739
#> [3,] 100.7187
#> [4,] 100.5599
#> [5,] 100.2410
#> [6,] 100.6761
Pretty close. Choosing a better crs would help if you need cm accuracy.
ggplot() +
geom_sf(data = point, col = 'red') +
geom_sf(data = buffer) +
geom_sf(data = six_points, color = 'turquoise', size = 4) +
theme_void()
On a map:
library(mapview)
mapview(point) +
buffer +
six_points
<sup>Created on 2023-03-31 by the reprex package (v2.0.1)</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论