-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathREADME.Rmd
267 lines (191 loc) · 4.96 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
---
output: github_document
---
<!-- badges: start -->
[![CRAN](http://www.r-pkg.org/badges/version/rsgeo)](https://cran.r-project.org/package=rsgeo)
[![R-CMD-check](https://github.com/JosiahParry/rsgeo/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/JosiahParry/rsgeo/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rsgeo
`rsgeo` is an interface to the Rust libraries `geo-types` and `geo`. `geo-types` implements pure rust geometry primitives. The `geo` library adds additional algorithm functionalities on top of `geo-types`. This package lets you harness the speed, safety, and memory efficiency of these libraries. `geo-types` does not support Z or M dimensions. There is no support for CRS at this moment.
```{r}
# install.packages(
# 'rsgeo',
# repos = c('https://josiahparry.r-universe.dev', 'https://cloud.r-project.org')
# )
library(rsgeo)
```
rsgeo works with vectors of geometries. When we compare this to `sf` this is always the geometry column which is a class `sfc` object (simple feature column).
```{r}
# get geometry from sf
data(guerry, package = "sfdep")
polys <- guerry[["geometry"]] |>
sf::st_cast("POLYGON")
# cast to rust geo-types
rs_polys <- as_rsgeo(polys)
head(rs_polys)
```
Cast geometries to sf
```{r}
sf::st_as_sfc(rs_polys)
```
Calculate the unsigned area of polygons.
```{r}
bench::mark(
rust = unsigned_area(rs_polys),
sf = sf::st_area(polys),
check = FALSE
)
```
Find centroids
```{r}
bench::mark(
centroids(rs_polys),
sf::st_centroid(polys),
check = FALSE
)
```
Extract points coordinates
```{r}
coords(rs_polys) |>
head()
```
Plot the polygons and their centroids
```{r}
plot(rs_polys)
plot(centroids(rs_polys), add = TRUE)
```
Calculate a distance matrix. Note that there is often floating point error differences so `check = FALSE` in this case.
```{r}
pnts <- centroids(rs_polys)
pnts_sf <- sf::st_as_sfc(pnts)
bench::mark(
rust = distance_euclidean_matrix(pnts, pnts),
sf = sf::st_distance(pnts_sf, pnts_sf),
check = FALSE
)
```
Simplify geometries.
```{r}
x <- rs_polys
x_simple <- simplify_geoms(x, 5000)
plot(x_simple)
```
```{r}
bench::mark(
rust = simplify_geoms(rs_polys, 500),
sf = sf::st_simplify(polys, FALSE, 500),
check = FALSE
)
```
Union geometries with `union_geoms()`. Some things sf is better at! One of which is performing unary unions of complex geometries.
```{r}
plot(union_geoms(rs_polys))
bench::mark(
union_geoms(rs_polys),
sf::st_union(polys),
check = FALSE
)
```
We can cast between geometries as well.
```{r}
lns <- cast_geoms(rs_polys, "linestring")
```
Some unions are faster when using rsgeo vectors like linestrings.
```{r}
lns_sf <- sf::st_cast(polys, "LINESTRING")
bench::mark(
union_geoms(lns),
sf::st_union(lns_sf),
check = FALSE
)
```
Find the closest point to a geometry
```{r}
close_pnt <- closest_point(
rs_polys,
geom_point(800000, 2090000)
)
plot(rs_polys[1])
plot(close_pnt, pch = 15, add = TRUE)
```
Find the haversine destination of a point, bearing, and distance. Compare to the very fast geosphere destination point function.
```{r}
bench::mark(
rust = haversine_destination(geom_point(10, 10), 45, 10000),
Cpp = geosphere::destPoint(c(10, 10), 45, 10000),
check = FALSE
)
```
```{r}
origin <- geom_point(10, 10)
destination <- haversine_destination(origin, 45, 10000)
plot(c(origin, destination), col = c("red", "blue"))
```
Find intermediate point on a great circle.
```{r}
middle <- haversine_intermediate(origin, destination, 1/2)
plot(origin)
plot(destination, add = TRUE, col = "red")
plot(middle, add = TRUE, col = "blue")
```
<!-- Utilize the chaikin smoothing algorithm with 5 iterations. -->
<!-- ```{r} -->
<!-- region <- rs_polys[[2]] -->
<!-- plot(chaikin_smoothing(region, 5)) -->
<!-- ``` -->
Find extreme coordinates with `extreme_coords()`
```{r}
france <- union_geoms(rs_polys)
plot(france)
plot(extreme_coords(france)[[1]], add = TRUE, pch = 15)
```
Get bounding rectangles
```{r}
rects <- bounding_rect(rs_polys)
plot(rects)
```
Convex hulls
```{r}
convex_hull(rs_polys) |>
plot()
```
Expand into constituent geometries as a list of geometry vectors
```{r}
expand_geoms(rs_polys) |>
head()
```
We can flatten the resultant geometries into a single vector using `flatten_geoms()`
```{r}
expand_geoms(rs_polys) |>
flatten_geoms() |>
head()
```
Combine geometries into a single multi- geometry
```{r}
combine_geoms(lns)
```
Spatial predicates
```{r}
x <- rs_polys[1:5]
intersects_sparse(x, rs_polys)
```
<!-- Convert to and from wkb and wkt -->
<!-- ```{r} -->
<!-- wkt <- wkt_from_geoms(x) -->
<!-- wkt_to_geoms(wkt) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- wkb <- wkb_from_geoms(x) -->
<!-- head(wkb[[1]]) -->
<!-- wkb_to_geoms(wkb) -->
<!-- ``` -->
#### Notes
Right now plotting is done using `wk` by first casting the rsgeo into an sfc object.