-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.Rmd
executable file
·206 lines (147 loc) · 6.87 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
dev = "svg"
)
```
# prepr <img src="man/figures/hex-prepr.png" align="right" height="139" />
<!-- badges: start -->
[![GitLab CI Build Status](https://gitlab.com/dickoa/prepr/badges/master/pipeline.svg)](https://gitlab.com/dickoa/prepr/pipelines)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/gitlab/dickoa/prepr?branch=master&svg=true)](https://ci.appveyor.com/project/dickoa/prepr)
[![Codecov Code Coverage](https://codecov.io/gl/dickoa/prepr/branch/master/graph/badge.svg)](https://codecov.io/gl/dickoa/prepr)
[![CRAN status](http://www.r-pkg.org/badges/version/prepr)](http://www.r-pkg.org/pkg/prepr)
<!-- badges: end -->
An R package to repair broken GIS polygons using the [`prepair`](https://github.com/tudelft3d/prepair) C++ library.
## Installation
The `prepair` C++ library need these two libraries to compile:
- [`CGAL`](https://www.cgal.org/)
- [`GDAL`](https://gdal.org/)
The R package `prepr` solves the CGAL dependencies by shipping with a subset of the CGAL header. We also use [`rwinlib`](https://github.com/rwinlib) to provide `GDAL` on Windows in order to build this package from source. You will need the latest version of [`rtools`](https://cran.r-project.org/bin/windows/Rtools/) in order to build the source code on Windows.
`prepair` can also use these optional libraries:
- [`GMP`](https://gmplib.org/)
- [`MPFR`](https://www.mpfr.org/)
They are disabled by default on Windows but required if you want to build the package in a Linux/OS X environment.
After installing all these libraries, you can now install the development version of the `prepr` R package from [Gitlab](https://gitlab.com/dickoa/prepr) using the `remotes` R package with:
```{r, eval = FALSE}
# install.packages("remotes")
remotes::install_gitlab("dickoa/prepr")
remotes::install_github("dickoa/prepr") ## mirror
```
## A quick tutorial
This is a simple tutorial which shows you how to solve common problems we can find with polygons:
### A 'bowtie' polygon:
```{r p1}
library(prepr)
library(sf)
p1 <- st_as_sfc("POLYGON((0 0, 0 10, 10 0, 10 10, 0 0))")
st_is_valid(p1, reason = TRUE)
p11 <- st_prepair(p1)
st_is_valid(p11)
st_as_text(p11)
par(mfrow = c(1, 2))
plot(p1, main = "RAW", col = "#D7722C")
plot(p11, main = "Repaired", col = "#D7722C")
```
### Square with wrong orientation:
```{r p2}
p2 <- st_as_sfc("POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))")
st_is_valid(p2, reason = TRUE)
plot(p2, main = "RAW", col = "#D7722C")
```
### Inner ring with one edge sharing part of an edge of the outer ring:
```{r p3}
p3 <- st_as_sfc("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0),(5 2, 5 7, 10 7, 10 2, 5 2))")
st_is_valid(p3, reason = TRUE)
p33 <- st_prepair(p3)
st_is_valid(p33)
st_as_text(p33)
par(mfrow = c(1, 2))
plot(p3, main = "RAW", col = "#D7722C")
plot(p33, main = "Repaired", col = "#D7722C")
```
### Dangling edge:
```{r p4}
p4 <- st_as_sfc("POLYGON((0 0, 10 0, 15 5, 10 0, 10 10, 0 10, 0 0))")
st_is_valid(p4, reason = TRUE)
p44 <- st_prepair(p4)
st_is_valid(p44)
st_as_text(p44)
par(mfrow = c(1, 2))
plot(p4, main = "RAW", col = "#D7722C")
plot(p44, main = "Repaired", col = "#D7722C")
```
### Two adjacent inner rings:
```{r p6}
p6 <- st_as_sfc("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 8, 3 8, 3 1, 1 1), (3 1, 3 8, 5 8, 5 1, 3 1))")
st_is_valid(p6, reason = TRUE)
p66 <- st_prepair(p6)
st_is_valid(p66)
st_as_text(p66)
par(mfrow = c(1, 2))
plot(p6, main = "RAW", col = "#D7722C")
plot(p66, main = "Repaired", col = "#D7722C")
```
### Polygon with an inner ring inside another inner ring:
```{r p7}
p7 <- st_as_sfc("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 8, 5 8, 5 2, 2 2, 2 8), (3 3, 4 3, 3 4, 3 3))")
st_is_valid(p7, reason = TRUE)
p77 <- st_prepair(p7)
st_is_valid(p77)
st_as_text(p77)
par(mfrow = c(1, 2))
plot(p7, main = "RAW", col = "#D7722C")
plot(p77, main = "Repaired", col = "#D7722C")
```
<!-- ## A exemple with a real dataset -->
<!-- ### Reading the data -->
<!-- ```{r read_data, cache = TRUE} -->
<!-- (clc1 <- read_sf("https://github.com/tudelft3d/prepair/raw/master/data/CLC2006_180927.geojson")) -->
<!-- (clc2 <- read_sf("https://github.com/tudelft3d/prepair/raw/master/data/CLC2006_2018418.geojson")) -->
<!-- par(mfrow = c(1, 2)) -->
<!-- plot(st_geometry(clc1), main = "CLC2006_180927", col = 'lightblue', axes = TRUE, graticule = TRUE, lwd = 0.2, cex.axis = 0.5) -->
<!-- plot(st_geometry(clc2), main = "CLC2006_2018418", col = "lightblue", axes = TRUE, graticule = TRUE, lwd = 0.2, cex.axis = 0.5) -->
<!-- ``` -->
<!-- ### Check if it's valid and repair it -->
<!-- ```{r clean_it, cache = TRUE} -->
<!-- st_is_valid(clc1, reason = TRUE) -->
<!-- (clc1_rpr <- st_prepair(clc1)) -->
<!-- st_is_valid(clc1_rpr) -->
<!-- st_is_valid(clc2, reason = TRUE) -->
<!-- (clc2_rpr <- st_prepair(clc2)) -->
<!-- st_is_valid(clc2_rpr) -->
<!-- ``` -->
<!-- ### How fast is `st_prepair` ? -->
<!-- `prepr::st_prepair` is fast and can be in some cases faster than `sf::st_make_valid` -->
<!-- ```{r bench, cache = TRUE} -->
<!-- (bnch1 <- bench::mark(st_make_valid(clc1), st_prepair(clc1), check = FALSE)) -->
<!-- summary(bnch1, relative = TRUE) -->
<!-- (bnch2 <- bench::mark(st_make_valid(clc2), st_prepair(clc2), check = FALSE)) -->
<!-- summary(bnch2, relative = TRUE) -->
<!-- ``` -->
<!-- You also have cases where it's slower to `sf::st_make_valid`, let's use this data from a [closed issue](https://github.com/r-spatial/sf/issues/1280) in the `sf` R package. -->
<!-- ```{r new_data, cache = TRUE} -->
<!-- ## need vsicurl -->
<!-- (agb <- read_sf("/vsicurl/http://files.hawaii.gov/dbedt/op/gis/data/2015AgBaseline.shp.zip")) -->
<!-- all(st_is_valid(agb)) -->
<!-- all(st_is_valid(st_make_valid(agb))) -->
<!-- all(st_is_valid(st_prepair(agb))) -->
<!-- plot(st_geometry(agb), main = "2015 Agriculture baseline", col = 'lightblue', axes = TRUE, graticule = TRUE, lwd = 0.2, cex.axis = 0.5) -->
<!-- ``` -->
<!-- `sf::st_make_valid` is faster with this data. -->
<!-- ```{r bench2, cache = TRUE} -->
<!-- (bnch3 <- bench::mark(st_make_valid(agb), st_prepair(agb), check = FALSE)) -->
<!-- summary(bnch3, relative = TRUE) -->
<!-- ``` -->
## Details and how to cite
Details of how we automatically repair broken polygons, and what results you can expect, are available in this scientific article by the original authors of `prepair`:
> Ledoux, H., Arroyo Ohori, K., and Meijers, M. (2014). A triangulation-based approach to automatically repair GIS polygons. *Computers & Geosciences* 66:121–131. [ [DOI] ](http://dx.doi.org/10.1016/j.cageo.2014.01.009) [ [PDF] ](http://3dgeoinfo.bk.tudelft.nl/hledoux/pdfs/14_cgeo_prepair.pdf)
If you use the R package `prepr` for a scientific project, please cite their original work.
## License
This package is released under the GPL-3 license.