knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

discrete

The goal of discrete is to ...

Installation

You can install discrete from github with:

# install.packages("devtools")
devtools::install_github("hypertidy/discrete")

Example

This is a basic example:

#f <- head(raadtools::sstfiles()$fullname, 1)
f <- 'NETCDF:"/rdsi/PUBLIC/raad/data/eclipse.ncdc.noaa.gov/pub/OI-daily-v2/NetCDF/1981/AVHRR/avhrr-only-v2.19810901.nc":sst'
#f <-  "/rdsi/PUBLIC/raad/data/www.ngdc.noaa.gov/mgg/global/relief/ETOPO2/ETOPO2v2-2006/ETOPO2v2c/netCDF/ETOPO2v2c_f4.nc"
library(vapour)
ri <- raster_info(f)
library(sf)
tiles <- tile_gon(transform6(ri$geotransform, ri$dimXY/240))


tiles$raster_data <- populate_raster(nrow(tiles))
library(spex)
extent <- function(x, ...) UseMethod("extent")
extent.sfc_POLYGON <- function(x, ...) raster::extent(attr(x, "bbox")[c("xmin", "xmax", "ymin", "ymax")])

itile <- 1
library(sf)
gs <- gdal_source(r, st_geometry(tiles[itile, ]))
library(vapour)
tiles$raster_data <- populate_raster(nrow(tiles), a$tile, raster_io(f, c(gs, c(diff(gs[c(1, 3)])+1, diff(gs[c(2, 3)])+1))))
tiles$tile <- seq_len(nrow(tiles))
par(mfrow = c(2, 1))
plot(crop(r, tiles[itile, ]), asp = "")
plot(tiles$raster_data[[itile]]$data, pch = ".")


library(mapedit)
a <- selectFeatures(tiles)
gs <- gdal_source(r, st_geometry(a))
tiles$raster_data <- populate_raster(nrow(tiles), a$tile, raster_io(f, c(gs, c(diff(gs[c(1, 3)])+1, diff(gs[c(2, 3)])+1))))
par(mfrow = c(2, 1))
plot(crop(r, tiles[itile, ]), asp = "")
plot(tiles$raster_data[[a$tile]]$data, pch = ".")

More notes

raster2 - raster has good names for things

## projection(), extent(), geometry() 

# fundamentals: 
## point-in-poly
## path-reconstruction-from-edges
## distance-to and distance-from
## discretization and other forms of coordinate transformation
## conversion to edge or mesh forms

# each data set has a background, like spatstat
## default is simply the bbox
## can be discretized, axes


projection <- function(x, ...) {
  UseMethod("projection")
}
projection.default <- function(x, ...) {
  raster::projection(x)
}
projection.BasicRaster <- function(x, ...) {
  x@crs
}

projection.sf <- function(x, ...) {
  projection(x[[attr(x, "sf_column")]])
}
projection.sfc <- function(x, ...) {
  attr(x, "crs")$proj4string
}

projection.Spatial <- function(x, ...) {
  x@proj4string@projargs
}

extent <- function(x, ...) {
  UseMethod("extent")
}
extent.BasicRaster <- function(x, ...) {
  e <- x@extent
  tibble::tibble(xmin = e@xmin, xmax = e@xmax, ymin = e@ymin, ymax = e@ymax)
}
extent.numeric <- function(x, ...) {
  nms <- c("xmin", "xmax", "ymin", "ymax")
  if (all(nms) %in% names(x)) {
    x <- x[nms]
  } 
  tibble::as_tibble(as.list(x))
}
extent.sf <- function(x, ...) {
  extent(geometry(x))
}
extent.sfc <- function(x, ...) {
  extent(attr(x, "bbox")[c("xmin", "xmax", "ymin", "ymax")])
}
geometry <- function(x, ...) {
 UseMethod("geometry") 
}
geometry.Spatial <- function(x, ...) {
  sp::geometry(x)
}
geometry.sf <- function(x, ...) {
  x[[attr(x, "sf_column")]]
}
geometry.BasicRaster <- function(x, ...) {
  cbind(extent(x), )
}
sf_column <- sf_geometry <- geometry.sf


hypertidy/discrete documentation built on May 9, 2019, 2:27 p.m.