Wikisym 2012 Demo
From Opasnet
Polygons on dynamic Google Maps
This example plots municipalities of Finland on Google Maps using data from National Land Survey of Finland.
library(OpasnetBaseUtils) library(sorvi) library(rgdal) # Get the shape data of Finnish municipalities using soRvi library data(MML) shp <- MML[["1_milj_Shape_etrs_shape"]][["kunta1_p"]] # Set the projection epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") proj4string(shp)<-("+init=epsg:3047") shp2<-spTransform(shp,epsg4326String) # Create the KML data using the shape out<-sapply(slot(shp2,"polygons"),function(x){kmlPolygon(x,name="name",col='#df0000aa',lwd=1,border='black',description="desc") }) data<-paste( paste(kmlPolygon(kmlname="This will be layer name", kmldescription="<i>More info about layer here</i>")$header, collapse="\n"), paste(unlist(out["style",]), collapse="\n"), paste(unlist(out["content",]), collapse="\n"), paste(kmlPolygon()$footer, collapse="\n"), sep='' ) # Show the KML data on Google Maps google.show_kml_data_on_maps(data) |
Points on dynamic Google Maps
This examples plots buildings of Kuopio on Google Maps. User can give the minimum age of buildings to plot as an input parameter.
library(rgdal) library(RColorBrewer) library(classInt) library(OpasnetBaseUtils) library(RODBC) if (age > 190) { age <- 190 } shp <- spatial_db_query(paste('SELECT * FROM kuopio_house WHERE ika >= ',age,';',sep='')) coordinates(shp)=c("y_koord","x_koord") plotvar<-shp@data$ika nclr<-8 plotclr<-brewer.pal(nclr,"BuPu") class<-classIntervals(plotvar,nclr,style="quantile") colcode<-findColours(class,plotclr) epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") proj4string(shp)<-("+init=epsg:3067") shp2<-spTransform(shp,epsg4326String) kmlname<-"Kuopio house data" kmldescription<-"Random stuff about here" icon<-"http://maps.google.com/mapfiles/kml/pal2/icon18.png" name<-paste("Value: ",shp2$ika) description <- paste("<b>Age:</b>",shp2$ika,"<br><b>Building ID:</b>",shp2$rakennustunnus) MyPointKML<-function(obj = NULL, kmlname = "", kmldescription = "", name = NULL, description = "", icon = "http://maps.google.com/mapfiles/kml/pal4/icon24.png",col=NULL) { if (is.null(obj)) return(list(header = c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<kml xmlns=\"http://earth.google.com/kml/2.2\">", "<Document>", paste("<name>", kmlname, "</name>", sep = ""), paste("<description><![CDATA[", kmldescription, "]]></description>", sep = "")), footer = c("</Document>", "</kml>"))) if (class(obj) != "SpatialPointsDataFrame") stop("obj must be of class 'SpatialPointsDataFrame' [package 'sp']") if (is.null(name)) { name = c() for (i in 1:nrow(obj)) name <- append(name, paste("site", i)) } col2kmlcolor <- function(col) paste(rev(sapply(col2rgb(col, TRUE), function(x) sprintf("%02x", x))), collapse = "") kml <- kmlStyle <- "" kmlHeader <- c("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","<kml xmlns=\"http://earth.google.com/kml/2.2\">", "<Document>") kmlFooter <- c("</Document>", "</kml>") for (i in 1:nrow(obj)) { point <- obj[i, ] pt_name = name[i] pt_description = description[i] pt_style <- paste("#style", ifelse(length(icon) == 1, 1, i), sep = "") kml <- append(kml, "<Placemark>") kml <- append(kml, paste(" <description><![CDATA[",pt_description, "]]></description>", sep = "")) #kml <- append(kml, "<Style><IconStyle>") #kml <- append(kml, paste("<color>", col2kmlcolor(col[i]), "</color>", sep ="")) #kml <- append(kml, paste(" <Icon><href>", icon, "</href></Icon>", sep = "")) #kml <- append(kml, "<scale>0.300000</scale>") #kml <- append(kml, "</IconStyle></Style>") kml <- append(kml, " <Point>") kml <- append(kml, " <coordinates>") kml <- append(kml, paste(point@coords[1], point@coords[2], sep = ",")) kml <- append(kml, " </coordinates>") kml <- append(kml, " </Point>") kml <- append(kml, "</Placemark>") } return(paste(paste(c(kmlHeader, kmlStyle, kml, kmlFooter), sep = "", collapse = "\n"), collapse="\n", sep = "")) } data <- MyPointKML(shp2,kmlname,kmldescription,name,description,icon,colcode) google.show_kml_data_on_maps(data) |
Large quantity of points on a static Google Maps
This example plots large number of point data on static Google Maps. The map produced in this example shows the age (in years) distribution of properites within Kuopio. User can select the number of age classes (4,6 or 8) and the type of classification.
#code goes here library(RgoogleMaps) library(rgdal) library(maptools) library(RColorBrewer) library(classInt) library(OpasnetBaseUtils) shp<-readOGR('PG:host=localhost user=postgres dbname=spatial_db','kuopio_house') plotvar<-shp@data$ika nclr<-myclasses plotclr<-brewer.pal(nclr,"Spectral") class<-classIntervals(plotvar,nclr,style=classtype) colcode<-findColours(class,plotclr) epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") proj4string(shp)<-("+init=epsg:3067") shp2<-spTransform(shp,epsg4326String) #get marker information for all points mymarkers<-cbind.data.frame(lat=c(shp2@coords[,2]),lon=c(shp2@coords[,1]),color=colcode); #get the bounding box: bb <- qbbox(lat = mymarkers[,"lat"], lon = mymarkers[,"lon"]) #MyMap function without the "file destination" parameter MyRmap<-function (lonR, latR, center, size = c(640, 640), MINIMUMSIZE = FALSE, RETURNIMAGE = TRUE, GRAYSCALE = FALSE, NEWMAP = TRUE, zoom, verbose = 1, ...) { if (missing(zoom)) zoom <- min(MaxZoom(latR, lonR, size)) if (missing(center)) { lat.center <- mean(latR) lon.center <- mean(lonR) } else { lat.center <- center[1] lon.center <- center[2] } if (MINIMUMSIZE) { ll <- LatLon2XY(latR[1], lonR[1], zoom) ur <- LatLon2XY(latR[2], lonR[2], zoom) cr <- LatLon2XY(lat.center, lon.center, zoom) ll.Rcoords <- Tile2R(ll, cr) ur.Rcoords <- Tile2R(ur, cr) if (verbose > 1) { cat("ll:") print(ll) print(ll.Rcoords) cat("ur:") print(ur) print(ur.Rcoords) cat("cr:") print(cr) } size[1] <- 2 * max(c(ceiling(abs(ll.Rcoords$X)), ceiling(abs(ur.Rcoords$X)))) + 1 size[2] <- 2 * max(c(ceiling(abs(ll.Rcoords$Y)), ceiling(abs(ur.Rcoords$Y)))) + 1 if (verbose) cat("new size: ", size, "\n") } return(google.get_map(center = c(lat.center, lon.center), zoom = zoom, size = size, RETURNIMAGE = RETURNIMAGE, GRAYSCALE = GRAYSCALE, verbose = verbose, ...)) } MyMap<-MyRmap(bb$lonR,bb$latR,maptype="mobile",scale="2") PlotOnStaticMap(MyMap,size=c(640,640)) PlotOnStaticMap(MyMap,size=c(640,640),lat=mymarkers[,"lat"],lon=mymarkers[,"lon"],pch=19,cex=0.3,col=colcode,add=T) legend("topleft", legend=names(attr(colcode, "table")),title="Building Age (Yr)", fill=attr(colcode, "palette"), cex=1.0, bty="y",bg="white") |
Retrieved from "https://en.opasnet.org/index.php?title=Wikisym_2012_Demo&oldid=26010"