Mollweide (and other bounded-projection) COGs draw stretched triangles across the map near the poles

Open
#850 1 comment 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
typescript

Research direction

Start with src/render/raster-layer.ts, RasterReprojector, and clipMeshToWorld, then read tests/raster-layer.test.ts and the proposed bounded-crs-mesh.test.ts. Reproduce the Mollweide case with the example COG and inspect how reprojection handles points outside the projection domain. Done means the edge tiles no longer create stretched triangles, fully off-globe tiles draw nothing, and valid tiles still render; the issue leaves the domain check, edge handling, and globe view decisions open.

Written by the indexing model from the issue text.

Description

[!NOTE]
This issue was written with AI assistance (Claude Code). The findings, measurements and test results were checked by running the code, but please read it with that in mind.

What happens

COGs in World Mollweide (ESRI:54009) render with long streaks near the poles: triangles that stretch across most of the map, repeated in every copy of the world. Seen on 0.9.0 and 0.10.0-beta.3; main (40f1b54) has the same code.

Example COG: https://data.source.coop/vizzuality/hfp-100/hfp_2017_100m_v1-2_cog.tif

Image
Example

Why

1. Part of the image is off the globe. A Mollweide map of the world is an ellipse, but a GeoTIFF is a rectangle. So the image's pixel grid covers the ellipse plus empty areas around it, and those areas don't correspond to any place on Earth. Near the poles the ellipse is narrow, so most of each tile in the top and bottom rows is off the globe.

2. proj4 doesn't say when a point is off the globe. To draw a tile, deck.gl-raster converts each mesh vertex from the image's coordinates (Mollweide metres) to longitude/latitude. For a point inside the ellipse, proj4 gives the right answer. For a point outside it, you might expect proj4 to return NaN or throw, but it doesn't: it returns a real-looking longitude in the wrong place. Points just past the edge get wrapped around to the far side of the world, and points further out get pinned to ±180°.

You can see this by converting a point to longitude/latitude and back again. A point on the globe comes back where it started; an off-globe point comes back somewhere else. With proj4 2.22.0, for points along one row near the top of the image (y = 8.8 million m, where the ellipse is only about ±3.75 million m wide):

Point's x (m) On the globe? proj4's longitude Converted back, it lands this far from where it started
3,000,000 yes 136.35° 0 m (correct)
5,000,000 no -132.75° (wrapped to the other side) 7,900 km
12,000,000 no 180° (pinned) 8,000 km
-18,000,000 no -180° (pinned) 14,000 km

3. deck.gl-raster only checks for NaN. Every check for invalid positions in the mesh code looks for NaN or infinite values. proj4's answers are finite, so the wrong ones get through:

  • RasterReprojector builds the mesh using the wrong positions. They don't fit with their neighbours, so refinement never converges and stops at its iteration limit (did not converge after 10001 iterations … currentError=195.7).
  • makeUnwrappedReprojectionFns, which keeps tiles continuous across the antimeridian, works out each tile's longitudes from its corners. For an edge tile, those corners are off-globe values.
  • clipMeshToWorld cuts the resulting stretched triangles at the edge of the world and draws them.

So a tile that is entirely off the globe still draws about 20,000 triangles, and some vertices of edge tiles are drawn up to 26,000 km from where their pixels belong.

#636 was an earlier attempt at the same class of projections. Most of it also checks for NaN, so it doesn't catch this. Its one check that does work, converting each tile corner there and back, only looks at the four corners, so it misses low-zoom tiles whose corners are all off the globe while the ellipse still passes through them.

Reproducing it in a test

The test runs RasterLayer._generateMesh directly, the same way makeBareLayer does in tests/raster-layer.test.ts. It uses proj4's Mollweide conversion and four tiles from the example COG's grid, and checks that:

  1. every vertex that gets drawn, converted back to Mollweide, lands within 5% of a tile's width of the pixel it came from;
  2. no triangle spans more than a quarter of the world's width;
  3. a tile entirely off the globe draws nothing;
  4. a tile entirely on the globe still draws, so ordinary tiles are unaffected.
Tile Result on 0.10.0-beta.3
Entirely on the globe passes
Crossing the edge of the ellipse fails: a vertex is 16,000 km out of place
One low-zoom tile covering the whole image, all four corners off the globe fails: 26,000 km
Entirely off the globe fails: draws 19,809 triangles

Full test: bounded-crs-mesh.test.ts

Proposed fix

Detect off-globe points by converting them there and back, not by looking for NaN:

  1. Off-globe check. A pixel is on the globe if converting it to the map and back lands within 1% of a tile's width of where it started. This has to use the original conversion functions, not the antimeridian-adjusted ones, because only the originals convert back correctly.
  2. Edge tiles. In RasterLayer._generateMesh, check 17 points along each side of the tile, not just the corners. If any is off the globe, draw the tile as a 64×64 grid instead of the usual adaptive mesh, leaving out every grid cell that touches an off-globe point. The grid then goes through clipMeshToWorld as today.
  3. Every other tile uses the adaptive mesh exactly as before. The added cost is 68 conversions per tile.

We're using this fix as a patch in source-cooperative/cog-viewer#54, plus the test above. With it, all four test cases pass and the streaks are gone. We'd be happy to turn it into a PR against src/ (a new domain-mesh.ts, a small change to raster-layer.ts, unit tests, and a design doc in dev-docs/specs/ if you'd like one), but wanted to agree on the approach first.

Open questions

  • Where should the off-globe check live? Converting there and back works for any projection without extra setup. Alternatively, ReprojectionFns (or the proj package) could offer an optional isInDomain for projections whose boundaries are known, which would be exact and cheaper. One option is to use the round trip by default and let projections override it.
  • Grid or smooth edge? The grid makes the edge of the data stair-stepped (1/64 of a tile), which is noticeable at low zoom. A smoother result would need RasterReprojector to treat off-globe points as invalid and refine toward the boundary. Is that worth doing now, or as a follow-up?
  • Globe view. buildUniformGridMesh also keeps off-globe cells. Fix it in the same change, or separately?
  • Tolerance. 1% of a tile's width separates on-globe from off-globe points by thousands of times in the cases above, but some projection might need a different value.
  • Points that come back one world-width away. A valid point can come back exactly 360° away (in a grid whose longitudes run 0–360°, 200° comes back as -160°), which a plain there-and-back check would wrongly drop. Our patch keeps a point when that shift is the same on every row of the tile, which holds for a whole-world offset but not for Mollweide's off-globe points. Is there a cleaner way to tell the two apart?

cc #636

Dominant language
TypeScript
Stars
240
Forks
34
Avg merge
17h 24m
Merged PRs (30d)
62

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from developmentseed/deck.gl-raster

All issues in developmentseed/deck.gl-raster

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.