Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: correlvar added)
(Answer: ova2spat updated)
Line 294: Line 294:
 
}
 
}
  
ova2spat <- function( # This function converts an ovariable into a SpatialPointsDataFrame.
+
ova2spat <- function( # This function converts an ovariable or a data.frame into a SpatialPointsDataFrame.
ovariable, # An evaluated ovariable that has coordinate indices.
+
  dat, # An evaluated ovariable or data.frame that has coordinate indices.
coords, # The names of the coordinate indices as a character vector, first x then y.
+
  coords = c("LO", "LA"), # The names of the coordinate indices as a character vector, first x then y.
proj4string # Projection identifier or specification as character string. See http://spatialreference.org/
+
  proj4string = NULL # Projection identifier or specification as character string. See http://spatialreference.org/
 +
  # If proj4string is NULL, longitude-latitude system is assumed.
 
) {
 
) {
temp <- ovariable@output
+
  if(class(dat) == "ovariable") temp <- dat@output else
+
    if(is.data.frame(dat)) temp <- dat else
# Transform coordinates into numeric format.
+
      stop("object must be either evaluated ovariable or data.frame\n")
+
 
for(i in coords) {  
+
  # Transform coordinates into numeric format.
if(is(temp[[i]], "factor"))    temp[[i]] <- levels(temp[[i]])[temp[[i]]]
+
 
if(is(temp[[i]], "character")) temp[[i]] <- as.numeric(temp[[i]])
+
  for(i in coords) {  
}
+
    temp[[i]] <- as.numeric(as.character(temp[[i]]))
+
  }
# Define the coordinate points first, then add other ovariable output to it.
+
 
+
  # Define the coordinate points first, then add other ovariable output to it.
sp <- SpatialPoints(temp[coords], CRS(proj4string))
+
 
out <- SpatialPointsDataFrame(sp, temp[!colnames(temp) %in% coords])
+
  if(is.null(proj4string)) {
 +
    sp <- SpatialPoints(temp[coords], CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
 +
    } else {
 +
      sp <- SpatialPoints(temp[coords], CRS(proj4string))
 +
    }
 +
  out <- SpatialPointsDataFrame(sp, temp[!colnames(temp) %in% coords])
 +
 
 +
  #Transform the projection to longitude-latitude system.
 +
  if(!is.null(proj4string)) {
 +
    epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 +
    out <- spTransform(out,epsg4326String)
 +
  }
 +
 
 +
  return(out)
 +
}
  
#Transform the projection to longitude-latitude system.
 
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 
out <- spTransform(out,epsg4326String)
 
 
return(out)
 
}
 
  
 
# MyRmap is a function for creating static Google maps as png.
 
# MyRmap is a function for creating static Google maps as png.

Revision as of 06:38, 30 August 2016



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer")

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>