plotKML: Methods for plotting results of spatial analysis in Google...

Description Usage Arguments Details Note See Also Examples

Description

The method writes inputs and outputs of spatial analysis (a list of point, gridded and/or polygon data usually) to KML and opens the KML file in Google Earth (or any other default package used to view KML/KMZ files).

Usage

  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
## S4 method for signature 'SpatialPointsDataFrame'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))),
      file.name = paste(folder.name, ".kml", sep=""), 
      size, colour, points_names, 
      shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png", 
      metadata = NULL, kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SpatialLinesDataFrame'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""),
      metadata = NULL, kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SpatialPolygonsDataFrame'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      colour, plot.labpt, labels, metadata = NULL, 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SpatialPixelsDataFrame'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      colour, raster_name, metadata = NULL, kmz = FALSE, open.kml = TRUE, ...)
## S4 method for signature 'SpatialGridDataFrame'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      colour, raster_name, metadata = NULL, kmz = FALSE, open.kml = TRUE, ...)
## S4 method for signature 'RasterLayer'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      colour, raster_name, metadata = NULL, kmz = FALSE, open.kml = TRUE, ...)
## S4 method for signature 'SpatialPhotoOverlay'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      dae.name, kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SoilProfileCollection'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      var.name, metadata = NULL, kmz = get("kmz", envir = plotKML.opts), 
      open.kml = TRUE, ...)
## S4 method for signature 'STIDF'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      colour, shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png", 
      points_names, kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'STFDF'
plotKML(obj, ...)
## S4 method for signature 'STSDF'
plotKML(obj, ...)
## S4 method for signature 'STTDF'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      colour, start.icon = "http://maps.google.com/mapfiles/kml/pal2/icon18.png", 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'RasterBrickTimeSeries'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""),
      pngwidth = 680, pngheight = 180, pngpointsize = 14, 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'RasterBrickSimulations'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      obj.summary = TRUE,
      pngwidth = 680, pngheight = 200, pngpointsize = 14, 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SpatialMaxEntOutput'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), 
      html.file = obj@maxent@html, 
      iframe.width = 800, iframe.height = 800, pngwidth = 280, 
      pngheight = 280, pngpointsize = 14, colour, 
      shape = "http://plotkml.r-forge.r-project.org/icon17.png", 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, 
      TimeSpan.begin = obj@TimeSpan.begin, TimeSpan.end = obj@TimeSpan.end, ...)
## S4 method for signature 'SpatialPredictions'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), colour, 
      grid2poly = FALSE, obj.summary = TRUE, plot.svar = FALSE, 
      pngwidth = 210, pngheight = 580, pngpointsize = 14, 
      metadata = NULL, kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SpatialSamplingPattern'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""),
      colour, kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'SpatialVectorsSimulations'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env = parent.frame()))), 
      file.name = paste(folder.name, ".kml", sep=""), colour, 
      grid2poly = FALSE, obj.summary = TRUE, plot.svar = FALSE, 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)
## S4 method for signature 'list'
plotKML(obj, 
      folder.name = normalizeFilename(deparse(substitute(obj, env=parent.frame()))),
      file.name = paste(folder.name, ".kml", sep=""), 
      size = NULL, colour, points_names = "",
      shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png",
      plot.labpt = TRUE, labels = "", metadata = NULL, 
      kmz = get("kmz", envir = plotKML.opts), open.kml = TRUE, ...)

Arguments

obj

input object of specific class; either some sp, or raster or spacetime package class object, or plotKML composite objects containing both inputs and outputs of analysis

folder.name

character; folder name in the KML file

file.name

character; output KML file name

size

for point objects for plotting (see aesthetics)

colour

colour variable for plotting (see aesthetics)

points_names

vector of characters that can be used as labels

shape

character; icons used for plotting (see aesthetics)

raster_name

(optional) specify the output file name (PNG)

var.name

target variable name (only valid for visualization of "SoilProfileCollection" class data

metadata

(optional) the metadata object

plot.labpt

logical; specifies whether to plot centroids for polygon data

labels

character vector; list of labels that will attached to the centroids

start.icon

icon for the start position (for trajectory data)

dae.name

output DAE file name

html.file

specify the location of the html file containing report data (if the input object is of class "SpatialMaxEntOutput")

iframe.width

integer; width of the screen for iframe

iframe.height

integer; height of the screen for iframe

TimeSpan.begin

object of class "POSIXct"; begin of the sampling period

TimeSpan.end

object of class "POSIXct"; end of the sampling period

pngwidth

integer; width of the PNG plot (screen image)

pngheight

integer; height of the PNG plot (screen image)

pngpointsize

integer; text size in the PNG plot (screen image)

grid2poly

logical; specifies whether to convert gridded object to polygons

obj.summary

logical; specifies whether to print the object summary

plot.svar

logical; specifies whether to plot the model uncertainty

kmz

logical; specifies whether to compress the output KML file

open.kml

logical; specifies whether to directly open the output KML file (i.e. in Google Earth)

...

(optional) arguments passed to the lower level functions

Details

This is a generic function to plot various spatial and spatio-temporal R objects that contain both inputs and outputs of spatial analysis. The resulting plots (referred to as ‘views’) are expected to be cartographically complete as they should contain legends, and data and model descriptions. In principle, plotKML works with both simple spatial objects, and complex objects such as "SpatialPredictions", "SpatialVectorsSimulations", "RasterBrickSimulations", "RasterBrickTimeSeries", "SpatialMaxEntOutput" and similar. To further customize visualizations consider combining the lower level functions kml_open, kml_close, kml_compress, kml_screen into your own plotKML() method.

All ST-classes are coerced to the STIDF format and hence use the plotKML method for STIDFs.

Note

To prepare a list of objects of class "SpatialPointsDataFrame", "SpatialLinesDataFrame", "SpatialPolygonsDataFrame", or "SpatialPixelsDataFrame" consider using the GSIF::tile function. Writting large spatial objects via plotKML can be time consuming. Please refer to the package manual for more information.

See Also

SpatialPredictions-class, SpatialVectorsSimulations-class, RasterBrickSimulations-class, RasterBrickTimeSeries-class, SpatialMaxEntOutput-class, SpatialSamplingPattern-class

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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
plotKML.env(silent = FALSE, kmz = FALSE)
## -------------- SpatialPointsDataFrame --------- ##
library(sp)
library(rgdal)
data(eberg)
coordinates(eberg) <- ~X+Y
proj4string(eberg) <- CRS("+init=epsg:31467")
## subset to 20 percent:
eberg <- eberg[runif(nrow(eberg))<.1,]
## Not run: ## bubble type plot:
plotKML(eberg["CLYMHT_A"])
plotKML(eberg["CLYMHT_A"], colour_scale=rep("#FFFF00", 2), points_names="")

## End(Not run)
## plot points with a legend:
shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png" 
kml_open("eberg_CLYMHT_A.kml")
kml_layer(eberg["CLYMHT_A"], colour=CLYMHT_A, z.lim=c(20,60),
   colour_scale=SAGA_pal[[1]], shape=shape, points_names="")
kml_legend.bar(x=eberg$CLYMHT_A, legend.file="kml_legend.png",
   legend.pal=SAGA_pal[[1]], z.lim=c(20,60))
kml_screen(image.file="kml_legend.png")
kml_close("eberg_CLYMHT_A.kml")

## -------------- SpatialLinesDataFrame --------- ##
data(eberg_contours)
## Not run: 
plotKML(eberg_contours)
## plot contour lines with actual altitudes:
plotKML(eberg_contours, colour=Z, altitude=Z)

## End(Not run)

## -------------- SpatialPolygonsDataFrame --------- ##
data(eberg_zones)
## Not run: 
plotKML(eberg_zones["ZONES"])
## add altitude:
zmin = 230
plotKML(eberg_zones["ZONES"], altitude=zmin+runif(length(eberg_zones))*500)

## End(Not run)

## -------------- SpatialPixelsDataFrame --------- ##
library(rgdal)
library(raster)
data(eberg_grid)
gridded(eberg_grid) <- ~x+y
proj4string(eberg_grid) <- CRS("+init=epsg:31467")
TWI <- reproject(eberg_grid["TWISRT6"])
data(SAGA_pal)
## Not run: ## set limits manually (increase resolution):
plotKML(TWI, colour_scale = SAGA_pal[[1]])
plotKML(TWI, z.lim=c(12,20), colour_scale = SAGA_pal[[1]])

## End(Not run)
## categorical data:
eberg_grid$LNCCOR6 <- as.factor(paste(eberg_grid$LNCCOR6))
levels(eberg_grid$LNCCOR6)
data(worldgrids_pal)
## attr(worldgrids_pal["corine2k"][[1]], "names")
pal = as.character(worldgrids_pal["corine2k"][[1]][c(1,11,13,14,16,17,18)])
LNCCOR6 <- reproject(eberg_grid["LNCCOR6"])
## Not run: 
plotKML(LNCCOR6, colour_scale=pal)

## End(Not run)

## -------------- SpatialPhotoOverlay --------- ##
## Not run: 
library(RCurl)
imagename = "Soil_monolith.jpg"
urlExists = url.exists("http://commons.wikimedia.org")
if(urlExists){
  x1 <- getWikiMedia.ImageInfo(imagename)
  sm <- spPhoto(filename = x1$url$url, exif.info = x1$metadata)
  # str(sm)
  plotKML(sm)
}

## End(Not run)

## -------------- SoilProfileCollection --------- ##
library(aqp)
library(plyr)
## sample profile from Nigeria:
lon = 3.90; lat = 7.50; id = "ISRIC:NG0017"; FAO1988 = "LXp" 
top = c(0, 18, 36, 65, 87, 127) 
bottom = c(18, 36, 65, 87, 127, 181)
ORCDRC = c(18.4, 4.4, 3.6, 3.6, 3.2, 1.2)
hue = c("7.5YR", "7.5YR", "2.5YR", "5YR", "5YR", "10YR")
value = c(3, 4, 5, 5, 5, 7); chroma = c(2, 4, 6, 8, 4, 3)
## prepare a SoilProfileCollection:
prof1 <- join(data.frame(id, top, bottom, ORCDRC, hue, value, chroma), 
   data.frame(id, lon, lat, FAO1988), type='inner')
prof1$soil_color <- with(prof1, munsell2rgb(hue, value, chroma))
depths(prof1) <- id ~ top + bottom
site(prof1) <- ~ lon + lat + FAO1988 
coordinates(prof1) <- ~ lon + lat
proj4string(prof1) <- CRS("+proj=longlat +datum=WGS84")
prof1
## Not run: 
plotKML(prof1, var.name="ORCDRC", color.name="soil_color")

## End(Not run)

## -------------- STIDF --------- ##
library(sp)
library(spacetime)
## daily temperatures for Croatia:
data(HRtemp08)
## format the time column:
HRtemp08$ctime <- as.POSIXct(HRtemp08$DATE, format="%Y-%m-%dT%H:%M:%SZ")
## create a STIDF object:
sp <- SpatialPoints(HRtemp08[,c("Lon","Lat")])
proj4string(sp) <- CRS("+proj=longlat +datum=WGS84")
HRtemp08.st <- STIDF(sp, time = HRtemp08$ctime, data = HRtemp08[,c("NAME","TEMP")])
## subset to first 500 records:
HRtemp08_jan <- HRtemp08.st[1:500]
str(HRtemp08_jan)
## Not run: 
plotKML(HRtemp08_jan[,,"TEMP"], LabelScale = .4)

## End(Not run)

## foot-and-mouth disease data:
data(fmd)
fmd0  <- data.frame(fmd)
coordinates(fmd0) <- c("X", "Y")
proj4string(fmd0) <- CRS("+init=epsg:27700")
fmd_sp <- as(fmd0, "SpatialPoints")
dates <- as.Date("2001-02-18")+fmd0$ReportedDay
library(spacetime)
fmd_ST <- STIDF(fmd_sp, dates, data.frame(ReportedDay=fmd0$ReportedDay))
data(SAGA_pal)
## Not run: 
plotKML(fmd_ST, colour_scale=SAGA_pal[[1]])

## End(Not run)

## -------------- STFDF --------- ##

## results of krigeST:
library(gstat)
library(sp)
library(spacetime)
library(raster)
## define space-time variogram
sumMetricVgm <- vgmST("sumMetric",
                      space=vgm( 4.4, "Lin", 196.6,  3),
                      time =vgm( 2.2, "Lin",   1.1,  2),
                      joint=vgm(34.6, "Exp", 136.6, 12),
                      stAni=51.7)
## example from the gstat package:
data(air)
rural = STFDF(stations, dates, data.frame(PM10 = as.vector(air)))
rr <- rural[,"2005-06-01/2005-06-03"]
rr <- as(rr,"STSDF")
x1 <- seq(from=6,to=15,by=1)
x2 <- seq(from=48,to=55,by=1)
DE_gridded <- SpatialPoints(cbind(rep(x1,length(x2)), rep(x2,each=length(x1))), 
                           proj4string=CRS(proj4string(rr@sp)))
gridded(DE_gridded) <- TRUE
DE_pred <- STF(sp=as(DE_gridded,"SpatialPoints"), time=rr@time)
DE_kriged <- krigeST(PM10~1, data=rr, newdata=DE_pred,
                     modelList=sumMetricVgm)
gridded(DE_kriged@sp) <- TRUE
stplot(DE_kriged)
## plot in Google Earth:
z.lim = range(DE_kriged@data, na.rm=TRUE)
## Not run: 
plotKML(DE_kriged, z.lim=z.lim)
## add observations points:
plotKML(rr, z.lim=z.lim)

## End(Not run)

## -------------- STTDF --------- ##
## Not run: 
library(fossil)
library(spacetime)
library(adehabitat)
data(gpxbtour)
## format the time column:
gpxbtour$ctime <- as.POSIXct(gpxbtour$time, format="%Y-%m-%dT%H:%M:%SZ")
coordinates(gpxbtour) <- ~lon+lat
proj4string(gpxbtour) <- CRS("+proj=longlat +datum=WGS84")
xy <- as.list(data.frame(t(coordinates(gpxbtour))))
gpxbtour$dist.km <- sapply(xy, function(x) { 
  deg.dist(long1=x[1], lat1=x[2], long2=xy[[1]][1], lat2=xy[[1]][2]) 
} )
## convert to a STTDF class:
gpx.ltraj <- as.ltraj(coordinates(gpxbtour), gpxbtour$ctime, id = "th")
gpx.st <- as(gpx.ltraj, "STTDF")
gpx.st$speed <- gpxbtour$speed
gpx.st@sp@proj4string <- CRS("+proj=longlat +datum=WGS84")
str(gpx.st)
plotKML(gpx.st, colour="speed")

## End(Not run)

## -------------- Spatial Metadata --------- ##
## Not run: 
eberg.md <- spMetadata(eberg, xml.file=system.file("eberg.xml", package="plotKML"),
  Target_variable="SNDMHT_A", Citation_title="Ebergotzen profiles")
plotKML(eberg[1:100,"CLYMHT_A"], metadata=eberg.md)

## End(Not run)

## -------------- RasterBrickTimeSeries --------- ##
library(raster)
library(sp)
data(LST)
gridded(LST) <- ~lon+lat
proj4string(LST) <- CRS("+proj=longlat +datum=WGS84")
dates <- sapply(strsplit(names(LST), "LST"), function(x){x[[2]]})
datesf <- format(as.Date(dates, "%Y_%m_%d"), "%Y-%m-%dT%H:%M:%SZ")
## begin / end dates +/- 4 days:
TimeSpan.begin = as.POSIXct(unclass(as.POSIXct(datesf))-4*24*60*60, origin="1970-01-01") 
TimeSpan.end = as.POSIXct(unclass(as.POSIXct(datesf))+4*24*60*60, origin="1970-01-01")
## pick climatic stations in the area:
pnts <- HRtemp08[which(HRtemp08$NAME=="Pazin")[1],]
pnts <- rbind(pnts, HRtemp08[which(HRtemp08$NAME=="Crni Lug - NP Risnjak")[1],])
pnts <- rbind(pnts, HRtemp08[which(HRtemp08$NAME=="Cres")[1],])
coordinates(pnts) <- ~Lon + Lat
proj4string(pnts) <- CRS("+proj=longlat +datum=WGS84")
## get the dates from the file names:
LST_ll <- brick(LST[1:5])
LST_ll@title = "Time series of MODIS Land Surface Temperature images"
LST.ts <- new("RasterBrickTimeSeries", variable = "LST", sampled = pnts, 
    rasters = LST_ll, TimeSpan.begin = TimeSpan.begin[1:5], 
    TimeSpan.end = TimeSpan.end[1:5])
data(SAGA_pal)
## Not run: ## plot MODIS images in Google Earth:
plotKML(LST.ts, colour_scale=SAGA_pal[[1]])

## End(Not run)

## -------------- Spatial Predictions --------- ##
library(sp)
library(rgdal)
library(gstat)
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")
## load grids:
data(meuse.grid)
gridded(meuse.grid) <- ~x+y
proj4string(meuse.grid) <- CRS("+init=epsg:28992")
## Not run: ## fit a model:
library(GSIF)
omm <- fit.gstatModel(observations = meuse, formulaString = om~dist, 
   family = gaussian(log), covariates = meuse.grid)
## produce SpatialPredictions:
om.rk <- predict(omm, predictionLocations = meuse.grid)
## plot the whole geostatical mapping project in Google Earth:
plotKML(om.rk, colour_scale = SAGA_pal[[1]])
## plot each cell as polygon:
plotKML(om.rk, colour_scale = SAGA_pal[[1]], grid2poly = TRUE)

## End(Not run)

## -------------- SpatialSamplingPattern --------- ##
## Not run: 
library(spcosa)
library(sp)
## read a polygon map:
shpFarmsum <- readOGR(dsn = system.file("maps", package = "spcosa"), 
  layer = "farmsum")
## stratify `Farmsum' into 50 strata
myStratification <- stratify(shpFarmsum, nStrata = 50)
## sample two sampling units per stratum
mySamplingPattern <- spsample(myStratification, n = 2)
## attach the correct proj4 string:
library(RCurl)
urlExists = url.exists("http://spatialreference.org/ref/sr-org/6781/proj4/")
if(urlExists){
  nl.rd <- getURL("http://spatialreference.org/ref/sr-org/6781/proj4/")
  proj4string(mySamplingPattern@sample) <- CRS(nl.rd) 
  # prepare spatial domain (polygons):
  sp.domain <- as(myStratification@cells, "SpatialPolygons")
  sp.domain <- SpatialPolygonsDataFrame(sp.domain, 
     data.frame(ID=as.factor(myStratification@stratumId)), match.ID = FALSE)
  proj4string(sp.domain) <- CRS(nl.rd) 
  # create new object:
  mySamplingPattern.ssp <- new("SpatialSamplingPattern", 
     method = class(mySamplingPattern), pattern = mySamplingPattern@sample, 
     sp.domain = sp.domain)
  # the same plot now in Google Earth:
  shape = "http://maps.google.com/mapfiles/kml/pal2/icon18.png"
  plotKML(mySamplingPattern.ssp, shape = shape)
}

## End(Not run)

## -------------- RasterBrickSimulations --------- ##
## Not run: 
library(sp)
library(gstat)
data(barxyz)
## define the projection system:
prj = "+proj=tmerc +lat_0=0 +lon_0=18 +k=0.9999 +x_0=6500000 +y_0=0 
  +ellps=bessel +units=m 
  +towgs84=550.499,164.116,475.142,5.80967,2.07902,-11.62386,0.99999445824"
coordinates(barxyz) <- ~x+y
proj4string(barxyz) <- CRS(prj)
data(bargrid)
coordinates(bargrid) <- ~x+y
gridded(bargrid) <- TRUE
proj4string(bargrid) <- CRS(prj)
## fit a variogram and generate simulations:
Z.ovgm <- vgm(psill=1352, model="Mat", range=650, nugget=0, kappa=1.2)
sel <- runif(length(barxyz$Z))<.2  
## Note: this operation can be time consuming
sims <- krige(Z~1, barxyz[sel,], bargrid, model=Z.ovgm, nmax=20, 
   nsim=10, debug.level=-1)
## specify the cross-section:
t1 <- Line(matrix(c(bargrid@bbox[1,1], bargrid@bbox[1,2], 5073012, 5073012), ncol=2))
transect <- SpatialLines(list(Lines(list(t1), ID="t")), CRS(prj))
## glue to a RasterBrickSimulations object:
library(raster)
bardem_sims <- new("RasterBrickSimulations", variable = "elevations", 
  sampled = transect, realizations = brick(sims))
## plot the whole project and open in Google Earth:
data(R_pal)
plotKML(bardem_sims, colour_scale = R_pal[[4]])

## End(Not run)

## -------------- SpatialVectorsSimulations --------- ##
## Not run: 
data(barstr)
data(bargrid)
library(sp)
coordinates(bargrid) <- ~ x+y
gridded(bargrid) <- TRUE
## output topology:
cell.size = bargrid@grid@cellsize[1]
bbox = bargrid@bbox
nrows = round(abs(diff(bbox[1,])/cell.size), 0) 
ncols = round(abs(diff(bbox[2,])/cell.size), 0)
gridT = GridTopology(cellcentre.offset=bbox[,1], 
  cellsize=c(cell.size,cell.size), 
  cells.dim=c(nrows, ncols))
bar_sum <- count.GridTopology(gridT, vectL=barstr[1:5])
## NOTE: this operation can be time consuming!
## plot the whole project and open in Google Earth:
plotKML(bar_sum)

## End(Not run)

## -------------- SpatialMaxEntOutput --------- ##
## Not run: 
library(maptools)
library(rgdal)
data(bigfoot)
aea.prj <- "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 
   +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"
data(USAWgrids)
gridded(USAWgrids) <- ~s1+s2
proj4string(USAWgrids) <- CRS(aea.prj)
bbox <- spTransform(USAWgrids, CRS("+proj=longlat +datum=WGS84"))@bbox
sel = bigfoot$Lon > bbox[1,1] & bigfoot$Lon < bbox[1,2] &
    bigfoot$Lat > bbox[2,1] & bigfoot$Lat < bbox[2,2]
bigfoot <- bigfoot[sel,]
coordinates(bigfoot) <- ~Lon+Lat
proj4string(bigfoot) <- CRS("+proj=longlat +datum=WGS84")
library(spatstat)
bigfoot.aea <- as.ppp(spTransform(bigfoot, CRS(aea.prj)))
## Load the covariates:
sel.grids <- c("globedem","nlights03","sdroads","gcarb","twi","globcov")
library(GSIF)
library(dismo)
## run MaxEnt analysis:
jar <- paste(system.file(package="dismo"), "/java/maxent.jar", sep='')
if(file.exists(jar)){
  bigfoot.smo <- MaxEnt(bigfoot.aea, USAWgrids[sel.grids])
  icon = "http://plotkml.r-forge.r-project.org/bigfoot.png"
  data(R_pal)
  plotKML(bigfoot.smo, colour_scale = R_pal[["bpy_colors"]], shape = icon)
}

## End(Not run)

plotKML documentation built on May 29, 2017, 9:36 p.m.