R/PlotPointsG.R

Defines functions PlotPointsG

PlotPointsG <- function(data,
					 var,
					 description,
					 map.name,
					 map.description,
					 decimals,
					 col.pallete,
					 legend.att, 
           cuts, 
					 cuts.type,
					 sizeBall = c(0.5, 2),
					 savekml){

	output <- GeraMapa(data, var, description, map.name, map.description, decimals, col.pallete, legend.att, cuts, cuts.type, savekml = savekml)
	
	data      <- output$data.out
	leg       <- output$leg
	leg.path  <- output$leg.path
	brks      <- output$brks
	plotclr   <- output$plotclr

	size.ball.brks  <- seq(sizeBall[1], sizeBall[2], length = cuts)	## Cria os tamanhos das bolinhas
	if (!is.null(savekml)){
	  name.ball <- tempfile(paste("ball", as.character(1:cuts), sep = ""), fileext = ".png", tmpdir = getwd())
	  #file.name.kml <- tempfile(pattern = "map", fileext = ".kml", tmpdir = getwd())
    file.name.kml <- file.path(getwd(), savekml)
	} else{
	  name.ball <- tempfile(paste("ball", as.character(1:cuts), sep = ""), fileext = ".png")
	  file.name.kml <- tempfile(pattern = "map", fileext = ".kml")
	}
	
        if (!is.null(var)){
		if (is.factor(data@data[, var])){
	          mapply(MakeBall, col = plotclr, file = name.ball, width = 100, height = 100, radius = min(size.ball.brks))	## Cria os arquivos .png das bolinhas
        	  size.ball <- size.ball.brks[data@data[, var]]
		}else{
		  mapply(MakeBall, col = plotclr, file = name.ball, width = 100, height = 100, radius = size.ball.brks, sizeMin = sizeBall[1], sizeMax = sizeBall[2])	## Cria os arquivos .png das bolinhas
		  size.ball <- size.ball.brks[findInterval(data@data[, var], brks, rightmost.closed = TRUE, all.inside = TRUE)]	## Atribui para cada valor calculado qual o tamanho da bolinha
		}
	}else{
	        mapply(MakeBall, col = plotclr, file = name.ball, width = 100, height = 100, radius = min(size.ball.brks))	## Cria os arquivos .png das bolinhas
		size.ball <- min(size.ball.brks)
	}

	if (dim(data)[1] != 1) {
		icon <- name.ball[findInterval(size.ball, size.ball.brks)]	## Atribui a bolinnha
	} else {
		# Escolhe o maior icone para apresentar no caso de um unico elemento
		icon <- name.ball[cuts]
	}
	
	icon <- basename(icon)
	
	#name <- cent.data[,3]			## Seleciona o nome da regiao
	ptDescriptions <- data$description 
	descr.default <- list(var = NULL, type = "table", title = NULL)
	descr.default[names(description)] <- description
	
    if (is.null(description$title)){
	  name.point <- ""
	}else{
	  name.point <- as.character(data@data[, description$title]) 
	}
	
	out <- maptools::kmlPoints(data, kmlfile = file.name.kml, name = name.point, description = ptDescriptions,
			       icon = icon, kmlname = "", kmldescription = "")

	## Le o arquivo kml do mapa das bolinhas
	if (dim(data)[2] != 1) {
		doc.aux <- readLines(file.name.kml)
		# doc.aux[1] <- gsub("UTF-8", "latin1", doc.aux[1])
		doc <- xmlParse(doc.aux)
	if (!is.null(var)){
		legenda.node <- getNodeSet(doc, "//x:Document","x")
		legenda.xml  <- xmlParse(leg)
		legenda.xml  <- getNodeSet(legenda.xml,"//ScreenOverlay")[[1]]
		legenda.node[[1]] <- addChildren(legenda.node[[1]], legenda.xml)
	}
	  	saveXML(doc, file.name.kml)	##Salva o mapa das bolinhas com a legenda
	}
#	dire <- getwd()
#	tempdire <- dirname(file.name.kml)
#	setwd(tempdire)
#	file.name.kmz <- gsub(".kml", ".kmz", file.name.kml)
#	invisible(zip(file.name.kmz, basename(c(file.name.kml, name.ball, leg.path))))
#	unlink(basename(c(file.name.kml, name.ball, leg.path)))
#	setwd(dire)
#       return(file.name.kmz)
	if (!is.na(leg.path)){
		leg.path <- basename(leg.path)
	}
        return(list(kmlpath = file.name.kml, leg.path = leg.path))
}
lgsilvaesilva/spGoogle documentation built on May 21, 2019, 6:08 a.m.