L10: Shapefiles, Coordinate Systems, and Basic Mapping

Bogdan G. Popescu

John Cabot University

Introduction

We previously covered the fundamentals of Geographic Information Systems (GIS)

We also learned about https://gadm.org

This is a good website to download shapefiles for countries and subdivisions

Introduction

Create a new folder for this week

Download again the shapefiles for the US and Italy and place them in a folder called data.

Mapping the US USA_0

library(sf)
#"s2" is an enhancement of the "sf" package
#It allows one to complete spatial operations with data that is in geographic coordinate system (the coordinates are in degrees)
#However, having it enables will take much longer computation time
sf_use_s2(FALSE)
library(ggplot2)
#Step1: Read country shape
usa_cntry <- st_read(dsn="./data/gadm41_USA_shp/gadm41_USA_0.shp", quiet = TRUE)

#Step2: Simplify lines
usa_cntry<-st_simplify(usa_cntry,  dTolerance = 0.05)

#Define zoom
min_lon_x_us<-(-130)
max_lon_x_us<-(-57)
min_lat_y_us<-(26)
max_lat_y_us<-(53)

#Map
fig<-ggplot()+
  geom_sf(data=usa_cntry, linewidth = 0.3, fill = NA)+
  theme_bw()+
  coord_sf(xlim = c(min_lon_x_us-3, max_lon_x_us+3), 
           ylim = c(min_lat_y_us-3, max_lat_y_us+3))

Mapping the US USA_0

fig

Mapping the US USA_0

Let us examine what the dataframe looks like

head(usa_cntry, 2)
Simple feature collection with 1 feature and 2 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -178.9911 ymin: 18.91028 xmax: 179.7485 ymax: 71.33542
Geodetic CRS:  WGS 84
  GID_0       COUNTRY                       geometry
1   USA United States MULTIPOLYGON (((179.6461 52...

Mapping the US USA_0

This is how we turn the sf object into a simple dataframe

usa_cntry_df<-st_drop_geometry(usa_cntry)
head(usa_cntry_df, 2)
  GID_0       COUNTRY
1   USA United States

The two objects are very similar.

The difference is that usa_cntry has a geometry column. usa_cntry_df does not.

We can see the difference better if we do:

class(usa_cntry)
[1] "sf"         "data.frame"

or

class(usa_cntry_df)
[1] "data.frame"

Shapefiles for Subdivsions of the US USA_1

#Step1: Read country borders
usa_cntry1 <- st_read(dsn="./data/gadm41_USA_shp/gadm41_USA_1.shp", quiet = TRUE)

#Step2: Simplify lines
usa_cntry1<-st_simplify(usa_cntry1,  dTolerance = 0.05)

#Step3: Map
fig<-ggplot()+
  geom_sf(data=usa_cntry1, linewidth = 0.3, fill = NA)+
  theme_bw()+
  coord_sf(xlim = c(min_lon_x_us-3, max_lon_x_us+3), 
           ylim = c(min_lat_y_us-3, max_lat_y_us+3))

Shapefiles for Subdivsions of the US

fig

Shapefiles for Subdivsions of the US USA_2

#Step1: Read country borders
usa_cntry2 <- st_read(dsn="./data/gadm41_USA_shp/gadm41_USA_2.shp", quiet = TRUE)

#Step2: Simplify lines
usa_cntry2<-st_simplify(usa_cntry2,  dTolerance = 0.05)

#Step3: Map
fig<-ggplot()+
  geom_sf(data=usa_cntry2, linewidth = 0.3, fill = NA)+
  theme_bw()+
  coord_sf(xlim = c(min_lon_x_us-3, max_lon_x_us+3), 
           ylim = c(min_lat_y_us-3, max_lat_y_us+3))

Shapefiles for Subdivsions of the US USA_2

fig

Shapefiles for World

We can do the same for Italy

Shapefiles for World ITA_0

#Step1: Read country borders
it_cntry0 <- st_read(dsn="./data/gadm41_ITA_shp/gadm41_ITA_0.shp", quiet = TRUE)
it_cntry1 <- st_read(dsn="./data/gadm41_ITA_shp/gadm41_ITA_1.shp", quiet = TRUE)
it_cntry2 <- st_read(dsn="./data/gadm41_ITA_shp/gadm41_ITA_2.shp", quiet = TRUE)
it_cntry3 <- st_read(dsn="./data/gadm41_ITA_shp/gadm41_ITA_3.shp", quiet = TRUE)

#Step2: Simplify lines
it_cntry0<-st_simplify(it_cntry0,  dTolerance = 0.05)
it_cntry1<-st_simplify(it_cntry1,  dTolerance = 0.05)
it_cntry2<-st_simplify(it_cntry2,  dTolerance = 0.01)
it_cntry3<-st_simplify(it_cntry3,  dTolerance = 0.01)

#Step3: Map
fig<-ggplot()+
  geom_sf(data=it_cntry0, linewidth = 0.3, fill = NA)+
  theme_bw()

Shapefiles for World ITA_0

fig

Shapefiles for World ITA_1

#Step3: Map
fig<-ggplot()+
  geom_sf(data=it_cntry1, linewidth = 0.3, fill = NA)+
  theme_bw()
fig

Shapefiles for World ITA_1

#Step3: Map
fig

Shapefiles for World ITA_2

#Step3: Map
fig<-ggplot()+
  geom_sf(data=it_cntry2, linewidth = 0.3, fill = NA)+
  theme_bw()

Shapefiles for World ITA_2

fig

Shapefiles for World ITA_3

#Step3: Map
fig<-ggplot()+
  geom_sf(data=it_cntry3, linewidth = 0.05, fill = NA)+
  theme_bw()

Shapefiles for World ITA_3

fig