public virtual void UpdateMappingMatrix(string methodDescription, IElementSet fromElements, IElementSet toElements, bool useSparseMatrix) { try { ElementSetChecker.CheckElementSet(fromElements); ElementSetChecker.CheckElementSet(toElements); CurrentMethod = GetMethod(methodDescription, fromElements.ElementType, toElements.ElementType); if(useSparseMatrix) { MappingMatrix = new DoubleSparseMatrix(RowsCount, ColumnsCount); intersectedLengthMatrix = new DoubleSparseMatrix(RowsCount, ColumnsCount); } else { MappingMatrix = new DoubleMatrix(RowsCount, ColumnsCount); intersectedLengthMatrix = new DoubleMatrix(RowsCount, ColumnsCount); } if (fromElements.ElementType == ElementType.XYPoint && toElements.ElementType == ElementType.XYPoint) // Point to Point { #region try { for (int i = 0; i < RowsCount; i++) { XYPoint ToPoint = CreateXYPoint(toElements, i); for (int j = 0; j < ColumnsCount; j++) { XYPoint FromPoint = CreateXYPoint(fromElements, j); MappingMatrix[i, j] = XYGeometryTools.CalculatePointToPointDistance(ToPoint, FromPoint); } } if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPoint.Nearest)) { for (int i = 0; i < RowsCount; i++) { double MinDist = MappingMatrix[i, 0]; for (int j = 1; j < ColumnsCount; j++) { if (MappingMatrix[i, j] < MinDist) { MinDist = MappingMatrix[i, j]; } } int Denominator = 0; for (int j = 0; j < ColumnsCount; j++) { if (MappingMatrix[i, j] == MinDist) { MappingMatrix[i, j] = 1; Denominator++; } else { MappingMatrix[i, j] = 0; } } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/Denominator; } } } // if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Nearest)) else if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPoint.Inverse)) { for (int i = 0; i < RowsCount; i++) { double MinDist = MappingMatrix[i, 0]; for (int j = 1; j < ColumnsCount; j++) { if (MappingMatrix[i, j] < MinDist) { MinDist = MappingMatrix[i, j]; } } if (MinDist == 0) { int Denominator = 0; for (int j = 0; j < ColumnsCount; j++) { if (MappingMatrix[i, j] == MinDist) { MappingMatrix[i, j] = 1; Denominator++; } else { MappingMatrix[i, j] = 0; } } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/Denominator; } } else { double Denominator = 0; for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = 1/MappingMatrix[i, j]; Denominator = Denominator + MappingMatrix[i, j]; } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/Denominator; } } } } // else if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Inverse)) else { throw new Exception("methodDescription unknown for point point mapping"); } // else if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Nearest)) and else if (currentMethodId.Equals((int) DefaultMethodType.PointToPoint.Inverse)) } catch (Exception e) // Catch for all of the Point to Point part { throw new Exception("Point to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.XYPoint && ((toElements.ElementType == ElementType.XYPolyLine) || (toElements.ElementType == ElementType.XYLine))) // Point to PolyLine/Line { #region try { for (int i = 0; i < RowsCount; i++) { XYPolyline toPolyLine = CreateXYPolyline(toElements, i); for (int j = 0; j < ColumnsCount; j++) { XYPoint fromPoint = CreateXYPoint(fromElements, j); MappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(toPolyLine, fromPoint); } } if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolyline.Nearest) || CurrentMethod.ID.Equals((int) DefaultMethodType.PointToLine.Nearest)) { for (int i = 0; i < RowsCount; i++) { double MinDist = MappingMatrix[i, 0]; for (int j = 1; j < ColumnsCount; j++) { if (MappingMatrix[i, j] < MinDist) { MinDist = MappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < ColumnsCount; j++) { if (MappingMatrix[i, j] == MinDist) { MappingMatrix[i, j] = 1; denominator++; } else { MappingMatrix[i, j] = 0; } } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/denominator; } } } // if (currentMethodId.Equals((int) DefaultMethodType.PointToPolyline.Nearest)) else if ((CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolyline.Inverse)) || (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToLine.Inverse))) { for (int i = 0; i < RowsCount; i++) { double minDist = MappingMatrix[i, 0]; for (int j = 1; j < ColumnsCount; j++) { if (MappingMatrix[i, j] < minDist) { minDist = MappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < ColumnsCount; j++) { if (MappingMatrix[i, j] == minDist) { MappingMatrix[i, j] = 1; denominator++; } else { MappingMatrix[i, j] = 0; } } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/denominator; } } else { double denominator = 0; for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = 1/MappingMatrix[i, j]; denominator = denominator + MappingMatrix[i, j]; } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/denominator; } } } } // else if (currentMethodId.Equals((int) DefaultMethodType.PointToPolyline.Inverse)) else // if currentMethodId != Nearest and Inverse { throw new Exception("methodDescription unknown for point to polyline mapping"); } } catch (Exception e) // Catch for all of the Point to Polyline part { throw new Exception("Point to polyline mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.XYPoint && toElements.ElementType == ElementType.XYPolygon) // Point to Polygon { #region try { XYPolygon polygon; XYPoint point; int count; for (int i = 0; i < RowsCount; i++) { polygon = toXYPolygons[i]; count = 0; for (int n = 0; n < ColumnsCount; n++) { point = CreateXYPoint(fromElements, n); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolygon.Mean)) { count = count + 1; } else if (CurrentMethod.ID.Equals((int) DefaultMethodType.PointToPolygon.Sum)) { count = 1; } else { throw new Exception( "methodDescription unknown for point to polygon mapping"); } } } for (int n = 0; n < ColumnsCount; n++) { point = CreateXYPoint(fromElements, n); if (XYGeometryTools.IsPointInPolygon(point, polygon)) { MappingMatrix[i, n] = 1.0/count; } } } } catch (Exception e) // Catch for all of the Point to Polyline part { throw new Exception("Point to polygon mapping failed", e); } #endregion } else if (((fromElements.ElementType == ElementType.XYPolyLine) || (fromElements.ElementType == ElementType.XYLine)) && toElements.ElementType == ElementType.XYPoint) // Polyline/Line to Point { #region try { for (int i = 0; i < RowsCount; i++) { XYPoint toPoint = CreateXYPoint(toElements, i); for (int j = 0; j < ColumnsCount; j++) { XYPolyline fromPolyLine = CreateXYPolyline(fromElements, j); MappingMatrix[i, j] = XYGeometryTools.CalculatePolylineToPointDistance(fromPolyLine, toPoint); } } if (CurrentMethod.ID.Equals((int) DefaultMethodType.PolylineToPoint.Nearest) || CurrentMethod.ID.Equals((int) DefaultMethodType.LineToPoint.Nearest)) { for (int i = 0; i < RowsCount; i++) { double minDist = MappingMatrix[i, 0]; for (int j = 1; j < ColumnsCount; j++) { if (MappingMatrix[i, j] < minDist) { minDist = MappingMatrix[i, j]; } } int denominator = 0; for (int j = 0; j < ColumnsCount; j++) { if (MappingMatrix[i, j] == minDist) { MappingMatrix[i, j] = 1; denominator++; } else { MappingMatrix[i, j] = 0; } } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/denominator; } } } // if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPoint.Nearest)) else if (CurrentMethod.ID.Equals((int) DefaultMethodType.PolylineToPoint.Inverse) || CurrentMethod.ID.Equals((int) DefaultMethodType.LineToPoint.Inverse)) { for (int i = 0; i < RowsCount; i++) { double minDist = MappingMatrix[i, 0]; for (int j = 1; j < ColumnsCount; j++) { if (MappingMatrix[i, j] < minDist) { minDist = MappingMatrix[i, j]; } } if (minDist == 0) { int denominator = 0; for (int j = 0; j < ColumnsCount; j++) { if (MappingMatrix[i, j] == minDist) { MappingMatrix[i, j] = 1; denominator++; } else { MappingMatrix[i, j] = 0; } } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/denominator; } } else { double denominator = 0; for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = 1/MappingMatrix[i, j]; denominator = denominator + MappingMatrix[i, j]; } for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/denominator; } } } } // if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPoint.Inverse)) else // MethodID != Nearest and Inverse { 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.XYPolyLine) || (fromElements.ElementType == ElementType.XYLine)) && toElements.ElementType == ElementType.XYPolygon) // PolyLine to Polygon { #region try { for (int i = 0; i < RowsCount; i++) { XYPolygon polygon = toXYPolygons[i]; if ( CurrentMethod.ID.Equals( (int) DefaultMethodType.PolylineToPolygon.WeightedMean) || CurrentMethod.ID.Equals((int) DefaultMethodType.LineToPolygon.WeightedMean)) { double totalLineLengthInPolygon = 0; for (int n = 0; n < ColumnsCount; n++) { XYPolyline polyline = CreateXYPolyline(fromElements, n); MappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon); IntersectedLengthMatrix[i, n] = MappingMatrix[i, n]; totalLineLengthInPolygon += MappingMatrix[i, n]; } if (totalLineLengthInPolygon > 0) { for (int n = 0; n < ColumnsCount; n++) { MappingMatrix[i, n] = MappingMatrix[i, n]/ totalLineLengthInPolygon; } } } // if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPolygon.WeightedMean)) else if ( CurrentMethod.ID.Equals( (int) DefaultMethodType.PolylineToPolygon.WeightedSum) || CurrentMethod.ID.Equals( (int) DefaultMethodType.LineToPolygon.WeightedSum)) { for (int n = 0; n < ColumnsCount; n++) { XYPolyline polyline = CreateXYPolyline(fromElements, n); MappingMatrix[i, n] = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon( polyline, polygon)/polyline.GetLength(); IntersectedLengthMatrix[i, n] = MappingMatrix[i, n]; } // for (int n = 0; n < ColumnsCount; n++) } // else if (currentMethodId.Equals((int) DefaultMethodType.PolylineToPolygon.WeightedSum)) else // if MethodID != WeightedMean and WeigthedSum { throw new Exception( "methodDescription unknown for polyline to polygon mapping"); } } // for (int i = 0; i < RowsCount; i++) } catch (Exception e) // Catch for all of polyLine to polygon { throw new Exception("Polyline to polygon mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.XYPolygon && toElements.ElementType == ElementType.XYPoint) // Polygon to Point { #region try { for (int n = 0; n < RowsCount; n++) { XYPoint point = CreateXYPoint(toElements, n); for (int i = 0; i < ColumnsCount; i++) { XYPolygon polygon = fromXYPolygons[i]; if (XYGeometryTools.IsPointInPolygon(point, polygon)) { if ( CurrentMethod.ID.Equals( (int) DefaultMethodType.PolygonToPoint.Value)) { MappingMatrix[n, i] = 1.0; } else // if currentMethodId != Value { throw new Exception( "methodDescription unknown for polygon to point mapping"); } } } } } catch (Exception e) // catch for all of Polygon to Point { throw new Exception("Polygon to point mapping failed", e); } #endregion } else if (fromElements.ElementType == ElementType.XYPolygon && ((toElements.ElementType == ElementType.XYPolyLine) || (toElements.ElementType == ElementType.XYLine))) // Polygon to PolyLine { #region try { for (int i = 0; i < RowsCount; i++) { XYPolyline polyline = CreateXYPolyline(toElements, i); if ( CurrentMethod.ID.Equals( (int) DefaultMethodType.PolygonToPolyline.WeightedMean) || CurrentMethod.ID.Equals( (int) DefaultMethodType.PolygonToLine.WeightedMean)) { for (int n = 0; n < ColumnsCount; n++) { XYPolygon polygon = fromXYPolygons[n]; var intersectedLength = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(polyline, polygon); MappingMatrix[i, n] = intersectedLength/polyline.GetLength(); IntersectedLengthMatrix[i, n] = intersectedLength; } double sum = 0; for (int n = 0; n < ColumnsCount; n++) { sum += MappingMatrix[i, n]; } for (int n = 0; n < ColumnsCount; n++) { MappingMatrix[i, n] = MappingMatrix[i, n]/sum; } } // if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolyline.WeightedMean)) else if ( CurrentMethod.ID.Equals((int) DefaultMethodType.PolygonToPolyline.WeightedSum) || CurrentMethod.ID.Equals((int) DefaultMethodType.PolygonToLine.WeightedSum)) { for (int n = 0; n < ColumnsCount; n++) { XYPolygon polygon = fromXYPolygons[n]; var intersectedLength = XYGeometryTools.CalculateLengthOfPolylineInsidePolygon(polyline, polygon); MappingMatrix[i, n] = intersectedLength/polyline.GetLength(); IntersectedLengthMatrix[i, n] = intersectedLength; } } // else if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolyline.WeightedSum)) else // currentMethodId != WeightedMean and WeightedSum { 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.XYPolygon && toElements.ElementType == ElementType.XYPolygon) // Polygon to Polygon { #region try { for (int i = 0; i < RowsCount; i++) { XYPolygon toPolygon = toXYPolygons[i]; for (int j = 0; j < ColumnsCount; j++) { XYPolygon fromPolygon = fromXYPolygons[j]; MappingMatrix[i, j] = XYGeometryTools.CalculateSharedArea(toPolygon, fromPolygon); } if ( CurrentMethod.ID.Equals( (int) DefaultMethodType.PolygonToPolygon.WeightedMean)) { double denominator = 0; for (int j = 0; j < ColumnsCount; j++) { denominator = denominator + MappingMatrix[i, j]; } for (int j = 0; j < ColumnsCount; j++) { if (denominator != 0) { MappingMatrix[i, j] = MappingMatrix[i, j]/ denominator; } } } // if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolygon.WeightedMean)) else if ( CurrentMethod.ID.Equals( (int) DefaultMethodType.PolygonToPolygon.WeightedSum)) { for (int j = 0; j < ColumnsCount; j++) { MappingMatrix[i, j] = MappingMatrix[i, j]/ toPolygon.GetArea(); } } // else if (currentMethodId.Equals((int) DefaultMethodType.PolygonToPolygon.WeightedSum)) else // currentMethodId != WeightedMean and WeightedSum { 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 (fromElements.ElementType == ElementType.XYPolyLine && toElements.ElementType == ElementType.XYPolyLine) // PolyLine to PolyLine { #region if (toElements.Equals(fromElements) && CurrentMethod.ID.Equals((int)DefaultMethodType.PolylineToPolyline.Value)) { int nElements = fromElements.ElementCount; if (useSparseMatrix) { MappingMatrix = new DoubleSparseMatrix(nElements, nElements); } else { MappingMatrix = new DoubleMatrix(nElements, nElements); } for (int i = 0; i < nElements; i++) { MappingMatrix[i, i] = 1; } } else { throw new Exception("Polyline to Polyline mapping failed"); } #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); } }