library(dplyr)
library(ggplot2)
library(sf)
library(terra)
library(tidyterra)Extracting buffers
BeeSpatial provides forage, nesting, and insecticide rasters as buffered summaries using distance-weighted means of 1, 3, and 5km radius buffers. This means that each pixel of the raster represents the distance-weighted mean of the index values within the corresponding buffer radius. Only the CDL output summary is not a distance-weighted mean. It is the proportion of each land cover class.
If you want your own buffer summaries of the CDL or forage, nesting, or insecticide indices, this tutorial shows how to do that using proportional area (for CDL) or simple means (habitat indices).
Load packages
First, load the necessary packages. More info on these packages and their installation can be found here.
Read in the CDL data downloaded from BeeSpatial.
centre_cdl <- rast("data/CDL_2021_FIPS_42027.tif") # change the filepath to reflect where you've stored the dataRead in point data
Often we collect and store point data as coordinates in a non-spatial format such as a csv file which we need to convert into a spatial object in R.
In the /data directory you should find a csv file with a set of points from Centre County Pa. We will read in and inspect these data.
centre_sites <- read.csv("data/centre_co_pts.csv")
centre_sites SiteID Long Lat
1 Site1 -77.70654 40.80497
2 Site2 -77.89991 40.74921
3 Site3 -77.93778 41.00641
4 Site4 -77.86590 40.81954
5 Site5 -77.64715 41.02197
6 Site6 -77.84409 40.87366
7 Site7 -77.77373 40.82634
8 Site8 -77.69934 40.87697
and convert them into an sf spatial object. We use the coords= argument to indicate the columns that hold the longitude (X) and latitude (Y). We can also specify the CRS using the ‘crs=’ arguments and the appropriate EPSG code or other crs object. In our case we will give the EPSG code 4326 which corresponds to WGS 84, the system commonly used by Google Earth.
centre_sites <- st_as_sf(centre_sites,
coords=c("Long","Lat"), #indicate the x and y columns
crs = 4326) #set the crswhen we inspect the center_sites object we see that there is now additional information at the top.
centre_sitesSimple feature collection with 8 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: -77.93778 ymin: 40.74921 xmax: -77.64715 ymax: 41.02197
Geodetic CRS: WGS 84
SiteID geometry
1 Site1 POINT (-77.70654 40.80497)
2 Site2 POINT (-77.89991 40.74921)
3 Site3 POINT (-77.93778 41.00641)
4 Site4 POINT (-77.8659 40.81954)
5 Site5 POINT (-77.64715 41.02197)
6 Site6 POINT (-77.84409 40.87366)
7 Site7 POINT (-77.77373 40.82634)
8 Site8 POINT (-77.69934 40.87697)
Visualize point data
Using ggplot2 and sf we can add our points to our earlier map of Centre county with the geom_sf() function.
#Centre county CDL from raster exercise
centre_cdl <- rast("data/centre_county_cdl_2021.tif")
ggplot()+
geom_spatraster(data = centre_cdl, aes(fill = class_name)) +
geom_sf(data=centre_sites) +
theme(legend.title = element_text(size = 7),
legend.text = element_text(size = 7),
legend.key.size = unit(0.25, 'cm'),
legend.position="bottom") <SpatRaster> resampled to 500797 cells.

Buffer around points
We can create a polygon object from our points by buffering around them in a 1000m radius. For example, we might do this when assessing land cover values around a site.
centre_sites_1000m <- st_buffer(centre_sites, 1000)
centre_sites_1000mSimple feature collection with 8 features and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -77.94977 ymin: 40.74008 xmax: -77.63518 ymax: 41.03112
Geodetic CRS: WGS 84
SiteID geometry
1 Site1 POLYGON ((-77.70945 40.8137...
2 Site2 POLYGON ((-77.90568 40.7412...
3 Site3 POLYGON ((-77.92781 41.0013...
4 Site4 POLYGON ((-77.85495 40.8230...
5 Site5 POLYGON ((-77.65648 41.0163...
6 Site6 POLYGON ((-77.84816 40.8651...
7 Site7 POLYGON ((-77.76805 40.8184...
8 Site8 POLYGON ((-77.69978 40.886,...
We can visualize this buffer by adding it to our plot. Note you may need to expand your plot size to see the buffers behind the points.
ggplot()+
geom_spatraster(data = centre_cdl, aes(fill = class_name)) +
geom_sf(data = centre_sites_1000m)+
geom_sf(data=centre_sites) +
theme(legend.title = element_text(size = 7),
legend.text = element_text(size = 7),
legend.key.size = unit(0.25, 'cm'),
legend.position="bottom") <SpatRaster> resampled to 500797 cells.

Extract CDL values within buffers
If we wanted to assess land cover around a site in a way that is similar to the results that beeshiny provides, we can use the extract() function from terra. First providing the raster we want to extract values from, then the sf object that defines the zones in which the raster will the summarized.
centre_table_1000m <- terra::extract(centre_cdl, centre_sites_1000m,
fun="table", ID=F, wide = TRUE)Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
Warning: [extract] transforming vector data to the CRS of the raster
centre_table_1000m count.Corn count.Sorghum count.Soybeans count.Winter Wheat count.Rye
1 1241 12 1021 225 2
2 1167 0 968 445 7
3 0 0 0 0 0
4 64 0 80 7 0
5 18 1 5 1 0
6 882 1 836 24 0
7 0 0 0 0 0
8 92 0 0 0 0
count.Oats count.Alfalfa count.Other Hay/Non Alfalfa count.Dry Beans
1 20 156 88 43
2 136 100 29 118
3 0 0 0 0
4 0 82 410 0
5 0 2 50 1
6 0 124 407 0
7 0 0 0 0
8 0 4 25 0
count.Developed/Open Space count.Developed/Low Intensity
1 224 69
2 52 95
3 208 9
4 298 367
5 172 95
6 299 214
7 135 1
8 162 8
count.Developed/Med Intensity count.Developed/High Intensity count.Barren
1 22 2 4
2 45 8 0
3 2 0 0
4 401 137 1
5 33 0 7
6 22 4 1
7 0 0 0
8 1 0 7
count.Deciduous Forest count.Evergreen Forest count.Mixed Forest
1 178 10 22
2 116 1 12
3 3263 1 56
4 474 106 303
5 391 35 692
6 236 6 56
7 3358 9 37
8 2216 533 458
count.Grassland/Pasture count.Barley count.Triticale
1 213 0 0
2 241 7 2
3 9 0 0
4 806 1 1
5 415 0 1
6 437 1 0
7 4 0 0
8 36 0 0
count.Dbl Crop WinWht/Corn count.Tomatoes count.Apples count.Shrubland
1 0 0 0 0
2 2 0 0 0
3 0 0 0 0
4 3 1 1 1
5 0 1 0 1
6 0 0 1 1
7 0 0 0 5
8 0 0 0 1
count.Dbl Crop Triticale/Corn count.Pumpkins count.Dbl Crop WinWht/Sorghum
1 0 0 0
2 0 0 0
3 0 0 0
4 1 1 1
5 0 0 0
6 0 0 0
7 0 0 0
8 0 0 0
count.Open Water count.Woody Wetlands count.Herbaceous Wetlands
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
5 1621 5 1
6 0 0 0
7 0 0 0
8 0 5 0
count.Dbl Crop Oats/Corn
1 0
2 0
3 0
4 0
5 1
6 0
7 0
8 0
We can calculate additional statistics such as the area within the buffer for each site.
Area_m2 <- rowSums(centre_table_1000m)*900and turn our table into proportional cover of different land classes.
centre_table_1000m_prop <- centre_table_1000m/rowSums(centre_table_1000m)
centre_table_1000m_prop <- cbind(Area_m2,centre_table_1000m_prop)
centre_table_1000m_prop Area_m2 count.Corn count.Sorghum count.Soybeans count.Winter Wheat
1 3196800 0.349380631 0.0033783784 0.287443694 0.0633445946
2 3195900 0.328639820 0.0000000000 0.272599268 0.1253168122
3 3193200 0.000000000 0.0000000000 0.000000000 0.0000000000
4 3192300 0.018043417 0.0000000000 0.022554271 0.0019734987
5 3194100 0.005071851 0.0002817695 0.001408848 0.0002817695
6 3196800 0.248310811 0.0002815315 0.235360360 0.0067567568
7 3194100 0.000000000 0.0000000000 0.000000000 0.0000000000
8 3193200 0.025930101 0.0000000000 0.000000000 0.0000000000
count.Rye count.Oats count.Alfalfa count.Other Hay/Non Alfalfa
1 0.0005630631 0.005630631 0.043918919 0.024774775
2 0.0019712757 0.038299071 0.028161081 0.008166714
3 0.0000000000 0.000000000 0.000000000 0.000000000
4 0.0000000000 0.000000000 0.023118128 0.115590640
5 0.0000000000 0.000000000 0.000563539 0.014088476
6 0.0000000000 0.000000000 0.034909910 0.114583333
7 0.0000000000 0.000000000 0.000000000 0.000000000
8 0.0000000000 0.000000000 0.001127396 0.007046223
count.Dry Beans count.Developed/Open Space count.Developed/Low Intensity
1 0.0121058559 0.06306306 0.0194256757
2 0.0332300760 0.01464376 0.0267530273
3 0.0000000000 0.05862458 0.0025366404
4 0.0000000000 0.08401466 0.1034677192
5 0.0002817695 0.04846436 0.0267681037
6 0.0000000000 0.08417793 0.0602477477
7 0.0000000000 0.03803888 0.0002817695
8 0.0000000000 0.04565953 0.0022547914
count.Developed/Med Intensity count.Developed/High Intensity count.Barren
1 0.0061936937 0.0005630631 0.0011261261
2 0.0126724866 0.0022528865 0.0000000000
3 0.0005636979 0.0000000000 0.0000000000
4 0.1130532845 0.0386241895 0.0002819284
5 0.0092983939 0.0000000000 0.0019723866
6 0.0061936937 0.0011261261 0.0002815315
7 0.0000000000 0.0000000000 0.0000000000
8 0.0002818489 0.0000000000 0.0019729425
count.Deciduous Forest count.Evergreen Forest count.Mixed Forest
1 0.05011261 0.0028153153 0.006193694
2 0.03266685 0.0002816108 0.003379330
3 0.91967306 0.0002818489 0.015783540
4 0.13363406 0.0298844094 0.085424302
5 0.11017188 0.0098619329 0.194984503
6 0.06644144 0.0016891892 0.015765766
7 0.94618202 0.0025359256 0.010425472
8 0.62457723 0.1502254791 0.129086809
count.Grassland/Pasture count.Barley count.Triticale
1 0.059966216 0.0000000000 0.0000000000
2 0.067868206 0.0019712757 0.0005632216
3 0.002536640 0.0000000000 0.0000000000
4 0.227234282 0.0002819284 0.0002819284
5 0.116934348 0.0000000000 0.0002817695
6 0.123029279 0.0002815315 0.0000000000
7 0.001127078 0.0000000000 0.0000000000
8 0.010146561 0.0000000000 0.0000000000
count.Dbl Crop WinWht/Corn count.Tomatoes count.Apples count.Shrubland
1 0.0000000000 0.0000000000 0.0000000000 0.0000000000
2 0.0005632216 0.0000000000 0.0000000000 0.0000000000
3 0.0000000000 0.0000000000 0.0000000000 0.0000000000
4 0.0008457852 0.0002819284 0.0002819284 0.0002819284
5 0.0000000000 0.0002817695 0.0000000000 0.0002817695
6 0.0000000000 0.0000000000 0.0002815315 0.0002815315
7 0.0000000000 0.0000000000 0.0000000000 0.0014088476
8 0.0000000000 0.0000000000 0.0000000000 0.0002818489
count.Dbl Crop Triticale/Corn count.Pumpkins count.Dbl Crop WinWht/Sorghum
1 0.0000000000 0.0000000000 0.0000000000
2 0.0000000000 0.0000000000 0.0000000000
3 0.0000000000 0.0000000000 0.0000000000
4 0.0002819284 0.0002819284 0.0002819284
5 0.0000000000 0.0000000000 0.0000000000
6 0.0000000000 0.0000000000 0.0000000000
7 0.0000000000 0.0000000000 0.0000000000
8 0.0000000000 0.0000000000 0.0000000000
count.Open Water count.Woody Wetlands count.Herbaceous Wetlands
1 0.0000000 0.000000000 0.0000000000
2 0.0000000 0.000000000 0.0000000000
3 0.0000000 0.000000000 0.0000000000
4 0.0000000 0.000000000 0.0000000000
5 0.4567484 0.001408848 0.0002817695
6 0.0000000 0.000000000 0.0000000000
7 0.0000000 0.000000000 0.0000000000
8 0.0000000 0.001409245 0.0000000000
count.Dbl Crop Oats/Corn
1 0.0000000000
2 0.0000000000
3 0.0000000000
4 0.0000000000
5 0.0002817695
6 0.0000000000
7 0.0000000000
8 0.0000000000
Write out shapefiles
we can write out vector data to shapefiles using st_write()
st_write(centre_sites, "data/centre_sites.shp", delete_layer = TRUE)Deleting layer `centre_sites' using driver `ESRI Shapefile'
Writing layer `centre_sites' to data source
`data/centre_sites.shp' using driver `ESRI Shapefile'
Writing 8 features with 1 fields and geometry type Point.
st_write(centre_sites_1000m, "data/centre_sites_buffer_1000m.shp", delete_layer = TRUE)Deleting layer `centre_sites_buffer_1000m' using driver `ESRI Shapefile'
Writing layer `centre_sites_buffer_1000m' to data source
`data/centre_sites_buffer_1000m.shp' using driver `ESRI Shapefile'
Writing 8 features with 1 fields and geometry type Polygon.