/// <summary> /// Method splitted to permit testing without reading DHI CrossSection /// </summary> /// <param name="P1"></param> /// <param name="P2"></param> /// <param name="Chainage"></param> public void SetPoints(IXYPoint P1, IXYPoint P2, double Chainage1, double Chainage2, double Chainage) { double dx = P2.X - P1.X; double dy = P2.Y - P1.Y; double dchainage = (Chainage - Chainage1)/( Chainage2 - Chainage1); MidStreamLocation = new XYPoint(P1.X + dchainage * dx, P1.Y + dchainage * dy); double lenght = Math.Pow(Math.Pow(dx,2)+ Math.Pow(dy,2),0.5); UnityVector = new XYPoint(dx / lenght, dy / lenght); //Now build the line where the cross section is located. if (_cs != null) { Line = new XYPolyline(); //MidPoint is set to where Marker 2 is placed double xOffset = _xsec.LowestPoint.X; for (int i = 0; i < _xsec.Points.Count(); i++) { Line.Points.Add(new XYPoint(MidStreamLocation.X - UnityVector.Y * (_xsec.Points[i].X - xOffset), MidStreamLocation.Y + UnityVector.X * (_xsec.Points[i].X - xOffset))); } } }
/// <summary> /// Constructor. Copies the contents of the xyPolyline parameter. /// </summary> /// <param name="xyPolyline">Polyline to copy.</param> /// <returns>None</returns> public XYPolyline(XYPolyline xyPolyline) : this() { foreach (XYPoint xypoint in xyPolyline.Points) { _points.Add(new XYPoint(xypoint.X, xypoint.Y)); } }
public MixingStream(string name, XYPolyline length,double width, double depth, int mixfactor):base(name, length,width,depth) { for (int i = 0; i < mixfactor; i++) { _wbs.Add(new Stream(name + i.ToString(),Length/mixfactor,Width,Depth)); } }
/// <summary> /// Constructor. Copies the contents of the xyPolyline parameter. /// </summary> /// <param name="xyPolyline">Polyline to copy.</param> /// <returns>None</returns> public XYPolyline(XYPolyline xyPolyline):this() { foreach (XYPoint xypoint in xyPolyline.Points) { _points.Add(new XYPoint(xypoint.X, xypoint.Y)); } }
public void Equals() { XYPolyline p1 = new XYPolyline(); p1.Points.Add(new XYPoint(0, 3)); p1.Points.Add(new XYPoint(3, 0)); p1.Points.Add(new XYPoint(8, 0)); p1.Points.Add(new XYPoint(8, 2)); p1.Points.Add(new XYPoint(3, 1)); p1.Points.Add(new XYPoint(3, 3)); p1.Points.Add(new XYPoint(8, 3)); p1.Points.Add(new XYPoint(4, 7)); XYPolyline p2 = new XYPolyline(); p2.Points.Add(new XYPoint(0, 3)); p2.Points.Add(new XYPoint(3, 0)); p2.Points.Add(new XYPoint(8, 0)); p2.Points.Add(new XYPoint(8, 2)); p2.Points.Add(new XYPoint(3, 1)); p2.Points.Add(new XYPoint(3, 3)); p2.Points.Add(new XYPoint(8, 3)); p2.Points.Add(new XYPoint(4, 7)); XYPolyline p3 = new XYPolyline(); p3.Points.Add(new XYPoint(0, 3)); p3.Points.Add(new XYPoint(3, 0)); p3.Points.Add(new XYPoint(8, 0)); p3.Points.Add(new XYPoint(8, 2)); p3.Points.Add(new XYPoint(3, 1.1)); p3.Points.Add(new XYPoint(3, 3)); p3.Points.Add(new XYPoint(8, 3)); p3.Points.Add(new XYPoint(4, 7)); XYPolyline p4 = new XYPolyline(); p4.Points.Add(new XYPoint(0, 3)); p4.Points.Add(new XYPoint(3, 0)); p4.Points.Add(new XYPoint(8, 0)); p4.Points.Add(new XYPoint(8, 2)); p4.Points.Add(new XYPoint(3, 1)); p4.Points.Add(new XYPoint(3, 3)); p4.Points.Add(new XYPoint(8, 3)); XYPolygon p5 = new XYPolygon(); p5.Points.Add(new XYPoint(0, 3)); p5.Points.Add(new XYPoint(3, 0)); p5.Points.Add(new XYPoint(8, 0)); p5.Points.Add(new XYPoint(8, 2)); p5.Points.Add(new XYPoint(3, 1.1)); p5.Points.Add(new XYPoint(3, 3)); p5.Points.Add(new XYPoint(8, 3)); p5.Points.Add(new XYPoint(4, 7)); Assert.AreEqual(true, p1.Equals(p1),"Test1"); Assert.AreEqual(true, p1.Equals(p2),"Test2"); Assert.AreEqual(false, p1.Equals(p3),"Test3"); Assert.AreEqual(false, p1.Equals(p4),"Test4"); Assert.AreEqual(false, p1.Equals(p5),"Test5"); }
/// <summary> /// Calculates the length of polyline inside polygon. Lines segments on the edges of /// polygons are included with half their length. /// </summary> /// <param name="polyline">Polyline</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of polyline inside polygon. /// </returns> public static double CalculateLengthOfPolylineInsidePolygon(XYPolyline polyline, XYPolygon polygon) { double lengthInside = 0; int numberOfLineSegments = polyline.Points.Count - 1; for (int i = 0; i < numberOfLineSegments; i++) { XYLine line = new XYLine(polyline.GetLine(i)); lengthInside += CalculateLengthOfLineInsidePolygon(line, polygon); } return(lengthInside); }
public void GetLength() { XYPolyline xyPolyline = new XYPolyline(); xyPolyline.Points.Add(new XYPoint(6,2)); xyPolyline.Points.Add(new XYPoint(2,2)); xyPolyline.Points.Add(new XYPoint(8,2)); xyPolyline.Points.Add(new XYPoint(8,4)); xyPolyline.Points.Add(new XYPoint(5,4)); xyPolyline.Points.Add(new XYPoint(9,7)); Assert.AreEqual((double) 20, xyPolyline.GetLength()); }
public void Equals() { XYLine l1 = new XYLine(0, 3, 3, 0); XYLine l2 = new XYLine(0, 3, 3, 0); XYLine l3 = new XYLine(3, 3, 3, 0); XYPolyline pl1 = new XYPolyline(); pl1.Points.Add(new XYPoint(0, 3)); pl1.Points.Add(new XYPoint(3, 0)); Assert.AreEqual(true, l1.Equals(l1),"Test1"); Assert.AreEqual(true, l1.Equals(l2),"Test2"); Assert.AreEqual(false, l1.Equals(l3),"Test3"); Assert.AreEqual(false, l1.Equals(pl1),"Test4"); }
public void GetLine() { XYPolyline xyPolyline = new XYPolyline(); xyPolyline.Points.Add(new XYPoint(6,2)); xyPolyline.Points.Add(new XYPoint(2,2)); xyPolyline.Points.Add(new XYPoint(8,2)); xyPolyline.Points.Add(new XYPoint(8,4)); xyPolyline.Points.Add(new XYPoint(5,4)); xyPolyline.Points.Add(new XYPoint(9,7)); Assert.AreEqual(new XYLine(6,2,2,2),xyPolyline.GetLine(0)); Assert.AreEqual(new XYLine(2,2,8,2),xyPolyline.GetLine(1)); Assert.AreEqual(new XYLine(8,2,8,4),xyPolyline.GetLine(2)); Assert.AreEqual(new XYLine(8,4,5,4),xyPolyline.GetLine(3)); Assert.AreEqual(new XYLine(5,4,9,7),xyPolyline.GetLine(4)); }
public static List<Stream> CreateBranch(int numberofWbs) { List<Stream> Branch = new List<Stream>(); for (int i = 0; i < numberofWbs; i++) { XYPolyline line = new XYPolyline(); line.Points.Add(new XYPoint(i, i)); line.Points.Add(new XYPoint(i+1, i+1)); Branch.Add(new Stream(i.ToString(),line, 1, 1)); if (i > 0) { Branch[i - 1].AddDownStreamWaterBody(Branch[i]); } } return Branch; }
/// <summary> /// Finds the shortest distance between any line segment of the polyline /// and the point. /// </summary> /// <param name="polyLine">PolyLine.</param> /// <param name="point">Point</param> /// <returns> /// <p>Length of the shortest path between the polyline and the point.</p> /// </returns> public static double CalculatePolylineToPointDistance(XYPolyline polyLine, IXYPoint point) { double dist = 0; int i = 0; while (i < polyLine.Points.Count - 1) { if (i == 0) { dist = CalculateLineToPointDistance(polyLine.GetLine(0), point); } else { dist = Math.Min(dist, CalculateLineToPointDistance(polyLine.GetLine(i), point)); } i++; } return(dist); }
/// <summary> /// Compares the object type and the coordinates of the object and the /// object passed as parameter. /// </summary> /// <returns>True if object type is XYPolygon and the coordinates are /// equal to to the coordinates of the current object. False otherwise.</returns> public override bool Equals(Object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } XYPolyline e = (XYPolygon)obj; if (Points.Count != e.Points.Count) { return(false); } for (int i = 0; i < Points.Count; i++) { if (!((XYPoint)Points[i]).Equals(e.Points[i])) { return(false); } } return(true); }
public static List<IWaterBody> CreateCombo(int númberofWbs, double vol) { List<IWaterBody> wbs = new List<IWaterBody>(); XYPolyline line = new XYPolyline(); line.Points.Add(new XYPoint(0, 0)); line.Points.Add(new XYPoint(1, 1)); wbs.Add(new Stream("0", line, 1, 1)); for (int i = 1; i < númberofWbs; ) { wbs.Add(new Lake(i.ToString(),vol)); wbs[i - 1].AddDownStreamWaterBody(wbs[i]); line = new XYPolyline(); line.Points.Add(new XYPoint(i, i)); line.Points.Add(new XYPoint(i+1, i+1)); wbs.Add(new Stream(i.ToString(), line, 1, 1)); wbs[i].AddDownStreamWaterBody(wbs[i+1]); i = i + 2; } return wbs; }
internal M11Branch(branch BranchFromPFS, SortedDictionary<int, point> Points) { _pfsdata = BranchFromPFS; //Loop the POINTS for (int i=0; i<_pfsdata.points.NumberOfParameters;i++) { M11Point mp =new M11Point(Points[_pfsdata.points.GetValue(i)]); _points.Add(mp); } //Sort by chainage _points.Sort(new Comparison<M11Point>((var1,var2) => var1.Chainage.CompareTo(var2.Chainage))); //Add to polyline Line = new XYPolyline(); foreach (var mp in _points) Line.Points.Add(mp); ID = new BranchID { Branchname = Name, StartChainage = ChainageStart }; CrossSections = new ObservableCollection<CrossSection>(); }
public void SaveToShape(string ShapeFileName) { using (ShapeWriter sw = new ShapeWriter(ShapeFileName)) { DataTable dt = new DataTable(); dt.Columns.Add("LinkID", typeof(string)); dt.Columns.Add("FromNode", typeof(string)); dt.Columns.Add("ToNode", typeof(string)); dt.Columns.Add("SpecifiedLength", typeof(double)); foreach (var b in Links.Values) { GeoRefData grf = new GeoRefData(); var l = new XYPolyline(); l.Points.Add(new XYPoint(b.UpstreamNode.pfsnode.X, b.UpstreamNode.pfsnode.Y)); l.Points.Add(new XYPoint(b.DownstreamNode.pfsnode.X, b.DownstreamNode.pfsnode.Y)); grf.Geometry = l; grf.Data = dt.NewRow(); grf.Data[0] = b.pfslink.LinkID; grf.Data[1] = b.pfslink.FromNode; grf.Data[2] = b.pfslink.ToNode; grf.Data[3] = b.pfslink.SpecifiedLength; sw.Write(grf); } } if (Branches != null && Branches.Count > 0) { using (ShapeWriter sw = new ShapeWriter(Path.Combine(Path.GetDirectoryName(ShapeFileName), Path.GetFileNameWithoutExtension(ShapeFileName)+"_branches.shp"))) { DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); dt.Columns.Add("Length", typeof(double)); foreach (var b in Branches) { var line = new XYPolyline(); line.Points.AddRange(b.Links.Select(p => p.UpstreamNode.Location)); line.Points.Add(b.Links.Last().DownstreamNode.Location); GeoRefData grf = new GeoRefData(); grf.Geometry = line; grf.Data = dt.NewRow(); grf.Data[0] = b.Name; grf.Data[1] = line.GetLength(); sw.Write(grf); } } } }
public Stream(string name, double Length, double Width, double Depth) : base(name) { Line = new XYPolyline(); Line.Points.Add(new XYPoint(0, 0)); Line.Points.Add(new XYPoint(Length, 0)); this.Width = Width; this.Depth = Depth; }
public Stream(string name, XYPolyline Line, double Width, double Depth):base(name) { this.Line = Line; this.Width = Width; this.Depth = Depth; }
public void CalculatePolylineToPointDistance() { XYPolyline polyline = new XYPolyline(); polyline.Points.Add(new XYPoint(0,0)); polyline.Points.Add(new XYPoint(1,1)); polyline.Points.Add(new XYPoint(2,2)); polyline.Points.Add(new XYPoint(4,2)); polyline.Points.Add(new XYPoint(6,0)); Assert.AreEqual(Math.Sqrt(2),XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(-1,-1)),1e-12,"Test1"); Assert.AreEqual(Math.Sqrt(2),XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(2,0)),1e-12,"Test2"); Assert.AreEqual(0,XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(2,2)),1e-12,"Test3"); Assert.AreEqual(1,XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(3,1)),1e-12,"Test4"); Assert.AreEqual(1,XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(3,3)),1e-12,"Test5"); Assert.AreEqual(2,XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(3,4)),1e-12,"Test6"); Assert.AreEqual(0,XYGeometryTools.CalculatePolylineToPointDistance(polyline, new XYPoint(6,0)),1e-12,"Test7"); }
internal CrossSection(ICrossSection cs) { _cs = cs; _xsec = (XSOpen)_cs.BaseCrossSection; Line = new XYPolyline(); }
/// <summary> /// Does the same as CalculatePolylineToPointDistance but also returns the line number /// </summary> /// <param name="polyLine"></param> /// <param name="point"></param> /// <returns></returns> public static Tuple<int, double> GetClosetLine (XYPolyline polyLine, IXYPoint point) { double dist = double.MaxValue; int i = 0; int mini=0; while (i < polyLine.Points.Count - 1) { var newdist = CalculateLineToPointDistance(polyLine.GetLine(i), point); if(newdist<dist) { dist=newdist; mini =i; } i++; } return new Tuple<int, double>(mini, dist); }
/// <summary> /// Finds the shortest distance between any line segment of the polyline /// and the point. /// </summary> /// <param name="polyLine">PolyLine.</param> /// <param name="point">Point</param> /// <returns> /// <p>Length of the shortest path between the polyline and the point.</p> /// </returns> public static double CalculatePolylineToPointDistance (XYPolyline polyLine, IXYPoint point) { double dist = 0; int i = 0; while (i < polyLine.Points.Count - 1) { if (i == 0) { dist = CalculateLineToPointDistance (polyLine.GetLine(0), point); } else { dist = Math.Min(dist, CalculateLineToPointDistance (polyLine.GetLine(i), point)); } i++; } return dist; }
/// <summary> /// Calculates the length of polyline inside polygon. Lines segments on the edges of /// polygons are included with half their length. /// </summary> /// <param name="polyline">Polyline</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of polyline inside polygon. /// </returns> public static double CalculateLengthOfPolylineInsidePolygon(XYPolyline polyline, XYPolygon polygon) { double lengthInside = 0; int numberOfLineSegments = polyline.Points.Count - 1; for (int i = 0; i < numberOfLineSegments; i++) { XYLine line = new XYLine(polyline.GetLine(i)); lengthInside += CalculateLengthOfLineInsidePolygon(line,polygon); } return lengthInside; }
public void CalculateLengthOfPolylineInsidePolygon() { XYPolygon xypolygon = new XYPolygon(); xypolygon.Points.Add(new XYPoint(1,1)); xypolygon.Points.Add(new XYPoint(9,1)); xypolygon.Points.Add(new XYPoint(5,5)); xypolygon.Points.Add(new XYPoint(5,3)); xypolygon.Points.Add(new XYPoint(3,3)); xypolygon.Points.Add(new XYPoint(3,8)); xypolygon.Points.Add(new XYPoint(9,8)); xypolygon.Points.Add(new XYPoint(9,11)); xypolygon.Points.Add(new XYPoint(1,11)); XYPolyline xypolyline = new XYPolyline(); xypolyline.Points.Add(new XYPoint(9,13)); xypolyline.Points.Add(new XYPoint(7,12)); xypolyline.Points.Add(new XYPoint(7,10)); xypolyline.Points.Add(new XYPoint(2,10)); xypolyline.Points.Add(new XYPoint(2,3)); Assert.AreEqual(13,XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(xypolyline, xypolygon)); XYPolygon rectangle = new XYPolygon(); rectangle.Points.Add(new XYPoint(10,10)); rectangle.Points.Add(new XYPoint(20,10)); rectangle.Points.Add(new XYPoint(20,40)); rectangle.Points.Add(new XYPoint(10,40)); XYPolyline line1 = new XYPolyline(); line1.Points.Add(new XYPoint(0,20)); line1.Points.Add(new XYPoint(30,20)); Assert.AreEqual(10, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line1, rectangle)); // horizontal line crossing XYPolyline line2 = new XYPolyline(); line2.Points.Add(new XYPoint(10,20)); line2.Points.Add(new XYPoint(20,20)); Assert.AreEqual(10, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line2, rectangle)); // fits inside XYPolyline line3 = new XYPolyline(); line3.Points.Add(new XYPoint(0,40)); line3.Points.Add(new XYPoint(30,40)); Assert.AreEqual(5, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line3, rectangle)); XYPolyline line4 = new XYPolyline(); line4.Points.Add(new XYPoint(20,40)); line4.Points.Add(new XYPoint(20,0)); Assert.AreEqual(15, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line4, rectangle)); XYPolyline line5 = new XYPolyline(); line5.Points.Add(new XYPoint(20,40)); line5.Points.Add(new XYPoint(20,10)); Assert.AreEqual(15, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line5, rectangle)); XYPolyline line6 = new XYPolyline(); line6.Points.Add(new XYPoint(10,40)); line6.Points.Add(new XYPoint(30,40)); Assert.AreEqual(5, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line6, rectangle)); XYPolyline line7 = new XYPolyline(); line7.Points.Add(new XYPoint(10,20)); line7.Points.Add(new XYPoint(30,20)); Assert.AreEqual(10, XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(line7, rectangle)); }
/// <summary> /// Reads the next shape /// </summary> /// <returns></returns> public IGeometry ReadNext(int RecordNumber) { IntPtr pShape = ShapeLib.SHPReadObject(_shapePointer, RecordNumber); ShapeLib.SHPObject shpObject = new ShapeLib.SHPObject(); Marshal.PtrToStructure(pShape, shpObject); double[] x = new double[shpObject.nVertices]; Marshal.Copy(shpObject.padfX, x, 0, x.Length); double[] y = new double[shpObject.nVertices]; Marshal.Copy(shpObject.padfY, y, 0, y.Length); double[] z = new double[shpObject.nVertices]; Marshal.Copy(shpObject.padfZ, z, 0, z.Length); int[] partstarts = null; if (shpObject.nParts > 0) { partstarts = new int[shpObject.nParts]; Marshal.Copy(shpObject.paPartStart, partstarts, 0, partstarts.Length); } ShapeLib.SHPDestroyObject(pShape); IGeometry geom = null; switch (type) { case ShapeLib.ShapeType.NullShape: break; case ShapeLib.ShapeType.MultiPoint: case ShapeLib.ShapeType.MultiPointZ: case ShapeLib.ShapeType.MultiPointM: case ShapeLib.ShapeType.PointM: case ShapeLib.ShapeType.PointZ: case ShapeLib.ShapeType.Point: geom = new XYPoint(x[0], y[0]); break; case ShapeLib.ShapeType.PolyLineM: case ShapeLib.ShapeType.PolyLineZ: case ShapeLib.ShapeType.PolyLine: geom = new XYPolyline(); for (int i = 0; i < x.Length; i++) ((XYPolyline)geom).Points.Add(new XYPoint(x[i], y[i])); break; case ShapeLib.ShapeType.PolygonM: case ShapeLib.ShapeType.PolygonZ: case ShapeLib.ShapeType.Polygon: if (partstarts.Count() == 1) { geom = new XYPolygon(); for (int i = 0; i < x.Length; i++) ((XYPolygon)geom).Points.Add(new XYPoint(x[i], y[i])); ((XYPolygon)geom).Points.Reverse(); } else { geom = new MultiPartPolygon(); //foreach (var partstart in partstarts.Reverse()) //{ // var poly = new XYPolygon(); // for (int i = end; i > partstart; i--) // poly.Points.Add(new XYPoint(x[i], y[i])); // end = partstart; // ((MultiPartPolygon)geom).Polygons.Add(poly); //} for (int j = 0; j < partstarts.Count(); j++) { int end; if (j < partstarts.Count() - 1) end = partstarts[j + 1]; else end = x.Length; var poly = new XYPolygon(); for (int i = partstarts[j]; i < end; i++) poly.Points.Add(new XYPoint(x[i], y[i])); poly.Points.Reverse(); ((MultiPartPolygon)geom).Polygons.Add(poly); } } break; case ShapeLib.ShapeType.MultiPatch: break; default: break; } return geom; }