Click on the three horizontally stacked lines at the bottom left corner of the slide, then you will see table of contents, and you can jump to the section you want
Hit letter “o” on your keyboard and you will have a panel view of all the slides
Running and writing codes
The box area with a hint of blue as the background color is where you can write code (hereafter referred to as the “code area”).
Hit the “Run Code” button to execute all the code inside the code area.
You can evaluate (run) code selectively by highlighting the parts you want to run and hitting Command + Enter for Mac (Ctrl + Enter for Windows).
If you want to run the codes on your computer, you can first click on the icon with two sheets of paper stacked on top of each other (top right corner of the code chunk), which copies the code in the code area. You can then paste it onto your computer.
You can click on the reload button (top right corner of the code chunk, left to the copy button) to revert back to the original code.
We use three sf objects: points, lines, and polygons. Inspect each of them.
If you are interested in codes to create them see below.
Code
#--- create points ---#point_1 <- sf::st_point(c(2, 2))point_2 <- sf::st_point(c(1, 1))point_3 <- sf::st_point(c(1, 3))#--- combine the points to make a single sf of points ---#points <-list(point_1, point_2, point_3) %>% sf::st_sfc() %>% sf::st_as_sf() %>%mutate(point_name =c("point 1", "point 2", "point 3"))#--- create lines ---#line_1 <- sf::st_linestring(rbind(c(0, 0), c(2.5, 0.5)))line_2 <- sf::st_linestring(rbind(c(1.5, 0.5), c(2.5, 2)))#--- combine the points to make a single sf of points ---#lines <-list(line_1, line_2) %>% sf::st_sfc() %>% sf::st_as_sf() %>%mutate(line_name =c("line 1", "line 2"))#--- create polygons ---#polygon_1 <- sf::st_polygon(list(rbind(c(0, 0), c(2, 0), c(2, 2), c(0, 2), c(0, 0)) ))polygon_2 <- sf::st_polygon(list(rbind(c(0.5, 1.5), c(0.5, 3.5), c(2.5, 3.5), c(2.5, 1.5), c(0.5, 1.5)) ))polygon_3 <- sf::st_polygon(list(rbind(c(0.5, 2.5), c(0.5, 3.2), c(2.3, 3.2), c(2, 2), c(0.5, 2.5)) ))#--- combine the polygons to make an sf of polygons ---#polygons <-list(polygon_1, polygon_2, polygon_3) %>% sf::st_sfc() %>% sf::st_as_sf() %>%mutate(polygon_name =c("polygon 1", "polygon 2", "polygon 3"))
st_intersects() checks which of sfgs in an sf geographically intersect with which of sfgs in another sf.
The output is a list of which polygon(s) each of the points intersect with.
The numbers 1, 2, and 3 in the first row mean that 1st (polygon 1), 2nd (polygon 2), and 3rd (polygon 3) objects of the polygons intersect with the first point (point 1) of the points object.
The fact that point 1 is considered to be intersecting with polygon 2 means that the area inside the border is considered a part of the polygon (of course).
If you would like the results of st_intersects() in a matrix form with boolean values filling the matrix, you can add sparse = FALSE option.
The output is a list of which polygon(s) each of the lines intersect with.
For polygons vs polygons interaction, st_intersects() identifies any polygons that either touches (even at a point like polygons 1 and 3) or share some area.
Select only the counties that intersect with the HPA boundary.
When subsetting a data.frame by specifying the row numbers you would like to select, you can do
#--- NOT RUN ---#data.frame[vector of row numbers, ]
Spatial subsetting of sf objects works in a similar syntax:
#--- NOT RUN ---#sf_1[sf_2, ]
where you are subsetting sf_1 based on sf_2. Instead of row numbers, you provide another sf object in place.
The following code spatially subsets Nebraska counties based on the HPA boundary.
You can see that only the counties that intersect with the HPA boundary remained.
This is because when you use the above syntax of sf_1[sf_2, ], the default underlying topological relation is st_intersects().
So, if an object in sf_1 intersects with any of the objects in sf_2 even slightly, then it will remain after subsetting.
Sometimes, you just want to flag whether two spatial objects intersect or not, instead of dropping non-overlapping observations like we saw with sf_1[sf_2, ] syntax. In that case, you can get a list of the IDs and then assign 1 (or TRUE) if in the list, 0 (or FALSE) otherwise.
Get the list of countyfp (ID) of the intersected counties:
Assign 1 or 0 to a new variable called in_hpa based on the list.
You can specify the topological relation as in
#--- NOT RUN ---#sf_1[sf_2, , op = topological_relation_type]
For example, if you only want counties that are completely within the HPA boundary, you can do the following:
Select only the wells that intersect with (or equivalently inside) the HPA boundary.
We can select only the wells that reside within the HPA boundary using the same syntax as the polygon-polygon example.
As you can see in the figure below, only the wells that are intersects the HPA remained because the default topological relation is st_intersects() (here, you will get the same results even if you use op = st_within.).
Get the list of wellid (ID) of the intersected wells:
Assign 1 or 0 to a new variable called in_hpa based on the list.