functions ot extract coordinates from a point geometry, convert a sf to ppp and create a raster
st_coordinates_tidy(sf_object)
tibble_as_raster(data)
sf_object input
tibble with three column names: x, y, z (for longitud, latitud, and grid value)
sf and data.frame object X and Y extracted from geometry column
st_coordinates_tidy()
: retrieve coordinates within the original sf/data.frame object. an alternative from st_coordinates()
tibble_as_raster()
: transform a x,y,z tibble to a raster
if (FALSE) {
# st_coordinates_tidy --------------------------------------
library(tidyverse)
library(sf)
library(epihelper)
sites <- tibble(gpx_point = c("a","b"),
longitude = c(-80.144005, -80.109),
latitude = c(26.479005,26.83)) %>% print()
sites_sf <- sites %>%
st_as_sf(coords = c("longitude", "latitude"),
remove = T,
crs = 4326) %>% print()
# current solution
sites_sf %>%
st_coordinates()
# our proposal
sites_sf %>%
st_coordinates_tidy()
# sf_to_ppp ------------------------------------------
# data packages
library(tidyverse)
library(epihelper)
library(sf)
# import sample data
library(spatstat)
data("flu")
flu_one <- flu$pattern$`wt M2-M1 13`
flu_one %>% plot()
# reverse engineering
# extract window from ppp
flu_one_window <- st_as_sf(flu_one) %>%
filter(label=="window") %>%
pull(geom)
# extract points from ppp
flu_one_points <- flu_one %>%
as_tibble() %>%
#tibble to sf
st_as_sf(coords = c("y", "x"),
remove = T,
crs = 4326,
agr = "constant")
# /deprecated/ re-create a ppp from points and bbox
# sf_as_ppp(sf_geometry_input = flu_one_points,
# sf_polygon_boundary = flu_one_window) %>%
# plot()
# /this works/ re-create a ppp from points and bbox
as.ppp(st_coordinates(flu_one_points),
st_bbox(flu_one_window)) %>%
plot()
# tibble_as_raster -------------------------------------
set.seed(33)
expand_grid(x=1:10,
y=1:10) %>%
mutate(z=rnorm(100)) %>%
# convert tibble to raster
tibble_as_raster() %>%
# plot to verify
plot()
}