Why can't I cut a buffer, both in R and QGIS?

38 views Asked by At

My question is very straightforward. I am working with a large coordinates dataset (animal GPS tracks). I would like to create a buffer of a 50km radius around the point where all my tracks start.

No matter what I do, there's something wrong with the CRS and the buffer ends up being in a different place or completely out of scale and not centered on the said point. My QGIS project is EPSG:32632, my tracks are visible only as Point-EPSG:4326. My buffer, on the other hand, ends up at the Equator.

I'm now trying to do it in R. My df is called codoni, I'll attach the str.

codoni df str:

> str(codoni)
'data.frame':   149366 obs. of  8 variables:
$ id      : chr  "N34246@096AFE12" "N34246@096AFE12" "N34246@096AFE12"
"N34246@096AFE12" ...
$ DateTime: POSIXct, format: "2022-02-23 03:36:00" "2022-02-23 05:36:00" "2022-02-23
07:36:00" "2022-02-23 09:36:00" ...
$ lat     : num  45.5 45.5 45.5 45.5 45.5 ...
 $ lon     : num  12.6 12.6 12.6 12.6 12.6 ...
$ Altitude: int  0 0 0 6 0 9 0 82 0 0 ...
$ Sex     : int  1 1 1 1 1 1 1 1 1 1 ...
$ Age     : int  6 6 6 6 6 6 6 6 6 6 ...
$ Tag     : chr  "GPS-GSM_TECHNOSMART" "GPS-GSM_TECHNOSMART" "GPS-GSM_TECHNOSMART"
"GPS-GSM_TECHNOSMART" ...

I managed to avoid all error messages but unfortunately, my buffer still ends up being in a completely wrong location. This is why my codoni_clipped ends up being empty (and also why instead of using the codoni_clipped in the ggplot, as I would want to do, I am using the codoni_sf). This is my code:

cavallino <- c(45.501518, 12.565928) 
cavallino_point <- st_sfc(st_point(cavallino))
st_crs(cavallino_point) <- 4326
# buffer around cavallino in EPSG:4326
buffer_4326 <- st_buffer(cavallino_point, dist = 50000)
st_crs(buffer_4326) <- st_crs(4326)
buffer_32632 <- st_transform(buffer_4326, 32632)
st_geometry(buffer_32632)
st_crs(buffer_32632)
# clip tracks to the extent of the buffer
codoni_sf <- st_as_sf(codoni, coords = c("lon", "lat"), crs =
4326) %>% st_transform(32632)
st_crs(codoni_sf)
codoni_clipped <- st_intersection(codoni_sf, buffer_32632)
st_geometry(codoni_clipped)
st_crs(codoni_clipped)
# plot tracks and buffer together
final_plot <- ggplot() +
geom_sf(data = codoni_sf, color = "red", size = 0.7) +
geom_sf(data = buffer_32632, fill = "blue", alpha = 0.3) +
labs(title = "Tracks and Buffer (50km radius)",
x = "Longitude",
y = "Latitude") +
theme_minimal()
print(final_plot)

Here's the result:

> dput(head(codoni, 5)) 
structure(list(id = c("N34246@096AFE12", "N34246@096AFE12",
"N34246@096AFE12", 
"N34246@096AFE12", "N34246@096AFE12"), DateTime =
structure(c(1645583760, 
1645590960, 1645598160, 1645605360, 1645612560), class 
c("POSIXct", 
"POSIXt"), tzone = ""), lat = c(45.504295, 45.50255, 45.50186,
45.5025867, 45.5005833), lon = c(12.5623333, 12.566, 12.56615,
12.56315, 12.5618667), Altitude = c(0L, 0L, 0L, 6L, 0L), Sex 
c(1L, 
1L, 1L, 1L, 1L), Age = c(6L, 6L, 6L, 6L, 6L), Tag = c("GPS
-GSM_TECHNOSMART", 
"GPS-GSM_TECHNOSMART", "GPS-GSM_TECHNOSMART", "GPS
GSM_TECHNOSMART", 
"GPS-GSM_TECHNOSMART")), row.names = c(NA, 5L), class =
"data.frame")

This is my graph, tracks in red, buffer as a small lilac dot on the right. Obviously in the wrong spot (should be around where the tracks start) but correct shape and size.

My ggplot

What's wrong???

Thank you :)

0

There are 0 answers