I have a dataset on countries in sub-Saharan Africa (which is set so that anybody can use it):
var SSA = ee.FeatureCollection('users/salem043/Africa_Districts')
.select("ADM0")
I would like to produce grids that cover this entire region but are within-country. That's to say, I do not want any grids to overlap a country's border.
For country-level grids, I usually use the country boundary, create a bounding box, then split that box up into grids. Here, for example, is my code for creating 20,000 grids for Iraq:
//Bounding box over Iraq
var lat_start = 29.0990251735;
var lat_end = 37.3852635768;
var lon_start = 38.7923405291;
var lon_end = 48.5679712258;
//Set number of grid cells
var num_cells = 20000;
var lon_edge = (lon_end-lon_start)/Math.sqrt(num_cells);
var lat_edge = (lat_end-lat_start)/Math.sqrt(num_cells);
// 3) Create the grid
var polys = [];
var polys_line = [];
var cell_id = 0;
for (var lon = lon_start; lon < lon_end; lon += lon_edge) {
var x1 = lon;
var x2 = lon + lon_edge;
for (var lat = lat_start; lat < lat_end; lat += lat_edge) {
cell_id = cell_id + 1;
var y1 = lat;
var y2 = lat + lat_edge;
polys.push(ee.Feature(ee.Geometry.Rectangle(x1, y1, x2, y2), {label: cell_id}));
}
}
I imagine there is a function that can call on each ADM0 (country boundary) in my feature collection, but I want the grids to be equal size, and it would be very tedious testing each one to figure out what to set the grid count to in order to ensure they have the same dimensions. What else can I do?