Skip to content

Instantly share code, notes, and snippets.

@oscarperpinan
Last active April 4, 2018 21:31
Show Gist options
  • Select an option

  • Save oscarperpinan/5d5bb51ff419621d5f63429b8f88d679 to your computer and use it in GitHub Desktop.

Select an option

Save oscarperpinan/5d5bb51ff419621d5f63429b8f88d679 to your computer and use it in GitHub Desktop.

Revisions

  1. oscarperpinan revised this gist Apr 4, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions graticule.R
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    library(raster)
    library(rgdal)
    library(rasterVis)
    library(graticule)

  2. oscarperpinan revised this gist Apr 4, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion graticule.R
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ r <- getRaster('temp', day = testDay, frames = 1)
    ## Here is where the graticule routine starts
    crs.longlat <- CRS("+init=epsg:4326")
    prj <- CRS(projection(r))
    extLL <- projectExtent(r, crs.longlat)
    extLL <- projectExtent(r, crs = crs.longlat)

    lons <- pretty(c(xmin(extLL), xmax(extLL)))
    lats <- pretty(c(ymin(extLL), ymax(extLL)))
  3. oscarperpinan created this gist Apr 9, 2017.
    48 changes: 48 additions & 0 deletions graticule.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    library(rasterVis)
    library(graticule)

    ## Only needed for downloading a raster example
    library(meteoForecast)

    today <- Sys.Date()
    testDay <- today - 7

    ## Retrieve raster data
    r <- getRaster('temp', day = testDay, frames = 1)

    ## Here is where the graticule routine starts
    crs.longlat <- CRS("+init=epsg:4326")
    prj <- CRS(projection(r))
    extLL <- projectExtent(r, crs.longlat)

    lons <- pretty(c(xmin(extLL), xmax(extLL)))
    lats <- pretty(c(ymin(extLL), ymax(extLL)))
    ## optionally, specify the extents of the meridians and parallels
    ## here we push them out a little on each side
    xl <- range(lons) + c(-0.4, 0.4)
    yl <- range(lats) + c(-0.4, 0.4)
    ## build the lines with our precise locations and ranges
    grat <- graticule(lons, lats, proj = prj,
    xlim = xl, ylim = yl)
    ## Labels
    labs <- graticule_labels(lons, lats,
    xline = lons[2],
    yline = lats[2],
    proj = prj)
    labsLon <- labs[labs$islon,]
    labsLat <- labs[!labs$islon,]


    ## Display the raster
    levelplot(r) +
    ## and the graticule
    layer(sp.lines(grat)) +
    layer(sp.text(coordinates(labsLon),
    txt = parse(text = labsLon$lab),
    adj = c(1.1, -0.25),
    cex = 0.6)) +
    layer(sp.text(coordinates(labsLat),
    txt = parse(text = labsLat$lab),
    adj = c(-0.25, -0.25),
    cex = 0.6))