[mapguide-commits] r10271 - in branches/4.0/MgDev: Common/CoordinateSystem Common/Geometry/CoordinateSystem Server/src/UnitTesting Web/src/mapviewerjava Web/src/mapviewernet Web/src/mapviewerphp
svn_mapguide at osgeo.org
svn_mapguide at osgeo.org
Thu Nov 5 23:54:02 PST 2026
Author: jng
Date: 2026-11-05 23:54:02 -0800 (Thu, 05 Nov 2026)
New Revision: 10271
Modified:
branches/4.0/MgDev/Common/CoordinateSystem/CoordSysMeasure.cpp
branches/4.0/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemMeasure.h
branches/4.0/MgDev/Server/src/UnitTesting/TestCoordinateSystem.cpp
branches/4.0/MgDev/Server/src/UnitTesting/TestGeometry.cpp
branches/4.0/MgDev/Web/src/mapviewerjava/measure.jsp
branches/4.0/MgDev/Web/src/mapviewernet/measure.aspx
branches/4.0/MgDev/Web/src/mapviewerphp/measure.php
Log:
Measure distance through the MapGuide measure API
The measure command picked its method from the coordinate system type, so a projected map was
measured as a plane, which overstates ground distance by 1/cos(latitude) on EPSG:3857 - a
factor of 2 at 60N.
MapGuide's MgCoordinateSystemMeasure now applies the rule itself: planar where the projection
is locally representative (State Plane, UTM, State Plane-style grids), ellipsoidal for a world
projection. Call it here instead of duplicating the decision, which also keeps this in step
with the MapAgent geo.distance operation.
Fixes #2891.
Modified: branches/4.0/MgDev/Common/CoordinateSystem/CoordSysMeasure.cpp
===================================================================
--- branches/4.0/MgDev/Common/CoordinateSystem/CoordSysMeasure.cpp 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Common/CoordinateSystem/CoordSysMeasure.cpp 2026-11-06 07:54:02 UTC (rev 10271)
@@ -25,6 +25,14 @@
using namespace CSLibrary;
+// The largest disagreement between the planar and the ellipsoidal measurement of the same segment
+// that is still reported as a planar distance. A projected grid chosen for its area is locally
+// representative -- at 5 km the two agree to within 0.04% for State Plane, a UTM zone and the
+// Victorian grid -- while a world projection diverges by whole percent (4% for WORLD-MERCATOR at
+// 16N, 100% for EPSG:3857 at 60N), so 1% separates the two families with room on both sides.
+// See MgCoordinateSystemMeasure::GetDistance and #2891.
+static const double kdPlanarMeasurementTolerance = 0.01;
+
///////////////////////////////////////////////////////////////////////////
///<summary>
/// Initializes a new instance of CCoordinateSystemMeasure given a
@@ -77,14 +85,48 @@
double distance = 0.0;
MG_TRY()
- if(m_pCoordSys->GetType() == MgCoordinateSystemType::Arbitrary)
+ INT32 type = m_pCoordSys->GetType();
+
+ // The euclidean measurement cannot fail whatever the coordinate system is, so it is both the
+ // answer for a non earth system and the fallback for a projected one whose inverse projection
+ // is unavailable.
+ distance = m_pCoordSys->MeasureEuclideanDistance(x1, y1, x2, y2);
+
+ if(MgCoordinateSystemType::Arbitrary != type)
{
- distance = m_pCoordSys->MeasureEuclideanDistance(x1, y1, x2, y2);
+ try
+ {
+ double geodesic = m_pCoordSys->MeasureGreatCircleDistance(x1, y1, x2, y2);
+
+ // A geographic system measures in angular units, where the euclidean value is not a
+ // length at all, so the geodesic is always its answer. A projected grid chosen for its
+ // area (State Plane, UTM, the Victorian grid) is locally representative -- at 5 km its
+ // planar and geodesic lengths agree to within 0.04% -- so the planar value is the one
+ // its users compute from the projected coordinates themselves and is kept. A world
+ // projection is not representative: EPSG:3857 inflates its eastings by 1/cos(latitude),
+ // 4% at 16N and 100% at 60N, and there the geodesic is the only ground distance. The
+ // tolerance sits 25x above the widest local-grid margin measured and 4x below the
+ // smallest world-projection margin (#2891).
+ //
+ // The two lengths are compared rather than the projection's grid scale (GetScale) being
+ // consulted because measuring the segment is direction-agnostic, whereas GetScale
+ // reports one direction only on an anisotropic projection -- WORLD-ROBINSON at 45N
+ // reports 1.076 while its north-south factor is 0.946 -- and because the comparison
+ // promises what callers need: a distance changes only when it was wrong by more than
+ // the tolerance for that measurement.
+ if(MgCoordinateSystemType::Geographic == type ||
+ ::fabs(geodesic - distance) > kdPlanarMeasurementTolerance * geodesic)
+ {
+ distance = geodesic;
+ }
+ }
+ catch(MgException* e)
+ {
+ // A few projected systems have no usable inverse projection (PNG-SG, MAUR-ZONE), so
+ // they keep the euclidean measurement rather than failing the measurement outright.
+ SAFE_RELEASE(e);
+ }
}
- else
- {
- distance = m_pCoordSys->MeasureGreatCircleDistance(x1, y1, x2, y2);
- }
MG_CATCH_AND_THROW(L"MgCoordinateSystemMeasure.GetDistance")
return distance;
Modified: branches/4.0/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemMeasure.h
===================================================================
--- branches/4.0/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemMeasure.h 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Common/Geometry/CoordinateSystem/CoordinateSystemMeasure.h 2026-11-06 07:54:02 UTC (rev 10271)
@@ -159,8 +159,20 @@
///
/// \return
/// Returns the distance between (x1, y1) and (x2, y2) as a
- /// double in meters.
+ /// double in coordinate system units.
///
+ /// \remarks
+ /// The measurement is a ground distance. A geographic coordinate system is always measured
+ /// along its ellipsoid. A projected coordinate system is measured along the ellipsoid only
+ /// when its projection distorts the measurement by more than 1%: a grid chosen for its area
+ /// (State Plane, a UTM zone) is locally representative, and its planar distance is the one
+ /// computed from the projected coordinates themselves, so it is kept. A world projection such
+ /// as EPSG:3857 is not representative -- it inflates its eastings by 1/cos(latitude), 4% at
+ /// 16N and 100% at 60N -- and is measured along the ellipsoid. A non-earth (arbitrary)
+ /// coordinate system has no ellipsoid to measure along and always returns its planar distance.
+ /// A projected system whose inverse projection is unavailable also falls back to the planar
+ /// distance rather than failing.
+ ///
/// <!-- Example (PHP) -->
/// \htmlinclude PHPExampleTop.html
/// (-74.806394, 40.714169) is New York City, and (-71.061342,
Modified: branches/4.0/MgDev/Server/src/UnitTesting/TestCoordinateSystem.cpp
===================================================================
--- branches/4.0/MgDev/Server/src/UnitTesting/TestCoordinateSystem.cpp 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Server/src/UnitTesting/TestCoordinateSystem.cpp 2026-11-06 07:54:02 UTC (rev 10271)
@@ -57,6 +57,10 @@
const STRING Projected_UTM18_NAD83 = L"PROJCS[\"UTM83-18F\",GEOGCS[\"LL83\",DATUM[\"NAD83\",SPHEROID[\"GRS1980\",6378137.000,298.25722210]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"false_easting\",1640416.667],PARAMETER[\"false_northing\",0.000],PARAMETER[\"central_meridian\",-75.00000000000000],PARAMETER[\"scale_factor\",0.9996],PARAMETER[\"latitude_of_origin\",0.000],UNIT[\"Meter\",1.000000000000]]";
+// EPSG:3857. A conformal projection whose scale is 1/cos(latitude) at every point, so its
+// projected coordinates cannot be measured with Pythagoras (see #2891).
+const STRING PseudoMercatorWkt = L"PROJCS[\"WGS84.PseudoMercator\",GEOGCS[\"LL84\",DATUM[\"WGS84\",SPHEROID[\"WGS84\",6378137.000,298.25722293]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Popular Visualisation Pseudo Mercator\"],PARAMETER[\"false_easting\",0.000],PARAMETER[\"false_northing\",0.000],PARAMETER[\"central_meridian\",0.00000000000000],UNIT[\"Meter\",1.00000000000000]]";
+
const STRING EPSG_4326_Wkt = L"GEOGCS[\"LL84\",DATUM[\"WGS84\",SPHEROID[\"WGS84\",6378137.000,298.25722356]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.01745329251994]]";
const STRING EPSG_4326_Wkt_Alt = L"GEOGCS[\"LL84\",DATUM[\"WGS 84\",SPHEROID[\"WGS 84\",6378137,298.25722293287],TOWGS84[0,0,0,0,0,0,0]],PRIMEM[\"Greenwich\",0],UNIT[\"Degrees\",0.01745329252]]";
@@ -3442,11 +3446,16 @@
MgCoordinateXY coord1(410961.8644, 1415078.441);
MgCoordinateXY coord2(432415.4557, 1362302.051);
+ // This projection is a locally representative grid (Transverse Mercator, scale factor
+ // 0.9999), so GetDistance keeps the planar measurement a user computes from the projected
+ // coordinates rather than substituting the geodesic 0.01% away from it (#2891). See
+ // PseudoMercator_Measure_GetDistance for the world-projection case, which does not.
double distance = measure->GetDistance(&coord1, &coord2);
- REQUIRE(MgUtil::ValuesEqual(56975.49704, distance));
+ REQUIRE(MgUtil::ValuesEqual(56970.20205, distance));
+ REQUIRE(MgUtil::ValuesEqual(pCoordinateSystem->MeasureEuclideanDistance(&coord1, &coord2), distance));
double meters = pCoordinateSystem->ConvertCoordinateSystemUnitsToMeters(distance);
- REQUIRE(MgUtil::ValuesEqual(17366.16623, meters));
+ REQUIRE(MgUtil::ValuesEqual(17364.55231, meters));
}
catch(MgException* e)
{
@@ -3519,6 +3528,102 @@
}
}
+// A world projection has to be measured along the ellipsoid, not with Pythagoras on its projected
+// coordinates. EPSG:3857 makes the difference unmistakable: its eastings are inflated by
+// 1/cos(latitude), so a 1 degree east-west segment at 60N measures twice its ground length if it is
+// treated as a plane. MgCoordinateSystemMeasure::GetDistance is what the viewers, the MapAgent
+// geo.distance operation and MgGeometry::Distance all report through, so it has to return the
+// geodesic value here, while leaving a locally representative grid on its planar measurement
+// (Projected_Measure_GetDistance, #2891).
+TEST_CASE("PseudoMercator_Measure_GetDistance", "[CoordinateSystem]")
+{
+ try
+ {
+ MgCoordinateSystemFactory factory;
+ STRING ogcWkt = PseudoMercatorWkt;
+ Ptr<MgCoordinateSystem> pCoordinateSystem = factory.Create(ogcWkt);
+ REQUIRE(pCoordinateSystem);
+ REQUIRE(MgCoordinateSystemType::Projected == pCoordinateSystem->GetType());
+
+ // 60N, one degree of longitude apart.
+ Ptr<MgCoordinate> pCoord1 = pCoordinateSystem->ConvertFromLonLat(-1.0, 60.0);
+ Ptr<MgCoordinate> pCoord2 = pCoordinateSystem->ConvertFromLonLat(0.0, 60.0);
+ REQUIRE(pCoord1);
+ REQUIRE(pCoord2);
+
+ Ptr<MgCoordinateSystemMeasure> measure = pCoordinateSystem->GetMeasure();
+ REQUIRE(measure);
+
+ double planar = pCoordinateSystem->MeasureEuclideanDistance(pCoord1, pCoord2);
+ double distance = measure->GetDistance(pCoord1, pCoord2);
+
+ REQUIRE(MgUtil::ValuesEqual(pCoordinateSystem->MeasureGreatCircleDistance(pCoord1, pCoord2), distance));
+ REQUIRE(planar > distance);
+
+ // planar / geodesic is 1/cos(latitude), i.e. 2.0 at 60N.
+ double ratio = planar / distance;
+ REQUIRE(ratio > 1.99);
+ REQUIRE(ratio < 2.01);
+
+ // ... and the geodesic value is the length of that parallel at 60N, about 55.8 km.
+ double meters = pCoordinateSystem->ConvertCoordinateSystemUnitsToMeters(distance);
+ REQUIRE(meters > 55700.0);
+ REQUIRE(meters < 55900.0);
+ }
+ catch(MgException* e)
+ {
+ STRING message = e->GetDetails(TEST_LOCALE);
+ SAFE_RELEASE(e);
+ FAIL(MG_WCHAR_TO_CHAR(message.c_str()));
+ }
+ catch(...)
+ {
+ throw;
+ }
+}
+
+// MgCoordinateSystemMeasure::GetDistance keeps the planar value for a projected coordinate system
+// unless the two measurements disagree by more than 1%, so that a grid a user has always measured
+// on -- State Plane, a UTM zone, the Victorian grid -- keeps reporting the number it always has,
+// and the MapAgent geo.distance operation reports the same thing the viewers do (#2891). That is
+// only safe while a grid's scale factor keeps the disagreement far below the threshold; at 5 km
+// the measured margins are 0.014% (EPSG:3110), 0.010% (the Transverse Mercator system below) and
+// 0.040% (UTM), against 4% for WORLD-MERCATOR at 16N and 100% for EPSG:3857 at 60N. This pins the
+// small end of that margin.
+TEST_CASE("Projected_Measure_PlanarIsLocallyRepresentative", "[CoordinateSystem]")
+{
+ try
+ {
+ MgCoordinateSystemFactory factory;
+ Ptr<MgCoordinateSystem> pCoordinateSystem = factory.Create(ProjectedWkt);
+ REQUIRE(pCoordinateSystem);
+ REQUIRE(MgCoordinateSystemType::Projected == pCoordinateSystem->GetType());
+
+ MgCoordinateXY coord1(410961.8644, 1415078.441);
+ MgCoordinateXY coord2(432415.4557, 1362302.051);
+
+ double planar = pCoordinateSystem->MeasureEuclideanDistance(&coord1, &coord2);
+ double geodesic = pCoordinateSystem->MeasureGreatCircleDistance(&coord1, &coord2);
+ REQUIRE(geodesic > 0.0);
+
+ // The difference is the grid scale factor of 0.9999: not zero, but two orders of
+ // magnitude below the 1% at which the measure command prefers the geodesic.
+ double relative = ::fabs(planar - geodesic) / geodesic;
+ REQUIRE(relative > 0.0);
+ REQUIRE(relative < 0.001);
+ }
+ catch(MgException* e)
+ {
+ STRING message = e->GetDetails(TEST_LOCALE);
+ SAFE_RELEASE(e);
+ FAIL(MG_WCHAR_TO_CHAR(message.c_str()));
+ }
+ catch(...)
+ {
+ throw;
+ }
+}
+
TEST_CASE("Projected_ConvertCode", "[CoordinateSystem]")
{
try
Modified: branches/4.0/MgDev/Server/src/UnitTesting/TestGeometry.cpp
===================================================================
--- branches/4.0/MgDev/Server/src/UnitTesting/TestGeometry.cpp 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Server/src/UnitTesting/TestGeometry.cpp 2026-11-06 07:54:02 UTC (rev 10271)
@@ -1687,6 +1687,43 @@
}
}
+// MgGeometry::Distance has to report whatever the measure's policy says, because the MapAgent
+// geo.distance operation goes through it while the viewer measure commands call the measure
+// directly. If the two disagreed, one of them would still have the EPSG:3857 problem (#2891).
+TEST_CASE("Distance (follows the measure's policy)", "[Geometry]")
+{
+ try
+ {
+ MgGeometryFactory factory;
+ Ptr<MgCoordinate> coord1 = factory.CreateCoordinateXY(0.0, 0.0);
+ Ptr<MgCoordinate> coord2 = factory.CreateCoordinateXY(1.0, 1.0);
+ Ptr<MgPoint> point1 = factory.CreatePoint(coord1);
+ Ptr<MgPoint> point2 = factory.CreatePoint(coord2);
+
+ // An arbitrary system has no ellipsoid, so the measure is planar and the two must agree.
+ Ptr<MgCoordinateSystemMeasure> arbitrary = CreateMeasureArbitrary();
+ REQUIRE(point1->Distance(point2, arbitrary) == arbitrary->GetDistance(coord1, coord2));
+
+ // A geographic and a projected system both route through the same policy, whichever
+ // branch of it applies to them.
+ Ptr<MgCoordinateSystemMeasure> geographic = CreateMeasureGeographic();
+ REQUIRE(point1->Distance(point2, geographic) == geographic->GetDistance(coord1, coord2));
+
+ Ptr<MgCoordinateSystemMeasure> projected = CreateMeasureProjected();
+ REQUIRE(point1->Distance(point2, projected) == projected->GetDistance(coord1, coord2));
+ }
+ catch (MgException* e)
+ {
+ STRING message = e->GetDetails(TestServiceFactory::TEST_LOCALE);
+ SAFE_RELEASE(e);
+ FAIL(MG_WCHAR_TO_CHAR(message.c_str()));
+ }
+ catch (...)
+ {
+ throw;
+ }
+}
+
TEST_CASE("Distance", "[Geometry]")
{
try
Modified: branches/4.0/MgDev/Web/src/mapviewerjava/measure.jsp
===================================================================
--- branches/4.0/MgDev/Web/src/mapviewerjava/measure.jsp 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Web/src/mapviewerjava/measure.jsp 2026-11-06 07:54:02 UTC (rev 10271)
@@ -116,11 +116,10 @@
MgCoordinateSystemFactory srsFactory = new MgCoordinateSystemFactory();
MgCoordinateSystem srsMap = srsFactory.create(srs);
- int srsType = srsMap.getType();
- if(srsType == MgCoordinateSystemType.Geographic)
- distance = srsMap.measureGreatCircleDistance(x1, y1, x2, y2);
- else
- distance = srsMap.measureEuclideanDistance(x1, y1, x2, y2);
+ // The measurement rule -- planar for a locally representative grid, ellipsoidal for a
+ // world projection such as EPSG:3857 -- lives in MgCoordinateSystemMeasure, so this
+ // agrees with the MapAgent geo.distance operation by construction (#2891).
+ distance = srsMap.getMeasure().getDistance(x1, y1, x2, y2);
distance = srsMap.convertCoordinateSystemUnitsToMeters(distance);
Modified: branches/4.0/MgDev/Web/src/mapviewernet/measure.aspx
===================================================================
--- branches/4.0/MgDev/Web/src/mapviewernet/measure.aspx 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Web/src/mapviewernet/measure.aspx 2026-11-06 07:54:02 UTC (rev 10271)
@@ -94,11 +94,10 @@
MgCoordinateSystemFactory srsFactory = new MgCoordinateSystemFactory();
MgCoordinateSystem srsMap = srsFactory.Create(srs);
- int srsType = srsMap.GetType();
- if (srsType == MgCoordinateSystemType.Geographic)
- distance = srsMap.MeasureGreatCircleDistance(x1, y1, x2, y2);
- else
- distance = srsMap.MeasureEuclideanDistance(x1, y1, x2, y2);
+ // The measurement rule -- planar for a locally representative grid, ellipsoidal
+ // for a world projection such as EPSG:3857 -- lives in MgCoordinateSystemMeasure,
+ // so this agrees with the MapAgent geo.distance operation by construction (#2891).
+ distance = srsMap.GetMeasure().GetDistance(x1, y1, x2, y2);
distance = srsMap.ConvertCoordinateSystemUnitsToMeters(distance);
Modified: branches/4.0/MgDev/Web/src/mapviewerphp/measure.php
===================================================================
--- branches/4.0/MgDev/Web/src/mapviewerphp/measure.php 2026-11-06 07:47:46 UTC (rev 10270)
+++ branches/4.0/MgDev/Web/src/mapviewerphp/measure.php 2026-11-06 07:54:02 UTC (rev 10271)
@@ -84,11 +84,11 @@
$srsFactory = new MgCoordinateSystemFactory();
$srsMap = $srsFactory->Create($srs);
- $srsType = $srsMap->GetType();
- if($srsType == MgCoordinateSystemType::Geographic)
- $distance = $srsMap->MeasureGreatCircleDistance($x1, $y1, $x2, $y2);
- else
- $distance = $srsMap->MeasureEuclideanDistance($x1, $y1, $x2, $y2);
+ // The measurement rule -- planar for a locally representative grid, ellipsoidal for a
+ // world projection such as EPSG:3857 -- lives in MgCoordinateSystemMeasure, so this
+ // agrees with the MapAgent geo.distance operation by construction (#2891). The
+ // four-double overload is GetDistanceSimple in PHP (monkey_patch.i).
+ $distance = $srsMap->GetMeasure()->GetDistanceSimple($x1, $y1, $x2, $y2);
$distance = $srsMap->ConvertCoordinateSystemUnitsToMeters($distance);
More information about the mapguide-commits
mailing list