Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
erikarasnick committed Dec 2, 2022
1 parent b4e0896 commit 18285df
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
hamilton_landcover/
Binary file added built_environment_pairs.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions get_nlcd_rasters.R
@@ -0,0 +1,33 @@
library(tidyverse)
library(sf)
library(terra)

hc <- cincy::county_7cc_2010 |> filter(county_name == "Hamilton")

# nlcd
nlcd_tif <- s3::s3_get(glue::glue("s3://geomarker/nlcd_cog/nlcd_landcover_2019.tif"))
nlcd <- terra::rast(nlcd_tif)
nlcd_hc_2019 <- terra::crop(nlcd, hc)
terra::writeRaster(nlcd_hc_2019, "rasters/nlcd_hc_2019.tif")
fs::dir_delete("s3_downloads")

# impervious
impervious_tif <- s3::s3_get(glue::glue("s3://geomarker/nlcd_cog/nlcd_impervious_2019.tif"))
impervious <- terra::rast(impervious_tif)
impervious_hc_2019 <- terra::crop(impervious, hc)
terra::writeRaster(impervious_hc_2019, "rasters/impervious_hc_2019.tif")
fs::dir_delete("s3_downloads")

# tree canopy
tree_tif <- s3::s3_get(glue::glue("s3://geomarker/nlcd_cog/nlcd_treecanopy_2016.tif"))
treecanopy <- terra::rast(tree_tif)
treecanopy_hc_2016 <- terra::crop(treecanopy, hc)
terra::writeRaster(treecanopy_hc_2016, "rasters/treecanopy_hc_2016.tif")
fs::dir_delete("s3_downloads")

# evi
evi_tif <- s3::s3_get(glue::glue("s3://geomarker/modis_evi_ndvi/evi_June_2018_5072.tif"))
evi <- terra::rast(evi_tif)
evi_hc_2018 <- terra::crop(evi, hc)
terra::writeRaster(evi_hc_2018, "rasters/evi_hc_2018.tif")
fs::dir_delete("s3_downloads")
92 changes: 92 additions & 0 deletions make_tract_nlcd.R
@@ -0,0 +1,92 @@
library(tidyverse)
library(sf)
library(terra)
library(CODECtools)
source("nlcd_legend.R")

tract <- terra::vect(cincy::tract_tigris_2010)

# NLCD - greenspace
nlcd_hc_2019 <- terra::rast("rasters/nlcd_hc_2019.tif")

pct_green <-
terra::extract(nlcd_hc_2019,
tract) |>
rename(value = Layer_1) |>
left_join(nlcd_legend, by = "value") |>
group_by(ID) |>
summarize(pct_green_2019 = round(sum(green) / n() * 100)) |>
select(-ID)

tract_nlcd <- bind_cols(cincy::tract_tigris_2010, pct_green)

# NLCD - impervious
impervious_hc_2019 <- terra::rast("rasters/impervious_hc_2019.tif")

pct_impervious <-
terra::extract(impervious_hc_2019,
tract,
fun = "mean") |>
rename(value = Layer_1) |>
summarize(pct_impervious_2019 = round(value))

tract_nlcd <- bind_cols(tract_nlcd, pct_impervious)

# NLCD - treecanopy
treecanopy_hc_2016 <- terra::rast("rasters/treecanopy_hc_2016.tif")

pct_treecanopy <-
terra::extract(treecanopy_hc_2016,
tract,
fun = "mean") |>
rename(value = Layer_1) |>
summarize(pct_treecanopy_2016 = round(value))

tract_nlcd <- bind_cols(tract_nlcd, pct_treecanopy)

# EVI
evi_hc_2018 <- terra::rast("rasters/evi_hc_2018.tif")

evi <-
terra::extract(evi_hc_2018,
tract,
fun = "mean",
na.rm = TRUE) |>
rename(evi_2018 = evi_June_2018_5072) |>
mutate(evi_2018 = round(evi_2018 * 0.0001, 4)) |>
select(-ID)

tract_nlcd <- bind_cols(tract_nlcd, evi)
tract_nlcd <- sf::st_drop_geometry(tract_nlcd)

