Difference between revisions of "Opasnet map"
From Testiwiki
m |
(→Rationale) |
||
Line 198: | Line 198: | ||
cat("<span style='font-size: 1.2em;font-weight:bold;'>PostgreSQL Test</span>\n") | cat("<span style='font-size: 1.2em;font-weight:bold;'>PostgreSQL Test</span>\n") | ||
google.show_data_on_maps() | google.show_data_on_maps() | ||
+ | </rcode> | ||
+ | |||
+ | ==== Kuopion rakennukset Google mapsilla test ==== | ||
+ | |||
+ | <rcode name='kuorakonmaps'> | ||
+ | |||
+ | library(rgdal) | ||
+ | library(maptools) | ||
+ | library(RColorBrewer) | ||
+ | library(classInt) | ||
+ | library(OpasnetUtilsExt) | ||
+ | |||
+ | 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> | ||
+ | |||
+ | ==== Google utils test 3 ==== | ||
+ | |||
+ | <rcode name='gutilstest3'> | ||
+ | library(rgdal) | ||
+ | library(maptools) | ||
+ | library(RColorBrewer) | ||
+ | library(classInt) | ||
+ | library(OpasnetUtils) | ||
+ | library(OpasnetUtilsExt) | ||
+ | |||
+ | 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> | ||
+ | |||
+ | ==== Google utils test 2 ==== | ||
+ | |||
+ | <rcode name='gutilstest2'> | ||
+ | library(OpasnetUtils) | ||
+ | library(OpasnetUtilsExt) | ||
+ | |||
+ | cat("<span style='font-size: 1.2em;font-weight:bold;'>PostgreSQL Test</span>\n") | ||
+ | google.show_data_on_maps() | ||
+ | cat("<span style='font-size: 1.2em;font-weight:bold;'>External Flicker KML test</span>\n") | ||
+ | google.show_kml_on_maps(url='http://api.flickr.com/services/feeds/geo/?g=322338@N20&lang=en-us&format=feed-georss') | ||
+ | </rcode> | ||
+ | |||
+ | ==== Google utils test ==== | ||
+ | |||
+ | <rcode name='gutilstest'> | ||
+ | library(OpasnetUtils) | ||
+ | library(OpasnetUtilsExt) | ||
+ | google.show_kml_on_maps(url='http://api.flickr.com/services/feeds/geo/?g=322338@N20&lang=en-us&format=feed-georss') | ||
+ | </rcode> | ||
+ | |||
+ | ==== Google Maps Test ==== | ||
+ | |||
+ | <rcode name='gmaptest'> | ||
+ | library(OpasnetUtilsExt) | ||
+ | |||
+ | data = list() | ||
+ | data[[1]] = c(62.8925, 27.678333, 9683, '#ff0000', 'kuopio') | ||
+ | data[[2]] = c(65.016667, 25.466667, 14398, '#00ff00', 'oulu') | ||
+ | data[[3]] = c(60.170833, 24.9375, 59623, '#0000ff', 'helsinki') | ||
+ | |||
+ | google.circles(data) | ||
+ | |||
</rcode> | </rcode> | ||
Revision as of 13:47, 20 December 2013
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
Kuopion rakennukset Google mapsilla test
Google utils test 3
Google utils test 2
Google utils test
Google Maps Test
See also
Keywords
References
Related files
<mfanonymousfilelist></mfanonymousfilelist>