Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Data

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)

It can be plotted as follows:

enter image description here

Question

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

Let's get Canada from the GADM data:

library(raster)
library(sf)
cdn = getData("GADM",country="can",level=1)

this should be Ontario:

ont = cdn[9,]
ont$NAME_1

convert to an sf object:

ont = st_as_sf(ont)

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...