This repository has been archived by the owner on Oct 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
82 lines (56 loc) · 2.35 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
[](https://travis-ci.org/mdsumner/scsf)
[](https://ci.appveyor.com/project/mdsumner/scsf)
[](https://codecov.io/github/mdsumner/scsf?branch=master)
# scsf
Convert simple features to a generic common form that is more general and can be used for a wide variety of data structures.
This is *work in progress* and at a very early stage. More to come.
## Example - sf to ggplot2 round trip
```{r}
library(sf)
## a MULTIPOLYGON layer
nc = st_read(system.file("shape/nc.shp", package="sf"))
```
The common form is the entity tables, objects, paths, vertices and a link table to allow de-duplication of shared vertices. Currently this de-duplication is done on all coordinate fields, but for most applications it will usually be done only in X-Y.
```{r}
library(scsf)
nc = st_read(system.file("gpkg/nc.gpkg", package="sf"))
(bmodel <- PATH(nc))
```
Prove that things work by round-tripping to the PATH model and onto the old fortify approach for `ggplot2`.
```{r}
inner_cascade <- function(x) {
tabnames <- silicate:::join_ramp(x)
tab <- x[[tabnames[1]]]
for (ni in tabnames[-1L]) tab <- dplyr::inner_join(tab, x[[ni]])
tab
}
## this just joins everything back together in one big fortify table
library(dplyr)
tab <- bmodel %>% inner_cascade()
library(ggplot2)
ggplot(tab) + aes(x = x_, y = y_, group = path) +
geom_polygon(aes(fill = AREA)) + geom_path(lwd = 2, col = "black")
```
What about polygons with holes and lots of tiny complicated parts.
```{r}
data("inlandwaters")
iw <- PATH(inlandwaters)
str(iw)
tab <- iw %>% inner_cascade()
library(ggplot2)
ggplot(tab) + aes(x = x_, y = y_, group = path) +
ggpolypath::geom_polypath(aes(fill = Province)) + geom_path(col = "black")
ggplot(tab %>% filter(Province == "South Australia")) + aes(x = x_, y = y_, group = path) +
ggpolypath::geom_polypath(fill = "dodgerblue") + geom_path(col = "black") + coord_fixed()
```