grd_build: Generate a grid from SpatialPolygonsDataFrame.

Description Usage Arguments Details Value Author(s) References Examples

View source: R/grd_build.R

Description

The function generates a grid from pseudo mercator or web Mercator (epsg:3857) projection system. It is recommended to use metrics projection to preserve areas. First, transform mappol into web Mercator projection. Then, the distances between the four coordinates given by the bounding box are calculated and divided by resolution argument to determine the number of cells per direction. Finally, GridTopology is applied to generate the coordinates that create the final object of class SpatialPolygonsDataFrame.

Usage

1
2
3
grd_build(mappol, keep.p4s = TRUE, tol = 100, TP = TRUE,
  resolution = 20, r.ceiling = TRUE, g.int = TRUE,
  pretty.int = FALSE)

Arguments

mappol

A SpatialPolygonsDataFrame object. bbox(mappol) should be different from NULL. See help(bbox) for more details.

keep.p4s

Logical. If TRUE (by default) the final grid polygon keep proj4string of original polygon. If FALSE the final grid polygon modify the proj4string to sp::CRS("+init=epsg:3857").

tol

Argument pass to gSimplify(). By default Tol = 100. See help(gSimplify) for more details.

TP

Argument pass to gSimplify(). This directs to topologyPreserve argument in gSimplify function. See help(gSimplify) for more details.

resolution

Double greater than 0. Units are in Km. Default resolution = 20.

r.ceiling

Logical. If TRUE the distances are ceiling rounded. If FALSE the distances are floor rounded.

g.int

Logical. If TRUE the grid's cells that intersects mappol are returned. If FALSE all grid's cells in the mappol bounding box are returned.

pretty.int

Logical (FALSE by default). If g.int=TRUE and pretty.int=TRUE the grid's cells that intersects mappol's boundaries are reshape for better visualization. Requires g.int=TRUE. This option expend more computing time.

Details

This function generates a grid from SpatialPolygonsDataFrame object. The main steps are: projection system transformation, geometry simplification, bounding box extraction, distance calculation, calculation of cells per direction (Longitude, Latitude), grid topology creation, class reassignment of grid topology into SpatialPolygons object and compute the intersected polygons between grid and mappol. Finally, the returned object is a SpatialPolygonsDataFrame object.

spTransform (see help(spTransform) for more details) is applied for transformation of mappol into a metric projection (by default web mercator, epsg:3857).

gSimplify (see help(gSimplify) for more details) is applied for simplify geometry of transformed polygon in order to reduce computation time and memory size.

bbox (see help(bbox) for more details) is applied to extract the bounding box of the transformed and simplify mappol. spDists (see help(spDists) for more details) is applied for distances calculation. ceiling and floor is applied for rounding options.

GridTopology (see help(GridTopology) for more details) is applied to generate the grid. Then, as.SpatialPolygons.GridTopology is applied to transform the GridTopology object into SpatialPolygonsDataFrame.

Finally, gIntersects (see help(gIntersects) for more details) is applied to extract the grid's cells that intersects mappol. gContains and gIntersection (see help(gContains) and help(gIntersection) for more details) is applied to extract the grid's cells that intersects mappol and generate a pretty visualization. The latter method may require considerable computing time depending on the number of coordinates contained in the mappol polygon.

Value

A SpatialPolygonsDataFrame object. It includes all the polygons that form the grid. Data slot contains the identifiers for each cells.

Author(s)

Enrique Del Callejo Canal ([email protected]), based on implemented algortihms in web platform SPECIES (see References).

References

http://species.conabio.gob.mx/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
library(sp)
library(rgeos)
library(rgdal)
library(raster)
data(Mex0)

# Whithout pretty intersection
# Default resolution is 20 km.

system.time(Mex0.grd<-grd_build(Mex0))
plot(Mex0.grd)
plot(Mex0, add = TRUE)

# ggplot2 interaction

library(ggplot2)
library(mapproj)

ggplot(data = fortify(Mex0.grd), aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "white") +
    coord_quickmap()

# leaflet interaction

library(leaflet)
leaflet() %>%
    addProviderTiles('OpenStreetMap.Mapnik',
    options = providerTileOptions(noWrap = TRUE))%>%
    addPolygons(data=Mex0.grd, stroke=TRUE, color = '#FFFFFF',
    layerId = Mex0.grd@data$ID, weight = 1, opacity = 0.3,
    fillColor = '#A9A9A9', fillOpacity = 0.6,
    popup = row.names(Mex0.grd@data))

# With Pretty options

system.time(Mex0.grd.p<-grd_build(Mex0, pretty.int=TRUE))
plot(Mex0.grd.p)

# ggplot2 interaction

ggplot(data = fortify(Mex0.grd.p), aes(x = long, y = lat, group = group)) +
    geom_polygon(colour = "white") +
    coord_map(projection = "mercator")

edelcallejoc/rspecies documentation built on Jan. 22, 2019, 5:50 a.m.