• Stars
    star
    261
  • Rank 153,753 (Top 4 %)
  • Language
    R
  • License
    Other
  • Created almost 6 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

R package for fast web gl rendering for leaflet

leafgl - performant WebGL rendering for leaflet

Travis build status monthly total CRAN status

An R package for fast web gl rendering of features on leaflet maps. It’s an R port of https://github.com/robertleeplummerjr/Leaflet.glify where more detailed information/documentation can be found. Also, if you like what you get here, make sure to star the original repo!


Installation

Stable version from CRAN
install.packages("leafgl")
Development version from Github
devtools::install_github("r-spatial/leafgl")

What does it do?

It allows rendering of a large amount of features on a leaflet map. What exactly a “large amount” is, depends

  1. on the types of features and
  2. on the hardware/software of the rendering machine.

Regarding 1. millions of points should be no problem, but millions of polygons might, depending on their complexity - i.e. the number of vertices.

With regard to 2., obviously the amount of RAM will matter most but there are other, more suptle, problems that can occur.

Given it’s name, leafgl is intended to fully integrate with the leaflet package, though it is very likely that it won’t be a 1:1 replacement for the respective leaflet::add* functions. For example, given the intention to render/visualise as many features as possible we need to make a compromise on what additional information we allow to be part of the rendering. So far, we allow coloring of features and popups based on one column of the feature attributes, hence you cannot provide your own popup content. This may seem drastic, but all this information is costly both in terms of performance/speed and memory. In the end, who wants to wait for a map that the browser isn’t able to render anyway…

What doesn’t it do?

For starters, it doesn’t guarantee to be working tomorrow. At this stage leafgl is pre-alpha and under heavy development so things are likely to change frequently. For example, we are thinking about shorter funtion names (e.g. addGlPolygons instead of the current addGlifyPolygons). Additionally, we are still figuring out which pathway is best to use in order to pass data from R to the browser. As a result, rendering environments other than the browser (or RStudio viewer) may not work properly or at all until we approach a more stable implementation.

NOTE

Depending on your operating system and browser, you may see some weird colors that do not correspond to the ones that you specified. The only known work-around at this stage is to set opacity = 1. For more details the inclined reader is referred to this issue

What can I do to help?

A lot! First and foremost you can use it as often as possible and report issues/bugreports and/or feature request (see end of page for details). If you have ideas on how to enhance functionality without impacting performance too much and feel confident enough to provide pull request, please don’t hesitate. Finally, if you have proficient knowledge of JavaScript and want/know how to improve the package in any way, we would very much love to hear from you!

Example usage

1 Mio. points on a map

This will render 1 Mio. points on a standard leaflet map.

library(leaflet)
library(leafgl)
library(sf)

n = 1e6

df1 = data.frame(id = 1:n,
                 x = rnorm(n, 10, 3),
                 y = rnorm(n, 49, 1.8))

pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)

options(viewer = NULL) # view in browser

leaflet() %>%
  addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
  addGlPoints(data = pts, group = "pts") %>%
  setView(lng = 10.5, lat = 49.5, zoom = 6)



Colouring points by value mapping

For this we use library(colourvalues) because it can create color voctors in the blink of an eye!

library(leaflet)
library(leafgl)
library(sf)
library(colourvalues)

n = 1e6

df1 = data.frame(id = 1:n,
                 x = rnorm(n, 10, 3),
                 y = rnorm(n, 49, 1.8))

pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)

cols = colour_values_rgb(pts$id, include_alpha = FALSE) / 255

leaflet() %>%
  addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
  addGlPoints(data = pts, fillColor = cols, group = "pts") %>%
  setView(lng = 10.5, lat = 49.5, zoom = 6)



100k polygons on a map

In reality, it only 97112 polygons… But who wants to be pedantic here?

This data was downloaded from https://download.geofabrik.de/europe/switzerland.html

library(leaflet)
library(leafgl)
library(sf)
library(colourvalues)

ch_lu = st_read("/media/timpanse/d8346522-ef28-4d63-9bf3-19fec6e13aab/bu_lenovo/software/testing/mapview/switzerland/landuse.shp")

ch_lu = ch_lu[, c(1, 3, 4)] # don't handle NAs so far

options(viewer = NULL)

