Difference between revisions of "Opasnet map"

From Testiwiki
Jump to: navigation, search
(MyPointData function moved to OpasnetUtils/Drafts)
(organised and links added)
Line 12: Line 12:
 
* Data is accessed by [[R]] using the RGDAL package and ?? connection.
 
* Data is accessed by [[R]] using the RGDAL package and ?? connection.
 
* Data in manipulated in [[R]].
 
* Data in manipulated in [[R]].
 +
* Ovariables are converted to SpatialPointsDataFrame objects using ova2spat function.
 
* Data is displayed by producing a [[:en:Keyhole markup language|KML]] file with ?? package.
 
* Data is displayed by producing a [[:en:Keyhole markup language|KML]] file with ?? package.
 +
** The KML file is created with MyPointKML function, if pins are shown.
 
** The KML file is saved at the R-tools server.
 
** The KML file is saved at the R-tools server.
 
** Google Maps is used to show the KML file on a web page.
 
** Google Maps is used to show the KML file on a web page.
 +
 +
Currently, ova2spat and MyPointKML functions are located in [[OpasnetUtils/Drafts]]. Therefore, you need this command:
 +
objects.latest("Op_en6007", code_name = "answer") # [[OpasnetUtils/Drafts]]. We need MyPointsKML and ova2spat.
 +
 +
