Intersect a regular grid and a projected world Map

I created several functions to make regular grids based on the size of my document and a step to vary. See Let's Make Grids / neocarto / Observable. Now I want to overlay this grid on a world map and find out which cells overlap this world map (or simply which centroids are contained in the world map). See World Grids / neocarto / Observable. But it can’t work. Because my basemap is in geographic coordinates while the coordinates of my grid depend on the size of the page (because I want a regular grid whatever the projection). How to proceed? Any ideas would be welcome.

Is this what you are looking for? Examples · emeeks/d3-carto-map Wiki · GitHub

Not really. These materials are a bit old.

I sent you a suggestion. The idea is:

  1. use d3.polygonCentroid(gridCell) to compute the center of every grid cell (in pixels)
  2. use projection.invert([cx,cy]) to get the longitude and latitude for the grid cell center
  3. use geoContains to test if the center is in the land

3 Likes

Thanks a lot :pray:

2 Likes

geocontains can be very slow, see if you can draw the map once to canvas, and read the relevant pixels instead:

4 Likes

Thank you very much @Fil. I don’t have time to deal with it now. But I have added your comments to my notebook.