无法使“平面”endcapStyle对R中的st_buffer起作用

9bfwbjaz  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(108)

我目前正在使用纬度/经度坐标创建一个sfc行字符串表,并绘制一个缓冲区。然而,我无法让endcapstyle =“FLAT”参数工作。我希望这能工作,因为我需要缓冲区不延伸超过行的端点。我错过了什么?

library(sf)
library(mapview)
library(tidyverse)

## random coordinates for example
df_tbl <- tibble::tribble(
  ~lat,   ~lon,   ~lat2,   ~lon2,
  39.977917887303626, -105.28350982984472, 40.026678120413961, -105.20312377997453,
  39.934422269952776, -105.25986687400055, 39.902061443935025, -105.28605599432022)

## create points
beg_t <- df_tbl %>% select(lon, lat) %>% st_as_sf(coords = c("lon", "lat"), crs = 4326)  
end_t <- df_tbl %>% select(lon2, lat2) %>% st_as_sf(coords = c("lon2", "lat2"), crs = 4326)  

# combine start and end geometries
cbind(beg_t,end_t) -> points_ready 

# create linestrings

line_segments <- points_ready %>% 
  mutate(
    path = st_sfc(purrr::map2(
      .x = geometry, 
      .y = geometry.1, 
      .f = ~{st_union(c(.x, .y)) %>% st_cast("LINESTRING")}
    ),
    crs = 4326)) 

## try to use the FLAT endCapStyles
line_segments$buffer <- st_buffer(line_segments$path, dist = 2000, endCapStyle = "FLAT")
mapview(line_segments$path) + mapview(line_segments$buffer)  ## the buffers are not flat at the ends
jgovgodb

jgovgodb1#

endCapStyle将仅在GEOS设置中工作=它与{s2}世界的球形操作不兼容。您可以完全关闭{s2}处理,或重新投影到平面CRS。
在下面的代码中,我使用了第二种方法,即Web Mercator(EPSG 3857)。
另外,我选择创建缓冲线作为一个单独的对象,因为点和线在WGS 84中,我不太喜欢在单个对象中混合CRS({sf}的活动/非活动几何特性应该处理它,但它感觉不对)。

library(sf)
library(mapview)
library(tidyverse)

## random coordinates for example
df_tbl <- tibble::tribble(
  ~lat,   ~lon,   ~lat2,   ~lon2,
  39.977917887303626, -105.28350982984472, 40.026678120413961, -105.20312377997453,
  39.934422269952776, -105.25986687400055, 39.902061443935025, -105.28605599432022)

## create points
beg_t <- df_tbl %>% select(lon, lat) %>% st_as_sf(coords = c("lon", "lat"), crs = 4326)  
end_t <- df_tbl %>% select(lon2, lat2) %>% st_as_sf(coords = c("lon2", "lat2"), crs = 4326)  

# combine start and end geometries
cbind(beg_t,end_t) -> points_ready 

# create linestrings

line_segments <- points_ready %>% 
  mutate(
    path = st_sfc(purrr::map2(
      .x = geometry, 
      .y = geometry.1, 
      .f = ~{st_union(c(.x, .y)) %>% st_cast("LINESTRING")}
    ),
    crs = 4326)) 

## try to use the FLAT endCapStyles
buffered_lines <- line_segments$path %>% 
  st_transform("EPSG:3857") %>% # to force GEOS processing
  st_buffer(endCapStyle = "FLAT",
            dist = 2000)

mapview(line_segments$path) + mapview(buffered_lines)

相关问题