-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid.qmd
53 lines (39 loc) · 1.05 KB
/
grid.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
---
eval: false
---
# Grids
## Make grid
```{r}
#| message: false
#| warning: false
library(sf)
library(dplyr)
library(mapview)
MUNICIPIOSgeo = st_read("data/Municipalities_geo.gpkg")
LISBON = MUNICIPIOSgeo |> filter(Municipality == "Lisboa") |> st_transform(3857)
```
To make a grid we is the `st_make_grid()` function.
```{r}
GRID = LISBON |>
st_make_grid(cellsize = 800, what = "polygons", square = TRUE) |> # 800m, we are using a projected crs
st_sf() |> # from list to sf
st_transform(crs = 4326) # to WGS84
GRID = GRID |>
mutate(id = c(1:nrow(GRID))) # just to give an ID to each cell
mapview(GRID)
```
## Count points in polygons
```{r}
SURVEY = read.csv("data/SURVEY.txt", sep = "\t")
SURVEYgeo = SURVEY |> st_as_sf(coords = c("lon", "lat"), crs = 4326)
SURVEY_with_GRIDid = SURVEYgeo |>
st_join(GRID,
join = st_intersects) |>
st_drop_geometry() |>
group_by(id) |>
summarise(count = n()) |>
ungroup()
# back to grid
GRIDdensity = GRID |> left_join(SURVEY_with_GRIDid)
mapview(GRIDdensity, zcol = "count")
```