1suppressPackageStartupMessages(library(sf)) 2 3tif = system.file("tif/geomatrix.tif", package = "sf") 4 5gdal_metadata(tif) 6gdal_metadata(tif, NA_character_) 7try(gdal_metadata(tif, "wrongDomain")) 8gdal_metadata(tif, c("IMAGE_STRUCTURE")) 9try(length(gdal_metadata(tif, c("DERIVED_SUBDATASETS")))) # fails on Fedora 26 10 11if (require(stars)) { 12 tif = system.file("tif/geomatrix.tif", package = "sf") 13 r = read_stars(tif) 14 d = (st_dimensions(r)) 15 gt = c(1841001.75, 1.5, -5, 1144003.25, -5, -1.5) 16 x1 = st_as_sfc(d, as_points = TRUE, use_cpp = TRUE, geotransform = gt) 17 x2 = st_as_sfc(d, as_points = TRUE, use_cpp = FALSE, geotransform = gt) 18 print(identical(x1, x2)) 19 y1 = st_as_sfc(d, as_points = FALSE, use_cpp = TRUE, geotransform = gt) 20 y2 = st_as_sfc(d, as_points = FALSE, use_cpp = FALSE, geotransform = gt) 21 print(identical(y1, y2)) 22 23 # rectilinear grid: 24 m = matrix(1:20, nrow = 5, ncol = 4) 25 x = c(0,0.5,1,2,4,5) 26 y = c(0.3,0.5,1,2,2.2) 27 r = st_as_stars(list(m = m), dimensions = st_dimensions(x = x, y = y, .raster = c("x", "y"))) 28 print(st_as_sfc(st_dimensions(r), as_points = TRUE)) 29 print(st_as_sfc(st_dimensions(r), as_points = FALSE)) 30 31 # curvilinear grid: 32 lon = st_as_stars(matrix(1:5, 4, 5, byrow = TRUE)) 33 lat = st_as_stars(matrix(1:4, 4, 5)) 34 ll = c(X1 = lon, X2 = lat) 35 curv = st_as_stars(st_as_stars(t(m)), curvilinear = setNames(ll, c("X1", "X2"))) 36 print(st_as_sfc(st_dimensions(curv), as_points = TRUE)) 37 print(st_as_sfc(st_dimensions(curv), as_points = FALSE)) 38 39 demo(nc, echo = FALSE, ask = FALSE) 40 print(x <- st_rasterize(nc)) # default grid: 41 print(p <- st_as_sf(x, as_points = FALSE)) # polygonize: follow raster boundaries 42 print(p <- st_as_sf(x, as_points = FALSE, use_integer = TRUE)) # polygonize integers: follow raster boundaries 43 print(try(p <- st_as_sf(x, as_points = TRUE))) # polygonize: contour, requies GDAL >= 2.4.0 44 if (utils::packageVersion("stars") >= "0.2-1") { 45 write_stars(read_stars(tif), tempfile(fileext = ".tif")) 46 write_stars(read_stars(tif, proxy = TRUE), tempfile(fileext = ".tif")) 47 write_stars(read_stars(tif, proxy = TRUE), tempfile(fileext = ".tif"), chunk_size = c(200,200)) 48 na.tif = read_stars(system.file("tif/na.tif", package = "stars")) 49 write_stars(na.tif, "na.tif") 50 write_stars(na.tif, "na.tif", NA_value = -999) 51 na.tif = read_stars(system.file("tif/na.tif", package = "stars"), NA_value = -999) 52 write_stars(na.tif, "na.tif") 53 write_stars(na.tif, "na.tif", NA_value = -999) 54 na.tif = read_stars(system.file("tif/na.tif", package = "stars"), NA_value = -999, proxy = TRUE) 55 write_stars(na.tif, "na.tif") 56 write_stars(na.tif, "na.tif", NA_value = -999) 57 } 58 # https://github.com/mtennekes/tmap/issues/368 59 if (utils::packageVersion("stars") > "0.4-0") { 60 lc = system.file('tif/lc.tif', package = 'stars') 61 if (lc != "") { 62 r = read_stars(lc, RAT = "Land Cover Class") 63 r <- droplevels(r) 64 } 65 } 66} 67 68r = gdal_read(tif) 69gt = c(0,1,0,0,0,1) 70gdal_inv_geotransform(gt) 71rc = expand.grid(x=1:3, y = 1:3) 72#(xy = xy_from_colrow(rc, gt)) 73#xy_from_colrow(xy, gt, inverse = TRUE) 74crs <- gdal_crs(tif) 75 76try(gdal_metadata("foo")) 77gdal_metadata(tif) 78