The function sets up metadata in the form of a SpatialGrid object for defining the size and placing of a PNG image overlay in Google Earth. The internal function Sobj_SpatialGrid can also be called to build a grid for arbitrary Spatial* objects.

GE_SpatialGrid(obj, asp = NA, maxPixels = 600)
Sobj_SpatialGrid(obj, asp=1, maxDim=100, n=NULL)

Arguments

obj

a Spatial* object

asp

if NA, will be set to the latitude corrected value

maxPixels

the maximum dimension of the output PNG

maxDim

the maximum dimension of the output grid; ignored if n not NULL

n

if not NULL, the minimum number of cells in the returned grid

Details

The function is used together with kmlOverlay to wrap around the opening of a PNG graphics device, plotting code, and the closing of the device. The computed values take account of the adjustment of the actual data bounding box to an integer number of rows and columns in the image file.

The approach may be used as an alternative to writing PNG files from SpatialGrid and SpatialPixel objects in rgdal using writeGDAL, and to writing KML files using writeOGR for vector data objects. The output PNG files are likely to be very much smaller than large vector data KML files, and hinder the retrieval of exact positional information.

Note that the geometries should be in geographical coordinates with datum WGS84 for export to KML.

Value

returns an S3 object of class GE_SG with components:

height

Integer raster height for png call

width

Integer raster width for png call

SG

a SpatialGrid object with the grid topology of the output PNG

asp

the aspect value used

xlim

xlim taken from SG

ylim

ylim taken from SG

Author

Duncan Golicher, David Forrest and Roger Bivand

See also

Examples

opt_exask <- options(example.ask=FALSE)
qk <- SpatialPointsDataFrame(quakes[, c(2:1)], quakes)
summary(Sobj_SpatialGrid(qk)$SG)
#> Object of class SpatialGrid
#> Coordinates:
#>      min      max
#> x 165.67 188.2447
#> y -38.59 -10.7200
#> Is projected: NA 
#> proj4string : [NA]
#> Grid attributes:
#>   cellcentre.offset cellsize cells.dim
#> x         165.80935   0.2787        81
#> y         -38.45065   0.2787       100
t2 <- Sobj_SpatialGrid(qk, n=10000)$SG
summary(t2)
#> Object of class SpatialGrid
#> Coordinates:
#>      min      max
#> x 165.67 188.3144
#> y -38.59 -10.7200
#> Is projected: NA 
#> proj4string : [NA]
#> Grid attributes:
#>   cellcentre.offset  cellsize cells.dim
#> x         165.79442 0.2488393        91
#> y         -38.46558 0.2488393       112
prod(slot(slot(t2, "grid"), "cells.dim"))
#> [1] 10192
proj4string(qk) <- CRS("+proj=longlat +ellps=WGS84")
tf <- tempfile()
SGqk <- GE_SpatialGrid(qk)
png(file=paste(tf, ".png", sep=""), width=SGqk$width, height=SGqk$height,
  bg="transparent")
par(mar=c(0,0,0,0), xaxs="i", yaxs="i")
plot(qk, xlim=SGqk$xlim, ylim=SGqk$ylim, setParUsrBB=TRUE)
dev.off()
#> agg_png 
#>       2 
kmlOverlay(SGqk, paste(tf, ".kml", sep=""), paste(tf, ".png", sep=""))
#> [1] "<?xml version='1.0' encoding='UTF-8'?>"                                                                           
#> [2] "<kml xmlns='http://earth.google.com/kml/2.0'>"                                                                    
#> [3] "<GroundOverlay>"                                                                                                  
#> [4] "<name>R image</name>"                                                                                             
#> [5] "<Icon><href>/tmp/RtmpY1blZ9/file8a34d5620b091.png</href><viewBoundScale>0.75</viewBoundScale></Icon>"             
#> [6] "<LatLonBox><north>-10.72</north><south>-38.59</south><east>188.158103454744</east><west>165.67</west></LatLonBox>"
#> [7] "</GroundOverlay></kml>"                                                                                           
if (FALSE) {
qk0 <- quakes
qk0$long <- ifelse(qk0$long <= 180, qk0$long, qk0$long-360)
qk0a <- SpatialPointsDataFrame(qk0[, c(2:1)], qk0)
proj4string(qk0a) <- CRS("+proj=longlat +ellps=WGS84")
# writeOGR(qk0a, paste(tf, "v.kml", sep=""), "Quakes", "KML")
# system(paste("googleearth ", tf, ".kml", sep=""))
}
options(example.ask=opt_exask)