Coder Social home page Coder Social logo

webglobe's Introduction

webglobe: Interactive 3D Maps

You want to understand your data, but it's spatially distributed and you're afraid that trying to make sense of it on something gross, like a Mercator projection, is going to lead you to bad intuitions.

Mercator Projection

(Greenland is nowhere near that big in reality.)

webglobe can help you do this! It allows you to interactively visualize your data on either a three-dimensional globe or a flat map.

Example: Earth quakes

library(webglobe)              #Load the library
data(quakes)                   #Load up some data

wg <- webglobe(immediate=TRUE) #Make a webglobe (should open a net browser)
Sys.sleep(10)                     #Wait for browser to start, or it won't work
wg + wgpoints(quakes$lat, quakes$lon, size=5*quakes$mag) #Plot quakes
wg + wgcamcenter(-24, 178.0650, 8000)                    #Move camera

Webglobe earthquakes visualization

Example: States

library(webglobe)                 #Load the library
m  <- ggplot2::map_data("state")  #Get data
m$extrude_height <- 1000000*runif(nrow(m),min=0,max=1)
wg <- webglobe(immediate=TRUE)    #Make a webglobe (should open a net browser)
Sys.sleep(10)                     #Wait for browser to start, or it won't work
wg + wgpolygondf(m,fill="blue",alpha=1,stroke=NA)
Sys.sleep(10)                     #Wait, in case you copy-pasted this
wg + wgclear()                    #Clears the above

Webglobe states visualization

Modes

webglobes have two modes: immediate and not-immediate. Immediate mode displays a webglobe upon initialization and immediately prints all commands to that globe. Not-immediate mode stores commands and displays them all at once, allowing you to stage visualization without intermediate display. The difference is illustrated below.

Display timing in intermediate mode:

library(webglobe)
data(quakes)                     #Get data
q   <- quakes                    #Alias data
wgi <- webglobe(immediate=TRUE)  #Webglobe is displayed now
Sys.sleep(10)                    #Ensure webglobe runs before continuing
wgi + wgpoints( q$lat,  q$lon)    #Data displays now!
wgi + wgpoints(-q$lat, -q$lon)    #Data displays now!
#Reloading the browser window clears everything

Display timing in not-intermediate mode:

library(webglobe)
data(quakes)                                  #Get data
q   <- quakes                                 #Alias data
wgn <- webglobe(immediate=FALSE)              #Webglobe is not displayed
Sys.sleep(0)                                  #No need to wait
#Note that we have to store commands
wgn <- wgn + wgpoints( q$lat,  q$lon)         #Nothing shown yet
wgn <- wgn + wgpoints(-q$lat, -q$lon)         #Nothing shown yet
wgn <- wgn + wgcamcenter(2.89,-175.962,21460) #Nothing shown yet
wgn                                           #Show it all now!
#Reloading the browser window keeps everything

You can also switch between modes:

library(webglobe)
data(quakes)                                  #Get data
q   <- quakes                                 #Alias data
wgn <- webglobe(immediate=FALSE)              #Webglobe is not displayed
Sys.sleep(0)                                  #No need to wait
#Note that we have to store commands
wgn <- wgn + wgpoints( q$lat,  q$lon)         #Nothing shown yet
wgn <- wgn + wgpoints(-q$lat, -q$lon)         #Nothing shown yet
wgn <- wgn + wgcamcenter(2.89,-175.962,21460) #Nothing shown yet
wgn + wgimmediate()                           #Make it all immediate
wgn
wgn + wgpoints(q$lat, -q$lon)                 #This is shown right away
#Reloading the browser window keeps everything up to `wgimmediate()`

Installation

webglobe hopefully will be available from CRAN via:

install.packages('webglobe')

If you want your code to be as up-to-date as possible, you can install it using:

library(devtools) #Use `install.packages('devtools')` if need be
install_github('r-barnes/webglobe', vignette=TRUE)

Developer Notes

How To Add Functionality

There are really only two files that are import to contributing developers: inst/client/webglobe.js and R/webglobe.R .

The package uses a JSON websocket message passing scheme to communicate data between R and the JavaScript client.

Each wg*() function generates a JSON payload as follows:

toString(jsonlite::toJSON(list(
  command = jsonlite::unbox("COMMAND_NAME"), #Required
  lat     = lat,                             #Example
  lon     = lon                              #Example
)))

The payload consists of a command and accompanying data.

For more complex data, geojsonio can be leveraged to conveniently encode the data. However, the resulting GeoJSON must be decoded, so that the whole packae can be sent with only one level of encoding, as follows:

toString(jsonlite::toJSON(list(
  command        = jsonlite::unbox("polygons"),
  polys          = jsonlite::fromJSON(geojsonio::geojson_json(df, group='group', geometry='polygon'))
)))

On the JavaScript side, look for an object named router. router contains a variety of fields which correspond to command names. To add a new command, add a field with a corresponding function, such as:

points: function(msg){
  var points = viewer.scene.primitives.add(new Cesium.PointPrimitiveCollection());
  for(var i=0;i<msg.lat.length;i++){
    points.add({
      position:  new Cesium.Cartesian3.fromDegrees(msg.lon[i], msg.lat[i], msg.alt[i]),
      color:     Cesium.Color[msg.colour[i].toUpperCase()],
      pixelSize: msg.size[i]
    });
  }
}

Note that it is standard for the package to accept arguments such as color, size, width, and so on as having either one value or a number of values equal to the number of input points, polygons, or lines. That is: you should be able to set the property of the entire group at once or at an individual level.

Note that functions added to R/webglobe.R should be accompanied by help text an examples, see the existing functions for templates. Documentation should then be regenerated using

roxygen2::roxygenise()

Changes to the vignettes (e.g. vignettes/webglobe.Rmd) can be built using:

devtools::build_vignettes()

It is polite to ensure that everything's good by using:

devtools::check()

Once you have added your function, documented it, added any pertinent explanations to the vignettes, and checked it, submit a pull request!

Licensing

This package uses the following libraries:

  • cesiumjs: Cesium is licensed under Apache v2

This package, and all code and documentation not otherwise mentioned above (essentially anything outside the src/ directory of this package) are released under the MIT (Expat) license, as stated in the LICENSE file. The LICENCE file exists for use with CRAN.

Roadmap

  • Make not-intermediate mode work

  • Additional graphics primitives

  • Submission to CRAN

Credits

This R package was developed by Richard Barnes (https://rbarnes.org/).

It uses the Cesium WebGL virtual globe and map engine (link).

webglobe's People

Contributors

r-barnes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

webglobe's Issues

ERROR: loading failed for 'i386'

Hi Richard,

Interesting project. Thanks for your contribution. After installing R 3.3.3 and packages rgeos, V8, magrittr, maptools and rgdal, I got the following error:

> devtools::install_github('r-barnes/webglobe', vignette=TRUE)
Downloading GitHub repo r-barnes/webglobe@master
from URL https://api.github.com/repos/r-barnes/webglobe/zipball/master
Installing webglobe
"C:/PROGRA~1/R/R-33~1.3/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD INSTALL  \
  "C:/Users/Alí/AppData/Local/Temp/RtmpqSDavB/devtools245c67013ed6/r-barnes-webglobe-d26bac4" --library="C:/Program  \
  Files/R/R-3.3.3/library" --install-tests 

* installing *source* package 'webglobe' ...
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
*** arch - i386

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
*** arch - x64
ERROR: loading failed for 'i386'
* removing 'C:/Program Files/R/R-3.3.3/library/webglobe'
Error: Command failed (1)

> sessionInfo()
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics

Thanks,

Ali

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.