Last active
August 29, 2015 14:27
-
-
Save pepijn-devries/d28234fa152016702a96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(rgl) | |
# Download a topographic image of Earth from NASA's website | |
# The file downloaded here is actually the lowest available | |
# resolution. Higher resolutions are available. | |
download.file("http://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73751/world.topo.bathy.200407.3x5400x2700.jpg", "world.topo.jpg", mode = "wb") | |
# Use third party software (ImageMagick) to convert the jpeg | |
# into a png file (rgl can only work with png). | |
# I also reduce the size and number of colours to achieve an | |
# acceptable file size | |
shell("convert world.topo.jpg -resize 50% -colors 64 world.topo.png") | |
# function to convert polar to Cartesian coordinates, | |
# assuming that the world is a perfect sphere | |
polar_to_cart <- function(long, lat, alt = 1) | |
{ | |
x <- outer(long*pi/180, lat*pi/180, function(x, y) alt*cos(y)*cos(x)) | |
y <- outer(long*pi/180, lat*pi/180, function(x, y) alt*cos(y)*sin(x)) | |
z <- outer(long*pi/180, lat*pi/180, function(x, y) alt*sin(y)) | |
return (list(x = x, y = y, z = z)) | |
} | |
# Specify reference coordinates to project our image onto | |
lat <- seq(90,-90, len=50) | |
long <- seq(-180, 180, len=50) | |
# get Cartesian coordinates from the specified longitudes and latitudes | |
world_coords <- polar_to_cart(long, lat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment