L16: Rasters and Raster Operations 2

Zonal Statistics, Vectorizing, Cropping

Bogdan G. Popescu

John Cabot University

Today

Today’s agenda includes:

  • cropping raster to a polygon
  • zonal statistics: mean
  • converting rasters (stars) to polygons
  • subtracting rasters

Luminosity

Previously, we used satellite luminosity.

As discussed, this has been used as a proxy for economic activity.

Working with Luminosity

Let us download the files that we have worked with.

Working with Luminosity 1992

Let us make a map of luminosity by focusing on Europe in 1992 and 2012

#Step1: Loadning relevant libraries
library(sf)
library(stars)
library(ggplot2)
sf_use_s2(FALSE)
#Step2: Read country borders
borders <- st_read(dsn="./data/boundaries/all-country-boundaries.shp", quiet = TRUE)
#Step3: Simplifying border to make them easier to use on our machine
borders<-st_simplify(borders, dTolerance=0.1)
#Step4: Creating a folder called "F101992"
target_dir <- "./data/luminosity/F101992"
#Step5: Create the target directory if it doesn't exist
if (!dir.exists(target_dir)) {
  dir.create(target_dir)
}
#Step6: Unzip the file
untar("./data/luminosity/F101992.v4.tar", exdir = target_dir)
#Step7: Unzip the file further
R.utils::gunzip("./data/luminosity/F101992/F101992.v4b_web.avg_vis.tif.gz", remove = FALSE, overwrite=TRUE)
#Step8: Read the raster
f1992<-read_stars("./data/luminosity/F101992/F101992.v4b_web.avg_vis.tif")
#Step9: Make sure the two files have the same CRS
st_crs(f1992)<-st_crs(borders)
#Step10: Reduce the resolution of the raster: Higher dx/day values mean lower resolution
f1992b<-st_warp(f1992, st_as_stars(st_bbox(f1992), dx = 0.3, dy = 0.3))
#Step11: Rename the raster
names(f1992b)<-"lights_1992"
#Step12:Defining the countries defining the regions of interest
norway<-subset(borders, SOVEREIGNT == "Norway")
max_lat_y_eu<-st_bbox(norway)["ymax"]
greece<-subset(borders, SOVEREIGNT == "Greece")
min_lat_y_eu<-st_bbox(greece)["ymin"]
ukraine<-subset(borders, SOVEREIGNT == "Ukraine")
max_lon_x_eu<-st_bbox(ukraine)["xmax"]
portugal<-subset(borders, SOVEREIGNT == "Portugal")
min_lon_x_eu<-st_bbox(portugal)["xmin"]
#Step13: Making the map
fig1992<-ggplot()+
  geom_stars(data=f1992b)+
  scale_fill_gradientn(name = "", colours=c("black","white"))+
  geom_sf(data=borders, linewidth = 0.1, fill = NA, color = "white", alpha=0.5)+
  coord_sf(xlim = c(min_lon_x_eu-3, max_lon_x_eu+3), ylim = c(min_lat_y_eu-1, max_lat_y_eu-6))+
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Working with Luminosity 1992

Let us make a map of luminosity by focusing on Europe in 1992 and 2012

Working with Luminosity 2012

Let us do the same for 2012

#Step1: Creating a folder called "F182012"
target_dir <- "./data/luminosity/F182012"
#Step2: Create the target directory if it doesn't exist
if (!dir.exists(target_dir)) {
  dir.create(target_dir)
}
#Step3: Unzip the file
untar("./data/luminosity/F182012.v4.tar", exdir = target_dir)
#Step4: Unzip the file further
R.utils::gunzip("./data/luminosity/F182012/F182012.v4c_web.avg_vis.tif.gz", remove = FALSE, overwrite=TRUE)
#Step5: Read the raster
f2012<-read_stars("./data/luminosity/F182012/F182012.v4c_web.avg_vis.tif")
#Step6: Make sure the two files have the same CRS
st_crs(f2012)<-st_crs(borders)
#Step7: Reduce the resolution of the raster: Higher dx/day values mean lower resolution
f2012b<-st_warp(f2012, st_as_stars(st_bbox(f2012), dx = 0.3, dy = 0.3))
#Step8: Rename the raster
names(f2012b)<-"lights_2012"
#Step9: Making the map
fig2012<-ggplot()+
  geom_stars(data=f2012b)+
  scale_fill_gradientn(name = "",
                       colours=c("black","white"))+
  geom_sf(data=borders, linewidth = 0.1, fill = NA, color = "white", alpha=0.5)+
  coord_sf(xlim = c(min_lon_x_eu-3, max_lon_x_eu+3), ylim = c(min_lat_y_eu-1, max_lat_y_eu-6))+
  theme_bw()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

Working with Luminosity 2012

fig2012

Working with Luminosity 2012

We can obviously put them side by side for comparison:

library(ggpubr)
ggarrange(fig1992, fig2012, common.legend = TRUE)

Context

The Fall of the Berlin wall is one of the crucial moments in recent European History.

It has marked the fall of communism throughout Eastern Europe.

It would be interesting if communism might have left a lingering legacy on development.

Looking at Germany alone as opposed to West vs. Eastern Europe reduces possibility of confounding variables:

  • institutions
  • legal systems
  • cultural norms

Germany facilitates direct examination of lingering disparities induced by communism.

Germany

Let us first download the county and country shape files for Germany

Germany

Let us first load the Germany county shape files.

#Step1: Read country borders
ger2 <- st_read(dsn="./data/gadm41_DEU_shp/gadm41_DEU_2.shp", quiet = TRUE)
ger2<-st_simplify(ger2, dTolerance=0.005)
ggplot()+
  geom_sf(data=ger2)

Germany

Let us also load the Germany country shape file.

#Step1: Read country borders
ger0 <- st_read(dsn="./data/gadm41_DEU_shp/gadm41_DEU_0.shp", quiet = TRUE, promote_to_multi = FALSE)
ger0<-st_simplify(ger0, dTolerance=0.02)
ggplot()+
  geom_sf(data=ger0)

Germany

Let us now crop the lights based on the Germany shape.

Note that we are reloading the raster to work with the raster’s original resolution.

#Step1: Read the raster
f1992<-read_stars("./data/luminosity/F101992/F101992.v4b_web.avg_vis.tif")
#Step2: Ensuring that the CRS is the same
st_crs(f1992)<-st_crs(ger0)
#Step3: Cropping raster
f1992_cropped<-st_crop(f1992, ger0)

Germany

Let us now compare the two

plot(f1992)