Skip to content

Fix spatial Length translation for polygons and collections - #4167

Merged
roji merged 2 commits into
mainfrom
roji-polygon-length-triage
Nov 26, 2026
Merged

roji merged 2 commits into
mainfrom
roji-polygon-length-triage

Conversation

@roji

@roji roji commented Nov 26, 2026

Copy link
Copy Markdown
Member

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 5e0d65ce8f013ac553cc5b03fb16a51c5f7610b2

Dimension Assessment Rationale
Change confidence 8/10 ↑ 40 targeted geometry and geography tests passed, including collection, dimensional, null and cast cases.
↓ Only PostGIS 3.6.3 was tested; other versions and expression combinations remain unverified.
Diagnosis confidence 9.5/10 ↑ Direct PostGIS and EF reproductions confirmed that ST_Length returns zero for polygons and ST_Perimeter returns their perimeter.
Evidence fit 9.5/10 ↑ The issue's polygon Length scenario reproduces the incorrect zero result and is covered by the fix.

NTS polygon Length translates to ST_Length, which returns zero instead of the perimeter. Collections and polymorphic geometries also require different handling depending on their actual types.

Translate concrete polygons and multipolygons to ST_Perimeter, keeping the single-function ST_Length translation for points and lines. For polymorphic values, select the appropriate function at runtime; geometry collections may need both length and perimeter. Preserve geography mappings through CLR casts so measurements remain geodesic.

Closes #4151

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:05
@roji
roji enabled auto-merge (squash) November 26, 2026 14:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the spatial Length translation fix.

No open issues 🎉

What changed in this PR

Corrects spatial Length translation in the EF Core PostgreSQL provider, addressing #4151 so polygons return their perimeter instead of zero.

Changes:

  • Uses ST_Perimeter for polygons and multipolygons, with type-aware handling for polymorphic values and collections.
  • Preserves geography mappings through CLR casts to keep measurements geodesic.
  • Adds regression coverage for spatial types, dimensions, nulls, and casts, and documents the polygon translations.
File Description
test/​EFCore.PG.FunctionalTests/​Query/​SpatialQueryNpgsqlGeometryTest.cs Adds geometry length assertions and SQL baselines.
test/​EFCore.PG.FunctionalTests/​Query/​SpatialQueryNpgsqlGeographyTest.cs Covers geodesic lengths, collections, casts, and per-property geography mappings.
src/​EFCore.PG.NTS/​Query/​ExpressionTranslators/​Internal/​NpgsqlNetTopologySuiteMemberTranslatorPlugin.cs Selects length or perimeter by spatial type while preserving geography mappings.
docs/​mapping/​nts.md Documents polygon and multipolygon Length translations.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 14:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The geography collection length implementation and assertions mishandle polygon perimeters, causing test failures on PostGIS 3.6.3 and incorrect results on 3.6.4.

1 open issue

Review effort: Balanced

Comment on lines +234 to +236
return _sqlExpressionFactory.Case(
[new CaseWhenClause(_sqlExpressionFactory.In(Function("GeometryType", [instance], typeof(string)), polygonTypes), perimeter)],
length);
@roji
roji merged commit 396705a into main Nov 26, 2026
17 checks passed
@roji
roji deleted the roji-polygon-length-triage branch November 26, 2026 14:41
roji added a commit that referenced this pull request Nov 26, 2026
Backport #4167 and #4168.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
roji added a commit that referenced this pull request Nov 26, 2026
Backport #4167 and #4168.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

NTS: Geometry.Length of a polygon translates to ST_Length, which returns 0

2 participants