Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created November 23, 2020 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njtierney/b42769442b2a3873f3965b3ba17eb68e to your computer and use it in GitHub Desktop.
Save njtierney/b42769442b2a3873f3965b3ba17eb68e to your computer and use it in GitHub Desktop.
source("https://gist.githubusercontent.com/njtierney/3d8f6b3714860fb2a91fd2c5700e3d53/raw/14226d5d78609e99896450fbd07d82c60875b6cb/oz-map-states.R")
source("https://gist.githubusercontent.com/njtierney/fa383d40c4923a25d39684788dfb6c0e/raw/f80e018a1675da5ac663bb5c5615c80c4cecc41e/aawt-tracks.R")
source("https://gist.githubusercontent.com/njtierney/781bffd9f86509e180d8c86f8289ba20/raw/7ad2b929c9f0001dc45b9e112ad57db41a0a372d/bom-stations-sf.R")

library(sf)
#> Linking to GEOS 3.8.1, GDAL 3.1.1, PROJ 6.3.1
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

bom_stations_subset <- bom_stations_sf %>% 
  filter(state %in% c("NSW",
                      "VIC",
                      "ACT"))

gg_track_states <- ggplot() + 
  geom_sf(data = oz_map_aawt_states) +
  geom_sf(data = aawt_tracks,
          colour = "salmon")

gg_track_states 

not sure how to buffer this correctly, as I get an error: “st_buffer does not correctly buffer longitude/latitude data” ideally would create a polygon that surrounded the whole track by 250Km, but not sure how to do that then I can find the bom stations within that polygon while this kind of works, I’m not sure how to intepret the degrees here

buffered_track_025 <- st_buffer(aawt_tracks, 0.25) %>% st_union()
#> Warning in st_buffer.sfc(st_geometry(x), dist, nQuadSegs, endCapStyle =
#> endCapStyle, : st_buffer does not correctly buffer longitude/latitude data
#> dist is assumed to be in decimal degrees (arc_degrees).

stations_in_polygon_025 <- st_intersection(bom_stations_subset, 
                                           buffered_track_025)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries

gg_track_states +
  geom_sf(data = stations_in_polygon_025,
          alpha = 0.5,
          colour = "forestgreen") +
  geom_sf(data = buffered_track_025,
          fill = NA,
          colour = "orange")

Another approach

Using bbox to get the points around it, but this is too wide, I think.

bbox_track <- st_bbox(aawt_tracks) %>% 
  st_as_sfc() %>% 
  st_buffer(dist = 0.5)
#> Warning in st_buffer.sfc(., dist = 0.5): st_buffer does not correctly buffer
#> longitude/latitude data
#> dist is assumed to be in decimal degrees (arc_degrees).

stations_in_bbox <- st_intersection(bom_stations_subset, bbox_track)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries

gg_track_states +
  geom_sf(data = bbox_track,
          fill = NA) + 
  geom_sf(data = stations_in_bbox,
          alpha = 0.5)

Created on 2020-11-23 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment