-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2tiff.R
49 lines (43 loc) · 1.07 KB
/
csv2tiff.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
library(sf)
library(tidyverse)
library(stars)
library(terra)
library(stringr)
setwd("~/Projects/alproj/")
interpolate <- function(in_path, out_path, res, max_dist, fun=terra::modal) {
print(str_c("Reading ", in_path, " ......"))
points <- read_csv(
in_path
) %>%
st_as_sf(coords = c("x", "y")) %>%
st_set_crs(6690) %>%
mutate(z = as.integer(z)) %>%
select(-c(u, v, z))
print("Rasterizing point data ......")
ras <- st_rasterize(points, dx = res, dy = res)
rm(points)
gc()
ras <- ras %>%
as("Raster") %>%
terra::rast()
times <- ceiling(max_dist / res)
for (i in 1:times) {
print(str_c("Interpolating ......", i, " of ", times, " iterations"))
ras <- ras %>%
terra::focal(3, fun, na.policy="only", na.rm=TRUE)
}
print(str_c("Saving file to ", out_path, " ......"))
terra::writeRaster(ras, out_path, overwrite=TRUE)
rm(ras)
gc()
print("Finished !")
}
file <- "devel_data/georectified.csv"
out_path <- stringr::str_replace(file, "csv", "tiff")
interpolate(
file,
out_path,
1.0,
1.0,
fun = mean
)