Created
September 22, 2020 14:41
-
-
Save FObermaier/27278975d91fa74f5b64bb53e8b491a3 to your computer and use it in GitHub Desktop.
Sample code for https://github.com/NetTopologySuite/NetTopologySuite/issues/434
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Globalization; | |
using System.IO; | |
using NetTopologySuite.Algorithm; | |
using NetTopologySuite.Geometries; | |
using NetTopologySuite.IO; | |
using NetTopologySuite.Simplify; | |
using NUnit.Framework; | |
namespace NetTopologySuite.Samples.Geometries | |
{ | |
public class convexhullexample | |
{ | |
public void TestConvexHull1() | |
{ | |
var rdr = new WKTReader(); | |
var g = rdr.Read("Wkt"); | |
var pts = g.Coordinates; | |
var ch = new Algorithm.ConvexHull(pts, g.Factory); | |
var ch1 = g.ConvexHull(); | |
Assert.AreEqual(ch.GetConvexHull(), ch1); | |
pts = ch1.Coordinates; | |
Assert.IsTrue(Orientation.IsCCW(ch1.Coordinates)); | |
var ch3 = TopologyPreservingSimplifier.Simplify(ch1, 1e-6); | |
TestContext.WriteLine(ch1.NumPoints); | |
TestContext.WriteLine(ch1.AsText()); | |
TestContext.WriteLine(); | |
//TestContext.WriteLine(ch2.AsText()); | |
//TestContext.WriteLine(); | |
TestContext.WriteLine(ch3.AsText()); | |
} | |
[TestCase(@"D:\obe\Downloads\border.txt")] | |
[TestCase(@"D:\obe\Downloads\s22a.xyz")] | |
public void TestFiles(string filePath) | |
{ | |
var pts = ReadFile(filePath); | |
var convexHullOp = new ConvexHull(pts, GeometryFactory.Default); | |
var ch = convexHullOp.GetConvexHull(); | |
Assert.That(ch, Is.Not.Null); | |
TestContext.WriteLine($"ConvexHull ({Path.GetFileName(filePath)}):"); | |
TestContext.WriteLine("Geometry type: {0}", ch.GeometryType); | |
TestContext.WriteLine("Number of points: {0}", ch.NumPoints); | |
if (ch is Polygon p) | |
{ | |
var ring = (LinearRing) p.ExteriorRing; | |
TestContext.WriteLine("Exterior ring orientation: {0}", ring.IsCCW ? "counter-clockwise" : "clockwise"); | |
} | |
var wktWriter = new WKTWriter {Formatted = true, MaxCoordinatesPerLine = 3, OutputOrdinates = Ordinates.XY}; | |
TestContext.WriteLine("WKT:\n{0}", wktWriter.WriteFormatted(ch)); | |
var simplifyOp = new TopologyPreservingSimplifier(ch) { DistanceTolerance = 1E-6 }; | |
var ch1 = simplifyOp.GetResultGeometry(); | |
TestContext.WriteLine("\nSimplified ConvexHull:"); | |
TestContext.WriteLine("Geometry type: {0}", ch1.GeometryType); | |
TestContext.WriteLine("Number of points: {0}", ch1.NumPoints); | |
if (ch1 is Polygon p1) | |
{ | |
var ring = (LinearRing)p1.ExteriorRing; | |
TestContext.WriteLine("Exterior ring orientation: {0}", ring.IsCCW ? "counter-clockwise" : "clockwise"); | |
} | |
TestContext.WriteLine("WKT:\n{0}", wktWriter.WriteFormatted(ch1)); | |
} | |
private IReadOnlyList<Coordinate> ReadFile(string filePath) | |
{ | |
if (!System.IO.File.Exists(filePath)) | |
throw new InconclusiveException($"File not present: '{filePath}'"); | |
using var sr = new StreamReader(File.OpenRead(filePath)); | |
var res = new List<Coordinate>(); | |
while (!sr.EndOfStream) | |
{ | |
string line = sr.ReadLine(); | |
if (string.IsNullOrWhiteSpace(line)) | |
continue; | |
string[] parts = line.Split(","); | |
if (parts.Length < 2) | |
throw new InvalidDataException(); | |
res.Add(new Coordinate(double.Parse(parts[0], NumberFormatInfo.InvariantInfo), | |
double.Parse(parts[1], NumberFormatInfo.InvariantInfo))); | |
} | |
return res; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Globalization; | |
using System.IO; | |
using NetTopologySuite.Algorithm; | |
using NetTopologySuite.Geometries; | |
using NetTopologySuite.IO; | |
using NetTopologySuite.Simplify; | |
using NUnit.Framework; | |
namespace NetTopologySuite.Samples.Geometries | |
{ | |
public class convexhullexample | |
{ | |
public void TestConvexHull1() | |
{ | |
var rdr = new WKTReader(); | |
var g = rdr.Read("Wkt"); | |
var pts = g.Coordinates; | |
var ch = new Algorithm.ConvexHull(pts, g.Factory); | |
var ch1 = g.ConvexHull(); | |
Assert.AreEqual(ch.GetConvexHull(), ch1); | |
pts = ch1.Coordinates; | |
Assert.IsTrue(Orientation.IsCCW(ch1.Coordinates)); | |
var ch3 = TopologyPreservingSimplifier.Simplify(ch1, 1e-6); | |
TestContext.WriteLine(ch1.NumPoints); | |
TestContext.WriteLine(ch1.AsText()); | |
TestContext.WriteLine(); | |
//TestContext.WriteLine(ch2.AsText()); | |
//TestContext.WriteLine(); | |
TestContext.WriteLine(ch3.AsText()); | |
} | |
[TestCase(@"D:\obe\Downloads\border.txt")] | |
[TestCase(@"D:\obe\Downloads\s22a.xyz")] | |
public void TestFiles(string filePath) | |
{ | |
var pts = ReadFile(filePath); | |
var convexHullOp = new ConvexHull(pts, GeometryFactory.Default); | |
var ch = convexHullOp.GetConvexHull(); | |
Assert.That(ch, Is.Not.Null); | |
TestContext.WriteLine($"ConvexHull ({Path.GetFileName(filePath)}):"); | |
TestContext.WriteLine("Geometry type: {0}", ch.GeometryType); | |
TestContext.WriteLine("Number of points: {0}", ch.NumPoints); | |
if (ch is Polygon p) | |
{ | |
var ring = (LinearRing) p.ExteriorRing; | |
TestContext.WriteLine("Exterior ring orientation: {0}", ring.IsCCW ? "counter-clockwise" : "clockwise"); | |
} | |
var wktWriter = new WKTWriter {Formatted = true, MaxCoordinatesPerLine = 3, OutputOrdinates = Ordinates.XY}; | |
TestContext.WriteLine("WKT:\n{0}", wktWriter.WriteFormatted(ch)); | |
var simplifyOp = new TopologyPreservingSimplifier(ch) { DistanceTolerance = 1E-6 }; | |
var ch1 = simplifyOp.GetResultGeometry(); | |
TestContext.WriteLine("\nSimplified ConvexHull:"); | |
TestContext.WriteLine("Geometry type: {0}", ch1.GeometryType); | |
TestContext.WriteLine("Number of points: {0}", ch1.NumPoints); | |
if (ch1 is Polygon p1) | |
{ | |
var ring = (LinearRing)p1.ExteriorRing; | |
TestContext.WriteLine("Exterior ring orientation: {0}", ring.IsCCW ? "counter-clockwise" : "clockwise"); | |
} | |
TestContext.WriteLine("WKT:\n{0}", wktWriter.WriteFormatted(ch1)); | |
} | |
private IReadOnlyList<Coordinate> ReadFile(string filePath) | |
{ | |
if (!System.IO.File.Exists(filePath)) | |
throw new InconclusiveException($"File not present: '{filePath}'"); | |
using var sr = new StreamReader(File.OpenRead(filePath)); | |
var res = new List<Coordinate>(); | |
while (!sr.EndOfStream) | |
{ | |
string line = sr.ReadLine(); | |
if (string.IsNullOrWhiteSpace(line)) | |
continue; | |
string[] parts = line.Split(","); | |
if (parts.Length < 2) | |
throw new InvalidDataException(); | |
res.Add(new Coordinate(double.Parse(parts[0], NumberFormatInfo.InvariantInfo), | |
double.Parse(parts[1], NumberFormatInfo.InvariantInfo))); | |
} | |
return res; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment