knitr::opts_chunk$set(echo = TRUE, message = FALSE, cache = TRUE, warning = FALSE)
library(sf)
library(raster)
library(tidyverse)
library(leaflet)
library(ggplot2)
library(ggspatial)
Convert foreign object to an sf
object.
data <- read.csv("data/spotted.csv", sep = ",")
prj4string <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
my_projection <- st_crs(prj4string)
class(data)
## [1] "data.frame"
# Create sf object
data_sf <- st_as_sf(data, coords = c("Longitude", "Latitude"), crs = my_projection)
class(data_sf)
## [1] "sf" "data.frame"
plot(data_sf)
ggplot() +
geom_sf(data = data_sf) +
coord_sf()
Transform or convert coordinates of simple feature.
data_sf_projected <- st_transform(data_sf, 32643)
data_sf_projected
## Simple feature collection with 9 features and 1 field
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 770510.9 ymin: 1428935 xmax: 795427.1 ymax: 1441787
## Projected CRS: WGS 84 / UTM zone 43N
## Name geometry
## 1 Rock agamas POINT (795427.1 1433670)
## 2 Jnana Slender Gecko POINT (790951.4 1431495)
## 3 Oriental Garden Lizard POINT (775552.3 1433187)
## 4 Termite Hill Gecko POINT (783134.5 1428935)
## 5 Rock agamas POINT (780472 1441787)
## 6 Termite Hill Gecko POINT (781029.6 1431762)
## 7 Jnana Slender Gecko POINT (780485.6 1429813)
## 8 Keeled Indian Mabuya POINT (770510.9 1441452)
## 9 Oriental Garden Lizard POINT (784605.3 1429453)
Computes a buffer around this geometry/each geometry.
buf <- st_buffer(data_sf_projected, dist = 3000)
plot(buf)
Returns TRUE
or FALSE.
st_intersects(data_sf_projected[1, ], data_sf_projected[2, ])
## Sparse geometry binary predicate list of length 1, where the predicate
## was `intersects'
## 1: (empty)
st_intersects(data_sf_projected[1, ], data_sf_projected[1, ])
## Sparse geometry binary predicate list of length 1, where the predicate
## was `intersects'
## 1: 1
data_join <- read.csv("data/spotted_join.csv", sep = ",")
# Create sf object
data_join_sf <- st_as_sf(data_join, coords = c("Longitude", "Latitude"), crs = my_projection)
# Join using predicate
joined <- st_join(data_sf, data_join_sf, join = st_equals)
joined
## Simple feature collection with 9 features and 3 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 77.49409 ymin: 12.91281 xmax: 77.72288 ymax: 13.02915
## CRS: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
## Name No Length_cm geometry
## 1 Rock agamas 1 25 POINT (77.72288 12.95442)
## 2 Jnana Slender Gecko 2 37 POINT (77.68145 12.9352)
## 3 Oriental Garden Lizard 3 40 POINT (77.53978 12.95191)
## 4 Termite Hill Gecko 4 18 POINT (77.60922 12.91281)
## 5 Rock agamas NA NA POINT (77.5859 13.02915)
## 6 Termite Hill Gecko NA NA POINT (77.5901 12.93854)
## 7 Jnana Slender Gecko NA NA POINT (77.58491 12.92098)
## 8 Keeled Indian Mabuya NA NA POINT (77.49409 13.02702)
## 9 Oriental Garden Lizard NA NA POINT (77.62281 12.91735)
temp <- raster("data/Ban_Temp_Mar2022.tif")
temp
## class : RasterLayer
## dimensions : 11, 12, 132 (nrow, ncol, ncell)
## resolution : 0.1000005, 0.1000005 (x, y)
## extent : 77.00035, 78.20036, 12.60006, 13.70006 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : Ban_Temp_Mar2022.tif
## names : Ban_Temp_Mar2022
To transform a RasterLayer
to another coordinate
reference system (projection)
# To transform
temp_proj <- projectRaster(temp, crs = "+proj=utm +zone=43 +datum=WGS84")
Mask values from a Raster
object at the locations of
spatial vector data
data_sf_subset <- data_sf[1, ]
data_sf_buffer <- st_buffer(data_sf_subset, dist = 12000)
masked_raster <- mask(x = temp, mask = data_sf_buffer)
bangalore_boundary <- st_read("data/BBMP_Boundary.shp")
## Reading layer `BBMP_Boundary' from data source
## `/Users/meenakshikushwaha/Dropbox/R projects/github/CSTEP_R_course/data/BBMP_Boundary.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 1 feature and 2 fields
## Geometry type: POLYGON
## Dimension: XY
## Bounding box: xmin: 766836.1 ymin: 1420254 xmax: 802134.7 ymax: 1454441
## Projected CRS: WGS 84 / UTM zone 43N
bangalore_boundary <- st_transform(bangalore_boundary, crs = my_projection)
ggplot() + geom_sf(data = bangalore_boundary, color = "green", size = 2, alpha = 0.1, fill = "green") +
geom_sf(data = data_sf, color = "blue", size = 2) + theme_bw() +
annotation_scale(location = "bl", width_hint = 0.5) +
annotation_north_arrow(location = "bl", which_north = "true",
pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"),
style = north_arrow_fancy_orienteering) +
coord_sf(xlim = c(77.4, 77.8), ylim = c(12.8, 13.2))
leaflet(bangalore_boundary) %>%
addTiles() %>%
addPolygons(color = "green") %>%
addCircles(
data = data,
lng = ~ Longitude,
lat = ~ Latitude,
radius = 20,
weight = 20,
fill = TRUE)