I'm trying to generate a grid of 6.72 km by 6.72 km points over the whole continent of Africa, but I'm having a lot of trouble in doing so.
I'm mainly running into 2 problems:
My first issue arises when I try to generate a grid on a single country. I downloaded the country level map of the world from GADM, kept only countries in Africa, and then I used the "Simplify Tool" In QGIS to make the polygons more simple so the code runs in a reasonable time.
Here is some example code for Angola based heavily on this answer on Stack Overflow, where I work with the original non-simplified GADM map, so I also changed the grid size to 200km so the code runs in a reasonable time. I had to change the CRS to EPSG:26910 for the code to run.
library(sf) library(tidyverse) #importing map of Angola angola <- readRDS(gzcon(url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_AGO_0_sf.rds"))) #checking shape plot(angola$geometry) #checking CRS st_crs(angola) #changing CRS angola <- st_transform(angola, 26910) st_crs(angola) #building grid grid <- angola %>% st_make_grid(cellsize =200000) %>% st_intersection(angola) %>% st_cast("MULTIPOLYGON") %>% st_sf() %>% mutate(id = row_number()) #checking grid plot(grid$geometry)
The problem here with the CRS is that the map with the grid is flipped. I tried changing back the CRS of the grid to the original after doing the calculations, but the dimensions of the pixels are not the desired when going back to the original CRS.
What CRS is the most adequate for this task? I'm very new to GIS, but I need a CRS that works properly in all Africa and that allows me to define a grid in meters and not in degrees.
My second issue is that I can't generate a grid over the whole continent of Africa. I downloaded the country level map of the world from GADM, kept only countries in Africa, and then I used the "Dissolve Tool" in QGIS to combine the shapes of all the countries and get the shape of the continent.
I can't provide code for people to replicate what I did in QGIS, so I attach this Dropbox link with the continent shapefile, and an example again with a grid with 200km, and that obviously suffer from the CRS problems mentioned in the previous point.
africa_comb <-st_read("africa_comb.shp") #changing CRS africa_comb <- st_transform(africa_comb, 26910) #making the grid grid_comb <- africa_comb %>% st_make_grid(cellsize =200000) %>% st_intersection(africa_comb) %>% st_cast("MULTIPOLYGON") %>% st_sf() %>% mutate(id = row_number())
Here I get the following error:
Error in CPL_geos_op2(op, x, y) : Evaluation error: TopologyException: Input geom 1 is invalid: Self-intersection at or near point...
I have tried before doing the same without dissolving the countries, but I run into the same error. Both maps look like this after changing the CRS, so I'm suspecting the CRS might be causing the issue here: