-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
156 lines (124 loc) · 5.86 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
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-"
)
```
# rosmium
[![CRAN
status](https://www.r-pkg.org/badges/version/rosmium)](https://CRAN.R-project.org/package=rosmium)
[![B
status](https://github.com/ipeaGIT/rosmium/workflows/check/badge.svg)](https://github.com/ipeaGIT/rosmium/actions?query=workflow%3Acheck)
[![Codecov test
coverage](https://codecov.io/gh/ipeaGIT/rosmium/branch/main/graph/badge.svg)](https://app.codecov.io/gh/ipeaGIT/rosmium?branch=main)
[![Lifecycle:
experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html)
**rosmium** allows one to use [Osmium Tool](https://osmcode.org/osmium-tool/)
from R. Osmium is a multipurpose command line tool that enables one to
manipulate and analyze OpenStreetMap (OSM) files through several different
commands. Currently, this package does not aim to offer functions that cover the
entirety of Osmium's API, instead making available functions that wrap only a
very limited set of Osmium's features.
## Installation
Development version:
```r
# install.packages("remotes")
remotes::install_github("ipeaGIT/rosmium")
```
Please note that **rosmium** requires Osmium to be installed and added to the
PATH environment variable of your local system. For instructions on how to
install Osmium, please check [its official
website](https://osmcode.org/osmium-tool/#download).
## Usage
The package currently includes only three entrypoints to Osmium's API. To
demonstrate them, we will use some sample data bundled with the package.
```{r, eval = requireNamespace("ggplot2", quietly = TRUE), fig.width = 6, fig.height = 5.5}
library(rosmium)
library(ggplot2)
cur_pbf <- system.file("extdata/cur.osm.pbf", package = "rosmium")
cur_pbf_lines <- sf::st_read(cur_pbf, layer = "lines", quiet = TRUE)
ggplot(cur_pbf_lines) + geom_sf()
```
### `extract()`
`extract()` creates geographical extracts from OSM files. In its most basic
form, the function takes the path to the OSM file whose geographical extent
should be extracted from, the extent, either as a bounding box or as a
(multi)polygon, and the path to the file where the output should be written to.
Additionally, the function can also take the strategy to be used when creating
the extract, which defaults to `"complete_ways"`. Please check the function
documentation for details on the available strategies and their behavior.
```{r, eval = requireNamespace("ggplot2", quietly = TRUE), fig.width = 6, fig.height = 4.5}
# buffering the pbf bounding box 4000 meters inward and using the result
# extent to extract the osm data inside it. transforming the crs because
# inward buffers only work with projected crs
bbox <- sf::st_bbox(cur_pbf_lines)
bbox_polygon <- sf::st_as_sf(sf::st_as_sfc(bbox))
smaller_bbox_poly <- sf::st_buffer(sf::st_transform(bbox_polygon, 5880), -4000)
smaller_bbox_poly <- sf::st_transform(smaller_bbox_poly, 4326)
output_path <- extract(
cur_pbf,
smaller_bbox_poly,
tempfile(fileext = ".osm.pbf"),
spinner = FALSE
)
extracted_pbf_lines <- sf::st_read(output_path, layer = "lines", quiet = TRUE)
ggplot() +
geom_sf(data = extracted_pbf_lines) +
geom_sf(data = smaller_bbox_poly, color = "red", fill = NA)
```
### `tags_filter()`
`tags_filter()` filters OSM files, keeping objects matching at least one of the
specified expressions from the input. In its most basic form, the function takes
the path to the OSM file to which the filters should be applied, the filter
expressions that should be applied and the path to the file where the output
should be written to. Please check the function documentation for a description
of the filter expression format.
By default, not only the objects matching the expressions will be kept in the
output, but also the objects referenced by them. This behavior can be changed
with the `omit_referenced` parameter. The function also includes the
`invert_match` parameter, that inverts the sense of matching (excluding objects
that match the filters), and the `remove_tags` parameter, used to remove tags
from objects that are referenced by objects matching the filters, but which do
not match the filter themselves. Both arguments default to `FALSE`.
```{r}
# get all amenity nodes
output <- tags_filter(cur_pbf, "n/amenity", tempfile(fileext = ".osm.pbf"))
nodes <- sf::st_read(output, layer = "points", quiet = TRUE)
head(nodes$other_tags)
# get all objects (nodes, ways or relations) with an addr:* tag
output <- tags_filter(
cur_pbf,
"addr:*",
tempfile(fileext = ".osm.pbf"),
omit_referenced = TRUE,
spinner = FALSE
)
nodes <- sf::st_read(output, layer = "points", quiet = TRUE)
head(nodes$other_tags)
```
### `show_content()`
Finally, `show_content()` displays the content of an OSM file either in `.html`,
`.xml` or `.opl` format. The function takes as input the path to the OSM file
whose content should be shown, the output format in which the content should be
displayed (defaulting to `.html`, the most human readable format, although also
the slowest to open and inspect, depending on the size of the input file) and
the type of objects that should be included in the output (defaulting to all
existing objects in the input). The function returns the path to the temporary
file in which the OSM file content was saved and opens the content in the web
browser or the most appropriate application, depending on the output format.
```{r}
# displays the content of the previous tags_filter() output in html format
show_content(output, spinner = FALSE)
```
```{r, echo = FALSE}
knitr::include_graphics("man/figures/filtered_file_content.png")
```
## Acknowledgement
**rosmium** is developed by a team at the Institute for Applied Economic
Research (Ipea), Brazil. We would like to thank the authors and contributors of
Osmium for the development of [Osmium
Tool](https://github.com/osmcode/osmium-tool).