Skip to content

Conversation

@benjamin-cooper-ai
Copy link

@benjamin-cooper-ai benjamin-cooper-ai commented Dec 10, 2025

This is an implementation of S2CellIndex. Making this useful required implementing S2ClosestCellQuery and S2ClosestEdgeQuery.

I tried to test this as well as possible - I ported over a union of tests from the C++ library, the Golang library, and added some other example tests that match what those libraries return.

Let me know what I can add to build confidence here!

Fixes #1

@benjamin-cooper-ai
Copy link
Author

Am a little nervous here, public tests for S2 are not great. Current coverage stats:

ℹ file                         | line % | branch % | funcs % | uncovered lines
ℹ -------------------------------------------------------------------------------------------
ℹ geojson                      |        |          |         | 
ℹ  geometry.ts                 |  91.07 |    82.50 |   78.57 | 18 53-56
ℹ  linestring.ts               |  92.68 |    83.33 |   93.33 | 33-35
ℹ  loop.ts                     |  94.44 |    80.65 |   84.21 | 19 31 67-68
ℹ  point.ts                    | 100.00 |    83.33 |  100.00 | 
ℹ  polygon.ts                  |  87.80 |    85.71 |  100.00 | 22-24 37-38
ℹ  position.ts                 | 100.00 |    91.30 |  100.00 | 
ℹ  rect.ts                     |  98.21 |    92.31 |  100.00 | 27
ℹ  RegionCoverer.ts            |  97.24 |    75.61 |  100.00 | 15 96-98
ℹ  testing.ts                  | 100.00 |    88.46 |  100.00 | 
ℹ r1                           |        |          |         | 
ℹ  Interval.ts                 | 100.00 |    97.37 |  100.00 | 
ℹ  math.ts                     |  97.80 |    95.45 |  100.00 | 76 78
ℹ r2                           |        |          |         | 
ℹ  Point.ts                    |  97.40 |    93.10 |  100.00 | 70-71
ℹ  Rect.ts                     |  96.85 |    94.44 |   94.59 | 24 71-73 122-124 142
ℹ r3                           |        |          |         | 
ℹ  PreciseVector.ts            |  97.44 |    92.86 |   96.30 | 63-64 108 115
ℹ  Vector.ts                   |  98.95 |    95.83 |  100.00 | 91-92
ℹ s1                           |        |          |         | 
ℹ  angle_constants.ts          | 100.00 |   100.00 |  100.00 | 
ℹ  angle.ts                    |  97.73 |    94.44 |   90.00 | 64-65
ℹ  chordangle_constants.ts     | 100.00 |    93.33 |  100.00 | 
ℹ  chordangle.ts               |  94.70 |    93.59 |   95.35 | 68-78 90-92
ℹ  Interval_constants.ts       | 100.00 |   100.00 |  100.00 | 
ℹ  Interval.ts                 | 100.00 |    97.18 |   97.50 | 
ℹ s2                           |        |          |         | 
ℹ  Cap.ts                      | 100.00 |    92.19 |   93.75 | 
ℹ  Cell.ts                     |  96.70 |    95.14 |   89.13 | 94-95 143-154 156-157 182 22…
ℹ  cellid_constants.ts         |  96.55 |    93.75 |  100.00 | 19
ℹ  cellid.ts                   |  97.59 |    95.19 |   94.26 | 39 148 207 407-422
ℹ  CellIndex.ts                |  98.41 |    91.49 |   92.86 | 82-84 119-121 154 157
ℹ  CellUnion.ts                |  98.49 |    90.15 |   89.47 | 164 195 211 213-216
ℹ  centroids.ts                | 100.00 |    83.33 |  100.00 | 
ℹ  ClosestCellQuery.ts         |  93.95 |    90.32 |   77.59 | 10 50-51 60 72-74 83 85-88 1…
ℹ  ClosestEdgeQuery.ts         |  87.55 |    87.10 |   79.00 | 97-99 101-107 116 124-127 13…
ℹ  ContainsPointQuery.ts       |  99.39 |    89.66 |   95.00 | 12
ℹ  ContainsVertexQuery.ts      | 100.00 |    91.30 |  100.00 | 
ℹ  CrossingEdgeQuery.ts        |  95.99 |    90.20 |   96.55 | 54 138-149 258
ℹ  edge_clipping.ts            |  96.27 |    98.73 |   81.48 | 25 28-30 57 306-319
ℹ  edge_crossings.ts           |  89.34 |    86.27 |   71.88 | 66-68 89 123-128 138-148 157…
ℹ  edge_distances.ts           |  93.69 |    93.94 |   64.86 | 85-86 145-163 165-170
ℹ  EdgeCrosser.ts              |  99.57 |    96.43 |  100.00 | 13
ℹ  EdgeVectorShape.ts          |  91.34 |    90.91 |   79.17 | 22-23 69-70 72-74 76 86-88
ℹ  LatLng.ts                   |  96.04 |    86.67 |  100.00 | 40-42 97
ℹ  LaxLoop.ts                  |  86.60 |    84.62 |   83.33 | 36-38 56-62 64-66
ℹ  LaxPolygon.ts               |  89.62 |    81.82 |   84.62 | 39 64-77 79-85
ℹ  LaxPolyline.ts              |  86.52 |    90.48 |   78.26 | 41 58-60 62-64 70-73 75
ℹ  lookupIJ.ts                 | 100.00 |    95.45 |  100.00 | 
ℹ  Loop.ts                     |  81.14 |    87.40 |   78.64 | 19 93-98 102-104 129-135 140…
ℹ  matrix3x3.ts                | 100.00 |    94.29 |  100.00 | 
ℹ  Metric_constants.ts         |  92.21 |    92.59 |  100.00 | 13 55-59
ℹ  Metric.ts                   | 100.00 |    90.63 |  100.00 | 
ℹ  PaddedCell.ts               | 100.00 |    93.94 |  100.00 | 
ℹ  point_measures.ts           | 100.00 |    92.86 |  100.00 | 
ℹ  Point.ts                    |  97.46 |    92.31 |   80.56 | 32-34 95-97 108 123
ℹ  PointVector.ts              |  91.14 |    95.24 |   90.91 | 63-66 68-70
ℹ  Polygon.ts                  |  96.06 |    87.98 |   90.16 | 46 51 57 60 63-70 78 83-89 2…
ℹ  Polyline.ts                 |  98.48 |    94.78 |   85.71 | 81-83 131-132 134 141
ℹ  predicates.ts               |  94.86 |    96.92 |   77.55 | 34 201-205 208 212-218 223-2…
ℹ  Rect.ts                     |  94.97 |    89.47 |   91.67 | 17 35-36 134-140 205 218-220…
ℹ  RectBounder.ts              |  98.75 |    93.48 |  100.00 | 47-48
ℹ  Region.ts                   |  98.48 |    81.82 |   57.14 | 32
ℹ  RegionCoverer.ts            |  98.65 |    90.91 |   90.57 | 23 111-113 182-183 203-204 2…
ℹ  Shape.ts                    |  89.44 |    89.13 |   52.38 | 25 44 56 77 85 87-90 92-100 …
ℹ  ShapeIndex.ts               |  92.29 |    91.53 |   79.41 | 93-96 139-146 169 205 284-28…
ℹ  ShapeIndexCell.ts           |  90.77 |    91.30 |   78.95 | 42 59 61-62 64-65
ℹ  ShapeIndexClippedShape.ts   |  97.06 |    91.67 |   94.12 | 14 48
ℹ  ShapeIndexIterator.ts       |  92.61 |    88.14 |  100.00 | 15 57-61 87-90 127-131
ℹ  ShapeIndexRegion.ts         |  98.17 |    76.67 |  100.00 | 16 28
ℹ  ShapeIndexTracker.ts        |  89.33 |    90.00 |   86.96 | 108-113 115-123 125
ℹ  shapeutil.ts                |  95.63 |    85.19 |   85.19 | 76-82 110-111 136
ℹ  stuv.ts                     |  98.44 |    97.06 |  100.00 | 125-131
ℹ  testing_pseudo.ts           | 100.00 |    95.24 |   88.89 | 
ℹ  testing_textformat.ts       |  94.04 |    79.07 |   93.94 | 32-33 76-78 106 112-114
ℹ  testing.ts                  |  96.02 |    94.29 |   96.55 | 26-27 186-192
ℹ  util.ts                     |  85.25 |    91.89 |   83.33 | 16-19 34 58-61
ℹ  wedge_relations.ts          | 100.00 |    97.22 |  100.00 | 

Collected with node --experimental-test-coverage --import tsx --test [^_]**/[^_]*_test.ts

@benjamin-cooper-ai benjamin-cooper-ai marked this pull request as ready for review January 21, 2026 21:52
@missinglink
Copy link
Owner

Fantastic thanks @benjamin-cooper-ai it might take me some time to review, thanks for all your work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support S2CellIndex

2 participants