0.1 Create Map of Points

# load packages
suppressWarnings(suppressPackageStartupMessages({
  library(leaflet)
  library(rgdal)
}))

# set paths
gdb = 'H:/esm296-4f/lab3/golf.gdb'

# read points from geodatabase
pts = readOGR(gdb, 'pts_tree', verbose=F)

# project points to geographic projection
pts_gcs = spTransform(pts, leaflet:::epsg4326)

# create map of points
leaflet() %>%
  addTiles() %>%
  addMarkers(data = pts_gcs) 

0.1.1 Add Popup

# create map with popups
leaflet() %>%
  addTiles() %>%
  addMarkers(data = pts_gcs, popup=~sprintf('id: %d', id)) 

0.1.2 Change Basemap

# create map with Stamen.Watercolor basemap
leaflet() %>%  
  addProviderTiles('Stamen.Watercolor') %>%
  addMarkers(data = pts_gcs, popup=~sprintf('id: %d', id)) %>%
  setView(lng=mean(pts_gcs@bbox[1,]), lat=mean(pts_gcs@bbox[2,]), zoom=16)

0.1.3 Add Lines and Polygons

# read points, lines and polygons from geodatabase
pts_gcs  = readOGR(gdb, 'pts_tree', verbose=F)    %>% spTransform(leaflet:::epsg4326)
lns_gcs  = readOGR(gdb, 'lns_cart', verbose=F)    %>% spTransform(leaflet:::epsg4326)
plys_gcs = readOGR(gdb, 'plys_bunker', verbose=F) %>% spTransform(leaflet:::epsg4326)

# map with lines and polygons
leaflet() %>%  
  addProviderTiles('Stamen.Toner') %>%
  addPolygons(data=plys_gcs, color='yellow', stroke=F) %>%  
  addPolylines(data=lns_gcs) %>%
  addMarkers(data=pts_gcs, popup=~sprintf('id: %d', id))

0.2 Create Raster Map

library(raster)
## Warning: package 'raster' was built under R version 3.2.2
# set path to your final chosen classified image
tif = 'H:/esm296-4f/lab3/images_rect/vid_sec166_5.tif'
  
# read raster, presumably in Web Mercator
r = raster(tif, crs = leaflet:::epsg3857)

# setup color palette
pal = colorFactor('Set3', values(r), na.color='transparent')

# map with raster
leaflet() %>%  
  addProviderTiles('Stamen.Toner') %>%
  addRasterImage(r, colors=pal, project=F, opacity=0.7) %>%
  addLegend(pal=pal, values=values(r), title='Habitats')

0.3 Embed GeoJSON Map from Github