class: center, middle, inverse, title-slide # Working with Geospatial Data ## Leaflet ### Ben Baumer ### SDS 192April 8th 2020(
http://beanumber.github.io/sds192/lectures/mdsr_geo_03-leaflet.html
) --- class: center, middle, inverse # `leaflet` --- ## Leaflet .center[![](http://leafletjs.com/docs/images/logo.png)] an open-source **JavaScript** library for mobile-friendly interactive maps .footnote[http://leafletjs.com] --- ## Leaflet...for R .footnote[http://rstudio.github.io/leaflet/] - one of many [`htmlwidgets`](http://www.htmlwidgets.org/showcase_leaflet.html) - `leaflet` - `Plotly` - `dygraphs` - `DataTables` - `rglwidget` - provides R interface to JavaScript [D3.js libraries](https://en.wikipedia.org/wiki/D3.js) --- ## Simple example .pull-left[ ```r library(leaflet) m <- leaflet() %>% addTiles() %>% addMarkers( lng = -72.638139, lat = 42.318119, popup = "Smith College" ) ``` ] .pull-right[ ```r m ```
] --- ## Leaflet supports `sf` ```r library(sf) ``` ``` ## Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3 ``` ```r nc <- system.file("shape/nc.shp", package = "sf") %>% st_read() ``` ``` ## Reading layer `nc' from data source `/home/bbaumer/R/x86_64-pc-linux-gnu-library/3.6/sf/shape/nc.shp' using driver `ESRI Shapefile' ## Simple feature collection with 100 features and 14 fields ## geometry type: MULTIPOLYGON ## dimension: XY ## bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 ## CRS: 4267 ``` ```r class(nc) ``` ``` ## [1] "sf" "data.frame" ``` --- ## Plotting `sf` objects in Leaflet ```r leaflet() %>% addTiles() %>% # st_transform does projection -- more on that later... addPolygons(data = st_transform(nc, 4326)) ```