Tämä sivu sisältää kopion vastaavasta englanninkielisestä koodista yhteensopivuuden takia.
Calculations
+ Näytä koodi- Piilota koodi
library(OpasnetUtils)
# Function concmap draws a concentration field on a dynamic Google map.
concmap <- function(Pitoisuus, ncol_rast = 41, nrow_rast = 41, ...) {
# Required packages:
#library(OpasnetUtils)
#library(ggplot2)
#library(xtable)
#library(OpasnetUtilsExt)
#library(rgdal)
#library(maptools)
#library(RColorBrewer)
#library(classInt)
#library(raster)
#par(mfrow=c(6,1), mar=c(3,1,0,1), cex=1.5) # Graphics settings
cat("Concentration field.\n")
colorstrip <- function(colors, labels) {
count <- length(colors)
m <- matrix(1:count, count, 1)
image(m, col=colors, ylab="", axes=FALSE)
axis(1,approx(c(0, 1), n=length(labels))$y, labels)
}
# x is a factor with ranges. Function range2sum averages the ranges and gives averages as the output.
range2num <- function(x) {
temp <- gsub("[\\(\\)\\[ ]|\\]", "", levels(x)) # Remove characters "()[] "
temp <- strsplit(temp, ",") # Split into two from the comma (assuming exactly one comma)
temp2 <- c()
for(i in 1:length(temp)) {
a <- as.numeric(temp[[i]][1])
b <- as.numeric(temp[[i]][2])
temp2[i] <- ((a + b) / 2) # Average of the lower and upper part of range.
}
levels(x) <- temp2
x <- as.numeric(as.character(x))
return(x)
}
data <- data.frame( # Create the data.frame that is used as the input for the concentration field.
LO = range2num(Pitoisuus@output$LObin),
LA = range2num(Pitoisuus@output$LAbin),
log_concentration = (log10(Pitoisuus@output$PitoisuusResult) - log10(0.001)) / 4 # Miksi jaetaan neljällä?
)
# Plot the data
coordinates(data) <- c("LO","LA")
proj4string(data) <- ("+init=epsg:4326")
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
shp <- spTransform(data,epsg4326String)
#Create blank raster
rast<-raster()
#Set raster extent to that of point data
extent(rast) <- extent(shp)
#Choose number of columns and rows
ncol(rast) <- ncol_rast
nrow(rast) <- nrow_rast
#Rasterize point data
rast2 <- rasterize(shp, rast, shp$log_concentration, fun=mean)
steps <- c(0.001,0.003,0.01,0.03,0.1,0.3,1,3,10,30)
steps <- c(steps[steps < max(data$log_concentration)], max(data$log_concentration)) # Limit upper part to max value.
colors <- rev(rainbow(length(steps), start=0, end=0.50))
cat(colorstrip(colors, steps)) # Plot the colorstrip (legend).
#Plot data
return(google.show_raster_on_maps(rast2, col=colors, style="height:500px;"))
}
objects.store(concmap)
cat("Object concmap stored. It plots a concentration field on a Google map.\n")
| |