Skip to content

Commit fe14723

Browse files
committed
initial upload
contains only some native gdal utils and metadata readers
1 parent c6b2b52 commit fe14723

32 files changed

+7017
-0
lines changed

DESCRIPTION

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Package: stars
2+
Version: 0.0
3+
Title: Scalable, Spatiotemporal Tidy Arrays for R
4+
Description: Support for Scalable, Spatiotemporal Arrays in R, using GDAL bindings.
5+
Authors@R: c(person("Edzer", "Pebesma", role = c("aut", "cre"), email = "edzer.pebesma@uni-muenster.de"),
6+
person("Michael", "Sumner", role = "ctb"),
7+
person("Etienne", "Racine", role = "ctb"))
8+
Depends:
9+
R (>= 3.3.0)
10+
Imports:
11+
Rcpp,
12+
magrittr,
13+
sf
14+
Suggests:
15+
testthat,
16+
knitr,
17+
tibble,
18+
dplyr,
19+
tidyr,
20+
geosphere (>= 1.5-5),
21+
maptools,
22+
maps
23+
LinkingTo: Rcpp, sf
24+
VignetteBuilder: knitr
25+
SystemRequirements: GDAL (>= 2.1.0), PROJ.4 (>= 4.8.0)
26+
License: Apache License
27+
URL: https://github.com/edzer/stars/
28+
BugReports: https://github.com/edzer/stars/issues/
29+
Collate:
30+
init.R
31+
dataset.R
32+
utils.R
33+
RcppExports.R
34+
RoxygenNote: 6.0.1

NAMESPACE

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export("%>%")
4+
export(st_gdalinfo)
5+
export(st_gdalrasterize)
6+
export(st_gdalwarp)
7+
export(st_get_metadata)
8+
export(st_get_subdatasets)
9+
export(st_read_crs)
10+
export(stars_extSoftVersion)
11+
importFrom(Rcpp,evalCpp)
12+
importFrom(magrittr,"%>%")
13+
importFrom(sf,sf_extSoftVersion)
14+
useDynLib(stars)

R/RcppExports.R

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
CPL_GetMetadata <- function(obj, domain_item, options) {
5+
.Call('stars_CPL_GetMetadata', PACKAGE = 'stars', obj, domain_item, options)
6+
}
7+
8+
CPL_get_crs <- function(obj, options) {
9+
.Call('stars_CPL_get_crs', PACKAGE = 'stars', obj, options)
10+
}
11+
12+
CPL_add_one <- function(inv) {
13+
.Call('stars_CPL_add_one', PACKAGE = 'stars', inv)
14+
}
15+
16+
CPL_gdal_init <- function() {
17+
invisible(.Call('stars_CPL_gdal_init', PACKAGE = 'stars'))
18+
}
19+
20+
CPL_gdal_cleanup_all <- function() {
21+
invisible(.Call('stars_CPL_gdal_cleanup_all', PACKAGE = 'stars'))
22+
}
23+
24+
CPL_gdal_version <- function(what = "RELEASE_NAME") {
25+
.Call('stars_CPL_gdal_version', PACKAGE = 'stars', what)
26+
}
27+
28+
CPL_proj_version <- function(b = FALSE) {
29+
.Call('stars_CPL_proj_version', PACKAGE = 'stars', b)
30+
}
31+
32+
CPL_proj_is_valid <- function(proj4string) {
33+
.Call('stars_CPL_proj_is_valid', PACKAGE = 'stars', proj4string)
34+
}
35+
36+
CPL_proj_info <- function(type) {
37+
.Call('stars_CPL_proj_info', PACKAGE = 'stars', type)
38+
}
39+
40+
CPL_gdalinfo <- function(obj, options) {
41+
.Call('stars_CPL_gdalinfo', PACKAGE = 'stars', obj, options)
42+
}
43+
44+
CPL_gdalwarp <- function(src, dst, options) {
45+
.Call('stars_CPL_gdalwarp', PACKAGE = 'stars', src, dst, options)
46+
}
47+
48+
CPL_gdalrasterize <- function(src, dst, options) {
49+
.Call('stars_CPL_gdalrasterize', PACKAGE = 'stars', src, dst, options)
50+
}
51+

