map2SpatialPolygons: Convert map objects to sp classes

Description Usage Arguments Details Value Note Author(s) See Also Examples

View source: R/spmaps.R

Description

These functions may be used to convert map objects returned by the map function in the maps package to suitable objects defined in the sp package. In the examples below, arguments are shown for retrieving first polygons by name, then lines by window.

Usage

1
2
3
map2SpatialPolygons(map, IDs, proj4string = CRS(as.character(NA)), checkHoles=FALSE)
map2SpatialLines(map, IDs=NULL, proj4string = CRS(as.character(NA)))
pruneMap(map, xlim=NULL, ylim=NULL)

Arguments

map

a map object defined in the maps package and returned by the map function

IDs

Unique character ID values for each output Polygons object; the input IDs can be an integer or character vector with duplicates, where the duplicates will be combined as a single output Polygons object

proj4string

Object of class "CRS"; holding a valid proj4 string

checkHoles

default=FALSE, if TRUE call checkPolygonsHolesinternally to check hole assignment, (by default no polygon objects are holes)

xlim,ylim

limits for pruning a map object - should only be used for lines, because polygons will not be closed

Details

Any zero area output geometries are dropped, and warnings are issued.

Value

map2SpatialPolygons returns a SpatialPolygons object and map2SpatialLines returns a SpatialLines object (objects defined in the sp package); pruneMap returns a modified map object defined in the maps package

Note

As the examples show, retrieval by name should be checked to see whether a window is not also needed: the "norway" polygons include "Norway:Bouvet Island", which is in the South Atlantic. Here, the IDs argument is set uniformly to "Norway" for all the component polygons, so that the output object contains a single Polygons object with multiple component Polygon objects. When retrieving by window, pruning may be needed on lines which are included because they begin within the window; interior=FALSE is used to remove country boundaries in this case.

Author(s)

Roger Bivand

See Also

map

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
44
45
46
47
48
49
50
51
52
53
run <- FALSE
if(require(maps)) run <- TRUE
if (run) {
nor_coast_poly <- map("world", "norway", fill=TRUE, col="transparent",
 plot=FALSE)
range(nor_coast_poly$x, na.rm=TRUE)
}
if (run) {
range(nor_coast_poly$y, na.rm=TRUE)
}
if (run) {
nor_coast_poly <- map("world", "norway", fill=TRUE, col="transparent",
 plot=FALSE, ylim=c(58,72))
nor_coast_poly$names
}
if (run) {
IDs <- sapply(strsplit(nor_coast_poly$names, ":"), function(x) x[1])
}
if (run) {
nor_coast_poly_sp <- map2SpatialPolygons(nor_coast_poly, IDs=IDs,
 proj4string=CRS("+proj=longlat +datum=WGS84"))
sapply(slot(nor_coast_poly_sp, "polygons"),
 function(x) length(slot(x, "Polygons")))
}
if (run) {
plot(nor_coast_poly_sp, col="grey", axes=TRUE)
}
if (run) {
nor_coast_lines <- map("world", interior=FALSE, plot=FALSE, xlim=c(4,32),
 ylim=c(58,72))
plot(nor_coast_lines, type="l")
}
if (run) {
nor_coast_lines <- pruneMap(nor_coast_lines, xlim=c(4,32), ylim=c(58,72))
lines(nor_coast_lines, col="red")
nor_coast_lines_sp <- map2SpatialLines(nor_coast_lines,
 proj4string=CRS("+proj=longlat +datum=WGS84"))
plot(nor_coast_poly_sp, col="grey", axes=TRUE)
}
if (run) {
plot(nor_coast_lines_sp, col="blue", add=TRUE)
}
if (run) {
worldmap <- map("world", fill=TRUE, plot=FALSE)
worldmapPolys <- map2SpatialPolygons(worldmap, 
 IDs=sapply(strsplit(worldmap$names, ":"), "[", 1L), 
 proj4string=CRS("+proj=longlat +datum=WGS84"))
if (rgeosStatus()) {
require(rgeos)
vals <- rgeos::gIsValid(worldmapPolys, byid=TRUE, reason=TRUE)
print(table(sapply(strsplit(vals, "\\["), "[", 1)))
}
}

Example output

Loading required package: sp
Checking rgeos availability: FALSE
 	Note: when rgeos is not available, polygon geometry 	computations in maptools depend on gpclib,
 	which has a restricted licence. It is disabled by default;
 	to enable gpclib, type gpclibPermit()
Loading required package: maps
[1] -9.098877 33.629295
[1] 58.02095 80.47783
 [1] "Norway:1"           "Norway:2"           "Norway:Smola"      
 [4] "Norway:Froya"       "Norway:5"           "Norway:6"          
 [7] "Norway:7"           "Norway:8"           "Norway:Moskenesoya"
[10] "Norway:Vestvagoy"   "Norway:Langoya"     "Norway:Hinnoya"    
[13] "Norway:Senja"       "Norway:14"          "Norway:Arnoy"      
[16] "Norway:16"          "Norway:Vannoy"      "Norway:Seiland"    
[19] "Norway:Kvaloya"     "Norway:Soroya"      "Norway"            
[22] "Norway:Mageroya"    "Norway:Jan Mayen"  
[1] 23

maptools documentation built on Sept. 7, 2021, 5:09 p.m.