我想找到某个边界框内的所有河流。我的最终目标是按名称对它们进行子集化,这样我就可以选择要绘制的河流,但首先我必须知道我范围内要素的名称!我更喜欢使用 ggplot/tidyverse 工具。
例如:
# Download river shapefile here: https://www.weather.gov/gis/Rivers
# Import river data as SF
st_read(dsn = 'rv16my07/', layer = 'rv16my07') %>%
{. ->> my_rivers}
# Add a common CRS to the river dataset
st_crs(my_rivers) <- CRS('+proj=longlat')
# Set x and y limits for the plot
ylims <- c(30.2, 31.4)
xlims <- c(-88.3, -87)
# Create sf for this bounding box
bounding.box <- st_as_sf(data.frame(long = xlims, lat = ylims), coords = c("lat", "long"), crs = CRS('+proj=longlat'))
# Try to …Run Code Online (Sandbox Code Playgroud) 我不想将线条粘贴到两个模式之间,而是想迭代地计算线条数。
例如,给出file.txt这些字符串
abc
123
daafsd
asdfas
asdcasdfa
123
sdfasdc
asdfasdcasd
asdfasdfasdf
asdfasdfasdf
ascasdcasdcasd
123
asdcasdfacasdcas
123
asdfasdcasdcasc
asadfasdfas
123
Run Code Online (Sandbox Code Playgroud)
我想计算 的模式之间的线条123。因此,预期输出将是:
3
5
1
2
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
标题确实总结了我的问题。Apply 似乎会删除 CRS,但其他函数不会。计算点向量上的地理函数的最佳方法是什么?
library(tidyverse)
library(sf)
# Generate 1000 lat longs, save as sf, and set crs
df1 <- data.frame(lat = runif(1000, 30, 33.4), long = runif(1000, -95, -82)) %>%
st_as_sf(coords = c("long", "lat"),
crs = 4326)
# Single point, with identical crs
df2 <- data.frame(lat = 32, long = -96) %>%
st_as_sf(coords = c("long", "lat"),
crs = 4326)
apply(df1, 1, function(x) st_distance(x, df2))
Run Code Online (Sandbox Code Playgroud)
这给出了错误:Error in st_distance(x, df2) : st_crs(x) == st_crs(y) is not TRUE
但这些都工作得很好:
st_distance(df1[1,], df2)
final.df <- …Run Code Online (Sandbox Code Playgroud)