This is the projection that the National Land Survey Finland uses: [http://spatialreference.org/ref/epsg/3067/].
 +
 +
Key guidance:
 +
* [http://cran.r-project.org/web/packages/rgdal/rgdal.pdf rgdal package for R]
 +
** [http://www.gdal.org/ GDAL - Geospatial Data Abstraction Library]
 +
* [http://cran.r-project.org/web/packages/sp/vignettes/intro_sp.pdf sp package for R]: contains descriptions about the key spatial objects.
 +
* [http://rwiki.sciviews.org/doku.php?id=tips:spatial-data:change_crs Changing the coordinate system of Spatial objects]
 +
  
 
== Rationale ==
 
== Rationale ==
Line 23: Line 37:
 
=== Dependencies ===
 
=== Dependencies ===
  
=== Formula ===
+
=== Calculations ===
  
==== Kuopio buildings on Google maps test ====
+
==== Kuopio buildings with Google pin map ====
  
 
<rcode name='kuorakonmaps'>
 
<rcode name='kuorakonmaps'>
Line 42: Line 56:
 
shp <- shp[1:100 , ] # Save only 100 buildings for demonstration
 
shp <- shp[1:100 , ] # Save only 100 buildings for demonstration
  
plotvar<-shp@data$ika
+
plotvar <- shp@data$ika
nclr<-8
+
nclr <- 8
plotclr<-brewer.pal(nclr,"BuPu")
+
plotclr <- brewer.pal(nclr,"BuPu")
class<-classIntervals(plotvar,nclr,style="quantile")
+
class <- classIntervals(plotvar,nclr,style="quantile")
colcode<-findColours(class,plotclr)
+
colcode <- findColours(class,plotclr)
  
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 
proj4string(shp)<-("+init=epsg:3067")
 
proj4string(shp)<-("+init=epsg:3067")
shp2<-spTransform(shp,epsg4326String)
+
shp2 <- spTransform(shp, epsg4326String)
  
kmlname<-"Kuopio house data"
+
kmlname <- "Kuopio house data"
kmldescription<-"Random stuff about here"
+
kmldescription <- "Random stuff about here"
icon<-"http://maps.google.com/mapfiles/kml/pal2/icon18.png"
+
icon <- "http://maps.google.com/mapfiles/kml/pal2/icon18.png"
name<-paste("ika value: ", shp2$ika)
+
name <- paste("ika value: ", shp2$ika)
 
description <- paste("<b>Value:</b>",shp2$ika,"<br><b>Description:</b>",shp2$kayttotark)
 
description <- paste("<b>Value:</b>",shp2$ika,"<br><b>Description:</b>",shp2$kayttotark)
  
data <- MyPointKML(shp2,kmlname,kmldescription,name,description,icon,colcode)
+
dat <- MyPointKML(shp2, kmlname, kmldescription, name, description, icon, colcode)
google.show_kml_data_on_maps(data)
+
 
 +
google.show_kml_data_on_maps(dat)
  
 
</rcode>
 
</rcode>
Line 65: Line 80:
  
 
==== GoogleMaps Sorvi MML TEST ====
 
==== GoogleMaps Sorvi MML TEST ====
 +
 +
{{attack|#|Something wrong with sorvi.|--[[User:Jouni|Jouni]] ([[User talk:Jouni|talk]]) 12:54, 23 December 2013 (EET)}}
  
 
<rcode name='gmapspsqltest3'>
 
<rcode name='gmapspsqltest3'>
Line 82: Line 99:
 
shp2<-spTransform(shp,epsg4326String)
 
shp2<-spTransform(shp,epsg4326String)
  
out<-sapply(slot(shp2,"polygons"),function(x){kmlPolygon(x,name="nimi",col='#df0000aa',lwd=1,border='black',description="selite") })
+
out<-sapply(slot(shp2,"polygons"),function(x
 +
{kmlPolygon(
 +
x,
 +
name="nimi",
 +
col='#df0000aa',
 +
lwd=1,
 +
border='black',
 +
description="selite"
 +
)}
 +
)
  
data<-paste(
+
data <- paste(
paste(kmlPolygon(kmlname="This will be layer name", kmldescription="<i>More info about layer here</i>")$header, collapse="\n"),
+
paste(
paste(unlist(out["style",]), collapse="\n"),
+
kmlPolygon(
paste(unlist(out["content",]), collapse="\n"),
+
kmlname =" This will be layer name",  
paste(kmlPolygon()$footer, collapse="\n"),
+
kmldescription = "<i>More info about layer here</i>"
sep=''
+
)$header,  
 +
collapse="\n"
 +
),
 +
paste(unlist(out["style",]), collapse="\n"),
 +
paste(unlist(out["content",]), collapse="\n"),
 +
paste(kmlPolygon()$footer, collapse="\n"),
 +
sep=''
 
)
 
)
  
Line 96: Line 128:
 
</rcode>
 
</rcode>
  
==== GoogleMaps PostgreSQL test 2 ====
+
==== Google with shapefiles ====
  
 
<rcode name='gmapspsqltest2'>
 
<rcode name='gmapspsqltest2'>
Line 107: Line 139:
 
library(OpasnetUtilsExt)
 
library(OpasnetUtilsExt)
  
shp<-readOGR('PG:host=localhost user=postgres dbname=spatial_db','watson_wkt')
+
shp <- readOGR('PG:host=localhost user=postgres dbname=spatial_db','watson_wkt')
  
plotvar<-shp@data$value_inhalation
+
plotvar <- shp@data$value_inhalation
nclr<-8
+
nclr <- 8
plotclr<-brewer.pal(nclr,"BuPu")
+
plotclr <- brewer.pal(nclr,"BuPu")
class<-classIntervals(plotvar,nclr,style="quantile")
+
class <- classIntervals(plotvar,nclr,style="quantile")
colcode<-findColours(class,plotclr)
+
colcode <- findColours(class,plotclr)
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 
proj4string(shp)<-("+init=epsg:3035")
 
proj4string(shp)<-("+init=epsg:3035")
shp2<-spTransform(shp,epsg4326String)
+
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"])) })
+
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(
+
data <- paste(
paste(kmlPolygon(kmlname="This will be layer name", kmldescription="<i>More info about layer here</i>")$header, collapse="\n"),
+
paste(
paste(unlist(out["style",]), collapse="\n"),
+
kmlPolygon(
paste(unlist(out["content",]), collapse="\n"),
+
kmlname = "This will be layer name",  
paste(kmlPolygon()$footer, collapse="\n"),
+
kmldescription = "<i>More info about layer here</i>")$header,  
sep=''
+
collapse="\n"
 +
),
 +
paste(unlist(out["style",]), collapse="\n"),
 +
paste(unlist(out["content",]), collapse="\n"),
 +
paste(kmlPolygon()$footer, collapse="\n"),
 +
sep=''
 
)
 
)
 +
 +
cat("PostgreSQL test with coloured data areas.\n")
  
 
google.show_kml_data_on_maps(data)
 
google.show_kml_data_on_maps(data)
 +
 +
cat("PostgreSQL Test with default data\n")
 +
 +
google.show_data_on_maps()
 +
 
</rcode>
 
</rcode>
  
==== GoogleMaps PostgreSQL test ====
+
==== Google show data from url on map ====
  
<rcode name='gmapspsqltest'>
+
<rcode name='gutilstest'>
 
library(OpasnetUtils)
 
library(OpasnetUtils)
 
library(OpasnetUtilsExt)
 
library(OpasnetUtilsExt)
cat("<span style='font-size: 1.2em;font-weight:bold;'>PostgreSQL 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')
google.show_data_on_maps()
 
 
</rcode>
 
</rcode>
  
==== Kuopion rakennukset Google mapsilla test ====
+
==== Google circles ====
  
<rcode name='kuorakonmaps'>
+
<rcode name='gmaptest'>
 
 
library(rgdal)
 
library(maptools)
 
library(RColorBrewer)
 
library(classInt)
 
 
library(OpasnetUtilsExt)
 
library(OpasnetUtilsExt)
  
objects.latest("Op_en6007", code_name = "answer") # [[OpasnetUtils/Drafts]]; we need MyPointsKML and ova2spat.
+
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')
  
shp<-readOGR('PG:host=localhost user=postgres dbname=spatial_db','kuopio_house')
+
google.circles(data)
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")
+
</rcode>
proj4string(shp)<-("+init=epsg:3067")
 
shp2<-spTransform(shp,epsg4326String)
 
  
 +
==== Static GoogleMaps test ====
  
kmlname<-"Kuopio house data"
+
<rcode name='staticmapstest' graphics='1'>
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)
 
  
data <- MyPointKML(shp2,kmlname,kmldescription,name,description,icon,colcode)
+
library(RgoogleMaps)
google.show_kml_data_on_maps(data)
 
 
 
</rcode>
 
 
 
==== Google utils test 3 ====
 
 
 
<rcode name='gutilstest3'>
 
 
library(rgdal)
 
library(rgdal)
 
library(maptools)
 
library(maptools)
Line 186: Line 222:
 
library(OpasnetUtilsExt)
 
library(OpasnetUtilsExt)
  
shp<-readOGR('PG:host=localhost user=postgres dbname=spatial_db','watson_wkt')
+
shp<-readOGR('PG:host=localhost user=postgres dbname=spatial_db','kuopio_house')
 
+
plotvar<-shp@data$ika
plotvar<-shp@data$value_inhalation
 
 
nclr<-8
 
nclr<-8
plotclr<-brewer.pal(nclr,"BuPu")
+
plotclr<-brewer.pal(nclr,"Spectral")
 
class<-classIntervals(plotvar,nclr,style="quantile")
 
class<-classIntervals(plotvar,nclr,style="quantile")
 
colcode<-findColours(class,plotclr)
 
colcode<-findColours(class,plotclr)
 +
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
 
epsg4326String <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
proj4string(shp)<-("+init=epsg:3035")
+
proj4string(shp)<-("+init=epsg:3067")
 
shp2<-spTransform(shp,epsg4326String)
 
shp2<-spTransform(shp,epsg4326String)
 +
#get marker information first 10 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"])
  
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"])) })
+
#MyMap function without the "file destination" parameter
 
+
MyRmap<-function (lonR, latR, center, size = c(640, 640),
data<-paste(
+
    MINIMUMSIZE = FALSE, RETURNIMAGE = TRUE, GRAYSCALE = FALSE,  
paste(kmlPolygon(kmlname="This will be layer name", kmldescription="<i>More info about layer here</i>")$header, collapse="\n"),
+
    NEWMAP = TRUE, zoom, verbose = 1, ...)
paste(unlist(out["style",]), collapse="\n"),
+
{
paste(unlist(out["content",]), collapse="\n"),
+
    if (missing(zoom))  
paste(kmlPolygon()$footer, collapse="\n"),
+
        zoom <- min(MaxZoom(latR, lonR, size))
sep=''
+
    if (missing(center)) {
)
+
        lat.center <- mean(latR)
 
+
        lon.center <- mean(lonR)
google.show_kml_data_on_maps(data)
+
    }
 
+
    else {
</rcode>
+
        lat.center <- center[1]
 
+
        lon.center <- center[2]
==== Google utils test 2 ====
+
    }
 
+
    if (MINIMUMSIZE) {
<rcode name='gutilstest2'>
+
        ll <- LatLon2XY(latR[1], lonR[1], zoom)
library(OpasnetUtils)
+
        ur <- LatLon2XY(latR[2], lonR[2], zoom)
library(OpasnetUtilsExt)
+
        cr <- LatLon2XY(lat.center, lon.center, zoom)
 
+
        ll.Rcoords <- Tile2R(ll, cr)
cat("<span style='font-size: 1.2em;font-weight:bold;'>PostgreSQL Test</span>\n")
+
        ur.Rcoords <- Tile2R(ur, cr)
google.show_data_on_maps()
+
        if (verbose > 1) {
cat("<span style='font-size: 1.2em;font-weight:bold;'>External Flicker KML test</span>\n")
+
            cat("ll:")
google.show_kml_on_maps(url='http://api.flickr.com/services/feeds/geo/?g=322338@N20&lang=en-us&format=feed-georss')
+
            print(ll)
</rcode>
+
            print(ll.Rcoords)
 
+
            cat("ur:")
==== Google utils test ====
+
            print(ur)
 
+
            print(ur.Rcoords)
<rcode name='gutilstest'>
+
            cat("cr:")
library(OpasnetUtils)
+
            print(cr)
library(OpasnetUtilsExt)
+
        }
google.show_kml_on_maps(url='http://api.flickr.com/services/feeds/geo/?g=322338@N20&lang=en-us&format=feed-georss')
+
        size[1] <- 2 * max(c(ceiling(abs(ll.Rcoords$X)), ceiling(abs(ur.Rcoords$X)))) +
</rcode>
+
            1
 
+
        size[2] <- 2 * max(c(ceiling(abs(ll.Rcoords$Y)), ceiling(abs(ur.Rcoords$Y)))) +
==== Google Maps Test ====
+
            1
 +
        if (verbose)  
 +
            cat("new size: ", size, "\n")
 +
    }
 +
    return(GetMap(center = c(lat.center, lon.center), zoom = zoom,
 +
        size = size, RETURNIMAGE = RETURNIMAGE,
 +
        GRAYSCALE = GRAYSCALE, verbose = verbose, ...))
 +
}
  
<rcode name='gmaptest'>
+
MyMap<-MyRmap(bb$lonR,bb$latR,maptype="mobile")
library(OpasnetUtilsExt)
 
  
data = list()
+
PlotOnStaticMap(MyMap)
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)
+
PlotOnStaticMap(MyMap,lat=mymarkers[,"lat"],lon=mymarkers[,"lon"],pch=19,cex=0.3,col=colcode,add=T)
  
 +
legend("topleft", legend=names(attr(colcode, "table")),title="Ika", fill=attr(colcode, "palette"),  cex=1.0, bty="y",bg="white")
 
</rcode>
 
</rcode>
  

Revision as of 10:54, 23 December 2013



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.
  • Ovariables are converted to SpatialPointsDataFrame objects using ova2spat function.
  • Data is displayed by producing a KML file with ?? package.
    • The KML file is created with MyPointKML function, if pins are shown.
    • The KML file is saved at the R-tools server.
    • Google Maps is used to show the KML file on a web page.

Currently, ova2spat and MyPointKML functions are located in OpasnetUtils/Drafts. Therefore, you need this command:

objects.latest("Op_en6007", code_name = "answer") # OpasnetUtils/Drafts. We need MyPointsKML and ova2spat.

This is the projection that the National Land Survey Finland uses: [1].

Key guidance:


Rationale

All pieces of the puzzle exist already.


Dependencies

Calculations

Kuopio buildings with Google pin map

+ Show code


GoogleMaps Sorvi MML TEST

#: Something wrong with sorvi. --Jouni (talk) 12:54, 23 December 2013 (EET)

+ Show code

Google with shapefiles

+ Show code

Google show data from url on map

+ Show code

Google circles

+ Show code

Static GoogleMaps test

+ Show code

See also

Keywords

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>