cols = colour_values_rgb(ch_lu$type, include_alpha = FALSE) / 255

leaflet() %>%
  addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
  addGlPolygons(data = ch_lu, 
                color = cols, 
                popup = "type",
                group = "pols") %>%
  setView(lng = 8.3, lat = 46.85, zoom = 9) %>% 
  addLayersControl(overlayGroups = "pols")



Shiny

Thanks to [@ColinFay](https://github.com/ColinFay) leafgl has dedicated shiny functions. Given that what leafgl produces is a leaflet map, we only need to use leafglOutput in our ui call. In the server call we can simply use renderLeaflet. Here an example:

library(leaflet)
library(leafgl)
library(sf)
library(shiny)

n = 1e6

df1 = data.frame(id = 1:n,
                 x = rnorm(n, 10, 3),
                 y = rnorm(n, 49, 1.8))

pts = st_as_sf(df1, coords = c("x", "y"), crs = 4326)

options(viewer = NULL) # view in browser

m = leaflet() %>%
  addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
  addGlPoints(data = pts, group = "pts") %>%
  setView(lng = 10.5, lat = 49.5, zoom = 4) %>% 
  addLayersControl(overlayGroups = "pts")

ui <- fluidPage(
  leafglOutput("mymap")
)

server <- function(input, output, session) {
  output$mymap <- renderLeaflet(m)
}

shinyApp(ui, server)


Contact

Please file Pull requests, bug reports and feature requests at https://github.com/r-spatial/leafgl/issues

More Repositories

1

sf

Simple Features for R
R
1,265
star
2

rgee

Google Earth Engine for R
R
650
star
3

stars

Spatiotemporal Arrays, Raster and Vector Data Cubes
R
540
star
4

mapview

Interactive viewing of spatial data in R
JavaScript
503
star
5

mapedit

Interactive editing of spatial data in R
R
216
star
6

qgisprocess

R package to use QGIS processing algorithms
R
196
star
7

RQGIS

RQGIS - integrating R with QGIS
R
189
star
8

gstat

Spatial and spatio-temporal geostatistical modelling, prediction and simulation
C
188
star
9

dtwSat

Time-Weighted Dynamic Time Warping for satellite image time series analysis
R
126
star
10

spdep

Spatial Dependence: Weighting Schemes and Statistics
R
111
star
11

leafpop

Include Tables, Images and Graphs in Leaflet Popups
R
111
star
12

leafem

leaflet extensions for mapview
JavaScript
104
star
13

RQGIS3

R
69
star
14

s2

Spherical Geometry Operators Using the S2 Geometry Library
C++
69
star
15

r-spatial.org

r-spatial.org blog sources
HTML
58
star
16

lwgeom

bindings to the liblwgeom library
C
57
star
17

discuss

a discussion repository: raise issues, or contribute!
54
star
18

sfdbi

DBI interface to sf
R
53
star
19

sftime

time extension to sf objects
R
49
star
20

spatialreg

spatialreg: spatial models estimation and testing
R
41
star
21

rspatial_spark

This is the repo that sparked https://github.com/r-spatial
HTML
36
star
22

cesium

Cesium wrapper to R
R
33
star
23

leafsync

Small Multiples for Leaflet Webmaps
JavaScript
32
star
24

global

R-global: analysing spatial data globally
HTML
31
star
25

classInt

Choose Univariate Class Intervals
R
29
star
26

RPyGeo

Update of RPyGeo package - currently WIP
R
27
star
27

evolution

Preparing CRAN for the retirement of rgdal, rgeos and maptools
TeX
25
star
28

link2GI

Simplify the linking of GIS/RS and CLI tools
R
25
star
29

slideview

Compare Rasters Side by Side with a Slider
R
25
star
30

cubeview

Interactively Explore 3D Raster Data Cubes
JavaScript
24
star
31

leafpm

Leaflet.pm for R Leaflet
R
22
star
32

RSAGA

R
19
star
33

plainview

View Raster Images Interactively on a Plain HTML Canvas
R
13
star
34

task_views

Local copy for editing CRAN Task Views
R
9
star
35

rgeopackage

R package with helper tools in creating or handling GeoPackage files
R
9
star
36

r-spatial.github.io

r-spatial.github.io website
HTML
5
star