tract_nlcd <- tract_nlcd |>
add_attrs(
name = "hamilton_landcover",
title = "Hamilton County Landcover and Built Environment Characteristics",
version = "0.1.0",
homepage = "https://geomarker.io/hamilton_landcover"
)

tract_nlcd <-
tract_nlcd |>
add_col_attrs(census_tract_id, title = "Census Tract Identifier") |>
add_col_attrs(pct_green_2019, title = "Percent Greenspace 2019", description = "percent of pixels in each tract classified as green") |>
add_col_attrs(pct_impervious_2019, title = "Percent Impervious 2019", description = "average percent imperviousness for pixels in each tract") |>
add_col_attrs(pct_treecanopy_2016, title = "Percent Treecanopy 2016", description = "average percent tree canopy for pixels in each tract") |>
add_col_attrs(evi_2018, title = "Enhanced Vegetation Index 2018", description = "average enhanced vegetation index for pixels in each tract")

tract_nlcd <- add_type_attrs(tract_nlcd)

# write metadata.md
cat("#### Metadata\n\n", file = "metadata.md", append = FALSE)
CODECtools::glimpse_attr(tract_nlcd) |>
knitr::kable() |>
cat(file = "metadata.md", sep = "\n", append = TRUE)

cat("\n#### Schema\n\n", file = "metadata.md", append = TRUE)
tract_nlcd |>
CODECtools::glimpse_schema() |>
knitr::kable() |>
cat(file = "metadata.md", sep = "\n", append = TRUE)

write_tdr_csv(tract_nlcd)
18 changes: 18 additions & 0 deletions metadata.md
@@ -0,0 +1,18 @@
#### Metadata

|name |value |
|:--------|:---------------------------------------------------------------|
|name |hamilton_landcover |
|version |0.1.0 |
|title |Hamilton County Landcover and Built Environment Characteristics |
|homepage |https://geomarker.io/hamilton_landcover |

#### Schema

|name |title |type |description |
|:-------------------|:------------------------------|:------|:----------------------------------------------------------|
|census_tract_id |Census Tract Identifier |string |NA |
|pct_green_2019 |Percent Greenspace 2019 |number |percent of pixels in each tract classified as green |
|pct_impervious_2019 |Percent Impervious 2019 |number |average percent imperviousness for pixels in each tract |
|pct_treecanopy_2016 |Percent Treecanopy 2016 |number |average percent tree canopy for pixels in each tract |
|evi_2018 |Enhanced Vegetation Index 2018 |number |average enhanced vegetation index for pixels in each tract |
24 changes: 24 additions & 0 deletions nlcd_legend.R
@@ -0,0 +1,24 @@
nlcd_legend <-
tibble::tribble(
~value, ~landcover_class, ~landcover, ~green,
11, "water", "water", FALSE,
12, "water", "ice/snow", FALSE,
21, "developed", "developed open", TRUE,
22, "developed", "developed low intensity", TRUE,
23, "developed", "developed medium intensity", FALSE,
24, "developed", "developed high intensity", FALSE,
31, "barren", "rock/sand/clay", FALSE,
41, "forest", "deciduous forest", TRUE,
42, "forest", "evergreen forest", TRUE,
43, "forest", "mixed forest", TRUE,
51, "shrubland", "dwarf scrub", TRUE,
52, "shrubland", "shrub/scrub", TRUE,
71, "herbaceous", "grassland", TRUE,
72, "herbaceous", "sedge", TRUE,
73, "herbaceous", "lichens", TRUE,
74, "herbaceous", "moss", TRUE,
81, "cultivated", "pasture/hay", TRUE,
82, "cultivated", "cultivated crops", TRUE,
90, "wetlands", "woody wetlands", TRUE,
95, "wetlands", "emergent herbaceous wetlands", TRUE
)
Binary file added rasters/evi_hc_2018.tif
Binary file not shown.
Binary file added rasters/impervious_hc_2019.tif
Binary file not shown.
Binary file added rasters/nlcd_hc_2019.tif
Binary file not shown.
Binary file added rasters/treecanopy_hc_2016.tif
Binary file not shown.

0 comments on commit 18285df

Please sign in to comment.