/// <summary> /// Constructor. Copies the contents of the xyPolyline parameter. /// </summary> /// <param name="xyPolyline">Polyline to copy.</param> /// <returns>None</returns> public XYPolyline(XYPolyline xyPolyline) { _points = new List <XYPoint>(); foreach (XYPoint xypoint in xyPolyline.Points) { _points.Add(xypoint); } }
/// <summary> /// Constructor. Copies the contents of the xyPolyline parameter. /// </summary> /// <param name="xyPolyline">Polyline to copy.</param> /// <returns>None</returns> public XYPolyline(XYPolyline xyPolyline) { Points = new List <XYPoint>(); 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> /// Constructor. Copies the contents of the xyPolyline parameter. /// </summary> /// <param name="xyPolyline">Polyline to copy.</param> /// <returns>None</returns> public XYPolyline(XYPolyline xyPolyline) { _points = new ArrayList(); foreach (XYPoint xypoint in xyPolyline.Points) { _points.Add(new XYPoint(xypoint.X, xypoint.Y)); } }
public static double CalculatePolylineLength(XYPolyline polyline) { double length = 0; for (var i = 0; i < polyline.Points.Count - 1; i++) { var line = polyline.GetLine(i); length += CalculatePointToPointDistance(line.P1, line.P2); } return(length); }
/// <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++) { var line = 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 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 XYPolyline CreateXYPolyline(IElementSet elementSet, int index) { if (!(elementSet.ElementType == ElementType.PolyLine)) { throw new Exception("Cannot create XYPolyline"); } var xyPolyline = new XYPolyline(); for (int i = 0; i < elementSet.GetVertexCount(index); i++) { xyPolyline.Points.Add(new XYPoint(elementSet.GetVertexXCoordinate(index, i), elementSet.GetVertexYCoordinate(index, i))); } return(xyPolyline); }
/// <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, XYPoint 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); }
protected void CalculateFactors(IElementSet elementSet) { for (int i = 0; i < elementSet.ElementCount; i++) { XYPolyline element = ElementMapper.CreateXYPolyline(elementSet, i); double length = element.GetLength(); if (_lengthExponent == 1) { _factors[i] = length; } else if (_lengthExponent == -1) { _factors[i] = 1.0 / length; } else { _factors[i] = Math.Pow(length, _lengthExponent); } } }
/// <summary> /// Compares the object type and the coordinates of the object and the /// object passed as parameter. /// </summary> /// <returns>True if object type is XYPolyline 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 = (XYPolyline)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); }
private void panelViewer_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { // write x and y coordinates double x = ((e.X - _margin) / _scale) + _minX; double y = _minY - (e.Y + _margin - panelViewer.ClientSize.Height) / _scale; if( _scale != double.MaxValue ) this.label3.Text = "(" + x.ToString("F3") +", " + y.ToString("F3") + ")"; else this.label3.Text = ""; // write elementSet ID and element index this.label4.Text = " "; this.label7.Text = " "; this.label8.Text = " "; this.label9.Text = " "; this.label10.Text = " "; this.label11.Text = " "; for (int elementSetNumber = 0; elementSetNumber < this._elementSets.Count; elementSetNumber++) { string elementID = " "; int elementIndex = -9; double distance = 10e30; IElementSet elementSet = (IElementSet) _elementSets[elementSetNumber]; if (elementSetNumber == 0) { this.label7.Text = elementSet.ID.Substring(0, Math.Min(20, elementSet.ID.Length)); } if (elementSetNumber == 1) { this.label9.Text = elementSet.ID.Substring(0, Math.Min(20, elementSet.ID.Length)); } for (int index = 0; index < elementSet.ElementCount; index++) { if (((IElementSet) _elementSets[elementSetNumber]).ElementType == ElementType.XYPolygon) { XYPolygon xyPolygon = new XYPolygon(); for (int i = 0; i < elementSet.GetVertexCount(index); i++) { xyPolygon.Points.Add(new XYPoint(elementSet.GetXCoordinate(index,i), elementSet.GetYCoordinate(index,i))); } if (XYGeometryTools.IsPointInPolygon(x,y,xyPolygon)) { elementID = elementSet.GetElementID(index); elementIndex = index; } } if (((IElementSet) _elementSets[elementSetNumber]).ElementType == ElementType.XYPolyLine) { XYPolyline xyPolyline = new XYPolyline(); for (int i = 0; i < elementSet.GetVertexCount(index); i++) { xyPolyline.Points.Add(new XYPoint(elementSet.GetXCoordinate(index,i), elementSet.GetYCoordinate(index,i))); } double xx = XYGeometryTools.CalculatePolylineToPointDistance(xyPolyline, new XYPoint(x,y)); if (xx < distance) { distance = xx; if (xx < 0.3 * xyPolyline.GetLength()) { elementIndex = index; elementID = elementSet.GetElementID(index); } } } if (elementSetNumber == 0 && elementIndex >= 0) { this.label4.Text = "Index: " + elementIndex.ToString(); this.label8.Text = "ID: " + elementID.Substring(0, Math.Min(17, elementID.Length)); } if (elementSetNumber == 1 && elementIndex >= 0) { this.label10.Text = "Index: " + elementIndex.ToString(); this.label11.Text = "ID: " + elementID.Substring(0, Math.Min(17, elementID.Length)); } } } }
/// <summary> /// Calculates the mapping matrix between fromElements and toElements. The mapping method /// is decided from the combination of methodDescription, fromElements.ElementType and /// toElements.ElementType. /// The valid values for methodDescription is obtained through use of the /// GetAvailableMethods method. /// </summary> /// /// <remarks> /// UpdateMappingMatrix is called during initialisation. UpdateMappingMatrix must be called prior /// to Mapvalues. /// </remarks> /// /// <param name="methodIdentifier">String identification of mapping method</param> /// <param name="fromElements">The IElementset to map from.</param> /// <param name="toElements">The IElementset to map to</param> /// /// <returns> /// The method has no return value. /// </returns> private void UpdateMappingMatrix(ref IIdentifiable methodIdentifier, ref IElementSet fromElements, ref IElementSet toElements) { try { ElementSetChecker.CheckElementSet(fromElements); ElementSetChecker.CheckElementSet(toElements); _method = SpatialAdaptedOutputFactory.GetMethod(methodIdentifier); _numberOfToRows = toElements.ElementCount; _numberOfFromColumns = fromElements.ElementCount; _mappingMatrix = new DoubleSparseMatrix(_numberOfToRows, _numberOfFromColumns); if (fromElements.ElementType == ElementType.Point && toElements.ElementType == ElementType.Point) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPoint toPoint = CreateXYPoint(toElements, i); for (int j = 0; j < _numberOfFromColumns; j++) { XYPoint fromPoint = CreateXYPoint(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculatePointToPointDistance(toPoint, fromPoint); } } if (_method == ElementMapperMethod.Nearest) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.Inverse) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } else { double denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = 1 / _mappingMatrix[i, j]; denominator = denominator + _mappingMatrix[i, j]; } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } } else { throw new Exception("methodDescription unknown for point point mapping"); } } catch (Exception e) { throw new Exception("Point to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Point && toElements.ElementType == ElementType.PolyLine) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPolyline toPolyLine = CreateXYPolyline(toElements, i); for (int j = 0; j < _numberOfFromColumns; j++) { XYPoint fromPoint = CreateXYPoint(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(toPolyLine, fromPoint); } } if (_method == ElementMapperMethod.Nearest) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.Inverse) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } else { double denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = 1 / _mappingMatrix[i, j]; denominator = denominator + _mappingMatrix[i, j]; } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } } else { throw new Exception("methodDescription unknown for point to polyline mapping"); } } catch (Exception e) { throw new Exception("Point to polyline mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Point && toElements.ElementType == ElementType.Polygon) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPolygon polygon = CreateXYPolygon(toElements, i); int count = 0; XYPoint point; for (int n = 0; n < _numberOfFromColumns; n++) { point = CreateXYPoint(fromElements, n); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { if (_method == ElementMapperMethod.Mean) { count = count + 1; } else if (_method == ElementMapperMethod.Sum) { count = 1; } else { throw new Exception( "methodDescription unknown for point to polygon mapping"); } } } for (int n = 0; n < _numberOfFromColumns; n++) { point = CreateXYPoint(fromElements, n); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { _mappingMatrix[i, n] = 1.0 / count; } } } } catch (Exception e) { throw new Exception("Point to polygon mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.PolyLine && toElements.ElementType == ElementType.Point) { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPoint toPoint = CreateXYPoint(toElements, i); for (int j = 0; j < _numberOfFromColumns; j++) { XYPolyline fromPolyLine = CreateXYPolyline(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(fromPolyLine, toPoint); } } if (_method == ElementMapperMethod.Nearest) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.Inverse) { for (int i = 0; i < _numberOfToRows; i++) { double minDist = _mappingMatrix[i, 0]; for (int j = 1; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] < minDist) { minDist = _mappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { if (_mappingMatrix[i, j] == minDist) { _mappingMatrix[i, j] = 1; denominator++; } else { _mappingMatrix[i, j] = 0; } } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } else { double denominator = 0; for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = 1 / _mappingMatrix[i, j]; denominator = denominator + _mappingMatrix[i, j]; } for (int j = 0; j < _numberOfFromColumns; j++) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } } else { throw new Exception("methodDescription unknown for polyline to point mapping"); } } catch (Exception e) // Catch for all of the Point to Polyline part { throw new Exception("Polyline to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.PolyLine && toElements.ElementType == ElementType.Polygon) { #region try { // For each polygon in target for (int i = 0; i < _numberOfToRows; i++) { XYPolygon polygon = CreateXYPolygon(toElements, i); if (_method == ElementMapperMethod.WeightedMean) { double totalLineLengthInPolygon = 0; for (int n = 0; n < _numberOfFromColumns; n++) { XYPolyline polyline = CreateXYPolyline(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon); totalLineLengthInPolygon += _mappingMatrix[i, n]; } if (totalLineLengthInPolygon > 0) { for (int n = 0; n < _numberOfFromColumns; n++) { _mappingMatrix[i, n] = _mappingMatrix[i, n] / totalLineLengthInPolygon; } } } else if (_method == ElementMapperMethod.WeightedSum) { // For each line segment in PolyLine for (int n = 0; n < _numberOfFromColumns; n++) { XYPolyline polyline = CreateXYPolyline(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon) / polyline.GetLength(); } } else { throw new Exception( "methodDescription unknown for polyline to polygon mapping"); } } } catch (Exception e) { throw new Exception("Polyline to polygon mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Polygon && toElements.ElementType == ElementType.Point) { #region try { if (_method != ElementMapperMethod.Value) { throw new Exception("methodDescription unknown for polygon to point mapping"); } // Only create search tree if number of cols/rows is larger than say 10/10. bool useSearchTree = _numberOfFromColumns > 10 && _numberOfToRows > 10; XYElementSearchTree <int> fromSearchTree = null; ICollection <int> fromCandidateElmts = null; if (useSearchTree) { fromSearchTree = XYElementSearchTree <int> .BuildSearchTree(fromElements); } else { fromCandidateElmts = new IntSequence(0, _numberOfFromColumns - 1); } for (int n = 0; n < _numberOfToRows; n++) { XYPoint point = CreateXYPoint(toElements, n); if (useSearchTree) { XYExtent toExtent = XYExtentUtil.GetExtent(point, XYGeometryTools.EPSILON); fromCandidateElmts = fromSearchTree.FindElements(toExtent); } int count = 0; // Check first for strict inclusion foreach (int i in fromCandidateElmts) { XYPolygon polygon = CreateXYPolygon(fromElements, i); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { _mappingMatrix[n, i] = 1.0; count++; } } if (count == 0) { // Not strictly inside any polygon, check also edges foreach (int i in fromCandidateElmts) { XYPolygon polygon = CreateXYPolygon(fromElements, i); if (XYGeometryTools.IsPointInPolygonOrOnEdge(point, polygon)) { _mappingMatrix[n, i] = 1.0; count++; } } } if (count > 1) { // In case of more than one hit, use average foreach (int i in fromCandidateElmts) { if (_mappingMatrix[n, i] != 0.0) { _mappingMatrix[n, i] = 1.0 / count; } } } } } catch (Exception e) { throw new Exception("Polygon to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Polygon && toElements.ElementType == ElementType.PolyLine) // Polygon to PolyLine { #region try { for (int i = 0; i < _numberOfToRows; i++) { XYPolyline polyline = CreateXYPolyline(toElements, i); if (_method == ElementMapperMethod.WeightedMean) { for (int n = 0; n < _numberOfFromColumns; n++) { XYPolygon polygon = CreateXYPolygon(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon) / polyline.GetLength(); } double sum = 0; for (int n = 0; n < _numberOfFromColumns; n++) { sum += _mappingMatrix[i, n]; } for (int n = 0; n < _numberOfFromColumns; n++) { _mappingMatrix[i, n] = _mappingMatrix[i, n] / sum; } } else if (_method == ElementMapperMethod.WeightedSum) { for (int n = 0; n < _numberOfFromColumns; n++) { XYPolygon polygon = CreateXYPolygon(fromElements, n); _mappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon) / polyline.GetLength(); } } else { throw new Exception( "methodDescription unknown for polygon to polyline mapping"); } } } catch (Exception e) // catch for all of Polygon to PolyLine { throw new Exception("Polygon to polyline mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.Polygon && toElements.ElementType == ElementType.Polygon) // Polygon to Polygon { #region try { // Only create search tree if number of cols/rows is larger than say 100/10. bool useSearchTree = _numberOfFromColumns > 10 && _numberOfToRows > 10; XYElementSearchTree <int> fromSearchTree = null; ICollection <int> fromCandidateElmts = null; if (useSearchTree) { fromSearchTree = XYElementSearchTree <int> .BuildSearchTree(fromElements); } else { fromCandidateElmts = new IntSequence(0, _numberOfFromColumns - 1); } for (int i = 0; i < _numberOfToRows; i++) { XYPolygon toPolygon = CreateXYPolygon(toElements, i); if (useSearchTree) { XYExtent toExtent = XYExtentUtil.GetExtent(toPolygon); fromCandidateElmts = fromSearchTree.FindElements(toExtent); } foreach (int j in fromCandidateElmts) { XYPolygon fromPolygon = CreateXYPolygon(fromElements, j); _mappingMatrix[i, j] = XYGeometryTools.CalculateSharedArea( toPolygon, fromPolygon); if (_method == ElementMapperMethod.Distribute) { _mappingMatrix[i, j] /= fromPolygon.GetArea(); } } if (_method == ElementMapperMethod.WeightedMean) { double denominator = 0; foreach (int j in fromCandidateElmts) { denominator = denominator + _mappingMatrix[i, j]; } foreach (int j in fromCandidateElmts) { if (denominator != 0) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / denominator; } } } else if (_method == ElementMapperMethod.WeightedSum) { foreach (int j in fromCandidateElmts) { _mappingMatrix[i, j] = _mappingMatrix[i, j] / toPolygon.GetArea(); } } else if (_method != ElementMapperMethod.Distribute) { throw new Exception( "methodDescription unknown for polygon to polygon mapping"); } } } catch (Exception e) // catch for all of Polygon to Polygon { throw new Exception("Polygon to polygon mapping failed", e); } #endregion } else // if the fromElementType, toElementType combination is no implemented { throw new Exception( "Mapping of specified ElementTypes not included in ElementMapper"); } } catch (Exception e) { throw new Exception("UpdateMappingMatrix failed to update mapping matrix", e); } }
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 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)); }
public static double CalculatePolylineLength(XYPolyline polyline) { double length = 0; for (var i = 0; i < polyline.Points.Count - 1; i++) { var line = polyline.GetLine(i); length += CalculatePointToPointDistance(line.P1, line.P2); } return length; }
/// <summary> /// Static method that validates an object with an IElementSet interface. The method /// raises an Exception in case IElementSet does not describe a valid ElementSet. /// The checks made are: /// <p>ElementType: Check</p> /// <p>XYPoint: Only one vertex in each element.</p> /// <p>XYPolyline: At least two vertices in each element.</p> /// <p> All line segments in each element has length > 0</p> /// <p>XYPolygon: At least three vertices in each element.</p> /// <p> Area of each element is larger than 0</p> /// <p> All line segments in each element has length > 0</p> /// <p> No line segments within an element crosses.</p> /// </summary> /// /// <param name="elementSet">Object that implement the IElementSet interface</param> /// /// <returns> /// The method has no return value. /// </returns> public static void CheckElementSet(IElementSet elementSet) { try { if(elementSet.ElementType == ElementType.XYPoint) { for (int i = 0; i < elementSet.ElementCount; i++) { try { if(elementSet.GetVertexCount(i) != 1) { throw new System.Exception("Number of vertices in point element is different from 1."); } } catch (System.Exception e) { throw new System.Exception("ElementID = "+elementSet.GetElementID(i),e); } } } else if(elementSet.ElementType == ElementType.XYPolyLine) { for (int i = 0; i < elementSet.ElementCount; i++) { try { XYPolyline xypolyline = new XYPolyline(); for (int j = 0; j < elementSet.GetVertexCount(i); j++) { XYPoint xypoint = new XYPoint(elementSet.GetXCoordinate(i,j),elementSet.GetYCoordinate(i,j)); xypolyline.Points.Add(xypoint); } xypolyline.Validate(); } catch (System.Exception e) { throw new System.Exception("ElementID = "+elementSet.GetElementID(i),e); } } } else if(elementSet.ElementType == ElementType.XYPolygon) { for (int i = 0; i < elementSet.ElementCount; i++) { try { XYPolygon xypolygon = new XYPolygon(); for (int j = 0; j < elementSet.GetVertexCount(i); j++) { XYPoint xypoint = new XYPoint(elementSet.GetXCoordinate(i,j),elementSet.GetYCoordinate(i,j)); xypolygon.Points.Add(xypoint); } xypolygon.Validate(); } catch (System.Exception e) { throw new System.Exception("ElementID = "+elementSet.GetElementID(i),e); } } } } catch (System.Exception e) { throw new System.Exception("ElementSet with ID = "+elementSet.ID+" is invalid",e); } }
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"); }
/// <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; }
private XYPolyline CreateXYPolyline(IElementSet elementSet, int index) { if (!(elementSet.ElementType == ElementType.XYPolyLine || elementSet.ElementType == ElementType.XYLine)) { throw new System.Exception("Cannot create XYPolyline"); } XYPolyline xyPolyline = new XYPolyline(); for (int i = 0; i < elementSet.GetVertexCount(index); i++) { xyPolyline.Points.Add(new XYPoint(elementSet.GetXCoordinate(index,i), elementSet.GetYCoordinate(index,i))); } return xyPolyline; }
/// <summary> /// Static method that validates an object with an IElementSet interface. The method /// raises an Exception in case IElementSet does not describe a valid ElementSet. /// The checks made are: /// <p>ElementType: Check</p> /// <p>XYPoint: Only one vertex in each element.</p> /// <p>XYPolyline: At least two vertices in each element.</p> /// <p> All line segments in each element has length > 0</p> /// <p>XYPolygon: At least three vertices in each element.</p> /// <p> Area of each element is larger than 0</p> /// <p> All line segments in each element has length > 0</p> /// <p> No line segments within an element crosses.</p> /// </summary> /// /// <param name="elementSet">Object that implement the IElementSet interface</param> /// /// <returns> /// The method has no return value. /// </returns> public static void CheckElementSet(IElementSet elementSet) { try { if (elementSet.ElementType == ElementType.XYPoint) { for (int i = 0; i < elementSet.ElementCount; i++) { try { if (elementSet.GetVertexCount(i) != 1) { throw new System.Exception("Number of vertices in point element is different from 1."); } } catch (System.Exception e) { throw new System.Exception("ElementID = " + elementSet.GetElementID(i), e); } } } else if (elementSet.ElementType == ElementType.XYPolyLine) { for (int i = 0; i < elementSet.ElementCount; i++) { try { XYPolyline xypolyline = new XYPolyline(); for (int j = 0; j < elementSet.GetVertexCount(i); j++) { XYPoint xypoint = new XYPoint(elementSet.GetXCoordinate(i, j), elementSet.GetYCoordinate(i, j)); xypolyline.Points.Add(xypoint); } xypolyline.Validate(); } catch (System.Exception e) { throw new System.Exception("ElementID = " + elementSet.GetElementID(i), e); } } } else if (elementSet.ElementType == ElementType.XYPolygon) { for (int i = 0; i < elementSet.ElementCount; i++) { try { XYPolygon xypolygon = new XYPolygon(); for (int j = 0; j < elementSet.GetVertexCount(i); j++) { XYPoint xypoint = new XYPoint(elementSet.GetXCoordinate(i, j), elementSet.GetYCoordinate(i, j)); xypolygon.Points.Add(xypoint); } xypolygon.Validate(); } catch (System.Exception e) { throw new System.Exception("ElementID = " + elementSet.GetElementID(i), e); } } } } catch (System.Exception e) { throw new System.Exception("ElementSet with ID = " + elementSet.ID + " is invalid", e); } }
/// <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, XYPoint 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; }