diff --git a/src/triangulator.tests/CityGmlTests.cs b/src/triangulator.tests/CityGmlTests.cs index 9328c85..3e9d024 100644 --- a/src/triangulator.tests/CityGmlTests.cs +++ b/src/triangulator.tests/CityGmlTests.cs @@ -25,8 +25,7 @@ public void FirstTestCityGml() var featureGml = buildingLayer.GetNextFeature(); var geometry = featureGml.GetGeometryRef(); - var wkt = string.Empty; - geometry.ExportToWkt(out wkt); + geometry.ExportToWkt(out string wkt); Assert.IsTrue(wkt.Contains("MULTILINESTRING")); var multilinestring = (MultiLineString)Wkx.Geometry.Deserialize(wkt); diff --git a/src/triangulator.tests/CityJsonTests.cs b/src/triangulator.tests/CityJsonTests.cs index 1249d68..fa6d0b6 100644 --- a/src/triangulator.tests/CityJsonTests.cs +++ b/src/triangulator.tests/CityJsonTests.cs @@ -16,62 +16,62 @@ public void FirstCityJsonParsingTest() var res = JsonConvert.DeserializeObject(json); - Assert.IsTrue(res.type == "CityJSON"); - Assert.IsTrue(res.version == "1.0"); - Assert.IsTrue(res.vertices.Length== 255); + Assert.IsTrue(res.Type == "CityJSON"); + Assert.IsTrue(res.Version == "1.0"); + Assert.IsTrue(res.Vertices.Length== 255); Assert.IsTrue(res.CityObjects.Count == 2); var firstBuilding = res.CityObjects.First(); var building = firstBuilding.Value; - Assert.IsTrue(building.type == "Building"); - Assert.IsTrue(building.geometry.Length == 1); - Assert.IsTrue(building.geometry.First().type == "MultiSurface"); - Assert.IsTrue(building.geometry.First().boundaries.Length == 110); - Assert.IsTrue(building.geometry.First().boundaries[0].Length == 1); - Assert.IsTrue(building.geometry.First().boundaries[0][0].Length == 3); - Assert.IsTrue(building.geometry.First().boundaries[0][0][0] == 198); - Assert.IsTrue(building.geometry.First().boundaries[0][0][1] == 199); - Assert.IsTrue(building.geometry.First().boundaries[0][0][2] == 200); + Assert.IsTrue(building.Type == "Building"); + Assert.IsTrue(building.Geometry.Length == 1); + Assert.IsTrue(building.Geometry.First().Type == "MultiSurface"); + Assert.IsTrue(building.Geometry.First().Boundaries.Length == 110); + Assert.IsTrue(building.Geometry.First().Boundaries[0].Length == 1); + Assert.IsTrue(building.Geometry.First().Boundaries[0][0].Length == 3); + Assert.IsTrue(building.Geometry.First().Boundaries[0][0][0] == 198); + Assert.IsTrue(building.Geometry.First().Boundaries[0][0][1] == 199); + Assert.IsTrue(building.Geometry.First().Boundaries[0][0][2] == 200); } } public class CityJSON { - public string type { get; set; } - public string version { get; set; } - public Metadata metadata { get; set; } + public string Type { get; set; } + public string Version { get; set; } + public Metadata Metadata { get; set; } public Dictionary CityObjects { get; set; } - public float[][] vertices { get; set; } + public float[][] Vertices { get; set; } } public class Metadata { - public float[] geographicalExtent { get; set; } + public float[] GeographicalExtent { get; set; } } public class Building { - public Geometry[] geometry { get; set; } - public string type { get; set; } + public Geometry[] Geometry { get; set; } + public string Type { get; set; } } public class Geometry { - public int[][][] boundaries { get; set; } - public int lod { get; set; } - public Semantics semantics { get; set; } - public Texture texture { get; set; } - public string type { get; set; } + public int[][][] Boundaries { get; set; } + public int Lod { get; set; } + public Semantics Semantics { get; set; } + public Texture Texture { get; set; } + public string Type { get; set; } } public class Semantics { - public Surface[] surfaces { get; set; } - public int[] values { get; set; } + public Surface[] Surfaces { get; set; } + public int[] Values { get; set; } } public class Surface { - public string type { get; set; } + public string Type { get; set; } } public class Texture @@ -81,6 +81,6 @@ public class Texture public class RhinoTexturing { - public int[][][] values { get; set; } + public int[][][] Values { get; set; } } } \ No newline at end of file diff --git a/src/triangulator.tests/GeometryExtensionsTests.cs b/src/triangulator.tests/GeometryExtensionsTests.cs index debaea0..6ec6797 100644 --- a/src/triangulator.tests/GeometryExtensionsTests.cs +++ b/src/triangulator.tests/GeometryExtensionsTests.cs @@ -23,6 +23,27 @@ public void MultipolygonZToPolyhedralSurfaceTest() var polyhedralsurface = multipolygon.ToPolyhedralSurface(); Assert.IsTrue(polyhedralsurface != null); Assert.IsTrue(polyhedralsurface.Geometries.Count == multipolygon.Geometries.Count); + Assert.IsTrue(Triangulator.Triangulate(polyhedralsurface).Geometries.Count == polyhedralsurface.Geometries.Count); + } + + [Test] + public void AsTextTest() + { + var expected = "MULTIPOLYGON(((43603.770435546874 361514.6418164063 1.2790000438690186,43603.50600073242 361509.7590039063 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186,43599.11101171875 361520.242890625 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186)),((43604.058002685546 361519.95211914065 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186)),((43598.60899755859 361510.0461132813 6.778176307678223,43598.85057470703 361514.95309570315 7.397906303405762,43598.85057470703 361514.95309570315 1.2790000438690186,43598.60899755859 361510.0461132813 6.778176307678223)),((43598.60899755859 361510.0461132813 1.2790000438690186,43598.60899755859 361510.0461132813 6.778176307678223,43598.85057470703 361514.95309570315 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186)),((43603.770435546874 361514.6418164063 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549,43603.50600073242 361509.7590039063 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186)),((43603.770435546874 361514.6418164063 1.2790000438690186,43603.770435546874 361514.6418164063 7.381703853607178,43603.50600073242 361509.7590039063 6.764927387237549,43603.770435546874 361514.6418164063 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43599.11101171875 361520.242890625 6.854876518249512,43599.11101171875 361520.242890625 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43598.85057470703 361514.95309570315 7.397906303405762,43599.11101171875 361520.242890625 6.854876518249512,43598.85057470703 361514.95309570315 1.2790000438690186)),((43604.058002685546 361519.95211914065 6.836284160614014,43603.770435546874 361514.6418164063 7.381703853607178,43603.770435546874 361514.6418164063 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014)),((43604.058002685546 361519.95211914065 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014,43603.770435546874 361514.6418164063 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014,43604.058002685546 361519.95211914065 1.2790000438690186,43599.11101171875 361520.242890625 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43599.11101171875 361520.242890625 6.854876518249512,43604.058002685546 361519.95211914065 6.836284160614014,43599.11101171875 361520.242890625 1.2790000438690186)),((43603.50600073242 361509.7590039063 6.764927387237549,43598.60899755859 361510.0461132813 6.778176307678223,43598.60899755859 361510.0461132813 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549)),((43603.50600073242 361509.7590039063 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549,43598.60899755859 361510.0461132813 1.2790000438690186,43603.50600073242 361509.7590039063 1.2790000438690186)),((43603.770435546874 361514.6418164063 7.381703853607178,43598.60899755859 361510.0461132813 6.778176307678223,43603.50600073242 361509.7590039063 6.764927387237549,43603.770435546874 361514.6418164063 7.381703853607178)),((43603.770435546874 361514.6418164063 7.381703853607178,43598.85057470703 361514.95309570315 7.397906303405762,43598.60899755859 361510.0461132813 6.778176307678223,43603.770435546874 361514.6418164063 7.381703853607178)),((43604.058002685546 361519.95211914065 6.836284160614014,43598.85057470703 361514.95309570315 7.397906303405762,43603.770435546874 361514.6418164063 7.381703853607178,43604.058002685546 361519.95211914065 6.836284160614014)),((43604.058002685546 361519.95211914065 6.836284160614014,43599.11101171875 361520.242890625 6.854876518249512,43598.85057470703 361514.95309570315 7.397906303405762,43604.058002685546 361519.95211914065 6.836284160614014)))"; + var multipolygon = (MultiPolygon)Wkx.Geometry.Deserialize(expected); + var actual = multipolygon.AsText(); + Assert.IsTrue(actual == expected); + } + + [Test] + public void AsBinaryTest() + { + var expected = "POLYHEDRALSURFACE Z (((43603.770435546874 361514.6418164063 1.2790000438690186,43603.50600073242 361509.7590039063 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186,43599.11101171875 361520.242890625 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186)),((43604.058002685546 361519.95211914065 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186)),((43598.60899755859 361510.0461132813 6.778176307678223,43598.85057470703 361514.95309570315 7.397906303405762,43598.85057470703 361514.95309570315 1.2790000438690186,43598.60899755859 361510.0461132813 6.778176307678223)),((43598.60899755859 361510.0461132813 1.2790000438690186,43598.60899755859 361510.0461132813 6.778176307678223,43598.85057470703 361514.95309570315 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186)),((43603.770435546874 361514.6418164063 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549,43603.50600073242 361509.7590039063 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186)),((43603.770435546874 361514.6418164063 1.2790000438690186,43603.770435546874 361514.6418164063 7.381703853607178,43603.50600073242 361509.7590039063 6.764927387237549,43603.770435546874 361514.6418164063 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43599.11101171875 361520.242890625 6.854876518249512,43599.11101171875 361520.242890625 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43598.85057470703 361514.95309570315 7.397906303405762,43599.11101171875 361520.242890625 6.854876518249512,43598.85057470703 361514.95309570315 1.2790000438690186)),((43604.058002685546 361519.95211914065 6.836284160614014,43603.770435546874 361514.6418164063 7.381703853607178,43603.770435546874 361514.6418164063 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014)),((43604.058002685546 361519.95211914065 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014,43603.770435546874 361514.6418164063 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014,43604.058002685546 361519.95211914065 1.2790000438690186,43599.11101171875 361520.242890625 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43599.11101171875 361520.242890625 6.854876518249512,43604.058002685546 361519.95211914065 6.836284160614014,43599.11101171875 361520.242890625 1.2790000438690186)),((43603.50600073242 361509.7590039063 6.764927387237549,43598.60899755859 361510.0461132813 6.778176307678223,43598.60899755859 361510.0461132813 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549)),((43603.50600073242 361509.7590039063 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549,43598.60899755859 361510.0461132813 1.2790000438690186,43603.50600073242 361509.7590039063 1.2790000438690186)),((43603.770435546874 361514.6418164063 7.381703853607178,43598.60899755859 361510.0461132813 6.778176307678223,43603.50600073242 361509.7590039063 6.764927387237549,43603.770435546874 361514.6418164063 7.381703853607178)),((43603.770435546874 361514.6418164063 7.381703853607178,43598.85057470703 361514.95309570315 7.397906303405762,43598.60899755859 361510.0461132813 6.778176307678223,43603.770435546874 361514.6418164063 7.381703853607178)),((43604.058002685546 361519.95211914065 6.836284160614014,43598.85057470703 361514.95309570315 7.397906303405762,43603.770435546874 361514.6418164063 7.381703853607178,43604.058002685546 361519.95211914065 6.836284160614014)),((43604.058002685546 361519.95211914065 6.836284160614014,43599.11101171875 361520.242890625 6.854876518249512,43598.85057470703 361514.95309570315 7.397906303405762,43604.058002685546 361519.95211914065 6.836284160614014)))"; + var polyhedralsurface = (PolyhedralSurface)Wkx.Geometry.Deserialize(expected); + var wkb = polyhedralsurface.AsBinary(); + var polyhedralsurface_after = (PolyhedralSurface)Wkx.Geometry.Deserialize(wkb); + var polyhedralsurface_after_wkt = polyhedralsurface_after.AsText(); + Assert.IsTrue(expected == polyhedralsurface_after_wkt); } } } diff --git a/src/triangulator.tests/triangulator.tests.csproj b/src/triangulator.tests/triangulator.tests.csproj index 6160b8b..8d7ea13 100644 --- a/src/triangulator.tests/triangulator.tests.csproj +++ b/src/triangulator.tests/triangulator.tests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 false @@ -12,7 +12,7 @@ - + diff --git a/src/triangulator/GeometryExtensions.cs b/src/triangulator/GeometryExtensions.cs index 20d264d..49e7af3 100644 --- a/src/triangulator/GeometryExtensions.cs +++ b/src/triangulator/GeometryExtensions.cs @@ -1,9 +1,26 @@ -using Wkx; +using System.IO; +using System.Text; +using Wkx; namespace Triangulate { public static class GeometryExtensions { + public static string AsText(this Geometry geometry) + { + var stream = new MemoryStream(); + geometry.Serialize(stream); + var wkt = Encoding.UTF8.GetString(stream.ToArray()); + return wkt; + } + + public static byte[] AsBinary(this Geometry geometry) + { + var stream = new MemoryStream(); + geometry.Serialize(stream); + return stream.ToArray(); + } + public static PolyhedralSurface ToPolyhedralSurface(this MultiPolygon multipolygon) { var polyhedralsurface = new PolyhedralSurface @@ -43,7 +60,7 @@ private static Polygon GetPolygon(LineString linestring) }; polygon.ExteriorRing.Points.AddRange(linestring.Points); - polygon.ExteriorRing.Points.Add(linestring.Points[linestring.Points.Count - 1]); + polygon.ExteriorRing.Points.Add(linestring.Points[^1]); return polygon; } diff --git a/src/triangulator/Triangulator.cs b/src/triangulator/Triangulator.cs index cf12336..78ca534 100644 --- a/src/triangulator/Triangulator.cs +++ b/src/triangulator/Triangulator.cs @@ -1,7 +1,6 @@ using EarcutNet; using System; using System.Collections.Generic; -using System.IO; using System.Numerics; using Wkx; @@ -28,9 +27,7 @@ public static byte[] Triangulate(byte[] wkb) { var polyhedral = (PolyhedralSurface)Geometry.Deserialize(wkb); var triangulatedPolyhedral = Triangulate(polyhedral); - var stream = new MemoryStream(); - triangulatedPolyhedral.Serialize(stream); - return stream.ToArray(); + return triangulatedPolyhedral.AsBinary(); } public static List Triangulate(Polygon inputpolygon)