【问题标题】:Ploting a Buffer Around a Point on a Map - R SF在地图上的点周围绘制缓冲区 - R SF
【发布时间】:2018-04-30 14:39:33
【问题描述】:

我一直试图在地图上的某个点周围绘制一个缓冲区,但是当我这样做时,缓冲区并没有出现在这样的正确位置。

Faulty R Map

正确的位置在加利福尼亚。

这是我的代码:

library(tigris)
library(sf)
library(tidyverse)

projection <- 102003

options(tigris_use_cache =  TRUE)
county_polys <- counties(class = 'sf') %>%
  filter(STATEFP %in% c('06','41','53','04','16','32','49')) %>%
  st_transform(projection)

  centroids <- county_polys %>%
  as_tibble %>% select(INTPTLON,INTPTLAT) %>%
  mutate(
    INTPTLON = as.double(INTPTLON),
    INTPTLAT = as.double(INTPTLAT)) %>%
  st_as_sf(coords = c('INTPTLON','INTPTLAT'), crs = projection)

 pt <- centroids[2,] 
 pt_buffer <- st_buffer(pt,150000) 


 ggplot() + geom_sf(data = county_polys) + geom_sf(data = pt_buffer,color = 'red')

【问题讨论】:

    标签: r sf


    【解决方案1】:

    我们可以使用st_centroid 函数来获取质心以避免错误。无需将sf 对象转换为其他类。

    # This is the only thing I changed from your original code
    # Get the centroid by st_centroid
    centroids <- county_polys %>% st_centroid()
    
    pt <- centroids[2,] 
    pt_buffer <- st_buffer(pt,150000) 
    
    ggplot() + geom_sf(data = county_polys) + geom_sf(data = pt_buffer,color = 'red')
    

    【讨论】:

      猜你喜欢
      • 2021-11-21
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 2014-10-27
      相关资源
      最近更新 更多