R/dataset.R

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#' get metadata of a raster layer
2+
#'
3+
#' get metadata of a raster layer
4+
#' @name get_get_metadata
5+
#' @export
6+
#' @param file file name
7+
#' @param domain_item character vector of length 0, 1 (with domain), or 2 (with domain and item); use \code{""} for the default domain, use \code{NA_character_} to query the domain names.
8+
#' @param options character; character vector with data open options
9+
#' @param parse logical; should metadata be parsed into a named list (\code{TRUE}) or returned as character data?
10+
#' @return named list with metadata items
11+
#' @examples
12+
#' f = system.file("tif/L7_ETMs.tif", package="stars")
13+
#' st_get_metadata(f)
14+
#' st_get_metadata(f, NA_character_)
15+
#' # try(st_get_metadata(f, "wrongDomain"))
16+
#' # st_get_metadata(f, c("", "AREA_OR_POINT"))
17+
st_get_metadata = function(file, domain_item = character(0), options = character(0), parse = TRUE) {
18+
stopifnot(is.character(file))
19+
stopifnot(is.character(domain_item))
20+
stopifnot(length(domain_item) <= 2)
21+
stopifnot(is.character(options))
22+
if (length(domain_item) >= 1 && !is.na(domain_item[1]) &&
23+
!(domain_item[1] %in% CPL_GetMetadata(file, NA_character_, options)))
24+
stop("domain_item[1] not found in available metadata domains")
25+
p = CPL_GetMetadata(file, domain_item, options)
26+
if (!is.na(domain_item[1]) && parse)
27+
parse_metadata(p)
28+
else
29+
p
30+
}
31+
32+
parse_metadata = function(md) {
33+
lst = lapply(strsplit(md, "="), function(x) x[[2]])
34+
names(lst) = sapply(strsplit(md, "="), function(x) x[[1]])
35+
structure(lst, class = "gdal_metadata")
36+
}
37+
38+
#' @name get_get_metadata
39+
#' @param name logical; retrieve name of subdataset? If \code{FALSE}, retrieve description
40+
#' @export
41+
#' @return \code{st_get_subdatasets} returns a zero-length list if \code{file} does not have subdatasets.
42+
st_get_subdatasets = function(file, options = character(0), name = TRUE) {
43+
if (!("SUBDATASETS" %in% CPL_GetMetadata(file, NA_character_, options)))
44+
list()
45+
else {
46+
md = st_get_metadata(file, "SUBDATASETS", options, TRUE)
47+
if (name)
48+
md[seq(1, length(md), by = 2)]
49+
else
50+
md[seq(2, length(md), by = 2)]
51+
}
52+
}
53+
54+
#' read coordinate reference system from GDAL data set
55+
#' @param file character; file name
56+
#' @param options character; raster layer read options
57+
#' @return object of class \code{crs}, see \link[sf]{st_crs}.
58+
#' @export
59+
st_read_crs = function(file, options = character(0)) {
60+
ret = CPL_get_crs(file, options)
61+
ret$crs = sf::st_crs(wkt = ret$crs)
62+
ret
63+
}

R/init.R

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#' @importFrom sf sf_extSoftVersion
2+
#' @importFrom Rcpp evalCpp
3+
#' @useDynLib stars
4+
NULL
5+
6+
#' @importFrom magrittr %>%
7+
#' @export
8+
magrittr::`%>%`
9+
10+
.stars_cache <- new.env(FALSE, parent=globalenv())
11+
12+
.onLoad = function(libname, pkgname) {
13+
if (file.exists(system.file("proj/nad.lst", package = "stars")[1])) {
14+
# nocov start
15+
assign(".stars.PROJ_LIB", Sys.getenv("PROJ_LIB"), envir=.stars_cache)
16+
prj = system.file("proj", package = "stars")[1]
17+
Sys.setenv("PROJ_LIB" = prj)
18+
assign(".stars.GDAL_DATA", Sys.getenv("GDAL_DATA"), envir=.stars_cache)
19+
gdl = system.file("gdal", package = "stars")[1]
20+
Sys.setenv("GDAL_DATA" = gdl)
21+
# nocov end
22+
}
23+
CPL_gdal_init()
24+
}
25+
26+
.onUnload = function(libname, pkgname) {
27+
CPL_gdal_cleanup_all()
28+
if (file.exists(system.file("proj/nad.lst", package = "stars")[1])) {
29+
# nocov start
30+
Sys.setenv("PROJ_LIB"=get(".stars.PROJ_LIB", envir=.stars_cache))
31+
Sys.setenv("GDAL_DATA"=get(".stars.GDAL_DATA", envir=.stars_cache))
32+
# nocov end
33+
}
34+
}
35+
36+
.onAttach = function(libname, pkgname) {
37+
m = paste0("Linking to GDAL ", CPL_gdal_version(), ", proj.4 ", CPL_proj_version())
38+
packageStartupMessage(m)
39+
}
40+
41+
#' Provide the external dependencies versions of the libraries linked to stars
42+
#'
43+
#' Provide the external dependencies versions of the libraries linked to stars
44+
#' @export
45+
stars_extSoftVersion = function() {
46+
structure(c(CPL_gdal_version(), CPL_proj_version()),
47+
names = c("GDAL", "proj.4"))
48+
}
49+
50+
.x = sf_extSoftVersion()

R/utils.R

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#' Native interface to gdal utils
2+
#' @name gdal_utils
3+
#' @param name character; name of input layer
4+
#' @param source character; name(s) of input layer(s)
5+
#' @param destination character; name of output layer
6+
#' @param quiet logical; suppress printed output
7+
#' @param options layer opening options
8+
#' @return \code{st_gdalwarp} and \code{st_gdalrasterize} return \code{NULL} (invisibly) on success, an error message on failure.
9+
#' \code{st_gdalinfo} returns the raster metadata
10+
#' @export
11+
st_gdalinfo = function(name, options, quiet = FALSE) {
12+
ret = CPL_gdalinfo(name, options)
13+
if (! quiet)
14+
cat(ret)
15+
invisible(ret)
16+
}
17+
18+
#' @name gdal_utils
19+
#' @export
20+
st_gdalwarp = function(source, destination, options) {
21+
if (! CPL_gdalwarp(source, destination, options))
22+
stop("st_gdalwarp: an error occured")
23+
else
24+
invisible(NULL)
25+
}
26+
27+
#' @name gdal_utils
28+
#' @export
29+
st_gdalrasterize = function(source, destination, options) {
30+
if (! CPL_gdalrasterize(source, destination, options))
31+
stop("st_gdalrasterize: an error occured")
32+
else
33+
invisible(NULL)
34+
}

cleanup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
# rm -fr man R/Rcpp*R src/Rcpp*cpp
4+
rm -fr src/Makevars config.log config.status autom4te.cache src/*.o src/*.so
5+
rm -fr vignette/*html
6+
rm -fr proj_conf_test*
7+
rm -fr */Rplots.pdf

0 commit comments

Comments
 (0)