Skip to content

Commit ce85ea4

Browse files
authored
LambertAzimuthalEqualAreaProjection: fix transformation when x = 0 (#91)
fixes #90
1 parent f6dac8c commit ce85ea4

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

src/ProjNet/CoordinateSystems/Projections/LambertAzimuthalEqualAreaProjection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ private void EllipsoidalMetersToRadians(ref double x, ref double y)
326326
rho = hypot(x, y);
327327
if (rho < EPS10)
328328
{
329-
x = 0.0; // lam
329+
x = central_meridian; // lam
330330
y = lat_origin; // phi
331331
return;
332332
}
@@ -354,7 +354,7 @@ private void EllipsoidalMetersToRadians(ref double x, ref double y)
354354
q = (x * x + y * y);
355355
if (q == 0.0)
356356
{
357-
x = 0.0; // lam
357+
x = central_meridian; // lam
358358
y = lat_origin; // phi
359359
return ;
360360
}

test/ProjNet.Tests/CoordinateTransformTests.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using NUnit.Framework;
44
using ProjNet.CoordinateSystems;
5+
using ProjNet.CoordinateSystems.Projections;
56
using ProjNet.CoordinateSystems.Transformations;
67
using ProjNet.Geometries;
78
using ProjNet.IO.CoordinateSystems;
@@ -322,6 +323,82 @@ public void TestLambertConicConformal2SP_Projection()
322323

323324
}
324325

326+
private ICoordinateTransformation CreateGeo2Laea(double centralMeridian, double latitudeOfOrigin)
327+
{
328+
var wgs84 = GeographicCoordinateSystem.WGS84;
329+
330+
var coordsys = CoordinateSystemFactory.CreateFromWkt("" +
331+
"PROJCS[\"Lambert_Azimuthal_Equal_Area_Custom\"," +
332+
"GEOGCS[\"GCS_WGS_1984\"," +
333+
"DATUM[\"D_WGS_1984\"," +
334+
"SPHEROID[\"WGS_1984\",6378137.0,298.257223563]]," +
335+
"PRIMEM[\"Greenwich\",0.0]," +
336+
"UNIT[\"Degree\",0.0174532925199433]]," +
337+
"PROJECTION[\"Lambert_Azimuthal_Equal_Area\"]," +
338+
"PARAMETER[\"False_Easting\",0.0]," +
339+
"PARAMETER[\"False_Northing\",0.0]," +
340+
$"PARAMETER[\"Central_Meridian\",{centralMeridian}]," +
341+
$"PARAMETER[\"Latitude_Of_Origin\",{latitudeOfOrigin}]," +
342+
"UNIT[\"Meter\",1.0]]");
343+
344+
return CoordinateTransformationFactory.CreateFromCoordinateSystems(wgs84, coordsys);
345+
}
346+
347+
[Test]
348+
[Repeat(1000)]
349+
public void TestLambertAzimuthalEqualArea_Projection_round_trip_on_origin()
350+
{
351+
double centralMeridian = Random.Next(-180, +180);
352+
double latitudeOfOrigin = Random.Next(-90, +90);
353+
354+
var trans = CreateGeo2Laea(centralMeridian, latitudeOfOrigin);
355+
356+
var forward = trans.MathTransform;
357+
var reverse = forward.Inverse();
358+
359+
double[] pGeo = new[] { centralMeridian, latitudeOfOrigin };
360+
361+
double[] pLaea = forward.Transform(pGeo);
362+
363+
double[] pGeo2 = reverse.Transform(pLaea);
364+
365+
double[] expectedPLaea = new double[2] { 0, 0 };
366+
367+
Assert.IsTrue(ToleranceLessThan(pLaea, expectedPLaea, 0.05), TransformationError("Lambert_Azimuthal_Equal_Area", expectedPLaea, pLaea));
368+
Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), TransformationError("Lambert_Azimuthal_Equal_Area", pGeo, pGeo2, true));
369+
}
370+
371+
[Test]
372+
[Repeat(1000)]
373+
public void TestLambertAzimuthalEqualArea_Projection_round_trip_on_arbitrary_point()
374+
{
375+
int GetRandomSign()
376+
{
377+
return Random.Next() % 2 == 0 ? -1 : +1;
378+
}
379+
380+
double centralMeridian = Random.Next(-150, +150);
381+
double latitudeOfOrigin = Random.Next(-70, +70);
382+
383+
var trans = CreateGeo2Laea(centralMeridian, latitudeOfOrigin);
384+
385+
var forward = trans.MathTransform;
386+
var reverse = forward.Inverse();
387+
388+
double lat = latitudeOfOrigin + (0.01 + Random.NextDouble()) * GetRandomSign();
389+
double lon = centralMeridian + (0.01 + Random.NextDouble()) * GetRandomSign();
390+
391+
double[] pGeo = new[] { lon, lat };
392+
393+
double[] pLaea = forward.Transform(pGeo);
394+
395+
double[] pGeo2 = reverse.Transform(pLaea);
396+
397+
Assert.NotZero(pLaea[0]);
398+
Assert.NotZero(pLaea[1]);
399+
Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), TransformationError("Lambert_Azimuthal_Equal_Area", pGeo, pGeo2, true));
400+
}
401+
325402
[Test]
326403
public void TestGeocentric()
327404
{

test/ProjNet.Tests/CoordinateTransformTestsBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class CoordinateTransformTestsBase
1010
{
1111
protected readonly CoordinateSystemFactory CoordinateSystemFactory = new CoordinateSystemFactory();
1212
protected readonly CoordinateTransformationFactory CoordinateTransformationFactory = new CoordinateTransformationFactory();
13+
protected readonly Random Random = new Random();
1314

1415
protected bool Verbose { get; set; }
1516

0 commit comments

Comments
 (0)