Difference between revisions of "Opasnet map"
From Testiwiki
(first draft) |
(map tests moved from Sandbox) |
||
Line 23: | Line 23: | ||
=== Formula === | === Formula === | ||
+ | |||
+ | ==== Kuopio buildings on Google maps test ==== | ||
+ | |||
+ | <rcode name='kuorakonmaps'> | ||
+ | |||
+ | 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<-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("ika value: ", shp2$ika) | ||
+ | description <- paste("<b>Value:</b>",shp2$ika,"<br><b>Description:</b>",shp2$kayttotark) | ||
+ | |||
+ | |||
+ | 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)) | ||
+ | } | ||
+ | if (length(name) < nrow(obj)) { | ||
+ | if (length(name) > 1) | ||
+ | warning("kmlPoints: length(name) does not match nrow(obj). The first name will be replicated.") | ||
+ | name <- rep(name, nrow(obj)) | ||
+ | } | ||
+ | if (length(description) < nrow(obj)) { | ||
+ | if (length(description) > 1) | ||
+ | warning("kmlPoints: length(description) does not match nrow(obj). The first description will be replicated.") | ||
+ | description <- rep(description, nrow(obj)) | ||
+ | } | ||
+ | if (length(icon) < nrow(obj)) { | ||
+ | if (length(icon) > 1) | ||
+ | warning("kmlPoints: length(icon) does not match nrow(obj). Only the first one will be used.") | ||
+ | icon <- icon[1] | ||
+ | } | ||
+ | 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)) { | ||
+ | for (i in 1:100) { | ||
+ | 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) | ||
+ | |||
+ | </rcode> | ||
+ | |||
+ | |||
+ | ==== GoogleMaps Sorvi MML TEST ==== | ||
+ | |||
+ | <rcode name='gmapspsqltest3'> | ||
+ | |||
+ | library(OpasnetBaseUtils) | ||
+ | library(sorvi) | ||
+ | library(rgdal) | ||
+ | |||
+ | data(MML) | ||
+ | |||
+ | shp <- MML[["1_milj_Shape_etrs_shape"]][["kunta1_p"]] | ||
+ | |||
+ | #epsg3857String <- CRS("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs") | ||
+ | epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") | ||
+ | proj4string(shp)<-("+init=epsg:3047") | ||
+ | shp2<-spTransform(shp,epsg4326String) | ||
+ | |||
+ | out<-sapply(slot(shp2,"polygons"),function(x){kmlPolygon(x,name="nimi",col='#df0000aa',lwd=1,border='black',description="selite") }) | ||
+ | |||
+ | 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='' | ||
+ | ) | ||
+ | |||
+ | google.show_kml_data_on_maps(data) | ||
+ | |||
+ | </rcode> | ||
+ | |||
+ | ==== GoogleMaps PostgreSQL test 2 ==== | ||
+ | |||
+ | <rcode name='gmapspsqltest2'> | ||
+ | |||
+ | library(rgdal) | ||
+ | library(maptools) | ||
+ | library(RColorBrewer) | ||
+ | library(classInt) | ||
+ | library(OpasnetBaseUtils) | ||
+ | |||
+ | shp<-readOGR('PG:host=localhost user=postgres dbname=spatial_db','watson_wkt') | ||
+ | |||
+ | plotvar<-shp@data$value_inhalation | ||
+ | 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:3035") | ||
+ | shp2<-spTransform(shp,epsg4326String) | ||
+ | |||
+ | |||
+ | out<-sapply(slot(shp2,"polygons"),function(x){kmlPolygon(x,name=as(shp2,"data.frame")[slot(x,"ID"),"country_code"],col=colcode[[((as.numeric(slot(x,"ID"))+1))]],lwd=1,border='black',description=paste("Value:",as(shp2,"data.frame")[slot(x,"ID"),"value_inhalation"])) }) | ||
+ | |||
+ | 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='' | ||
+ | ) | ||
+ | |||
+ | google.show_kml_data_on_maps(data) | ||
+ | </rcode> | ||
+ | |||
+ | ==== GoogleMaps PostgreSQL test ==== | ||
+ | |||
+ | <rcode name='gmapspsqltest'> | ||
+ | library('OpasnetBaseUtils') | ||
+ | cat("<span style='font-size: 1.2em;font-weight:bold;'>PostgreSQL Test</span>\n") | ||
+ | google.show_data_on_maps() | ||
+ | </rcode> | ||
==See also== | ==See also== | ||
+ | |||
+ | * [https://gist.github.com/2292662 Plotting sp-objects to Google Maps with different CRSs] | ||
==Keywords== | ==Keywords== |
Revision as of 03:50, 12 May 2012
This page is a variable.
The page identifier is Op_en5490 |
---|
Moderator:Jouni (see all) |
This page is a stub. You may improve it into a full page, and then a rating bar will appear here. |
Upload data
|
Contents
Question
How should GIS data be handled and visualised in Opasnet?
Answer
- Original GIS data is stored in a PostgreSQL database.
- Data is accessed by R using the RGDAL package and ?? connection.
- Data in manipulated in R.
- Data is displayed by producing a KML file with ?? package.
- The KML file is saved at the R-tools server.
- Google Maps is used to show the KML file on a web page.
Rationale
All pieces of the puzzle exist already.
Dependencies
Formula
Kuopio buildings on Google maps test
GoogleMaps Sorvi MML TEST
GoogleMaps PostgreSQL test 2
GoogleMaps PostgreSQL test
See also
Keywords
References
Related files
<mfanonymousfilelist></mfanonymousfilelist>