-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Add polar projection support to RasterReprojector #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
espg
wants to merge
4
commits into
developmentseed:main
Choose a base branch
from
englacial:feat/polar-projections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import type { ReprojectionFns } from "../src/delatin"; | ||
| import { RasterReprojector } from "../src/delatin"; | ||
|
|
||
| /** Wrap a longitude to [-180, 180] */ | ||
| function wrapLng(lng: number): number { | ||
| return ((((lng + 180) % 360) + 360) % 360) - 180; | ||
| } | ||
|
|
||
| /** | ||
| * Create ReprojectionFns that simulate a raster tile in a source CRS where | ||
| * the forward transform produces coordinates in source CRS space, and | ||
| * forwardReproject converts them to WGS84 with longitude wrapping (as proj4 | ||
| * does for geographic output CRS). | ||
| */ | ||
| function makeReprojectionFns( | ||
| originX: number, | ||
| originY: number, | ||
| pixelSizeX: number, | ||
| pixelSizeY: number, | ||
| opts?: { wrapLongitude?: boolean }, | ||
| ): ReprojectionFns { | ||
| const wrap = opts?.wrapLongitude ?? false; | ||
| return { | ||
| forwardTransform(pixelX: number, pixelY: number): [number, number] { | ||
| return [originX + pixelX * pixelSizeX, originY + pixelY * pixelSizeY]; | ||
| }, | ||
| inverseTransform(crsX: number, crsY: number): [number, number] { | ||
| return [(crsX - originX) / pixelSizeX, (crsY - originY) / pixelSizeY]; | ||
| }, | ||
| forwardReproject(x: number, y: number): [number, number] { | ||
| // Simulate proj4 behavior: wrap longitude to [-180, 180] | ||
| return wrap ? [wrapLng(x), y] : [x, y]; | ||
| }, | ||
| inverseReproject(x: number, y: number): [number, number] { | ||
| // Identity inverse — extended longitudes (>180) pass through | ||
| // unchanged, matching how proj4 handles inverse projection | ||
| // (proj4 normalizes longitude internally before inverse-projecting) | ||
| return [x, y]; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe("antimeridian detection", () => { | ||
| it("does not flag a tile that does not cross the antimeridian", () => { | ||
| // A tile centered around longitude 0, latitude 45 | ||
| const fns = makeReprojectionFns(-10, 40, 0.1, -0.05, { | ||
| wrapLongitude: true, | ||
| }); | ||
| const reprojector = new RasterReprojector(fns, 200, 200); | ||
| expect(reprojector.crossesAntimeridian).toBe(false); | ||
| }); | ||
|
|
||
| it("flags a tile that crosses the antimeridian", () => { | ||
| // Tile spans from 170° to 190° in source CRS. | ||
| // With wrapLongitude=true, forwardReproject wraps 190° → -170°, | ||
| // so corner longitudes are [170, -170] — a 340° range that triggers | ||
| // antimeridian detection. | ||
| const fns = makeReprojectionFns(170, 40, 0.1, -0.05, { | ||
| wrapLongitude: true, | ||
| }); | ||
| const reprojector = new RasterReprojector(fns, 200, 200); | ||
| expect(reprojector.crossesAntimeridian).toBe(true); | ||
| }); | ||
|
|
||
| it("normalizes longitudes to continuous range when crossing antimeridian", () => { | ||
| const fns = makeReprojectionFns(170, 40, 0.1, -0.05, { | ||
| wrapLongitude: true, | ||
| }); | ||
| const reprojector = new RasterReprojector(fns, 200, 200); | ||
| reprojector.run(0.5); | ||
|
|
||
| // All longitudes should be in [170, 190] — no jumps to negative values | ||
| for (let i = 0; i < reprojector.exactOutputPositions.length; i += 2) { | ||
| const lng = reprojector.exactOutputPositions[i]!; | ||
| expect(lng).toBeGreaterThanOrEqual(170 - 0.1); | ||
| expect(lng).toBeLessThanOrEqual(190 + 0.1); | ||
| } | ||
| }); | ||
|
|
||
| it("mesh triangles do not span more than 180 degrees of longitude", () => { | ||
| const fns = makeReprojectionFns(170, 40, 0.1, -0.05, { | ||
| wrapLongitude: true, | ||
| }); | ||
| const reprojector = new RasterReprojector(fns, 200, 200); | ||
| reprojector.run(0.5); | ||
|
|
||
| const { triangles, exactOutputPositions } = reprojector; | ||
| for (let t = 0; t < triangles.length; t += 3) { | ||
| const a = triangles[t]!; | ||
| const b = triangles[t + 1]!; | ||
| const c = triangles[t + 2]!; | ||
|
|
||
| const lngA = exactOutputPositions[a * 2]!; | ||
| const lngB = exactOutputPositions[b * 2]!; | ||
| const lngC = exactOutputPositions[c * 2]!; | ||
|
|
||
| const maxLng = Math.max(lngA, lngB, lngC); | ||
| const minLng = Math.min(lngA, lngB, lngC); | ||
| expect(maxLng - minLng).toBeLessThan(180); | ||
| } | ||
| }); | ||
|
|
||
| it("does not affect tiles far from the antimeridian", () => { | ||
| // A tile centered at longitude 0, no wrapping needed | ||
| const fns = makeReprojectionFns(-10, 40, 0.1, -0.05, { | ||
| wrapLongitude: true, | ||
| }); | ||
| const reprojector = new RasterReprojector(fns, 200, 200); | ||
| reprojector.run(0.5); | ||
|
|
||
| expect(reprojector.crossesAntimeridian).toBe(false); | ||
| // All longitudes should be in [-10, 10] | ||
| for (let i = 0; i < reprojector.exactOutputPositions.length; i += 2) { | ||
| const lng = reprojector.exactOutputPositions[i]!; | ||
| expect(lng).toBeGreaterThanOrEqual(-10 - 0.1); | ||
| expect(lng).toBeLessThanOrEqual(10 + 0.1); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the presence of this test file, I assume that this PR has a dependency on #269?