-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslides-turning-data-frames-into-sf-objects.qmd
96 lines (70 loc) · 1.53 KB
/
slides-turning-data-frames-into-sf-objects.qmd
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
---
title: Turning Data Frames into sf Objects
---
```{r}
#| echo: false
library(tidyverse)
library(sf)
```
```{r}
#| eval: false
#| echo: false
# https://www.portlandmaps.com/metadata/index.cfm?&action=DisplayLayer&LayerID=52777
library(janitor)
portland_corners <-
read_sf("data/Corners_Improved.geojson") |>
st_make_valid() |>
st_centroid()
portland_corners |>
clean_names() |>
select(asset_id, ramp_style) |>
st_coordinates() |>
as_tibble() |>
rename(longitude = X, latitude = Y) |>
write_csv("data/portland-corners.csv")
```
### `st_as_sf()`
```{r}
#| eval: false
library(sf)
csv_file |>
st_as_sf(
coords = c("x_variable", "y_variable"),
crs = 4326
)
```
::: {.notes}
Talk about having to put variable names in quotes
Talk about crs: show how not adding CRS makes it not show up correctly on map
:::
---
```{r}
portland_corners_csv <-
read_csv("data/portland-corners.csv")
```
. . .
```{r}
portland_corners_csv
```
---
```{r}
portland_corners_sf <-
portland_corners_csv |>
st_as_sf(
coords = c("longitude", "latitude"),
crs = 4326
)
```
. . .
```{r}
portland_corners_sf
```
---
```{r}
portland_corners_sf |>
mapview::mapview()
```
### Your Turn {.your-turn}
1. [Download this CSV](https://raw.githubusercontent.com/rfortherestofus/mapping-with-r-v2/refs/heads/main/data/portland-traffic-signals.csv) of traffic signal data for the city of Portland
1. Import the CSV and turn it into an `sf` object using `st_as_sf()`
1. Run `mapview::mapview()` to ensure you imported it correctly