Nightlights II

[This article was first published on Steven Mosher's Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve modified some routines so that we are always grabbing a roughly equal area regardless of the latitude. Basically, you do this:


getLonScaleFactor <- function(lat){

kmAtEq <-111.3195

kmAtLat <- 111.41288*cos(lat*DEGREES.TO.RADIANS)-.09350*cos(3*lat*DEGREES.TO.RADIANS)+0.00012*cos(5*lat*DEGREES.TO.RADIANS)

return(kmAtEq/kmAtLat)

}

# the above function returns a scale factor for km per degree @ a given latitude

getExtent <-function(x,halfLength=.5,LonLat) {

lonAdjust<-getLonScaleFactor(LonLat[2])*halfLength

yMin <- max(ymin(x),LonLat[2] - halfLength)

yMax <- min(ymax(x),LonLat[2] + halfLength)

xMin <- max(xmin(x),LonLat[1] - lonAdjust)

xMax <- min(xmax(x),LonLat[1] + lonAdjust)

e <- extent(xMin,xMax,yMin,yMax)

return(e)

}

 

Simply we feed getExtent a “raster”, a  point, and a “halfLength”  The last parameter tells you how wide and tall your plot will be. So the default of .5 means that your “point” will be bracketed by 1/2 a degree of latitude  (+- 1/2 degree) and the longitude will be scaled depending upon the latitude. At the equator, this entails 1/2 degree in each direction. Toward the pole, I adjust the width to keep the areas generally similar. Note, the underlying data is not globally complete, there are small gaps at the pole. In a better implementation of “getExtent” I would wrap at the extrema. Then I played with colors. Arrg I hate doing color by number:

And by changing the call to “getExtent() I can request a grid that only looks out .25 degrees in latitude and longitude.


To leave a comment for the author, please follow the link and comment on their blog: Steven Mosher's Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)