Last active
January 1, 2017 12:29
-
-
Save valentinitnelav/67ca9b315e2557b6ac2ac534b5528ce1 to your computer and use it in GitHub Desktop.
Shift central meridian - fails concave polygon & closure of split polygons, Greenland example.R
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
# =================================================================================== | |
# Test case - Shift central/prime meridian | |
# Fails concave polygons and closure of split polygons | |
# Show case example for Greenland | |
# =================================================================================== | |
# __________ Load needed libraries __________ # | |
library(data.table) | |
library(ggplot2) | |
library(maps) | |
library(maptools) | |
# __________ Load some data __________ # | |
load(url("https://github.com/valentinitnelav/RandomScripts/blob/master/NaturalEarth.RData?raw=true")) | |
# __________ Prepare data for ggplot and shift coordinates __________ # | |
# shift central/prime meridian towards west – positive values only | |
center <- 180+20 | |
# transform map in a data table that ggplot can use | |
XY <- data.table(map_data(NE_countries)) | |
# Shift coordinates (data.table way) | |
# inspired from the answer of @Joris Meys at: | |
# http://stackoverflow.com/questions/5353184/fixing-maps-library-data-for-pacific-centred-0-360-longitude-display | |
# and code of Claudia Engel at: https://gist.github.com/cengel/2165374#file-greatcircleflights-r | |
# or at: http://web.stanford.edu/~cengel/cgi-bin/anthrospace/great-circles-on-a-recentered-worldmap-in-ggplot | |
XY[, long.new := long + center] | |
XY[, long.new := ifelse(long.new > 180, long.new-360, long.new)] | |
# split up coordinates of polygons that differ too much (got split across the globe) | |
XY[, to.split := sum(diff(long.new) > 300, na.rm=TRUE) > 0, by=group] | |
XY[, gr.split := ifelse(to.split & long.new < 0, paste0(group, ".", 1), group)] | |
# __________ plot shifted map __________ # | |
ggplot() + | |
geom_polygon(data=XY, | |
aes(x=long.new, y=lat, group=gr.split), | |
colour="black", | |
fill="gray80", | |
size = 0.25) + | |
coord_equal() | |
# __________ Case example Greenland __________ # | |
# the second Greenland polygon | |
XY[gr.split=="117.1"] | |
ggplot() + | |
geom_polygon(data=XY[gr.split=="117.1"], | |
aes(x=long.new, y=lat, group=gr.split), | |
colour="black", | |
fill="gray80", | |
size = 0.25) | |
# the first Greenland polygon | |
XY[gr.split=="117"] | |
ggplot() + | |
geom_polygon(data=XY[gr.split=="117"], | |
aes(x=long.new, y=lat, group=gr.split), | |
colour="black", | |
fill="gray80", | |
size = 0.25) | |
# Greenland - original polygon | |
ggplot() + | |
geom_polygon(data=XY[group==117], | |
aes(x=long, y=lat, group=group), | |
colour="black", | |
fill="gray80", | |
size = 0.25) | |
# Greenland - split polygon (together) | |
ggplot() + | |
geom_polygon(data=XY[group==117], | |
aes(x=long, y=lat, group=gr.split), | |
colour="black", | |
fill="gray80", | |
size = 0.25) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment