Skip to content

Rasters

jlhapp edited this page Sep 7, 2016 · 3 revisions

WHAT ARE RASTERS?

In short, rasters are cells that have values and are arranged into rows and columns. Examples of rasters include scanned maps, imagery data, and surface maps (the example that is used in this repository).

intro to rasters

IMPORT A RASTER

Use either the raster library or the sp package to import raster data.

  1. RASTER LIBRARY
  • Using the raster package will return a RasterLayout object and you will need to visualize using the plot() function.
  • To import name a object and use raster() function. For example: object <- raster("folder location.tif")
  • To view the map with a legend, use plot(). For example: plot(object name)
  1. SP PACKAGE
  • Using the SP package will return a SpatialGridDataFrame and you will need to visualize using the spplot() function.
  • To import name a object and use readGDAL() function. For example: object <- readGDAL("folder location.tif")
  • To view the map with a legend, use spplot(). For example: spplot(object)

Code is on "Rasters.R" script.

RASTERS IN LEAFLET

Use the raster() function to import the raster layer. You will need to specify color scheme using the colorNumeric() function. Name an object 'pal', and call the colorNumeric() funtion. Then specify color scheme choices using "c()", set the color values to the raster object and write code that specifies any part of the raster that has no value to transparent. For example:

pal <- colorNumeric(c("#0C2C84", "#41B6C4", "#FFFFCC"), values(object), na.color = "transparent")

Now that there's a object named 'pal' for the color scheme we can add this to leaflet. First add leaflet() and tiles() separated by the maggrittr (%>%) operator. Then add the raster using the addRasterImage() fucntion. Within the addRasterImage() function, you'll need to specify the data, colors, and opacity. Then you can add legend if you'd like using the addLegend() function. For example:

leaflet() %>% addTiles() %>% addRasterImage(climate, colors = pal, opacity = 0.8) %>%
addLegend(pal = pal, values = values(climate), title = "Annual Rain")

*You will get a result that looks like this. Code is available in the "Leaflet.R" script under "RASTER LEAFLET" section.

raster_leaflet

Clone this wiki locally