I am a beginner with R Programming (which means, patience!, please)...
In the process of following a tutorial on drawing maps (“Make beautiful world maps in RStudio | R programming”), I discovered the projection site (https://proj.org/operations/projections/), which shows all sorts of projection methods to make maps.
I decided to make a map with each and every method presented there (more than 150.)
To do that, I parsed the code with beautifulsoup and extracted all the names for the various projections.
Then I wrote an R program, see below, inspired from the aforementioned video, to create and save a map for each method.
The program works fine.
However, for some methods, I was told by R that there was something wrong.
Most often, when I went to the projection site to run the corresponding map, I could see that there were some extra parameters required after the “+proj=xxx”, such as “+proj=nsper +h=3000000 +lat_0=-20 +lon_0=145” for the “nsper” projection method.
In the case of “nsper”, it did fix the problem. However, for the vast majority of the ones that crashed, adding the extra parameters I found on the proj site did not help. It kept crashing.
I show the code below, and you can see the array of projection methods with a mention of the ones that crash and the corresponding error message.
Uncomment some of the entries to see the error message...
In the video, it is mentioned that for the projection methods that do not work, adding “coord_sf(datum = NA)” will help. It did not seem to help me...
The question is, of course, what do I do wrong, and what can I do to fix it.
And, I have compared some of the maps I have generated with the ones on the site, and quite a few of the ones I have generated did not look as good. Some of them have weird lines all over the place, for instance...
Thank you.
Here comes the code:
# Install packages if needed -------------------------------
if (!require("tidyverse")) install.packages("tidyverse", dependencies = TRUE)
if (!require("sf")) install.packages("sf", dependencies = TRUE)
# Load the packages
------------------------------------------
library("tidyverse") # Load dplyr, ggplot2, stringr, etc
library("sf") # Working with geographic _S_imple _F_eatures in R
# From https://www.youtube.com/watch?v=FoqiFR5ZCic
# Projection list: https://proj.org/operations/projections
projs <- c('+proj=adams_hemi', '+proj=adams_ws1', '+proj=adams_ws2', '+proj=aea +lat_1=29.5 +lat_2=42.5',
'+proj=aeqd', '+proj=airy', '+proj=aitoff', '+proj=alsk',
'+proj=apian', '+proj=august', '+proj=bacon', '+proj=bertin1953',
'+proj=bipc', '+proj=boggs', '+proj=bonne +lat_1=10', '+proj=calcofi',
'+proj=cass', '+proj=cc',
#'+proj=ccon +lat_1=52 +lon_0=19 +axis=esu +a=6390000 +x_0=330000 +y_0=-350000', # Does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=cea',
#'+proj=chamb +lat_1=10 +lon_1=30 +lon_2=40', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=collg', '+proj=col_urban', '+proj=comill', '+proj=crast',
'+proj=denoy', '+proj=eck1', '+proj=eck2', '+proj=eck3', '+proj=eck4', '+proj=eck5', '+proj=eck6',
'+proj=eqc',
#'+proj=eqdc', # does not work
# Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, :
# crs not found: is it missing?
#'+proj=chamb +lat_1=10 +lon_1=30 +lon_2=40', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=eqearth',
#'+proj=euler+lat_1=67+lat_2=75', # does not work
# Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, :
# crs not found: is it missing?
'+proj=fahey', '+proj=fouc', '+proj=fouc_s',
'+proj=gall',
#'+proj=geos +h=35785831.0 +lon_0=-60 +sweep=y', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4.
'+proj=gins8',
#'+proj=gn_sinu +m=2 +n=3', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=gnom', '+proj=goode', '+proj=gs48',
'+proj=gs50', '+proj=guyou', '+proj=hammer', '+proj=hatano', '+proj=healpix', '+proj=rhealpix',
'+proj=igh', '+proj=igh_o',
#'+proj=imw_p +lat_1=30 +lat_2=-40', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.
'+proj=isea', '+proj=kav5', '+proj=kav7', '+proj=krovak',
'+proj=labrd +lon_0=40 +lat_0=-10', '+proj=laea', '+proj=lagrng', '+proj=larr', '+proj=lask', '+proj=lcc +lon_0=-90 +lat_1=33 +lat_2=45',
#'+proj=lcca +lat_0=35', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=leac', '+proj=lee_os', '+proj=loxim',
#'+proj=lsat +ellps=GRS80 +lat_1=-60 +lat_2=60 +lsat=2 +path=2', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=mbt_s', '+proj=mbt_fps', '+proj=mbtfpp',
'+proj=mbtfpq', '+proj=mbtfps', '+proj=merc', '+proj=mil_os', '+proj=mill',
#'+proj=misrsom +path=1', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=moll',
#'+proj=murd1 +lat_1=30 +lat_2=50', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
#'+proj=murd2 +lat_1=30 +lat_2=50', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
#'+proj=murd3', # does not work
# Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, :
# crs not found: is it missing?
'+proj=natearth',
'+proj=natearth2', '+proj=nell',
'+proj=nell_h', '+proj=nicol', '+proj=nsper +h=3000000 +lat_0=-20 +lon_0=145', '+proj=nzmg',
#'+proj=ob_tran +o_proj=moll +o_lon_p=40 +o_lat_p=50 +lon_0=60', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=ocea',
#'+proj=oea', # does not work
# Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, :
# crs not found: is it missing?
'+proj=omerc +lat_1=45 +lat_2=55', '+proj=ortel', '+proj=ortho', '+proj=patterson',
#'+proj=pconic +lat_1=25 +lat_2=75', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=peirce_q',
#'+proj=poly', # does not work
# Error in CPL_geos_op2(op, x, y) :
# Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.
'+proj=putp1', '+proj=putp2', '+proj=putp3', '+proj=putp3p', '+proj=putp4p', '+proj=putp5',
'+proj=putp5p', '+proj=putp6', '+proj=putp6p', '+proj=qua_aut', '+proj=qsc', '+proj=robin', '+proj=rouss',
'+proj=rpoly',
#'+proj=s2+lat_0=0 +lon_0=0', # does not work
# Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, :
# crs not found: is it missing?
#'+proj=sch +plat_0=XX +plon_0=XX +phdg_0=XX', # does not work
# Error in CPL_transform(x, crs, aoi, pipeline, reverse, desired_accuracy, :
# crs not found: is it missing?
'+proj=sinu',
#'+proj=somerc', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Points of LinearRing do not form a closed linestring.
'+proj=stere', '+proj=sterea',
'+proj=gstmerc', '+proj=tcc', '+proj=tcea', '+proj=times',
#'+proj=tissot +lat_1=60 +lat_2=65', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=tmerc', '+proj=tobmerc',
'+proj=tpeqd +lat_1=60 +lat_2=65',
#'+proj=tpers +h=5500000 +lat_0=40', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=ups',
#'+proj=urm5 +n=0.9 +alpha=2 +q=4', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
#'+proj=urmfps +n=0.5', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=utm', '+proj=vandg',
'+proj=vandg2', '+proj=vandg3', '+proj=vandg4',
#'+proj=vitk1 +lat_1=45 +lat_2=55', # does not work
# Error in CPL_geos_is_empty(st_geometry(x)) :
# Evaluation error: IllegalArgumentException: Invalid number of points in LinearRing found 2 - must be 0 or >= 4.
'+proj=wag1', '+proj=wag2', '+proj=wag3',
'+proj=wag4', '+proj=wag5', '+proj=wag6', '+proj=wag7', '+proj=webmerc', '+proj=weren', '+proj=wink1',
'+proj=wink2'
#'+proj=wintri' # does not work
# Error in CPL_geos_op2(op, x, y) :
# Evaluation error: IllegalArgumentException: point array must contain 0 or >1 elements.
)
typeof(projs[1])
projs
i<- 1
i
for (proj in projs[i:length(projs)]) {
print(paste(i, ": ", proj))
i <- i + 1
world %>%
st_transform(crs = proj) %>%
ggplot() +
geom_sf()+
labs(title = "Various Projection Methods",
subtitle = paste("Method# ", (i - 1), ": ", substring(proj, 7)),
x = NULL, y = NULL,
caption = "?")+
#coord_sf(datum = NA) # Removes the graticule
theme_minimal()
ggsave(paste("C:/_D_DRIVE/__Programming JP/R Language/Maps/images/png/", substring(proj, 7) ,".png"),
width = 30, height = 20, units = "cm")
ggsave(paste("C:/_D_DRIVE/__Programming JP/R Language/Maps/images/jpg/", substring(proj, 7) ,".jpg"),
width = 30, height = 20, units = "cm")
# Change map projection ---------------------------------------
} # end of do all projection methods