private void buttonXOK_Click(object sender, EventArgs e) { try { if (IMUFeatureList.Count < 2) { MessageBox.Show("最少需要2个特征点"); return; } IQueryFilter pQF = null; IFeatureClass pLineFC = CenterlinePointLayer.FeatureClass; DataTable CenterlinePointTable = AOFunctions.GDB.ITableUtil.GetDataTableFromITable(pLineFC as ITable, pQF); IFeatureClass pPointFC = IMULayer.FeatureClass; DataTable IMUTable; IMUTable = AOFunctions.GDB.ITableUtil.GetDataTableFromITable(pPointFC as ITable, pQF); Dictionary <DataRow, DataRow> MatchedDataRowPair = new Dictionary <DataRow, DataRow>(); List <IFeature> sortedIMUList = (from IFeature f in IMUFeatureList select f).OrderBy(x => x.get_Value(x.Fields.FindField(EvConfig.IMUMoveDistanceField))).ToList(); List <IFeature> sortedCenterlinePointList = (from IFeature f in CenterlinePointFeatureList select f).OrderBy(x => x.get_Value(x.Fields.FindField(EvConfig.CenterlineMeasureField))).ToList(); DataView dv = CenterlinePointTable.DefaultView; // dv.Sort = "里程(m) ASC"; dv.Sort = EvConfig.CenterlineMeasureField + " ASC"; CenterlinePointTable = dv.ToTable(); dv = IMUTable.DefaultView; // dv.Sort = "记录距离__ ASC"; dv.Sort = EvConfig.IMUMoveDistanceField + " ASC"; IMUTable = dv.ToTable(); // double centerlineLength = endM - beginM; if (!IMUTable.Columns.Contains("X")) { IMUTable.Columns.Add("X", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("Y")) { IMUTable.Columns.Add("Y", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("Z")) { IMUTable.Columns.Add("Z", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("里程差")) { IMUTable.Columns.Add("里程差", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("对齐里程")) { IMUTable.Columns.Add("对齐里程", System.Type.GetType("System.Double")); } for (int i = 0; i < IMUFeatureList.Count; i++) { int oid1 = sortedIMUList[i].OID; int oid2 = sortedCenterlinePointList[i].OID; DataRow IMURow = IMUTable.AsEnumerable().First(x => Convert.ToInt16(x[0]) == oid1); DataRow CenterlineRow = CenterlinePointTable.AsEnumerable().First(x => Convert.ToInt16(x[0]) == oid2); MatchedDataRowPair.Add(IMURow, CenterlineRow); } foreach (DataRow r in IMUTable.Rows) { r["对齐里程"] = DBNull.Value; r["里程差"] = DBNull.Value; } double endIMUM = Convert.ToDouble(IMUTable.Rows[IMUTable.Rows.Count - 1][EvConfig.IMUMoveDistanceField]); double beginIMUM = Convert.ToDouble(IMUTable.Rows[0][EvConfig.IMUMoveDistanceField]); double IMULength = endIMUM - beginIMUM; double CenterlineBeginM = Convert.ToDouble(CenterlinePointTable.Rows[0][EvConfig.CenterlineMeasureField]); double CenterlineEndM = Convert.ToDouble(CenterlinePointTable.AsEnumerable().Last()[EvConfig.CenterlineMeasureField]); foreach (DataRow r in MatchedDataRowPair.Keys) { r["对齐里程"] = MatchedDataRowPair[r][EvConfig.CenterlineMeasureField]; r["里程差"] = 0; } //List<DataRow> WantouPointList = (from DataRow r in IMUTable.Rows // where r["类型"].ToString().Contains("弯头") // select r).ToList(); //List<DataRow> GuandianPointList = (from DataRow r in CenterlinePointTable.Rows // where r["测点属性"].ToString().Contains("拐点") // select r).ToList(); DataTable alignmentPointTable = IMUTable; double beginM = 0; double endM = 0; var query = (from r in alignmentPointTable.AsEnumerable() where r["对齐里程"] != DBNull.Value select r).ToList(); if (query.Count > 0) { DataRow r = query[0]; if (IMUTable.Rows[0]["对齐里程"] != DBNull.Value) { beginM = Convert.ToDouble(IMUTable.Rows[0]["对齐里程"]); } else { beginM = Convert.ToDouble(r["对齐里程"]) - (Convert.ToDouble(r[EvConfig.IMUMoveDistanceField]) - Convert.ToDouble(alignmentPointTable.Rows[0][EvConfig.IMUMoveDistanceField])); alignmentPointTable.Rows[0]["对齐里程"] = beginM; } int idx = alignmentPointTable.Rows.Count - 1; r = query[query.Count - 1]; if (IMUTable.AsEnumerable().Last()["对齐里程"] != DBNull.Value) { endM = Convert.ToDouble(IMUTable.AsEnumerable().Last()["对齐里程"]); } else { endM = Convert.ToDouble(r["对齐里程"]) + (Convert.ToDouble(alignmentPointTable.Rows[idx][EvConfig.IMUMoveDistanceField]) - Convert.ToDouble(r[EvConfig.IMUMoveDistanceField])); alignmentPointTable.Rows[idx]["对齐里程"] = endM; } } if (beginM < CenterlineBeginM) { beginM = CenterlineBeginM; alignmentPointTable.Rows[0]["对齐里程"] = beginM; } if (endM > CenterlineEndM) { int idx = alignmentPointTable.Rows.Count - 1; endM = CenterlineEndM; alignmentPointTable.Rows[idx]["对齐里程"] = endM; } DataRow PrevRowWithM = null; for (int i = 0; i < IMUTable.Rows.Count; i++) { DataRow r = IMUTable.Rows[i]; if (r["对齐里程"] != DBNull.Value) { PrevRowWithM = r; } else { DataRow NextRowWithM = null; for (int j = i + 1; j < IMUTable.Rows.Count; j++) { DataRow r2 = IMUTable.Rows[j]; if (r2["对齐里程"] != DBNull.Value) { NextRowWithM = r2; break; } } if (PrevRowWithM == null || NextRowWithM == null) { break; } double BeginJiluM = Convert.ToDouble(PrevRowWithM[EvConfig.IMUMoveDistanceField]); double endJiluM = Convert.ToDouble(NextRowWithM[EvConfig.IMUMoveDistanceField]); double BeginAM = Convert.ToDouble(PrevRowWithM["对齐里程"]); double endAM = Convert.ToDouble(NextRowWithM["对齐里程"]); double currentJiluM = Convert.ToDouble(r[EvConfig.IMUMoveDistanceField]); r["对齐里程"] = (currentJiluM - BeginJiluM) * (endAM - BeginAM) / (endJiluM - BeginJiluM) + BeginAM; } } IFeatureLayer pLinearlayer = CenterlineLinarLayer; IFeatureCursor pcursor = pLinearlayer.FeatureClass.Search(null, false); IFeature pFeature = pcursor.NextFeature(); IPolyline pline = pFeature.Shape as IPolyline; IMSegmentation mSegment = pline as IMSegmentation; double maxM = mSegment.MMax; double minM = mSegment.MMin; //if (beginM > maxM || beginM < minM || endM > maxM || endM < minM) //{ // MessageBox.Show("输入的起始或终点里程值超出中线里程范围!"); // return; //} for (int i = 0; i < IMUTable.Rows.Count; i++) { DataRow r = IMUTable.Rows[i]; double M = Convert.ToDouble(r["对齐里程"]); IGeometryCollection ptcollection = mSegment.GetPointsAtM(M, 0); IPoint pt = ptcollection.get_Geometry(0) as IPoint; r["X"] = pt.X; r["Y"] = pt.Y; r["Z"] = pt.Z; } FrmIMUAlignmentresult frm = new FrmIMUAlignmentresult(IMUTable); frm.CenterlinePointTable = CenterlinePointTable; frm.setResultType("内检测对齐中线报告"); frm.ShowDialog(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btOK_Click(object sender, EventArgs e) { try { string pPointFileName = cboBoxPointLayer.SelectedItem.ToString(); string pCenterlinName = comboBoxExCenterlineLayer.SelectedItem.ToString(); IFeatureLayer pIMUPointLayer = null; IFeatureLayer pCenterlinePointLayer = null; for (int i = 0; i < pMapcontrol.LayerCount; i++) { if (pPointFileName == pMapcontrol.get_Layer(i).Name) { pIMUPointLayer = pMapcontrol.get_Layer(i) as IFeatureLayer; } if (pCenterlinName == pMapcontrol.get_Layer(i).Name) { pCenterlinePointLayer = pMapcontrol.get_Layer(i) as IFeatureLayer; } } IQueryFilter pQF = null; IFeatureClass pLineFC = pCenterlinePointLayer.FeatureClass; DataTable CenterlinePointTable = AOFunctions.GDB.ITableUtil.GetDataTableFromITable(pLineFC as ITable, pQF); IFeatureClass pPointFC = pIMUPointLayer.FeatureClass; DataTable IMUTable; if (radioButtonFromMap.Checked) { IMUTable = AOFunctions.GDB.ITableUtil.GetDataTableFromITable(pPointFC as ITable, pQF); } else { string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + this.textBoxFile.Text + ";Extended Properties=Excel 8.0"; OleDbConnection conn = new OleDbConnection(strCon); string sql1 = "select * from [Sheet1$]"; conn.Open(); OleDbDataAdapter myCommand = new OleDbDataAdapter(sql1, strCon); DataSet ds = new DataSet(); myCommand.Fill(ds); conn.Close(); IMUTable = ds.Tables[0]; IMUTable.Columns[EvConfig.IMUMoveDistanceField].DataType = System.Type.GetType("System.Double"); } double beginM = (double)numericUpDown1.Value; double endM = (double)numericUpDown2.Value; DataView dv = CenterlinePointTable.DefaultView; // dv.Sort = "里程(m) ASC"; dv.Sort = EvConfig.CenterlineMeasureField + " ASC"; CenterlinePointTable = dv.ToTable(); dv = IMUTable.DefaultView; // dv.Sort = "记录距离__ ASC"; dv.Sort = EvConfig.IMUMoveDistanceField + " ASC"; IMUTable = dv.ToTable(); double centerlineLength = endM - beginM; if (!IMUTable.Columns.Contains("X")) { IMUTable.Columns.Add("X", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("Y")) { IMUTable.Columns.Add("Y", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("Z")) { IMUTable.Columns.Add("Z", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("里程差")) { IMUTable.Columns.Add("里程差", System.Type.GetType("System.Double")); } if (!IMUTable.Columns.Contains("对齐里程")) { IMUTable.Columns.Add("对齐里程", System.Type.GetType("System.Double")); } foreach (DataRow r in IMUTable.Rows) { r["对齐里程"] = DBNull.Value; } double endIMUM = Convert.ToDouble(IMUTable.Rows[IMUTable.Rows.Count - 1][EvConfig.IMUMoveDistanceField]); double beginIMUM = Convert.ToDouble(IMUTable.Rows[0][EvConfig.IMUMoveDistanceField]); double IMULength = endIMUM - beginIMUM; Dictionary <string, string> TypeMatchDic = new Dictionary <string, string>(); //key 中线点属性, value 内检测点属性 TypeMatchDic.Add("阀门", "阀门"); TypeMatchDic.Add("拐点", "弯头"); TypeMatchDic.Add("三通", "三通"); TypeMatchDic.Add("地面标记", "地面标记"); TypeMatchDic.Add("开挖点", "开挖点"); Dictionary <string, string> SpecialMatchDic = new Dictionary <string, string>(); SpecialMatchDic.Add("阀门", "阀门"); SpecialMatchDic.Add("三通", "三通"); SpecialMatchDic.Add("地面标记", "地面标记"); SpecialMatchDic.Add("开挖点", "开挖点"); //根据特殊控制点强制对齐 if (!PreMatchBySpecialControlPoint(SpecialMatchDic, IMUTable, CenterlinePointTable)) { return; } List <DataRow> NeijianceControlPointList = (from DataRow r in IMUTable.Rows where r["类型"] != DBNull.Value && IsNeiJianCeDianControlPointType(r["类型"].ToString(), TypeMatchDic) select r).ToList(); List <DataRow> ZhongXianControlPointList = (from DataRow r in CenterlinePointTable.Rows where r["测点属性"] != DBNull.Value && IsZhongXianDianControlPointType(r["测点属性"].ToString(), TypeMatchDic) select r).ToList(); Dictionary <DataRow, DataRow> MatchedDataRowPair = new Dictionary <DataRow, DataRow>(); int LastMatchedPointsCount = -1; while (true) { int matchedrowCount = IMUTable.AsEnumerable().Where(x => x["对齐里程"] != DBNull.Value).Count(); MatchedDataRowPair.Clear(); for (int i = 0; i < NeijianceControlPointList.Count; i++) { DataRow IMUr = NeijianceControlPointList[i]; double ActionIMUM = (Convert.ToDouble(IMUr[EvConfig.IMUMoveDistanceField]) - beginIMUM) * centerlineLength / IMULength + beginM; if (IMUr["对齐里程"] != DBNull.Value) { ActionIMUM = Convert.ToDouble(IMUr["对齐里程"]); } else { // 还没找到对齐点通过整体长度计算里程 if (matchedrowCount == 0) { ActionIMUM = (Convert.ToDouble(IMUr[EvConfig.IMUMoveDistanceField]) - beginIMUM) * centerlineLength / IMULength + beginM; } //找到了对齐点,用最近对齐点计算里程 else { DataRow beforeMatchedRow = IMUTable.AsEnumerable().Where(x => x["对齐里程"] != DBNull.Value && Convert.ToDouble(x[EvConfig.IMUMoveDistanceField]) < Convert.ToDouble(IMUr[EvConfig.IMUMoveDistanceField])).Last(); double beforeM = Convert.ToDouble(beforeMatchedRow["对齐里程"]); double beforeD = Convert.ToDouble(beforeMatchedRow[EvConfig.IMUMoveDistanceField]); DataRow nextMatchedRow = IMUTable.AsEnumerable().Where(x => x["对齐里程"] != DBNull.Value && Convert.ToDouble(x[EvConfig.IMUMoveDistanceField]) > Convert.ToDouble(IMUr[EvConfig.IMUMoveDistanceField])).First(); double nextM = Convert.ToDouble(nextMatchedRow["对齐里程"]); double nextD = Convert.ToDouble(nextMatchedRow[EvConfig.IMUMoveDistanceField]); double currentD = Convert.ToDouble(IMUr[EvConfig.IMUMoveDistanceField]); ActionIMUM = (currentD - beforeD) * (nextM - beforeM) / (nextD - beforeD) + beforeM; } } List <DataRow> Featurerow = (from r in ZhongXianControlPointList where Math.Abs(Convert.ToDouble(r[EvConfig.CenterlineMeasureField]) - ActionIMUM) < Convert.ToDouble(numericUpDown3.Value) && IsZhongXianNejianceCouldMatch(r["测点属性"].ToString(), IMUr["类型"].ToString(), TypeMatchDic) select r).OrderBy(x => Math.Abs(Convert.ToDouble(x[EvConfig.CenterlineMeasureField]) - ActionIMUM)).ToList(); if (Featurerow.Count > 0) { DataRow NearestR = Featurerow[0]; if (MatchedDataRowPair.Values.Contains(NearestR) == false) { IMUr["里程差"] = Convert.ToDouble(NearestR[EvConfig.CenterlineMeasureField]) - ActionIMUM; MatchedDataRowPair.Add(IMUr, NearestR); } else { DataRow mathcedIMUr = (from DataRow k in MatchedDataRowPair.Keys where MatchedDataRowPair[k].Equals(NearestR) select k).ToList().First(); double dis = Math.Abs(Convert.ToDouble(NearestR[EvConfig.CenterlineMeasureField]) - ActionIMUM); double olddis = Math.Abs(Convert.ToDouble(mathcedIMUr["里程差"])); if (dis < olddis) { MatchedDataRowPair.Remove(mathcedIMUr); mathcedIMUr["里程差"] = DBNull.Value; IMUr["里程差"] = Convert.ToDouble(NearestR[EvConfig.CenterlineMeasureField]) - ActionIMUM; MatchedDataRowPair.Add(IMUr, NearestR); } else { continue; } } } } if (MatchedDataRowPair.Count <= LastMatchedPointsCount) { break; } else { LastMatchedPointsCount = MatchedDataRowPair.Count; } foreach (DataRow r in MatchedDataRowPair.Keys) { r["对齐里程"] = MatchedDataRowPair[r][EvConfig.CenterlineMeasureField]; } //未匹配上的点里程设置为null foreach (DataRow r in IMUTable.Rows) { if (!MatchedDataRowPair.ContainsKey(r)) { r["对齐里程"] = DBNull.Value; } } //更新起始点和终点对齐里程 if (IMUTable.AsEnumerable().Where(x => x["对齐里程"] != DBNull.Value).Count() > 0) { DataRow begrow = IMUTable.Rows[0]; DataRow endrow = IMUTable.Rows[IMUTable.Rows.Count - 1]; DataRow fistMatchRow = IMUTable.AsEnumerable().Where(x => x["对齐里程"] != DBNull.Value).First(); DataRow lastMatchRow = IMUTable.AsEnumerable().Where(x => x["对齐里程"] != DBNull.Value).Last(); //根据匹配控制点和 记录距离计算终点和起点的对齐里程 if (begrow["对齐里程"] == DBNull.Value) { begrow["对齐里程"] = Convert.ToDouble(fistMatchRow["对齐里程"]) - (Convert.ToDouble(fistMatchRow[EvConfig.IMUMoveDistanceField]) - Convert.ToDouble(begrow[EvConfig.IMUMoveDistanceField])); } if (endrow["对齐里程"] == DBNull.Value) { endrow["对齐里程"] = Convert.ToDouble(lastMatchRow["对齐里程"]) + (Convert.ToDouble(endrow[EvConfig.IMUMoveDistanceField]) - Convert.ToDouble(lastMatchRow[EvConfig.IMUMoveDistanceField])); } } //IMUTable.Rows[0]["对齐里程"] = beginM; //IMUTable.Rows[IMUTable.Rows.Count - 1]["对齐里程"] = endM; } CalculateNullMeasureBasedOnControlpointMeasure(ref IMUTable); //DataRow PrevRowWithM = null; //for (int i = 0; i < IMUTable.Rows.Count; i++) //{ // DataRow r = IMUTable.Rows[i]; // if (r["对齐里程"] != DBNull.Value) // { // PrevRowWithM = r; // } // else // { // DataRow NextRowWithM = null; // for (int j = i + 1; j < IMUTable.Rows.Count; j++) // { // DataRow r2 = IMUTable.Rows[j]; // if (r2["对齐里程"] != DBNull.Value) // { // NextRowWithM = r2; // break; // } // } // if (PrevRowWithM == null || NextRowWithM == null) // { // break; // } // double BeginJiluM = Convert.ToDouble(PrevRowWithM[EvConfig.IMUMoveDistanceField]); // double endJiluM = Convert.ToDouble(NextRowWithM[EvConfig.IMUMoveDistanceField]); // double BeginAM = Convert.ToDouble(PrevRowWithM["对齐里程"]); // double endAM = Convert.ToDouble(NextRowWithM["对齐里程"]); // double currentJiluM = Convert.ToDouble(r[EvConfig.IMUMoveDistanceField]); // r["对齐里程"] = (currentJiluM - BeginJiluM) * (endAM - BeginAM) / (endJiluM - BeginJiluM) + BeginAM; // } //} IFeatureLayer pLinearlayer = null; string pCenterlineLinearName = comboBoxExCenterlineLinearLayer.SelectedItem.ToString(); for (int i = 0; i < pMapcontrol.LayerCount; i++) { if (pCenterlineLinearName == pMapcontrol.get_Layer(i).Name) { pLinearlayer = pMapcontrol.get_Layer(i) as IFeatureLayer; } } IFeatureCursor pcursor = pLinearlayer.FeatureClass.Search(null, false); IFeature pFeature = pcursor.NextFeature(); IPolyline pline = pFeature.Shape as IPolyline; IMSegmentation mSegment = pline as IMSegmentation; double maxM = mSegment.MMax; double minM = mSegment.MMin; if (beginM > maxM || beginM < minM || endM > maxM || endM < minM) { MessageBox.Show("输入的起始或终点里程值超出中线里程范围!"); return; } for (int i = 0; i < IMUTable.Rows.Count; i++) { DataRow r = IMUTable.Rows[i]; double M = Convert.ToDouble(r["对齐里程"]); if (M < mSegment.MMin) { M = mSegment.MMin; } if (M > mSegment.MMax) { M = mSegment.MMax; } IGeometryCollection ptcollection = mSegment.GetPointsAtM(M, 0); IPoint pt = ptcollection.get_Geometry(0) as IPoint; r["X"] = pt.X; r["Y"] = pt.Y; r["Z"] = pt.Z; } FrmIMUCeterlineAlignmentresult frm = new FrmIMUCeterlineAlignmentresult(IMUTable, MatchedDataRowPair); frm.InsideCenterlineTolerance = Convert.ToDouble(numericUpDown3.Value); frm.CenterlineLayer = pLinearlayer; frm.CenterlinePointTable = CenterlinePointTable; frm.setResultType("内检测对齐中线报告"); frm.ShowDialog(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }