# Packages and Classes

R has a large number of packages to read, write and manipulate GIS. We describe here a few “core” ones.

sp defines a large number of data classes for GIS and includes many functions for data manipulation such as coordinate transformation. Its data classes are S4 and may include a data frame, particularly "SpatialGridDataFrame", "SpatialPointsDataFrame", "SpatialLinesDataFrame", and "SpatialPo- lygonsDataFrame" with the first one for rasters and the others for geometric objects, sf (*simple features)* is an alternative package to manipulate geometrical data with many plotting facilities interacting with the mapping and visualization packages mapview, tmap, and ggplot2.

rgdal can read and write GIS data in many formats and returns objects of one of the above classes. The functions to read GIS files are readGDAL for rasters and readOGR for geometrical data. They have many options including the possibility to read a subset of the data from GIS files (which can be very big).

raster has its own data classes making the manipulation and plotting of rasters easy. It can store and manipulate several large rasters using disk cache (see p. 78). Its companion package rasterVis has many graphical functions for flexible graphical display.

maps provides low-resolution databases of the world. It has a very simple function to draw maps; for instance, mapO draws a low-resolution map of the world with the main country boundaries, mapdata provides databases with higher resolutions (more suitable to produce final maps for publication), and mapproj includes more than thirty projections for drawing maps.

# Calculating Geographical Distances

Maybe one of the most common operations with geographical data is to calculate distances between two locations or individuals. This is often done with GPS coordinates (longitude and latitude). Consider Montpellier in the South of France which has approximate coordinates N 43°36', E 4°00'. What is the distance from this city to a place located three degrees eastward (close to Cannes)? This can be calculated with the geodesic distance which involves angular calculations. It is implemented in the function geod in pegas which takes as arguments two vectors of coordinates (or a matrix with two columns) and returns a (symmetric) matrix with the distances in kilometers (km):

> Ion <- c(4, 7); lat <- c(43.6, 43.6)

> geod(lon, lat)

**[,1] [,2]**

[1,] 0.0000 241.5596

[2,] 241.5596 0.0000

What is the distance separating two locations with the same longitudes but on the equator?

> geod(lon, c(0, 0))

[,1] [,2]

[1,] 0.0000 333.5848

[2,] 333.5848 0.0000

The calculations must be done with coordinates in decimal degrees; however, geographical coordinates are usually stored in angular units. To help convert these data, pegas also provides the function geoTrans which is quite flexible with respect to the input format and outputs the coordinates in decimal degrees:

> geoTrans(c("N 34°36’", "S 34°36’"))

[1] 34.6 -34.6

> geoTrans(c("E 4°00^{,}"_{)} "W 4°00’"))

[1] 4 -4

If the coordinates are in UTM, then simple Euclidean distances can be used because the squares of the UTM system are approximately flat (Fig. 8.1).

In natural populations, geographical distances are not always meaningful to quantify the connections among them. The package gdistance computes general distance matrices using GIS data that must be in the class of the package raster. The operation proceeds along several steps. The first step is to have a raster with the appropriate variable(s) to calculate the distances (altitude, land cover, ...) The second step is to define transitions among the cells of the raster with the function transition and a function that will define transitions depending, for instance, on land cover and/or altitude, and so on. The third step is to correct for geographical bias with the function geoCorrection because the transitions among cells do not have the same distance (particularly because of the different sizes of the cells related to latitude; see above the two examples of geodesic distance calculations). The fourth step is to calculate distances among a set of locations given the coordinates on the raster and the above transition definitions.