|
| 1 | + |
| 2 | + |
| 3 | +suppressPackageStartupMessages(library(dplyr)) |
| 4 | +library(stars) |
| 5 | +# Loading required package: abind |
| 6 | +# Loading required package: sf |
| 7 | +# Linking to GEOS 3.5.0, GDAL 2.2.2, PROJ 4.8.0 |
| 8 | +tif = system.file("tif/L7_ETMs.tif", package = "stars") |
| 9 | +read_stars(tif) %>% |
| 10 | + slice(index = 1, along = "band") %>% |
| 11 | + plot() |
| 12 | + |
| 13 | + |
| 14 | +prec_file = system.file("nc/test_stageiv_xyt.nc", package = "stars") |
| 15 | +(prec = read_ncdf(prec_file, curvilinear = c("lon", "lat"), ignore_bounds = TRUE)) |
| 16 | + |
| 17 | + |
| 18 | +################ |
| 19 | +sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf"), "nc.gpkg") %>% |
| 20 | + st_transform(st_crs(prec)) -> nc # transform from NAD27 to WGS84 |
| 21 | +nc_outline = st_union(st_geometry(nc)) |
| 22 | +plot_hook = function() plot(nc_outline, border = 'red', add = TRUE) |
| 23 | +prec %>% |
| 24 | + slice(index = 1:12, along = "time") %>% |
| 25 | + plot(downsample = c(5, 5, 1), hook = plot_hook) |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +a = aggregate(prec, by = nc, FUN = max) |
| 30 | +# although coordinates are longitude/latitude, st_intersects assumes that they are planar |
| 31 | +# although coordinates are longitude/latitude, st_intersects assumes that they are planar |
| 32 | +plot(a, max.plot = 23, border = 'grey', lwd = .5) |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +index_max = function(x) ifelse(all(is.na(x)), NA, which.max(x)) |
| 38 | +st_apply(a, "geometry", index_max) %>% |
| 39 | + mutate(when = st_get_dimension_values(a, "time")[.$index_max]) %>% |
| 40 | + select(when) %>% |
| 41 | + plot(key.pos = 1, main = "time of maximum precipitation") |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +library(sf) |
| 47 | +library(leaflet) |
| 48 | +library(mapview) |
| 49 | +library(mapedit) |
| 50 | +library(leafpm) |
| 51 | +# make a contrived polygon with holes for testing |
| 52 | +outer1 = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE) |
| 53 | +hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE) |
| 54 | +hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE) |
| 55 | +outer2 = matrix(c(11,0,11,1,12,1,12,0,11,0),ncol=2, byrow=TRUE) |
| 56 | +pts1 = list(outer1, hole1, hole2) |
| 57 | +pts2 = list(outer2) |
| 58 | +pl1 = st_sf(geom = st_sfc(st_polygon(pts1))) |
| 59 | +pl2 = st_sf(geom = st_sfc(st_polygon(pts2))) |
| 60 | +mpl = st_sf(geom = st_combine(rbind(pl1, pl2)), crs=4326) |
| 61 | +tst = editFeatures(mpl, editor = "leafpm") |
| 62 | +# look at our creation |
| 63 | +mapview(tst) |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +library(sp) |
| 69 | +grd = SpatialPoints(expand.grid(x=1:100, y=1:100)) |
| 70 | +gridded(grd) = TRUE |
| 71 | +fullgrid(grd) = TRUE |
| 72 | +pts = spsample(grd, 50, "random") |
| 73 | +pts$z = rnorm(50) |
| 74 | +library(gstat) |
| 75 | +v = vgm(1, "Sph", 90) |
| 76 | +out = krige(z~1, pts, grd, v, nmax = 20, nsim = 4) |
| 77 | + |
| 78 | +## drawing 4 GLS realisations of beta... |
| 79 | +## [using conditional Gaussian simulation] |
| 80 | + |
| 81 | +out[[3]] = 0.5 * out[[3]] + 0.5 * rnorm(1e4) |
| 82 | +out[[4]] = rnorm(1e4) |
| 83 | +spplot(out, as.table = TRUE) |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +library(tmap) |
| 88 | +budapest_df = data.frame(name = "Budapest", x = 19, y = 47.5) |
| 89 | +class(budapest_df) |
| 90 | +#> [1] "data.frame" |
| 91 | +budapest_sf = sf::st_as_sf(budapest_df, coords = c("x", "y")) |
| 92 | +class(budapest_sf) |
| 93 | +#> [1] "sf" "data.frame" |
| 94 | +tmap_mode("view") |
| 95 | +#> tmap mode set to interactive viewing |
| 96 | +m = tm_shape(budapest_sf) + tm_dots() + tm_view(basemaps = "OpenStreetMap", |
| 97 | + set.view = 9) |
| 98 | +tmap_leaflet(m) |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +library(tmap) |
| 104 | +tm_shape(nz) + |
| 105 | + tm_polygons("Median_income", palette = "RdYlBu") |
| 106 | + |
| 107 | + |
| 108 | + |
0 commit comments