S2 coverings on a sphere 00:00:01 A delivery region crosses the date line A delivery zone crosses the date line. One dock sits at longitude one hundred seventy-nine degrees east, another at one hundred seventy-nine west. A flat minimum-to-maximum longitude interval wraps the long way around. At the equator the short separation between these two meridians is two degrees of longitude. The signed coordinate jumps at the date line, although the sea between the docks is continuous. Now I use an S2 covering to find all cell IDs that might contain any part of this spherical region. Specify a border policy before you count deliveries. A point exactly on a shared service boundary needs one predictable owner. That decision is separate from which approximate cells were used to find candidates. 00:00:47 Map the sphere to hierarchical cells S two begins with six faces of a cube projected onto the sphere. Each face covers one part of the globe, including places where longitude alone makes a poor organizing coordinate. Divide one face into four child cells. Divide each child into four again. That one face now has sixteen grandchildren. A subdivision multiplies its cell count by four. A connected Hilbert-style order traverses the faces and the subdivisions. It helps keep nearby records together in storage. The numeric difference between two cell identifiers is not a distance in metres. The hierarchy gives a convenient storage key. Points can be assigned at a fine level, and a coarser cell can represent a region of descendants. The cells are spherical quadrilaterals, even if their outlines look curved on a flat drawing. 00:01:38 Choose a conservative covering An exterior covering is a collection of cells whose union contains the whole delivery zone. Some selected cells cross the actual coast or service border, adding harmless candidate area. Use larger cells and the cover may use fewer identifiers while admitting more outside addresses. Finer boundary cells can tighten it at the cost of a longer list. Interior and exterior coverings answer opposite questions at a boundary. For candidate retrieval, ask whether every eligible address is guaranteed to lie somewhere within the chosen covering. Departments sometimes ask for cells known to be entirely inside the service zone. That different question calls for an interior covering. Using it to retrieve every customer would quietly drop customers near the border. 00:02:26 Filter against the real boundary Retrieve candidate point records whose cell identifiers lie within the exterior cover. The points just inside and just outside the delivery boundary can arrive in the very same batch. Apply spherical point-in-polygon containment to each original coordinate. Honor the service contract for a point exactly on the boundary. The index alone cannot determine the answer. The inside dock survives, while a dock across the same cell but outside the service polygon is removed. A covering false positive is expected. A missed inside dock is a correctness failure. Keep the original polygon, including any exclusion holes, available for this final test. Distance from an arbitrary border is insufficient. A house can be very near the border on either side. 00:03:15 Set a budget for coverage Tune the allowed levels and target cell count. The coverer may need more than the desired count under level constraints or when the region spans multiple face cells. Nearby coordinates can belong to distant numeric key ranges. Measure range reads and exact geometry tests instead of interpreting a difference in identifiers as kilometres. Test a polygon crossing the date line, points on both sides of its boundary, and a high-latitude case. Compare final results to direct spherical containment using the same rules. Read a few more cell intervals or test a few more candidate points? Compare both costs under the smallest level, largest level and cell-count settings, and verify the final decisions with known inside and outside locations.