Skip to content

MapWinGIS6 – Vector Layer Projection Plan - #98

Open
pmeems wants to merge 2 commits into
developmentfrom
phase-1-vector-layer-projection
Open

pmeems wants to merge 2 commits into
developmentfrom
phase-1-vector-layer-projection

Conversation

@pmeems

@pmeems pmeems commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Motivation

Implement Phase 1 of the vector-layer projection plan so Core can represent CRS values and track layer/map projection state.

Context and design

  • Adds immutable mwg::Projection with EPSG-first identity, WKT/PROJ4 support, autodetection, and stateless transforms.
  • Replaces VectorLayer::ProjectionWkt() with VectorLayer::Projection().
  • Adopts the first known layer CRS in Map and preserves it across remove/clear until explicitly changed.

Validation

CMake Debug x64 and MSBuild Debug x64 built successfully. CTest passed all 524 non-interactive cases; the opt-in real embankments dataset resolved to EPSG:28992. README and Core architecture/porting docs were updated. No project-memory change was needed because the verified decisions are captured in authoritative documentation.

- add immutable EPSG, WKT, and PROJ4 projection values
- expose vector and persistent map projection state
- cover projection identity, adoption, and persistence in tests

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

@pmeems pmeems left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Independent review: recommend addressing the findings below before merging. CI was still in progress at review time. Final decision is with the human reviewer.

}
if (code == nullptr)
{
code = reference.GetAuthorityCode("GEOGCS");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Severity: Major | Confidence: High

Issue: Epsg() accepts numeric IDs from any authority and falls back to the nested GEOGCS ID. GDAL's AutoIdentifyEPSG() can populate that nested ID even when it cannot identify the projected CRS; one example is a custom Lambert Conformal Conic on a base WGS 84 CRS tagged EPSG:4326.

Impact: The projected CRS can report 4326 and IsSame(Projection::FromEpsg(4326)) then returns true for coordinates in metres versus degrees. A root ESRI or private numeric identifier can likewise be presented as an EPSG code.

Fix: Read the authority name and code only from the full CRS, require the EPSG authority, and continue to full-CRS FindMatches when no EPSG ID is available. Apply the same authority check to matches. Add cases with an identified base CRS and a non-EPSG root ID.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 340585b: EPSG autodetection now accepts only full-CRS EPSG authority codes and continues matching otherwise.

NormalizedName(_impl->_reference->GetAttrValue("PROJECTION"));
const std::string otherProjection =
NormalizedName(other._impl->_reference->GetAttrValue("PROJECTION"));
return !projection.empty() && projection == otherProjection;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Severity: Major | Confidence: High

Issue: After OGR has reported that the definitions differ, a matching CRS name or projection method still makes IsSame() return true. For example, two custom Transverse Mercator CRSs with different central meridians or false eastings can share a method and have no EPSG IDs.

Impact: A host using this predicate to decide whether to transform a layer can leave its coordinates untransformed and silently place it in the wrong location.

Fix: Do not use shared names or methods as sufficient evidence of equivalence. If aliases must be accepted, normalize the definitions and then compare the datum, axes, units, and conversion parameters. Add a negative test for two unidentified definitions with the same method and different parameters.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 340585b: CRS equivalence now requires OGR semantics or a canonical static PROJ4 round trip; names and methods alone are rejected.

{
const OGRSpatialReference* srs = _impl->_layer->GetSpatialRef();
if (srs == nullptr) return {};
if (srs == nullptr || !GeoJsonDeclaresCrs(_impl->_dataset))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Severity: Major | Confidence: High

Issue: The GeoJSON branch treats the absence of crs as unknown, but RFC 7946 section 4 defines WGS 84 longitude/latitude and removed the crs member. GDAL 3.12.4 already supplies the default CRS. The extra probe also reopens and scans the entire file in this ordinary case; async ActiveX loading reaches it both in the worker's candidate map and again in the UI-thread commit.

Impact: Standard GeoJSON fails to seed the map with its known CRS, and loading a large local or VSI GeoJSON can block the control thread even after background preparation finishes.

Fix: Use the driver-provided GeoJSON SRS without reopening the source. Assert that an RFC 7946 fixture reports WGS 84, and use a format that can genuinely omit its CRS for the unknown-CRS test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 340585b: layer projection now trusts GDAL's RFC 7946 WGS 84 SRS and no longer reopens or scans GeoJSON.

std::string VectorLayer::ProjectionWkt() const
Projection VectorLayer::Projection() const
{
const OGRSpatialReference* srs = _impl->_layer->GetSpatialRef();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Severity: Major | Confidence: High

Issue: A non-null OGR SRS does not always mean the source defines a CRS. GDAL 3.12.4 returns synthetic SRS objects for the GeoPackage reserved IDs 0 (undefined geographic) and -1 (undefined Cartesian). Those reach projection construction here, unlike the GDAL-specific undefined SRS used by the new null-CRS fixture, which the driver returns as null.

Impact: An otherwise valid GeoPackage with a standard undefined SRS is treated as having a known CRS, blocking the map from adopting the first genuinely known CRS that follows it.

Fix: Recognize the GeoPackage undefined SRS sentinels and return an empty projection for them. Exercise standard IDs 0 and -1 in dataset and map adoption tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 340585b: GeoPackage synthetic undefined SRS values are treated as unknown, with reserved IDs 0 and -1 covered.

CPLFree(wkt);
return result;
const std::optional<mwg::Projection> projection =
mwg::Projection::TryFromWkt(result);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Severity: Major | Confidence: High

Issue: Converting the layer's SRS to WKT and reparsing it drops its coordinate epoch. GDAL's GeoPackage reader sets the epoch from the epoch column on the SRS object; exportToWkt() serializes the CRS definition, not that per-dataset epoch. OGR's transformation initialization reads the epoch from the SRS object.

Impact: A dynamic-CRS vector layer can be transformed at the wrong epoch even though the source supplies the correct one.

Fix: Add an internal path that clones the driver's OGRSpatialReference directly into the projection value, preserving its coordinate epoch without exposing GDAL in public headers. Cover it with an epoch-bearing GeoPackage fixture.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 340585b: WKT2 export/import retains coordinate epochs from dynamic GeoPackage layer SRS values.

_impl->_lastId = id.value;
if (!_impl->_projection.has_value() && !layerProjection.IsEmpty())
{
_impl->_projection = std::move(layerProjection);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Severity: Major | Confidence: High

Issue: Projection adoption is committed in Map::AddLayer() before MapView::AddLayer() can fail while fitting the extent. Its catch block rolls back by calling RemoveLayer(), which now intentionally retains the projection. The ActiveX load paths use the same remove-only rollback after a fit failure.

Impact: Adding the first known-CRS layer can fail and leave the map with no layers but with the rejected layer's CRS. A subsequent successful add then cannot adopt its own CRS.

Fix: Include CRS adoption in the layer-load transaction: defer adoption until fit/commit succeeds, or restore the previous projection in rollback paths. Test a failed initial fit followed by a successful add in a different CRS.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not changed: a transactionally correct fix requires coordinated rollback changes in MapView and/or ActiveX, which are outside the explicitly approved comment-file/test scope; confirmation is required.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Critical flaws in CRS identification and equivalence checks can incorrectly treat distinct coordinate systems as compatible, making coordinate-compatibility decisions unsafe.

4 open issues
What changed in this PR

Implements Phase 1 of Core projection support: a shared coordinate reference system (CRS) value, layer CRS access, and persistent map projection state. Display-time reprojection and ActiveX projection interfaces remain future work.

Changes:

  • Adds Projection parsing, EPSG identification, comparison, export, and coordinate transforms.
  • Replaces ProjectionWkt() with Projection() and adopts the first known layer CRS in an unset map.
  • Adds tests, CMake/MSBuild integration, and projection documentation.

Builds and tests were not run during this review.

File Description
tests/​MapWinGIS.Core.Tests/​VectorLayerTests.cpp Updates layer projection assertions.
tests/​MapWinGIS.Core.Tests/​VectorDatasetTests.cpp Tests CRS values and projectionless layers.
tests/​MapWinGIS.Core.Tests/​TestData.h Declares the projectionless GeoJSON fixture.
tests/​MapWinGIS.Core.Tests/​TestData.cpp Generates the projectionless fixture.
tests/​MapWinGIS.Core.Tests/​ProjectionTests.cpp Tests parsing, identity, export, and transforms.
tests/​MapWinGIS.Core.Tests/​MapWinGIS.Core.Tests.vcxproj Registers projection tests with MSBuild.
tests/​MapWinGIS.Core.Tests/​MapTests.cpp Tests map CRS adoption and persistence.
tests/​MapWinGIS.Core.Tests/​CMakeLists.txt Registers projection tests with CMake.
src/​MapWinGIS.Core/​src/​vector/​VectorDataset.cpp Returns CRS values and checks GeoJSON CRS declarations.
src/​MapWinGIS.Core/​src/​projection/​Projection.cpp Implements CRS values and transforms.
src/​MapWinGIS.Core/​src/​map/​Map.cpp Stores, adopts, and explicitly changes map CRS.
src/​MapWinGIS.Core/​MapWinGIS.Core.vcxproj Adds projection sources to MSBuild.
src/​MapWinGIS.Core/​include/​mapwingis/​vector/​VectorDataset.h Replaces the WKT accessor with a CRS value.
src/​MapWinGIS.Core/​include/​mapwingis/​projection/​Projection.h Declares public projection and transform APIs.
src/​MapWinGIS.Core/​include/​mapwingis/​map/​Map.h Exposes map projection state and controls.
src/​MapWinGIS.Core/​CMakeLists.txt Adds projection sources to CMake.
README.md Summarizes Core projection support.
docs/​PORTING.md Explains the projection API migration.
docs/​ARCHITECTURE.md Documents CRS state and current rendering limits.
.github/​plans/​Vector-Layer-Projection.md Defines the phased projection roadmap.

Review effort: Balanced

$- Restrict EPSG identity to complete-CRS authority metadata\n- Require semantic CRS equivalence without name or method fallbacks\n- Trust GeoJSON driver CRS defaults and recognize undefined GeoPackage SRS values\n- Preserve coordinate epochs and avoid projection reads after map adoption\n- Add focused regression coverage for CRS identity, GeoJSON, GeoPackage, and epoch behavior\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 20:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved CRS-handling defects can retain a rejected layer’s projection, incorrectly treat distinct CRSs as equivalent or empty, and corrupt or lose coordinate epochs during WKT import/export.

5 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Restore the prior projection when adding a layer fails

src/​MapWinGIS.Core/​src/​map/​Map.cpp:132

If the initial FitToMap() throws, MapView::AddLayer() removes the new layer but leaves its newly adopted projection behind. The failed load can therefore leave an empty map with the rejected layer's CRS, preventing the next successful layer from supplying its own. Save and restore the previous projection in the rollback at src/MapWinGIS.Core/src/map/MapView.cpp:162-166, while preserving projection persistence for ordinary layer removal.

4 resolved since last review

Review effort: Balanced

Comment on lines +96 to +99
std::ostringstream epochText;
epochText << std::setprecision(
std::numeric_limits<double>::max_digits10)
<< epoch;
Comment on lines +100 to +101
wkt.insert(wkt.size() - 1,
",COORDINATEEPOCH[" + epochText.str() + "]");
Comment on lines +155 to +159
OGRSpatialReference firstNormalized;
OGRSpatialReference secondNormalized;
return firstNormalized.importFromProj4(firstProj4.c_str()) == OGRERR_NONE &&
secondNormalized.importFromProj4(secondProj4.c_str()) == OGRERR_NONE &&
firstNormalized.IsSame(&secondNormalized) == TRUE;
Comment on lines +364 to +365
const std::optional<double> epoch = CoordinateEpoch(wkt);
const std::string input = RemoveCoordinateEpoch(std::string(wkt));
Comment on lines +157 to +160
const char* const name = srs.GetName();
return name != nullptr &&
(std::string_view(name) == "Undefined geographic SRS" ||
std::string_view(name) == "Undefined Cartesian SRS");
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.

2 participants