Skip to content

Commit

Permalink
Add Shapefile projection test.
Browse files Browse the repository at this point in the history
  • Loading branch information
KubaSzostak committed Sep 7, 2024
1 parent 6bf2cd6 commit 30d76ce
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/NetTopologySuite.IO.Esri.Test/Issues/Issue056.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using NetTopologySuite.Geometries;
using NUnit.Framework;
using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;

namespace NetTopologySuite.IO.Esri.Test.Issues;

/// <summary>
/// https://github.com/NetTopologySuite/NetTopologySuite.IO.Esri/issues/56
/// </summary>
internal class Issue056
{
[Test]
public void ReadShpProjection()
{
//var shpPath = TestShapefiles.PathTo("D:\\GIS\\NTS\\cs_1992_projection.shp");
var shpPath = TestShapefiles.PathTo("Issues/039/Line/Line.shp");
using var shpReader = Shapefile.OpenRead(shpPath);

var csFactory = new CoordinateSystemFactory();
var sourceCs = csFactory.CreateFromWkt(shpReader.Projection);
var targetCs = GeographicCoordinateSystem.WGS84;
var targetCsEnvelope = GetWgs84Envelope();

Assert.IsTrue(sourceCs is ProjectedCoordinateSystem);
Assert.IsTrue(targetCs is GeographicCoordinateSystem);

var transformationFactory = new CoordinateTransformationFactory();
var transformation = transformationFactory.CreateFromCoordinateSystems(sourceCs, targetCs);

foreach (var feature in shpReader)
{
var sourcePoint = feature.Geometry.Centroid;
var sourceCoords = new double[] { sourcePoint.X, sourcePoint.Y };
var tergetCoords = transformation.MathTransform.Transform(sourceCoords);
var targetPoint = new Point(tergetCoords[0], tergetCoords[1]);

Assert.IsTrue(targetCsEnvelope.Contains(targetPoint));
}
}

private Geometry GetWgs84Envelope()
{
double minX = -90.0;
double minY = -180.0;
double maxX = 90.0;
double maxY = 180.0;

var coordinates = new Coordinate[]
{
new(minX, minY), // Bottom-left
new(minX, maxY), // Top-left
new(maxX, maxY), // Top-right
new(maxX, minY), // Bottom-right
new(minX, minY) // Closing the polygon
};

var linearRing = new LinearRing(coordinates);
return new Polygon(linearRing);
}
}

0 comments on commit 30d76ce

Please sign in to comment.