如何按纬度过滤 sf 对象? [英] How to filter a sf object by latitude?
问题描述
我有一个安大略省边界的 shapefile,内容如下:
I have a shapefile for Ontario province boundary that I read as follows:
library(sf)
library(here)
ontario <- sf::st_read(here::here("data", "messy_data", "Ontario.shp"), quiet = TRUE) %>%
st_transform(4326)
它可以绘制如下:
我想保留 ontario
数据仅用于 latitude <51
.但由于它是一个 sf
对象,dplyr::filter(latitude <51)
不起作用.我知道我可以使用 st_coordinates()
提取坐标,但是如何将它们连接回数据以过滤掉更高的高度?
或者,sf
中是否有任何函数可以用来进行过滤?我查看了帮助,但到目前为止找不到任何相关内容.
I want to keep the ontario
data only for latitude < 51
. But since it is a sf
object, dplyr::filter(latitude < 51)
doesn't work. I know that I can extract the coordinates with st_coordinates()
, but how can I join them back to the data to filter out the higher altitude?
Alternatively, is there any function in sf
that I could use to do the filter? I looked into the help but couldn't find anything relevant so far.
推荐答案
让我们从 GADM 数据中获取加拿大:
Let's get Canada from the GADM data:
library(raster)
library(sf)
cdn = getData("GADM",country="can",level=1)
这应该是安大略:
ont = cdn[9,]
ont$NAME_1
转换为 sf
对象:
ont = st_as_sf(ont)
现在开始 - 裁剪到北纬 51 度:
Now to business - crop to 51 degrees north:
ont_south = st_crop(ont, xmin=-180, xmax=180, ymin=-90, ymax=51)
plot(ont_south$geometry)
这篇关于如何按纬度过滤 sf 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!