public void CanReadWriteSinglePartPolylineXy() { var points = new WKSPointZ[4]; points[0] = new WKSPointZ { X = 2600000, Y = 1200000, Z = double.NaN }; points[1] = new WKSPointZ { X = 2600030, Y = 1200020, Z = double.NaN }; points[2] = new WKSPointZ { X = 2600020, Y = 1200030, Z = double.NaN }; points[3] = new WKSPointZ { X = 2600040, Y = 1200040, Z = double.NaN }; IPolyline polyline = GeometryFactory.CreatePolyline(points, null); GeometryUtils.MakeNonZAware(polyline); GeometryUtils.Simplify(polyline); WkbGeometryWriter writer = new WkbGeometryWriter(); byte[] wkb = writer.WritePolyline(polyline); // ArcObjects byte[] arcObjectsWkb = GeometryUtils.ToWkb(polyline); Assert.AreEqual(wkb, arcObjectsWkb); // Wkx byte[] wkx = ToChristianSchwarzWkb(ToWkxLineString(points, Ordinates.Xy)); Assert.AreEqual(wkx, wkb); // Bonus test: Geom WkbGeomWriter geomWriter = new WkbGeomWriter(); MultiPolycurve multiPlycurve = GeometryConversionUtils.CreateMultiPolycurve(polyline); byte[] wkbGeom = geomWriter.WriteMultiLinestring(multiPlycurve, Ordinates.Xy); Assert.AreEqual(wkb, wkbGeom); WkbGeometryReader reader = new WkbGeometryReader(); IPolyline restored = reader.ReadPolyline(new MemoryStream(wkb)); Assert.IsTrue(GeometryUtils.AreEqual(polyline, restored)); // Geom WkbGeomReader geomReader = new WkbGeomReader(); Assert.IsTrue( multiPlycurve.Equals(geomReader.ReadMultiPolycurve(new MemoryStream(wkbGeom)))); }
public void CanCutAlongUsingZSources() { GetOverlappingPolygons(out GdbFeature sourceFeature, out GdbFeature targetFeature); GeometryUtils.MakeZAware(sourceFeature.Shape); GeometryUtils.MakeZAware(targetFeature.Shape); var normal = new Vector(new[] { 0.5, 0.5, 2 }); Pnt3D planePoint = new Pnt3D(2600000, 1200000, 600); Plane3D sourcePlane = new Plane3D(normal, planePoint); ChangeAlongZUtils.AssignZ((IPointCollection)sourceFeature.Shape, sourcePlane); GeometryUtils.ApplyConstantZ(targetFeature.Shape, 500); CalculateCutLinesRequest calculationRequest = CreateCalculateCutLinesRequest(sourceFeature, targetFeature); CalculateCutLinesResponse calculateResponse = ChangeAlongServiceUtils.CalculateCutLines(calculationRequest, null); Assert.AreEqual(ReshapeAlongCurveUsability.CanReshape, (ReshapeAlongCurveUsability)calculateResponse.ReshapeLinesUsability); AssertReshapeLineCount(calculateResponse.CutLines, 1, 1); IPolyline reshapeLine = (IPolyline)ProtobufGeometryUtils.FromShapeMsg(calculateResponse.CutLines[0].Path); Assert.NotNull(reshapeLine); Assert.AreEqual(1000, (reshapeLine).Length); Linestring cutLinestring = GeometryConversionUtils.GetLinestring(GeometryUtils.GetPaths(reshapeLine).Single()); Pnt3D midPoint = (Pnt3D)cutLinestring.GetPointAlong(0.5, true); List <Pnt3D> points = GeometryConversionUtils.GetPntList(reshapeLine); Assert.IsTrue(points.All(p => MathUtils.AreEqual(p.Z, 500.0))); // // Cutting - TargetZ // var applyRequest = new ApplyCutLinesRequest(); applyRequest.CutLines.Add(calculateResponse.CutLines[0]); applyRequest.CalculationRequest = calculationRequest; applyRequest.InsertVerticesInTarget = false; applyRequest.ChangedVerticesZSource = (int)ChangeAlongZSource.Target; ApplyCutLinesResponse applyResponse = ChangeAlongServiceUtils.ApplyCutLines(applyRequest, null); Assert.AreEqual(2, applyResponse.ResultFeatures.Count); List <IGeometry> cutGeometries = applyResponse.ResultFeatures.Select(GetShape).ToList(); Assert.AreEqual(1000 * 1000, cutGeometries.Sum(g => ((IArea)g).Area)); List <MultiPolycurve> multiPolycurves = cutGeometries .Select(g => GeometryConversionUtils.CreateMultiPolycurve((IPolycurve)g)) .ToList(); foreach (MultiPolycurve multiPolycurve in multiPolycurves) { Line3D segment = multiPolycurve.FindSegments(midPoint, 0.001).First().Value; Assert.AreEqual(500, segment.StartPoint.Z); Assert.AreEqual(500, segment.EndPoint.Z); } // // Cutting - Interpolate // applyRequest = new ApplyCutLinesRequest(); applyRequest.CutLines.Add(calculateResponse.CutLines[0]); applyRequest.CalculationRequest = calculationRequest; applyRequest.InsertVerticesInTarget = false; applyRequest.ChangedVerticesZSource = (int)ChangeAlongZSource.InterpolatedSource; applyResponse = ChangeAlongServiceUtils.ApplyCutLines(applyRequest, null); Assert.AreEqual(2, applyResponse.ResultFeatures.Count); cutGeometries = applyResponse.ResultFeatures.Select(GetShape).ToList(); Assert.AreEqual(1000 * 1000, cutGeometries.Sum(g => ((IArea)g).Area)); multiPolycurves = cutGeometries .Select(g => GeometryConversionUtils.CreateMultiPolycurve((IPolycurve)g)) .ToList(); foreach (MultiPolycurve multiPolycurve in multiPolycurves) { List <Line3D> segments = multiPolycurve.FindSegments(midPoint, 0.001) .Select(kvp => kvp.Value).ToList(); Assert.AreEqual(2, segments.Count); // Check if they are properly ordered and same length Assert.AreEqual(segments[0].EndPoint, segments[1].StartPoint); Assert.AreEqual(segments[0].Length2D, segments[1].Length2D); // Check if they are interpolated double average = (segments[0].StartPoint.Z + segments[1].EndPoint.Z) / 2; Assert.AreEqual(average, segments[0].EndPoint.Z); } // // Cutting - Plane // applyRequest = new ApplyCutLinesRequest(); applyRequest.CutLines.Add(calculateResponse.CutLines[0]); applyRequest.CalculationRequest = calculationRequest; applyRequest.InsertVerticesInTarget = false; applyRequest.ChangedVerticesZSource = (int)ChangeAlongZSource.SourcePlane; applyResponse = ChangeAlongServiceUtils.ApplyCutLines(applyRequest, null); Assert.AreEqual(2, applyResponse.ResultFeatures.Count); cutGeometries = applyResponse.ResultFeatures.Select(GetShape).ToList(); Assert.AreEqual(1000 * 1000, cutGeometries.Sum(g => ((IArea)g).Area)); multiPolycurves = cutGeometries .Select(g => GeometryConversionUtils.CreateMultiPolycurve((IPolycurve)g)) .ToList(); foreach (MultiPolycurve multiPolycurve in multiPolycurves) { bool?coplanar = ChangeAlongZUtils.AreCoplanar( multiPolycurve.GetPoints().ToList(), sourcePlane, 0.01, out double _, out string _); Assert.IsTrue(coplanar); } }
private static IList <IGeometry> TryCutXY( IPolygon inputPolygon, IPolyline cutPolyline, ChangeAlongZSource zSource) { // TODO: // In order to avoid the arbitrary grouping of multipart polygons, try to apply left/right logic // provided by GeomTopoOpUtils double tolerance = GeometryUtils.GetXyTolerance(inputPolygon); double zTolerance = GeometryUtils.GetZTolerance(inputPolygon); MultiPolycurve inputMultipoly = GeometryConversionUtils.CreateMultiPolycurve(inputPolygon); var cutLine = GeometryFactory.Clone(cutPolyline); if (GeometryUtils.IsZAware(cutLine) && zSource != ChangeAlongZSource.Target) { ((IZAware)cutLine).DropZs(); } Plane3D plane = null; if (zSource == ChangeAlongZSource.SourcePlane) { plane = ChangeAlongZUtils.GetSourcePlane( inputMultipoly.GetPoints().ToList(), zTolerance); } GeometryUtils.Simplify(cutLine, true, true); MultiPolycurve cutLinestrings = GeometryConversionUtils.CreateMultiPolycurve(cutLine); bool isMultipart = GeometryUtils.GetExteriorRingCount(inputPolygon) > 1; IList <MultiLinestring> resultGeoms = GeomTopoOpUtils.CutXY(inputMultipoly, cutLinestrings, tolerance, !isMultipart); var result = new List <IGeometry>(); foreach (MultiLinestring resultPoly in resultGeoms) { if (plane != null) { resultPoly.AssignUndefinedZs(plane); } else { resultPoly.InterpolateUndefinedZs(); } result.Add(GeometryConversionUtils.CreatePolygon(inputPolygon, resultPoly.GetLinestrings())); } Marshal.ReleaseComObject(cutLine); return(result.Count == 0 ? null : result); }
public void CanReadWriteMultipartPolygonXyz() { ISpatialReference sr = SpatialReferenceUtils.CreateSpatialReference( WellKnownHorizontalCS.LV95, WellKnownVerticalCS.LHN95); IPolygon outerRing = GeometryFactory.CreatePolygon(2600000, 1200000, 2601000, 1201000, 432); outerRing.SpatialReference = sr; IPolygon innerRing = GeometryFactory.CreatePolygon(2600100, 1200100, 2600200, 1200200, 421); innerRing.SpatialReference = sr; IPolygon polyWithHole = (IPolygon)IntersectionUtils.Difference(outerRing, innerRing); IPolygon poly2 = GeometryFactory.CreatePolygon(2610000, 1200000, 2611000, 1201000, 543); poly2.SpatialReference = sr; IPolygon multiPolygon = (IPolygon)GeometryUtils.Union(polyWithHole, poly2); Assert.IsTrue(GeometryUtils.IsZAware(multiPolygon)); GeometryUtils.Simplify(multiPolygon); WkbGeometryWriter writer = new WkbGeometryWriter(); byte[] wkb = writer.WritePolygon(multiPolygon); // Wkx var ordinates = Ordinates.Xyz; IList <IGeometry> parts = GeometryUtils.GetParts(multiPolygon).ToList(); IGeometry exteriorRing = parts[0]; IGeometry interiorRing = parts[1]; IGeometry secondExteriorRing = parts[2]; LinearRing wkxOuterRing = ToWkxLinearRing(exteriorRing, ordinates); LinearRing wkxInnerRing = ToWkxLinearRing(interiorRing, ordinates); Polygon wkxPolygon1 = ToWkxPolygon(wkxOuterRing, new[] { wkxInnerRing }); Polygon wkxPolygon2 = ToWkxPolygon(ToWkxLinearRing(secondExteriorRing, ordinates)); MultiPolygon wkxMultiPolygon = new MultiPolygon(new[] { wkxPolygon1, wkxPolygon2 }); byte[] wkx = ToChristianSchwarzWkb(wkxMultiPolygon); Assert.AreEqual(wkx, wkb); // Bonus test: Geom WkbGeomWriter geomWriter = new WkbGeomWriter(); MultiPolycurve multiPly = GeometryConversionUtils.CreateMultiPolycurve(multiPolygon); byte[] wkbGeom = geomWriter.WriteMultipolygon(multiPly, ordinates); Assert.AreEqual(wkb, wkbGeom); WkbGeometryReader reader = new WkbGeometryReader(); IPolygon restored = reader.ReadPolygon(new MemoryStream(wkb)); Assert.IsTrue(GeometryUtils.AreEqual(multiPolygon, restored)); // Geom: WkbGeomReader geomReader = new WkbGeomReader(); IList <RingGroup> readMultiPolygon = geomReader.ReadMultiPolygon(new MemoryStream(wkbGeom)); var readMultiPolycurve = new MultiPolycurve( readMultiPolygon.SelectMany(g => g.GetLinestrings())); Assert.IsTrue(multiPly.Equals(readMultiPolycurve)); // As multipatch IMultiPatch multipatch = GeometryFactory.CreateMultiPatch(multiPolygon); wkb = writer.WriteMultipatch(multipatch); IMultiPatch readMultipatch = (IMultiPatch)reader.ReadGeometry(new MemoryStream(wkb)); // TODO: Implement AreEqual for multipatch that is more permissive regarding First/Outer ring type AssertEqual(multipatch, readMultipatch); }
public void CanReadWriteMultiPartPolylineXyz() { // The spatial reference is important to avoid small differences in // coordinates (snap to spatial reference!) ISpatialReference sr = SpatialReferenceUtils.CreateSpatialReference( WellKnownHorizontalCS.LV95, WellKnownVerticalCS.LHN95); var points1 = new WKSPointZ[4]; points1[0] = new WKSPointZ { X = 2600000, Y = 1200000, Z = 456 }; points1[1] = new WKSPointZ { X = 2600030, Y = 1200020, Z = 457 }; points1[2] = new WKSPointZ { X = 2600020, Y = 1200030, Z = 459 }; points1[3] = new WKSPointZ { X = 2600040, Y = 1200040, Z = 416 }; IPolyline polyline1 = GeometryFactory.CreatePolyline(points1, sr); var points2 = new WKSPointZ[4]; points2[0] = new WKSPointZ { X = 2610000, Y = 1200000, Z = 656 }; points2[1] = new WKSPointZ { X = 2610030, Y = 1200020, Z = 657 }; points2[2] = new WKSPointZ { X = 2610020, Y = 1200030, Z = 659 }; points2[3] = new WKSPointZ { X = 2610040, Y = 1200040, Z = 616 }; IPolyline polyline2 = GeometryFactory.CreatePolyline(points2, sr); IPolyline polyline = (IPolyline)GeometryUtils.Union(polyline1, polyline2); GeometryUtils.Simplify(polyline); WkbGeometryWriter writer = new WkbGeometryWriter(); byte[] wkb = writer.WritePolyline(polyline); // Wkx var ordinates = Ordinates.Xyz; MultiLineString multiLineString = new MultiLineString( new List <LineString> { ToWkxLineString(points1, ordinates), ToWkxLineString(points2, ordinates) }); byte[] wkx = ToChristianSchwarzWkb(multiLineString); Assert.AreEqual(wkx, wkb); // Bonus test: Geom WkbGeomWriter geomWriter = new WkbGeomWriter(); MultiPolycurve multiPlycurve = GeometryConversionUtils.CreateMultiPolycurve(polyline); byte[] wkbGeom = geomWriter.WriteMultiLinestring(multiPlycurve, ordinates); Assert.AreEqual(wkb, wkbGeom); WkbGeometryReader reader = new WkbGeometryReader(); IPolyline restored = reader.ReadPolyline(new MemoryStream(wkb)); Assert.IsTrue(GeometryUtils.AreEqual(polyline, restored)); // Geom: WkbGeomReader geomReader = new WkbGeomReader(); Assert.IsTrue( multiPlycurve.Equals(geomReader.ReadMultiPolycurve(new MemoryStream(wkbGeom)))); }