简单特性 – 创建具有特定属性的新点

huangapple go评论53阅读模式
英文:

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)
#&gt; 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 &lt;- st_sfc(st_point(c(0, 51.5))) %&gt;% st_set_crs(4326) # a point around London

buffer &lt;- st_buffer(point, dist = 100) %&gt;% 
  st_cast(&#39;LINESTRING&#39;)

six_points &lt;- st_sample(buffer, size = 6, type = &#39;regular&#39;)
#&gt; although coordinates are longitude/latitude, st_sample assumes that they are
#&gt; planar

#distances:
six_points %&gt;% st_as_sf() %&gt;% st_cast(&#39;POINT&#39;) %&gt;% st_distance(point)
#&gt; Units: [m]
#&gt;          [,1]
#&gt; [1,] 100.5366
#&gt; [2,] 100.2739
#&gt; [3,] 100.7187
#&gt; [4,] 100.5599
#&gt; [5,] 100.2410
#&gt; [6,] 100.6761

Pretty close. Choosing a better crs would help if you need cm accuracy.


ggplot() +
  geom_sf(data = point, col = &#39;red&#39;) + 
  geom_sf(data = buffer) +
  geom_sf(data = six_points, color = &#39;turquoise&#39;, 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>

huangapple
  • 本文由 发表于 2023年3月31日 18:43:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定