From c96e3a542cecf085d6d58fac97c81b8fc9f1f7f3 Mon Sep 17 00:00:00 2001 From: nuest <daniel.nuest@uni-muenster.de> Date: Tue, 14 Aug 2018 16:44:49 +0200 Subject: [PATCH] update sf test file, update vignettes --- .gitignore | 2 - .../package_markdown/sfr/2016-07-17-sf2.Rmd | 213 ----------- .../package_markdown/sfr/Dockerfile_original | 75 ---- tests/testthat/package_markdown/sfr/sf2.Rmd | 339 ++++++++++++++++++ .../package_markdown/spacetime/Dockerfile | 12 - .../spacetime/Dockerfile.filtered | 6 - .../package_markdown/spacetime/main.Rmd | 107 ------ tests/testthat/test_baseimage.R | 17 +- vignettes/container.Rmd | 5 +- 9 files changed, 351 insertions(+), 425 deletions(-) delete mode 100644 tests/testthat/package_markdown/sfr/2016-07-17-sf2.Rmd delete mode 100644 tests/testthat/package_markdown/sfr/Dockerfile_original create mode 100644 tests/testthat/package_markdown/sfr/sf2.Rmd delete mode 100644 tests/testthat/package_markdown/spacetime/Dockerfile delete mode 100644 tests/testthat/package_markdown/spacetime/Dockerfile.filtered delete mode 100644 tests/testthat/package_markdown/spacetime/main.Rmd diff --git a/.gitignore b/.gitignore index dec9edc..272c46e 100644 --- a/.gitignore +++ b/.gitignore @@ -32,8 +32,6 @@ tests/testthat/package_markdown/units/*\.html tests/testthat/Rplots\.pdf -tests/testthat/package_markdown/spacetime/main\.html - vignettes/sessionInfo\.Rdata sessionInfo\.Rdata diff --git a/tests/testthat/package_markdown/sfr/2016-07-17-sf2.Rmd b/tests/testthat/package_markdown/sfr/2016-07-17-sf2.Rmd deleted file mode 100644 index d7711d3..0000000 --- a/tests/testthat/package_markdown/sfr/2016-07-17-sf2.Rmd +++ /dev/null @@ -1,213 +0,0 @@ ---- -layout: post -title: "Simple features for R, part 2" -date: "Jul 18, 2016" -comments: true -author: Edzer Pebesma -categories: r ---- -<script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> - -[This ERC is based on the blog post [Simple features for R, part 2](http://r-spatial.org/r/2016/07/18/sf2.html), published under [CC-BY](http://creativecommons.org/licenses/by/4.0/) license.] - -# What happened so far? - -* in an earlier [blog post](http://r-spatial.org/r/2016/02/15/simple-features-for-r.html) I introduced the idea of having simple features mapped directly into simple R objects -* an R Consortium [ISC proposal](https://github.com/edzer/sfr/blob/master/PROPOSAL.md) to implement this got [granted](https://www.r-consortium.org/news/announcement/2016/03/r-consortium-funds-technical-initiatives-community-events-and-training) -* during [UseR! 2016](https://user2016.sched.org/event/7BRR/spatial-data-in-r-simple-features-and-future-perspectives) I presented this proposal ([slides](http://pebesma.staff.ifgi.de/pebesma_sfr.pdf)), which we followed up with an open discussion on future directions -* first steps to implement this in the [sf](https://github.com/edzer/sfr/) package have finished, and are described below - -This blog post describes current progress. - -# Install & test - -You can install package `sf` directly from github: -```{r eval=FALSE} -library(devtools) # maybe install first? -install_github("edzer/sfr", ref = "16e205f54976bee75c72ac1b54f117868b6fafbc") -``` -if you want to try out `read.sf`, which reads through GDAL 2.0, -you also need my fork of the R package [rgdal2](https://github.com/thk686/rgdal2), installed by -```{r eval=FALSE} -install_github("edzer/rgdal2") -``` -this, obviously, requires that GDAL 2.0 or later is installed, along with development files. - -After installing, a vignette contains some basic operations, and is shown by -```{r eval=FALSE} -library(sf) -vignette("basic") -``` - -# How does it work? - -Basic design ideas and constraints have been written in [this document](https://github.com/edzer/sfr/blob/master/DESIGN.md). - -Simple features are one of the following [17 types](https://en.wikipedia.org/wiki/Well-known_text): Point, -LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection, CircularString, CompoundCurve, CurvePolygon, MultiCurve, MultiSurface, Curve, Surface, PolyhedralSurface, TIN, and Triangle. Each type can have 2D points (XY), 3D points (XYZ), 2D points with measure (XYM) and 3D points with measure (XYZM). This leads to 17 x 4 = 68 combinations. - -The first seven of these are most common, and *have been implemented*, allowing for XY, XYZ, XYM and XYZM geometries. - -## Simple feature instances: `sfi` - -A single simple feature is created by calling the constructor function, along with a modifier in case a three-dimensional geometry has measure "M" as its third dimension: -```{r} -library(sf) -POINT(c(2,3)) -POINT(c(2,3,4)) -POINT(c(2,3,4), "M") -POINT(c(2,3,4,5)) -``` -what is printed is a [well kown text](https://en.wikipedia.org/wiki/Well-known_text) representation of the object; the data itself is however stored as a regular R vector or matrix: -```{r} -str(POINT(c(2,3,4), "M")) -str(LINESTRING(rbind(c(2,2), c(3,3), c(3,2)))) -``` - -By using the two simple rules that - -1. sets of points are kept in a `matrix` -1. other sets are kept in a `list` - -we end up with the following structures, with increasing complexity: - -### Sets of points (matrix): -```{r} -str(LINESTRING(rbind(c(2,2), c(3,3), c(3,2)))) -str(MULTIPOINT(rbind(c(2,2), c(3,3), c(3,2)))) -``` -### Sets of sets of points: -```{r} -str(MULTILINESTRING(list(rbind(c(2,2), c(3,3), c(3,2)), rbind(c(2,1),c(0,0))))) -outer = matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE) -hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE) -hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE) -str(POLYGON(list(outer, hole1, hole2))) -``` - -### Sets of sets of sets of points: -```{r} -pol1 = list(outer, hole1, hole2) -pol2 = list(outer + 12, hole1 + 12) -pol3 = list(outer + 24) -mp = MULTIPOLYGON(list(pol1,pol2,pol3)) -str(mp) -``` -### Sets of sets of sets of sets of points: -```{r} -str(GEOMETRYCOLLECTION(list(MULTIPOLYGON(list(pol1,pol2,pol3)), POINT(c(2,3))))) -``` -where this is of course a worst case: `GEOMETRYCOLLECTION` objects -with simpler elements have less nesting. - -### Methods for `sfi` - -The following methods have been implemented for `sfi` objects: -```{r} -methods(class = "sfi") -``` - -### Alternatives to this implementation - -1. Package [rgdal2](https://github.com/thk686/rgdal2) reads point sets not in a matrix, but into a list with numeric vectors named `x` and `y`. This is closer to the GDAL (OGR) data model, and would allow for easier disambiguation of the third dimension (`m` or `z`) in case of three-dimensional points. It is more difficult to select a single point, and requires validation of vector lenghts being identical. I'm inclined to keep using `matrix` for point sets. -1. Currently, `POINT Z` is of class `c("POINT Z", "sfi")`. An alternative would be to have it derive from `POINT`, i.e. give it class `c("POINT Z", "POINT", "sfi")`. This would make it easier to write methods for XYZ, XYM and XYZM geometries. This may be worth trying out. - -## Simple feature list columns: `sfc` - -Collections of simple features can be added together into a list. If all elements of this list - -* are of identical type (have identical class), or are a mix of `X` and `MULTIX` (with `X` being one of `POINT`, `LINESTRING` or `POLYGON`) -* have an identical coordinate reference system - -then they can be combined in a `sfc` object. This object - -* converts, if needed, `X` into `MULTIX` (this is also what PostGIS does), -* registers the coordinate reference system in attributes `epsg` and `proj4string`, -* has the bounding box in attribute `bbox`, and updates it after subsetting - -```{r} -ls1 = LINESTRING(rbind(c(2,2), c(3,3), c(3,2))) -ls2 = LINESTRING(rbind(c(5,5), c(4,1), c(1,2))) -sfc = sfc(list(ls1, ls2), epsg = 4326) -attributes(sfc) -attributes(sfc[1]) -``` - -The following methods have been implemented for `sfc` simple feature list columns: -```{r} -methods(class = "sfc") -``` - -## data.frames with simple features: `sf` - -Typical spatial data contain attribute values and attribute geometries. When combined in a table, they can be converted into `sf` objects, e.g. by -```{r} -roads = data.frame(widths = c(5, 4.5)) -roads$geom = sfc -roads.sf = sf(roads) -roads.sf -summary(roads.sf) -attributes(roads.sf) -``` -here, attribute `relation_to_geometry` allows documenting how attributes relate to the geometry: are they constant (field), aggregated over the geometry (lattice), or do they identify individual entities (buildings, parcels etc.)? - -The following methods have been implemented for `sfc` simple feature list columns: -```{r} -methods(class = "sf") -``` - - -## Coercion to and from `sp` - -Points, MultiPoints, Lines, MultiLines, Polygons and MultiPolygons can be converted between `sf` and [sp](https://cran.r-project.org/web/packages/sp/index.html), both ways. A round trip is demonstrated by: -```{r} -df = data.frame(a=1) -df$geom = sfc(list(mp)) -sf = sf(the_dockerfile) -library(methods) -a = as(sf, "Spatial") -class(a) -b = as.sf(a) -all.equal(sf, b) # round-trip sf-sp-sf -a2 = as(a, "SpatialPolygonsDataFrame") -all.equal(a, a2) # round-trip sp-sf-sp -``` - -## Reading through GDAL - -Function `read.sf` works, if `rgdal2` is installed (see above), and reads -simple features through GDAL: -```{r} -(s = read.sf(system.file("shapes/", package="maptools"), "sids"))[1:5,] -summary(s) -``` -This also shows the abbreviation of long geometries when printed or summarized, -provided by the `format` methods. - -The following works for me, with PostGIS installed and data loaded: -```{r, eval=FALSE} -(s = read.sf("PG:dbname=postgis", "meuse2"))[1:5,] -summary(s) -``` - -# Still to do/to be decided - -The following issues need to be decided upon: - -* reproject sf objects through `rgdal2`? support well-known-text for CRS? or use PROJ.4 directly? -* when subsetting attributes from an `sf` objects, make geometry sticky (like sp does), or drop geometry and return `data.frame` (data.frame behaviour)? - -The following things still need to be done: - -* write simple features through GDAL (using rgdal2) -* using gdal geometry functions in `rgdal2` -* extend rgdal2 to also read `XYZ`, `XYM`, and `XYZM` geometries - my feeling is that this will be easier than modifying `rgdal` -* reprojection of sf objects -* link to GEOS, using GEOS functions: GDAL with GEOS enabled (and `rgdal2`) has some of this, but not for instance `rgeos::gRelate` -* develop better and more complete test cases; also check the OGC [test suite](http://cite.opengeospatial.org/teamengine/) -* improve documentation, add tutorial (vignettes, paper) -* add plot functions (base, grid) -* explore direct WKB - sf conversion, without GDAL -* explore how meaningfulness of operations can be verified when for attributes their `relation_to_geometry` has been specified - -Please let me know if you have any comments, suggestions or questions! diff --git a/tests/testthat/package_markdown/sfr/Dockerfile_original b/tests/testthat/package_markdown/sfr/Dockerfile_original deleted file mode 100644 index 387a0fd..0000000 --- a/tests/testthat/package_markdown/sfr/Dockerfile_original +++ /dev/null @@ -1,75 +0,0 @@ -FROM rocker/tidyverse:3.3.2 -# user tidyverse so have littler install scripts and use MRAN -MAINTAINER Daniel NĂ¼st <daniel.nuest@uni-muenster.de> - -RUN apt-get update -qq \ - && apt-get install -y --no-install-recommends \ - wget \ - build-essential \ - ## Packages required by R extension packages - # for rmarkdown: - lmodern \ - pandoc \ - # for devtools (requires git2r, httr): - libcurl4-openssl-dev \ - libssl-dev \ - git \ - # for rgeos: - libgeos-3.4.2 \ - libgeos-dev \ - # required when knitting the document - pandoc-citeproc \ - # clean up apt - && apt-get clean \ - && rm -rf /var/lib/apt/lists/* - -# install latest GDAL and PROJ, based on https://github.com/edzer/sfr/blob/master/.travis.yml -WORKDIR /tmp/gdal -RUN wget http://download.osgeo.org/gdal/2.1.0/gdal-2.1.0.tar.gz \ - && tar zxf gdal-2.1.0.tar.gz \ - && cd gdal-2.1.0 \ - && ./configure \ - && make \ - && make install -WORKDIR /tmp/proj -RUN wget http://download.osgeo.org/proj/proj-4.9.3.tar.gz \ - && tar zxvf proj-4.9.3.tar.gz \ - && cd proj-4.9.3 \ - && ./configure \ - && make \ - && make install \ - && ldconfig -RUN rm -r /tmp/gdal /tmp/proj - -# install R extension packages -RUN install2.r -r "http://cran.rstudio.com" \ - rmarkdown \ - devtools \ - rgeos \ - maptools \ - && rm -rf /tmp/downloaded_packages/ /tmp/*.rd -RUN installGithub.r edzer/rgdal2 - -# install specific version of package to be demonstrated -RUN git clone https://github.com/edzer/sfr.git /sfr \ - && cd /sfr \ - && git reset --hard 16e205f54976bee75c72ac1b54f117868b6fafbc \ - && R --vanilla -e "devtools::install('/sfr')" - -# save installed packages to file -RUN dpkg -l > /dpkg-list.txt - -LABEL Description="This is an ERC image." \ - info.o2r.bag.id="332ed55649c2" - -COPY . /erc -WORKDIR /erc - -ENTRYPOINT ["sh", "-c"] -CMD ["R --vanilla -e \"rmarkdown::render(input = '/erc/2016-07-17-sf2.Rmd', output_format = rmarkdown::html_document())\""] - -# CMD on cli: -# R --vanilla -e "rmarkdown::render(input = '2016-07-17-sf2.Rmd', output_format = rmarkdown::html_document())" - -# docker build --tag markdowntainer-sfr . -# docker run -it markdowntainer-sfr \ No newline at end of file diff --git a/tests/testthat/package_markdown/sfr/sf2.Rmd b/tests/testthat/package_markdown/sfr/sf2.Rmd new file mode 100644 index 0000000..dd004e5 --- /dev/null +++ b/tests/testthat/package_markdown/sfr/sf2.Rmd @@ -0,0 +1,339 @@ +--- +title: "2. Reading, Writing and Converting Simple Features" +output: + html_document: + toc: true + toc_float: + collapsed: false + smooth_scroll: false + toc_depth: 2 +vignette: > + %\VignetteIndexEntry{2. Reading, Writing and Converting Simple Features} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r echo=FALSE, include=FALSE} +knitr::opts_chunk$set(collapse = TRUE) +if (file.exists("nc1.shp")) + file.remove("nc1.shp", "nc1.dbf", "nc1.shx") +``` + +This vignetted describes how simple features can be read in R +from files or databases, and how they can be converted to other +formats (text, [sp](https://cran.r-project.org/package=sp)) + +## Reading and writing through GDAL + +The Geospatial Data Abstraction Library +([GDAL](http://www.gdal.org/)) is the swiss army knife for spatial +data: it reads and writes vector and raster data from and to practically +every file format, or database, of significance. Package `sf` reads +and writes using GDAL by the functions `st_read` and `st_write`. + +The data model GDAL uses needs + +* a data source, which may be a file, directory, or database +* a layer, which is a single geospatial dataset inside a file or directory or e.g. a table in a database. +* the specification of a driver (i.e., which format) +* driver-specific reading or writing data sources, or layers + +This may sound complex, but it is needed to map to over 200 data +formats! Package `sf` tries hard to simplify this where possible +(e.g. a file contains a single layer), but this vignette will try +to point you to the options. + +### Using st_read + +As an example, we read the North Carolina counties SIDS dataset, +which comes shipped with the `sf` package by: + +```{r} +library(sf) +fname <- system.file("shape/nc.shp", package="sf") +fname +nc <- st_read(fname) +``` + +Typical users will use a file name with path for `fname`, or +first set R's working directory with `setwd()` and use file name +without path. + +We see here that a single argument is used to find both the +datasource and the layer. This works when the datasource contains a +single layer. In case the number of layers is zero (e.g. a database +with no tables), an error message is given. In case there are more layers +than one, the first layer is returned, but a message and a +warning are given: + +```{r eval=FALSE} +> st_read("PG:dbname=postgis") +Multiple layers are present in data source PG:dbname=postgis, reading layer `meuse'. +Use `st_layers' to list all layer names and their type in a data source. +Set the `layer' argument in `st_read' to read a particular layer. +Reading layer `meuse' from data source `PG:dbname=postgis' using driver `PostgreSQL' +Simple feature collection with 155 features and 12 fields +geometry type: POINT +dimension: XY +bbox: xmin: 178605 ymin: 329714 xmax: 181390 ymax: 333611 +epsg (SRID): 28992 +proj4string: +proj=sterea +lat_0=52.15616055555555 ... +Warning message: +In eval(substitute(expr), envir, enclos) : + automatically selected the first layer in a data source containing more than one. +``` + +The message points to the `st_layers` command, which lists the driver +and layers in a datasource, e.g. + +```{r eval=FALSE} +> st_layers("PG:dbname=postgis") +Driver: PostgreSQL +Available layers: + layer_name geometry_type features fields +1 meuse Point 155 12 +2 meuse_sf Point 155 12 +3 sids Multi Polygon 100 14 +4 meuse_tbl Point 155 13 +5 meuse_tbl2 Point 155 13 +> +``` + +A particular layer can now be read by e.g. +```{r eval=FALSE} +st_read("PG:dbname=postgis", "sids") +``` + +`st_layers` has the option to count the number of features in case +these are missing: some datasources (e.g. OSM xml files) do not +report the number of features, but need to be completely read for +this. GDAL allows for more than one geometry column for a feature +layer; these are reported by `st_layers`. + +In case a layer contains only geometries but no attributes (fields), +`st_read` still returns an `sf` object, with a geometry column only. + +We see that GDAL automatically detects the driver (file format) +of the datasource, by trying them all in turn. + +`st_read` follows the conventions of base R, similar to how it +reads tabular data into `data.frame`s. This means that character +data are read, by default as `factor`s. For those who insist +on retrieving character data as character vectors, the argument +`stringsAsFactors` can be set to `FALSE`: + +```{r eval=FALSE} +st_read(fname, stringsAsFactors = FALSE) +``` + +Alternatively, a user can set the global option `stringsAsFactors`, and this will have the same effect: + +```{r} +options(stringsAsFactors = FALSE) +st_read(fname) +``` + +### Using st_write + +To write a simple features object to a file, we need at least +two arguments, the object and a filename: + +```{r eval=FALSE} +st_write(nc, "nc1.shp") +``` + +The file name is taken as the data source name. The default for +the layer name is the basename (filename without path) of the the +data source name. For this, `st_write` needs to guess the driver. The +above command is, for instance, equivalent to: + +```{r} +st_write(nc, dsn = "nc1.shp", layer = "nc.shp", driver = "ESRI Shapefile") +``` + +How the guessing of drivers works is explained in the next section. + +### Guessing a driver for output + +The output driver is guessed from the datasource name, either from +its extension (`.shp`: `ESRI Shapefile`), or its prefix (`PG:`: +`PostgreSQL`). The list of extensions with corresponding driver +(short driver name) is: + +| extension| driver short name | +| ---------| -----------------------------------------------------| +| `bna` |`BNA` | +| `csv` |`CSV` | +| `e00` |`AVCE00` | +| `gdb` |`FileGDB` | +| `geojson`|`GeoJSON` | +| `gml` |`GML` | +| `gmt` |`GMT` | +| `gpkg` |`GPKG` | +| `gps` |`GPSBabel` | +| `gtm` |`GPSTrackMaker`| +| `gxt` |`Geoconcept` | +| `jml` |`JML` | +| `map` |`WAsP` | +| `mdb` |`Geomedia` | +| `nc` |`netCDF` | +| `ods` |`ODS` | +| `osm` |`OSM` | +| `pbf` |`OSM` | +| `shp` |`ESRI Shapefile` | +| `sqlite` |`SQLite` | +| `vdv` |`VDV` | +| `xls` |`xls` | +| `xlsx` |`XLSX` | + +The list with prefixes is: + +| prefix | driver short name | +| ----------| ------------------------------------------------------------------| +| `couchdb:`|`CouchDB` | +| `DB2ODBC:`|`DB2ODBC` | +| `DODS:` |`DODS` | +| `GFT:` |`GFT` | +| `MSSQL:` |`MSSQLSpatial` | +| `MySQL:` |`MySQL` | +| `OCI:` |`OCI` | +| `ODBC:` |`ODBC` | +| `PG:` |`PostgreSQL` | +| `SDE:` |`SDE` | + + +### Dataset and layer reading or creation options + +Various GDAL drivers have options that influences the reading +or writing process, for example what the driver should do when a +table already exists in a database: append records to the table +or overwrite it: + +```{r eval=FALSE} +st_write(st_as_sf(meuse), "PG:dbname=postgis", "meuse", + layer_options = "OVERWRITE=true") +``` + +In case the table exists and the option is not specified, the driver +will give an error. Driver-specific options are documented in the +driver manual of [gdal](http://www.gdal.org/ogr_formats.html). Multiple options can be +given by multiple strings in `options`. + +For `st_read`, there is only `options`; for `st_write`, one needs +to distinguish between `dataset_options` and `layer_options`, the +first related to opening a dataset, the second to creating layers +in the dataset. + +## Reading and writing directly to and from spatial databases + +Package `sf` supports reading and +writing from and to spatial databases using the `DBI` interface. +So far, testing has mainly be done with `PostGIS`, other databases +might work but may also need more work. An example of reading is: + +```{r eval=FALSE} +library(RPostgreSQL) +conn = dbConnect(PostgreSQL(), dbname = "postgis") +meuse = st_read(conn, "meuse") +meuse_1_3 = st_read(conn, query = "select * from meuse limit 3;") +dbDisconnect(conn) +``` + +We see here that in the second example a query is given. This +query may contain spatial predicates, which could be a way to work +through massive spatial datasets in R without having to read them +completely in memory. + +Similarly, tables can be written: + +```{r eval=FALSE} +conn = dbConnect(PostgreSQL(), dbname = "postgis") +st_write(conn, meuse, drop = TRUE) +dbDisconnect(conn) +``` + +Here, the default table (layer) name is taken from the object name +(`meuse`). Argument `drop` informs to drop (remove) the table +before writing; logical argument `binary` determines whether to +use well-known binary or well-known text when writing the geometry +(where well-known binary is faster and lossless). + +## Conversion to other formats: WKT, WKB, sp + +### Conversion to and from well-known text + +The usual form in which we see simple features printed is well-known text: + +```{r} +st_point(c(0,1)) +st_linestring(matrix(0:9,ncol=2,byrow=TRUE)) +``` + +We can create these well-known text strings explicitly using `st_as_text`: + +```{r} +x = st_linestring(matrix(0:9,ncol=2,byrow=TRUE)) +str = st_as_text(x) +x +``` + +We can convert back from WKT by using `st_as_sfc`: + +```{r} +st_as_sfc(str) +``` + +### Conversion to and from well-known binary + +Well-known binary is created from simple features by `st_as_binary`: + +```{r} +x = st_linestring(matrix(0:9,ncol=2,byrow=TRUE)) +(x = st_as_binary(x)) +class(x) +``` + +The object returned by `st_as_binary` is of class `WKB` and is +either a list with raw vectors, or a single raw vector. These +can be converted into a hexadecimal character vector using `rawToHex`: + +```{r} +rawToHex(x) +``` + +Converting back to `sf` uses `st_as_sfc`: + +```{r} +x = st_as_binary(st_sfc(st_point(0:1), st_point(5:6))) +st_as_sfc(x) +``` + +### Conversion to and from sp + +Spatial objects as maintained by package `sp` can be converted into +simple feature objects or geometries by `st_as_sf` and `st_as_sfc`, +respectively: + +```{r} +methods(st_as_sf) +methods(st_as_sfc) +``` + +An example would be: + +```{r} +library(sp) +data(meuse) +coordinates(meuse) = ~x+y +m.sf = st_as_sf(meuse) +opar = par(mar=rep(0,4)) +plot(m.sf) +``` + +Conversion of simple feature objects of class `sf` or `sfc` into corresponding +`Spatial*` objects is done using the `as` method, coercing to `Spatial`: + +```{r} +x = st_sfc(st_point(c(5,5)), st_point(c(6,9)), crs = 4326) +as(x, "Spatial") +``` diff --git a/tests/testthat/package_markdown/spacetime/Dockerfile b/tests/testthat/package_markdown/spacetime/Dockerfile deleted file mode 100644 index 325c4e9..0000000 --- a/tests/testthat/package_markdown/spacetime/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM rocker/r-ver:3.4.4 -LABEL maintainer="o2r" -RUN export DEBIAN_FRONTEND=noninteractive; apt-get -y update \ - && apt-get install -y gdal-bin \ - git-core \ - libgdal-dev \ - libproj-dev \ - pandoc \ - pandoc-citeproc -RUN ["install2.r", "abind", "ade4", "adehabitatLT", "adehabitatMA", "backports", "bdsmatrix", "bitops", "boot", "caTools", "CircStats", "cshapes", "deldir", "digest", "diveMove", "DoseFinding", "evaluate", "FNN", "foreign", "Formula", "geosphere", "goftest", "gstat", "htmltools", "intervals", "KernSmooth", "knitr", "lattice", "lmtest", "magrittr", "mapdata", "maps", "maptools", "MASS", "Matrix", "MatrixModels", "maxLik", "mgcv", "miscTools", "mvtnorm", "nlme", "plm", "plyr", "polyclip", "quadprog", "quantreg", "raster", "RColorBrewer", "Rcpp", "rgdal", "rmarkdown", "rpart", "rprojroot", "sandwich", "SEL", "sp", "spacetime", "SparseM", "spatstat", "spatstat.data", "spatstat.utils", "stringi", "stringr", "tensor", "trip", "uniReg", "xts", "yaml", "zoo"] -WORKDIR /payload/ -CMD ["R"] diff --git a/tests/testthat/package_markdown/spacetime/Dockerfile.filtered b/tests/testthat/package_markdown/spacetime/Dockerfile.filtered deleted file mode 100644 index 6190850..0000000 --- a/tests/testthat/package_markdown/spacetime/Dockerfile.filtered +++ /dev/null @@ -1,6 +0,0 @@ -FROM rocker/geospatial:3.4.4 -LABEL maintainer="o2r" -# Packages skipped because they are in the base image: abind, backports, bitops, boot, caTools, deldir, evaluate, FNN, foreign, Formula, geosphere, goftest, grid, gstat, htmltools, intervals, knitr, lattice, magrittr, mapdata, maps, maptools, MASS, Matrix, MatrixModels, mgcv, mvtnorm, nlme, parallel, plyr, polyclip, quantreg, raster, RColorBrewer, Rcpp, rgdal, rmarkdown, rpart, rprojroot, sandwich, sp, spacetime, SparseM, spatstat, spatstat.data, spatstat.utils, splines, stringi, stringr, tensor, tools, xts, yaml, zoo -RUN ["install2.r", "ade4", "adehabitatLT", "adehabitatMA", "bdsmatrix", "CircStats", "cshapes", "diveMove", "DoseFinding", "KernSmooth", "lmtest", "maxLik", "miscTools", "plm", "quadprog", "SEL", "trip", "uniReg"] -WORKDIR /payload/ -CMD ["R"] diff --git a/tests/testthat/package_markdown/spacetime/main.Rmd b/tests/testthat/package_markdown/spacetime/main.Rmd deleted file mode 100644 index 582609e..0000000 --- a/tests/testthat/package_markdown/spacetime/main.Rmd +++ /dev/null @@ -1,107 +0,0 @@ ---- -title: 'spacetime: Spatio-Temporal Data in R' -author: - - affiliation: University of Muenster, Institute for Geoiformatics, 52North - email: edzer.pebesma@uni-muenster.de - name: Edzer Pebesma - url: http://ifgi.uni-muenster.de/ -date: "13 November 2013" -abstract: | - This document describes classes and methods designed to deal with different types of spatio-temporal data in \proglang{R} implemented in the \proglang{R} package \pkg{spacetime}, and provides examples for analyzing them. It builds upon the classes and methods for spatial data from package \pkg{sp}, and for time series data from package \pkg{xts}. The goal is to cover a number of useful representations for spatio-temporal sensor data, and results from predicting (spatial and/or temporal interpolation or smoothing), aggregating, or subsetting them, and to represent trajectories. The goals of this paper are to explore how spatio-temporal data can be sensibly represented in classes, and to find out which analysis and visualisation methods are useful and feasible. We discuss the time series convention of representing time intervals by their starting time only. This vignette is the main reference for the \proglang{R} package \code{spacetime}; it has been published as Pebesma (2012), but is kept up-to-date with the software. -output: html_document ---- - -**This file only contains the library and file loading/saving statements of the spacetime vignette.** -This probably works on a developer system, but does not work in a plain `rocker/geospatial`-based container. - -``` -daniel@gin-nuest:~/git/o2r/containerit/tests/testthat/package_markdown/spacetime$ docker run --rm -it -v $(pwd):/erc o2rproject/containerit:geospatial - -R version 3.4.3 [[...] - -> dockerfile(from = "/erc") -INFO [2018-03-23 10:15:11] Found file for packaging in workspace: /erc/main.Rmd -INFO [2018-03-23 10:15:11] Processing the given file d locally using rmarkdown::render(...) -INFO [2018-03-23 10:15:11] Creating an R session with the following arguments: - R --silent --vanilla -e "rmarkdown::render(\"/erc/main.Rmd\")" -e "info <- sessionInfo()" -e "save(list = \"info\", file = \"/tmp/RtmpKdYYco/rdata-sessioninfo125c083b0\")" -> rmarkdown::render("/erc/main.Rmd") - - -processing file: main.Rmd - |................ | 25% - ordinary text without R code - - |................................ | 50% -label: manual_install - |................................................. | 75% - ordinary text without R code - - |.................................................................| 100% -label: libraries_and_data -Quitting from lines 27-73 (main.Rmd) -Error in library("plm") : there is no package called 'plm' -Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> library - -Execution halted -Error in obtain_localSessionInfo(rmd_file = file, vanilla = vanilla, slave = silent, : - Failed to execute the script locally! A sessionInfo could not be determined. -> -``` - -```{r manual_install} -#install.packages(c("diveMove", "trip", "adehabitatLT", "plm", "cshapes")) -#if (!require("plm")) install.packages("plm", repos = "http://cloud.r-project.org") -#if (!require("diveMove")) install.packages("diveMove", repos = "http://cloud.r-project.org") -#if (!require("trip")) install.packages("trip", repos = "http://cloud.r-project.org") -#if (!require("adehabitatLT")) install.packages("adehabitatLT", repos = "http://cloud.r-project.org") -#if (!require("cshapes")) install.packages("cshapes", repos = "http://cloud.r-project.org") -``` - -```{r libraries_and_data} -library("foreign") -read.dbf(system.file("shapes/sids.dbf", package = "maptools")) - -library("plm") -data("Produc", package = "plm") -#write.csv(Produc[1:5,1:9], "producSubset.csv") -#read.csv("producSubset.csv") -#read.csv("windSubset.csv") - -library("sp") -library("spacetime") - -library("gstat") -data("wind") - -library("mapdata") - -library("rgdal") - -library("lattice") -library("RColorBrewer") - -library(xts) - -library("maptools") -fname = system.file("shapes/sids.shp", package = "maptools")[1] -nc = readShapePoly(fname, proj4string = CRS("+proj=longlat +datum=NAD27")) - -library("maps") -library("maptools") - -data("Produc") -library("RColorBrewer") - -library("maptools") - -library("lattice") -library("RColorBrewer") - -library("diveMove") -library("trip") - -library("adehabitatLT") -data("puechabonsp") - -library("cshapes") -``` diff --git a/tests/testthat/test_baseimage.R b/tests/testthat/test_baseimage.R index a5a9e1d..b8453cb 100644 --- a/tests/testthat/test_baseimage.R +++ b/tests/testthat/test_baseimage.R @@ -25,15 +25,16 @@ test_that("installed packages are a data.frame with the image as an attribute", }) test_that("list of installed packages can be filtered when creating a Dockerfile", { - the_dockerfile <- dockerfile(from = "package_markdown/spacetime/", + the_dockerfile <- dockerfile(from = "package_markdown/sfr/", maintainer = "o2r", image = "rocker/geospatial:3.4.4", filter_baseimage_pkgs = TRUE) - expect_true(object = any(grepl("# Packages skipped", x = toString(the_dockerfile))), info = "Packages skipped are mentioned in a comment") - # these packages are not in rocker/r-ver: - expect_true(object = any(grepl("^#.*gstat", x = toString(the_dockerfile))), info = "gstat is filtered") - expect_true(object = any(grepl("^#.*raster", x = toString(the_dockerfile))), info = "raster is filtered") - # these packages are not in rocker/geospatial - expect_true(object = any(grepl("^RUN.*\"adehabitatLT\"", x = toString(the_dockerfile))), info = "adehabitatLT is not filtered") - expect_true(object = any(grepl("^RUN.*\"trip\"", x = toString(the_dockerfile))), info = "trip is not filtered") + the_dockerfile_string <- toString(the_dockerfile) + + expect_true(object = any(grepl("# Packages skipped", x = the_dockerfile_string)), info = "Packages skipped are mentioned in a comment") + expect_true(object = any(grepl("^#.*rgdal", x = the_dockerfile_string)), info = "rgdal") + expect_true(object = any(grepl("^#.*spData", x = the_dockerfile_string)), info = "spData") + + unlink("package_markdown/sfr/nc1.*") + unlink("package_markdown/sfr/*.html") }) diff --git a/vignettes/container.Rmd b/vignettes/container.Rmd index 3039770..013aa6c 100644 --- a/vignettes/container.Rmd +++ b/vignettes/container.Rmd @@ -85,8 +85,9 @@ The following command executed in the `containerit` source directory creates an docker build --file inst/docker/local/Dockerfile --tag containerit . ``` -You can try the `predetect` feature by packaging the R Markdown file of the spacetime vignette: +You can try the `predetect` feature by packaging the R Markdown file of the second spacetime vignette with the non-`geospatial` image of containerit that was just built. +The following command will install missing packages before running the file, namely `sf` itself, but it's installation will fail due to missing system dependencies, which are not covered by predetection. ```{bash, eval=FALSE} -docker run --rm -it -v $(pwd)/tests/testthat/package_markdown/spacetime:/erc -e CONTAINERIT_FLOG_THRESHOLD=DEBUG containerit R -e "setwd('/erc'); print(dockerfile(from = '/erc'))" +docker run --rm -i -v $(pwd)/tests/testthat/package_markdown/sfr:/erc -e CONTAINERIT_FLOG_THRESHOLD=DEBUG containerit R --quiet -e "setwd('/erc'); list.files(); print(dockerfile(from = '.'))" ```