diff --git a/NEWS.md b/NEWS.md new file mode 100644 index 0000000..9da1467 --- /dev/null +++ b/NEWS.md @@ -0,0 +1,3 @@ +# MDCSpatialData (development version) + +* Fixed NLCD-correcting the mask diff --git a/R/data.R b/R/data.R index 8107d15..f0b5da3 100644 --- a/R/data.R +++ b/R/data.R @@ -65,7 +65,7 @@ #' #' See vignettes for examples on how to batch process #' -#' Cropping performed with terra::crop +#' Cropping performed with terra::crop and the terra:mask #' #' Area converted to square km based on a 30x30m cell size and summing all #' all pixels with in a given class. No other processing occurred. @@ -73,24 +73,61 @@ #' #' @examples #' \dontrun{ -#' #pixel classes were condensed as follows: +#' #data processed as follows: +#' +#' +#' +#' # list projected NLCD files in a folder +#'list.nlcd<-list.files(path='./prj_NLCD/', +#' pattern='.tif$', +#' full.names=TRUE) +#' +#' +#' +#' MDCSpatialData::counties.mo->co +#' +#' +#' +#'co%>%split(.$NAME)%>% +#' map(.,~terra::vect(.x))->tmp #' +#'nlcd.fxn<-function(nlcd, +#' county,year){ +#' terra::rast(nlcd)->Z #' -#' terra::rast(nlcd)->Z #' mo.co.list[[county]]->Q -#' terra::crop(Z,Q)%>% -#' terra::freq()%>% -#' filter(str_detect(value, -#' 'Water|Developed|Forest|Shrub|Grass|Hay|Crops|Wetlands'))%>% -#' mutate(LC=case_when(str_detect(value,'Developed')~"develop", -#' str_detect(value,'Forest')~"forest", -#' str_detect(value,'Shrub|Grassland|Hay')~'grass', -#' str_detect(value,'Crops')~'crop', -#' str_detect(value,'Wetlands')~'wetland', -#' str_detect(value,'Water')~'water', -#' TRUE~as.character("other") -#' )) -#' } +#' +#' terra::crop(Z,Q)->P +#' terra::mask(P,Q)%>% +#' terra::freq()%>% +#' rename(LC=value)%>% +#' select(LC,count)%>% +#' group_by(LC)%>% +#' summarize(n.cells=sum(count))%>% +#' ungroup()%>% +#' mutate(pct.area=n.cells/sum(n.cells), +#' area.sqkm=(n.cells*(30*30)/1E6), +#' cty=county, +#' yr=year) +#'} +#' +#' +#' +#' +#'crossing(list.nlcd,names(mo.co.list))%>% +#' rename(path=1, +#' county=2)%>% +#' mutate(year=str_extract(path,pattern='20\\d{2}'))%>% +#' pmap(~nlcd.fxn(..1,..2,..3))->nlcd.tab +#' +#' +#' bind_rows(nlcd.tab)%>% +#' select(-n.cells,-pct.area)%>% +#' mutate(LC=paste0(LC,'.sqkm'))%>% +#' pivot_wider(names_from=LC, +#' values_from=area.sqkm)->nlcd.area.tab +#'} +#' #' #' @format #' \describe{ @@ -124,23 +161,59 @@ #' #' @examples #' \dontrun{ -#' #pixel classes were condensed as follows: +#' #' #data processed as follows: +#' +#' +#' +#' # list projected NLCD files in a folder +#'list.nlcd<-list.files(path='./prj_NLCD/', +#' pattern='.tif$', +#' full.names=TRUE) +#' +#' +#' +#' MDCSpatialData::counties.mo->co +#' +#' #' +#'co%>%split(.$NAME)%>% +#' map(.,~terra::vect(.x))->tmp +#' +#'nlcd.fxn<-function(nlcd, +#' county,year){ +#' terra::rast(nlcd)->Z #' -#' terra::rast(nlcd)->Z #' mo.co.list[[county]]->Q -#' terra::crop(Z,Q)%>% -#' terra::freq()%>% -#' filter(str_detect(value, -#' 'Water|Developed|Forest|Shrub|Grass|Hay|Crops|Wetlands'))%>% -#' mutate(LC=case_when(str_detect(value,'Developed')~"develop", -#' str_detect(value,'Forest')~"forest", -#' str_detect(value,'Shrub|Grassland|Hay')~'grass', -#' str_detect(value,'Crops')~'crop', -#' str_detect(value,'Wetlands')~'wetland', -#' str_detect(value,'Water')~'water', -#' TRUE~as.character("other") -#' )) +#' +#' terra::crop(Z,Q)->P +#' terra::mask(P,Q)%>% +#' terra::freq()%>% +#' rename(LC=value)%>% +#' select(LC,count)%>% +#' group_by(LC)%>% +#' summarize(n.cells=sum(count))%>% +#' ungroup()%>% +#' mutate(pct.area=n.cells/sum(n.cells), +#' area.sqkm=(n.cells*(30*30)/1E6), +#' cty=county, +#' yr=year) +#'} +#' +#' +#' +#' +#'crossing(list.nlcd,names(mo.co.list))%>% +#' rename(path=1, +#' county=2)%>% +#' mutate(year=str_extract(path,pattern='20\\d{2}'))%>% +#' pmap(~nlcd.fxn(..1,..2,..3))->nlcd.tab +#' +#' bind_rows(nlcd.tab)%>% +#' select(-n.cells,-area.sqkm)%>% +#' mutate(LC=paste0(LC,'.pct'))%>% +#' pivot_wider(names_from=LC, +#' values_from=pct.area)->nlcd.pct.tab +#' #' } #' #' @format diff --git a/data/nlcd.area.tab.rda b/data/nlcd.area.tab.rda index 43cfc8d..6202775 100644 Binary files a/data/nlcd.area.tab.rda and b/data/nlcd.area.tab.rda differ diff --git a/data/nlcd.pct.tab.rda b/data/nlcd.pct.tab.rda index aa64ce2..8d2c7ce 100644 Binary files a/data/nlcd.pct.tab.rda and b/data/nlcd.pct.tab.rda differ diff --git a/man/nlcd.area.tab.Rd b/man/nlcd.area.tab.Rd index 5143fa0..35e7908 100644 --- a/man/nlcd.area.tab.Rd +++ b/man/nlcd.area.tab.Rd @@ -28,31 +28,68 @@ to NAD83 UTM 15 N before areal calculations. \details{ See vignettes for examples on how to batch process -Cropping performed with terra::crop +Cropping performed with terra::crop and the terra:mask Area converted to square km based on a 30x30m cell size and summing all all pixels with in a given class. No other processing occurred. } \examples{ \dontrun{ -#pixel classes were condensed as follows: +#data processed as follows: -terra::rast(nlcd)->Z + +# list projected NLCD files in a folder +list.nlcd<-list.files(path='./prj_NLCD/', + pattern='.tif$', + full.names=TRUE) + + + +MDCSpatialData::counties.mo->co + + + +co\%>\%split(.$NAME)\%>\% + map(.,~terra::vect(.x))->tmp + +nlcd.fxn<-function(nlcd, + county,year){ + terra::rast(nlcd)->Z + mo.co.list[[county]]->Q - terra::crop(Z,Q)\%>\% - terra::freq()\%>\% - filter(str_detect(value, - 'Water|Developed|Forest|Shrub|Grass|Hay|Crops|Wetlands'))\%>\% - mutate(LC=case_when(str_detect(value,'Developed')~"develop", - str_detect(value,'Forest')~"forest", - str_detect(value,'Shrub|Grassland|Hay')~'grass', - str_detect(value,'Crops')~'crop', - str_detect(value,'Wetlands')~'wetland', - str_detect(value,'Water')~'water', - TRUE~as.character("other") - )) - } + + terra::crop(Z,Q)->P + terra::mask(P,Q)\%>\% + terra::freq()\%>\% + rename(LC=value)\%>\% + select(LC,count)\%>\% + group_by(LC)\%>\% + summarize(n.cells=sum(count))\%>\% + ungroup()\%>\% + mutate(pct.area=n.cells/sum(n.cells), + area.sqkm=(n.cells*(30*30)/1E6), + cty=county, + yr=year) +} + + + + +crossing(list.nlcd,names(mo.co.list))\%>\% + rename(path=1, + county=2)\%>\% + mutate(year=str_extract(path,pattern='20\\\\d{2}'))\%>\% + pmap(~nlcd.fxn(..1,..2,..3))->nlcd.tab + + +bind_rows(nlcd.tab)\%>\% + select(-n.cells,-pct.area)\%>\% + mutate(LC=paste0(LC,'.sqkm'))\%>\% + pivot_wider(names_from=LC, + values_from=area.sqkm)->nlcd.area.tab +} + } \keyword{datasets} diff --git a/man/nlcd.pct.tab.Rd b/man/nlcd.pct.tab.Rd index ac7a8cd..38abeb8 100644 --- a/man/nlcd.pct.tab.Rd +++ b/man/nlcd.pct.tab.Rd @@ -34,23 +34,59 @@ all pixels with in a given class. No other processing occurred. } \examples{ \dontrun{ -#pixel classes were condensed as follows: +#' #data processed as follows: -terra::rast(nlcd)->Z + +# list projected NLCD files in a folder +list.nlcd<-list.files(path='./prj_NLCD/', + pattern='.tif$', + full.names=TRUE) + + + +MDCSpatialData::counties.mo->co + + + +co\%>\%split(.$NAME)\%>\% + map(.,~terra::vect(.x))->tmp + +nlcd.fxn<-function(nlcd, + county,year){ + terra::rast(nlcd)->Z + mo.co.list[[county]]->Q - terra::crop(Z,Q)\%>\% - terra::freq()\%>\% - filter(str_detect(value, - 'Water|Developed|Forest|Shrub|Grass|Hay|Crops|Wetlands'))\%>\% - mutate(LC=case_when(str_detect(value,'Developed')~"develop", - str_detect(value,'Forest')~"forest", - str_detect(value,'Shrub|Grassland|Hay')~'grass', - str_detect(value,'Crops')~'crop', - str_detect(value,'Wetlands')~'wetland', - str_detect(value,'Water')~'water', - TRUE~as.character("other") - )) + + terra::crop(Z,Q)->P + terra::mask(P,Q)\%>\% + terra::freq()\%>\% + rename(LC=value)\%>\% + select(LC,count)\%>\% + group_by(LC)\%>\% + summarize(n.cells=sum(count))\%>\% + ungroup()\%>\% + mutate(pct.area=n.cells/sum(n.cells), + area.sqkm=(n.cells*(30*30)/1E6), + cty=county, + yr=year) +} + + + + +crossing(list.nlcd,names(mo.co.list))\%>\% + rename(path=1, + county=2)\%>\% + mutate(year=str_extract(path,pattern='20\\\\d{2}'))\%>\% + pmap(~nlcd.fxn(..1,..2,..3))->nlcd.tab + + bind_rows(nlcd.tab)\%>\% + select(-n.cells,-area.sqkm)\%>\% + mutate(LC=paste0(LC,'.pct'))\%>\% + pivot_wider(names_from=LC, + values_from=pct.area)->nlcd.pct.tab + } }