Created
October 2, 2019 17:42
-
-
Save moldach/552cc704ced328a0120394271404779f to your computer and use it in GitHub Desktop.
Shapefiles and Raster of Relief do not match for Hawaii
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### This method works for Switzerland and The USA (shown here, at the top) but not Hawaii (shown at the bottom) | |
library(sf) | |
library(raster) | |
library(dplyr) | |
library(ggplot2) | |
## USA | |
# download shapefile: https://catalog.data.gov/dataset/tiger-line-shapefile-2017-nation-u-s-current-state-and-equivalent-national | |
usa_geo <- read_sf("data/shapefiles/usa/tl_2017_us_state.shp") | |
# download relief: http://www.naturalearthdata.com/downloads/50m-raster-data/50m-shaded-relief/ | |
relief <- raster("data/relief/world/SR_50M/SR_50M.tif") %>% | |
as("SpatialPixelsDataFrame") %>% | |
as.data.frame() %>% | |
rename(., value = "SR_50M") %>% | |
filter(value != 255) | |
ggplot( | |
# define main data source | |
data = usa_geo | |
) + | |
# first: draw the relief | |
geom_raster( | |
data = relief, | |
inherit.aes = FALSE, | |
aes( | |
x = x, | |
y = y, | |
alpha = value | |
) | |
) + | |
# use the "alpha hack" (as the "fill" aesthetic is already taken) | |
scale_alpha(name = "", | |
range = c(0.4, 0.01), | |
guide = F) + # suppress legend | |
# add main fill aesthetic | |
# use thin white stroke for municipality borders | |
geom_sf(color = "red", | |
size = 0.1 | |
) | |
## Hawaii | |
# https://earthworks.stanford.edu/catalog/stanford-qh711pf3383 | |
relief <- raster("data/relief/100m/srgrhii0100a.tif") %>% | |
as("SpatialPixelsDataFrame") %>% | |
as.data.frame() %>% | |
rename(., value = "srgrhii0100a") %>% | |
filter(value != 255) | |
# http://geoportal.hawaii.gov/datasets/045b1d5147634e2380566668e04094c6_3 | |
hawaii_geo <- read_sf("data/shapefiles/coastline/Coastline.shp") | |
ggplot( | |
# define main data source | |
data = hawaii_geo | |
) + | |
# first: draw the relief | |
geom_raster( | |
data = relief, | |
inherit.aes = FALSE, | |
aes( | |
x = x, | |
y = y, | |
alpha = value | |
) | |
) + | |
# use the "alpha hack" (as the "fill" aesthetic is already taken) | |
scale_alpha(name = "", | |
range = c(0.4, 0.01), | |
guide = F) + # suppress legend | |
# add main fill aesthetic | |
# use thin white stroke for municipality borders | |
geom_sf(color = "red", | |
size = 0.1 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please read some of my comments in the script: