private static bool isOverlap(DsBusWeb.vwDupLineDetailRow line1, DsBusWeb.vwDupLineDetailRow line2) { return(!(line2.maxLatitude < (line1.minLatitude - DELTA_RADIUS) || line2.minLatitude > (line1.maxLatitude + DELTA_RADIUS) || line2.minLongitude > (line1.maxLongitude + DELTA_RADIUS) || line2.maxLongitude < (line1.minLongitude - DELTA_RADIUS))); }
/// <summary> /// Move all stops under these duplicated lines, then delete these duplicated lines. /// </summary> /// <param name="dupLines">List of duplicated lines</param> /// <param name="primaryLine">The primary line which will be attached within these moving stops.</param> private void DoMerging(List <DsBusWeb.vwDupLineDetailRow> dupLines, DsBusWeb.vwDupLineDetailRow primaryLine) { if (dupLines.Count > 0) { #region do merging try { _db.BeginTrans(); foreach (var line in dupLines) { _db.ExecuteCommand.CommandText = string.Format("UPDATE Line2Stop SET LineID = {0} WHERE LineID = {1}", OleDbStrHelper.getParamStr(primaryLine.LineID), OleDbStrHelper.getParamStr(line.LineID)); _db.Execute(); // Move all stop under the line to the primary line. _db.ExecuteCommand.CommandText = string.Format("DELETE FROM Lines WHERE LineID = {0}", OleDbStrHelper.getParamStr(line.LineID)); _db.Execute(); // Delete the duplicate line. line.Delete(); // Marked as deleted row means it had been merged. } _db.CommitTrans(); } catch { _db.RollBack(); throw; } finally { _db.Close(); } #endregion } }
/// <summary> /// Merge if 2 lines have the same stop inside. /// </summary> public void MergeLines() { // 1. Get all duplicate line in detail DsBusWeb.vwDupLineDetailDataTable dt = new DsBusWeb.vwDupLineDetailDataTable(); FillDt("SELECT * FROM vwDupLineDetail", dt); List <DsBusWeb.vwDupLineDetailRow> dupLines = new List <DsBusWeb.vwDupLineDetailRow>(); #region Scanning for merging process for (int i = 0; i < dt.Count; i++) { if (dt[i].RowState != DataRowState.Deleted) // If the row has not been merged... { DsBusWeb.vwDupLineDetailRow primaryLine = dt[i]; dupLines.Clear(); for (int j = i + 1; j < dt.Count; j++) { if (dt[j].RowState != DataRowState.Deleted) { var currLine = dt[j]; if (currLine.LineName == primaryLine.LineName) { if (isOverlap(primaryLine, currLine)) { dupLines.Add(currLine); } } else { break; } } } DoMerging(dupLines, primaryLine); } } #endregion MergeStops(); }