# Intersect a regular grid and a projected world Map

**URL:** <https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928>\
**Category:** Help\
**Created:** [April 16, 2021, 6:55am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928 "2021-04-16T06:55:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [April 16, 2021, 6:55am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/1 "2021-04-16T06:55:49Z")

</div>

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](https://observablehq.com/@neocartocnrs/grids). 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](https://observablehq.com/@neocartocnrs/world-grids). 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.

---

<div class="post-metadata">

**Author:** ![hellonearthis](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/hellonearthis/32/3935_2.png) [@hellonearthis](https://talk.observablehq.com/u/hellonearthis)\
**Post date:** [April 16, 2021, 7:23am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/2 "2021-04-16T07:23:41Z")

</div>

Is this what you are looking for? [Examples · emeeks/d3-carto-map Wiki · GitHub](https://github.com/emeeks/d3-carto-map/wiki/Examples)

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [April 16, 2021, 7:45am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/3 "2021-04-16T07:45:38Z")

</div>

Not really. These materials are a bit old.

---

<div class="post-metadata">

**Author:** ![severo](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/severo/32/786_2.png) [@severo](https://talk.observablehq.com/u/severo)\
**Post date:** [April 16, 2021, 8:04am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/4 "2021-04-16T08:04:21Z")

</div>

I sent you a suggestion. The idea is:

1. use [d3.polygonCentroid(gridCell)](https://github.com/d3/d3-polygon#polygonCentroid) to compute the center of every grid cell (in pixels)
2. use [projection.invert([cx,cy])](https://github.com/d3/d3-geo/#projection_invert) to get the longitude and latitude for the grid cell center
3. use [geoContains](https://github.com/d3/d3-geo/#geoContains) to test if the center is in the land

 ![Capture d’écran de 2021-04-16 10-01-06](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/4/4b75a8c5e0efc8e10d85229608aa34b39ecc9f70.png)

 ![Capture d’écran de 2021-04-16 10-00-21](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/5/54002659653122ef70f2016215b156859247997f.png)

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [April 16, 2021, 8:24am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/5 "2021-04-16T08:24:38Z")

</div>

Thanks a lot 🙏

 ![hexagons](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/1/1a527661df7972b42c41d64f79f88fd789e462e7.png) ![squares](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/7/7a9fdcc3bf9449fdef3967a9ecbd28ddf8f00f8f.png) ![triangles](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/4/418f39bf9f56a8b864ce0c75edbf209c86b6d189.png) ![voronoi](https://canada1.discourse-cdn.com/flex030/uploads/observablehq/original/2X/3/3cd1119fc48190c665df208b9818d970a2cffd1f.png)

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [April 17, 2021, 7:09am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/6 "2021-04-17T07:09:25Z")

</div>

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

> **[Another hex map](https://observablehq.com/@fil/another-hex-map)**
>
> An optimization: Calling d3.geoContains(feature, point) repeatedly for many points is about the same as projecting the feature again and again. Slow. With geoContainsCache(land), we project the feature only once, on a hidden canvas, and read from the...

> **[Hex Globe](https://observablehq.com/@visionscarto/hex-globe)**
>
> Land represents 29% of the Globe's surface; the Gray-Fuller grid with parameter N has pts; to get about dots we set N = .

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [April 26, 2021, 7:16am UTC](https://talk.observablehq.com/t/intersect-a-regular-grid-and-a-projected-world-map/4928/7 "2021-04-26T07:16:15Z")

</div>

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