地圖平移校訂,當加載兩幅空間參考同樣的影像,其中一幅影像有點偏移,這時就以另外一幅影像爲基準將其進行平移校訂,而後保存,再次加載就不會出現偏移了。工具
下面來介紹下實現的主要代碼:spa
首先經過選中目錄樹中的要平移的圖層,拖動圖層進行平移校訂,計算出圖層平移的偏移量,而後修改柵格數據集的仿射變換參數的座標便可,核心方法就是PIE SDK的SetOffset 和GetOffset方法;code
平移校訂的過程主要涉及到鼠標的MouseDown、MouseUp和MouseMove事件。orm
當鼠標按下,記錄鼠標的座標,爲開始座標點m_StartPoint,鼠標移動就記錄鼠標移動的座標,爲結束座標點m_EndPoint,兩個座標去差值就是圖層當前偏移的距離,而後設置圖層的偏移量SetOffset()就能夠移動圖層。要注意的是當移動一次的時候再在上一次移動的基礎上進行移動,須要設置開始座標點的座標爲鼠標按下的點加上地圖上一次偏移的距離的和,具體過程如代碼所示。視頻
1 /// <summary> 2 /// 圖層平移校訂按鈕 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void tbn_PanConrrection_Click(object sender, EventArgs e) 7 { 8 //選中圖層 根據目錄樹選中的圖層爲平移圖層 9 IHookHelper hookHelper = new HookHelper(); 10 hookHelper.Hook = mapControlMain; 11 IPmdContents pmdContents = hookHelper.GetContent(); 12 PIETOCNodeTag tag = pmdContents.CustomerProperty as PIETOCNodeTag; 13 if (tag == null) return; 14 if (tag.Layer == null) return; 15 m_SelectedLayer = tag.Layer; 16 m_IsPan = true; 17 } 18 /// <summary> 19 /// 保存平移校訂結果按鈕 20 /// </summary> 21 /// <param name="sender"></param> 22 /// <param name="e"></param> 23 private void btn_SavePan_Click(object sender, EventArgs e) 24 { 25 if (m_IsPan != true) return; 26 double offsetX = 0; 27 double offsetY = 0; 28 m_SelectedLayer.GetOffset(ref offsetX, ref offsetY);//獲取偏移量 29 30 string filePath = m_SelectedLayer.DataSourcePath; 31 IRasterDataset RasterDataset = DatasetFactory.OpenRasterDataset(filePath, OpenMode.Update); 32 double[] geoTrans = RasterDataset.GetGeoTransform(); 33 34 geoTrans[0] = geoTrans[0] - offsetX; 35 geoTrans[3] = geoTrans[3] - offsetY; 36 RasterDataset.SetGeoTransform(geoTrans); 37 (RasterDataset as IDisposable).Dispose();//將平移校訂後的結果進行保存 38 MessageBox.Show("保存成功"); 39 m_IsPan = false; 40 }/// <summary> 41 /// 還原按鈕 42 /// </summary> 43 /// <param name="sender"></param> 44 /// <param name="e"></param> 45 private void tbn_CancelPan_Click(object sender, EventArgs e) 46 { 47 if (m_SelectedLayer == null) return; 51 m_SelectedLayer.SetOffset(0, 0); 52 mapControlMain.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll);
}
1、鼠標按下事件,觸發MouseDown事件,記錄開始座標blog
1 /// <summary> 2 /// 鼠標按下 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void mapControlMain_MouseDown(object sender, MouseEventArgs e) 7 { 8 if (m_IsPan == false || m_SelectedLayer == null) return; 9 mapControlMain.CurrentTool = null;//防止點擊平移等其餘按鈕衝突問題 10 m_StartPoint = mapControlMain.ActiveView.DisplayTransformation.ToMapPoint(e.X, e.Y); 11 double offsetX = 0; 12 double offsetY = 0; 13 m_SelectedLayer.GetOffset(ref offsetX, ref offsetY);//獲取偏移量 14 m_StartPoint.PutCoords(m_StartPoint.X + offsetX, m_StartPoint.Y + offsetY);//在上一次移動的基礎上繼續平移 15 }
2、當鼠標在地圖上移動時會觸發MouseMove事件,計算平移量,設置圖層平移教程
1 /// <summary> 2 /// 鼠標移動 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void mapControlMain_MouseMove(object sender, MouseEventArgs e) 7 { 8 if (m_IsPan == false) return; 9 if (m_StartPoint == null || m_SelectedLayer == null) return; 10 m_EndPoint = mapControlMain.ActiveView.DisplayTransformation.ToMapPoint(e.X, e.Y); 11 12 double offsetX = 0; 13 double offsetY = 0; 14 //計算偏移量 15 offsetX = m_StartPoint.X - m_EndPoint.X; 16 offsetY = m_StartPoint.Y - m_EndPoint.Y; 17 18 m_SelectedLayer.SetOffset(offsetX, offsetY);//設置圖層平移 19 mapControlMain.ActiveView.PartialRefresh(ViewDrawPhaseType.ViewAll); 20 }
3、最後鼠標彈起時,將開始點的座標設置爲空。事件
1 /// <summary> 2 /// 鼠標彈起事件 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void mapControlMain_MouseUp(object sender, MouseEventArgs e) 7 { 8 m_StartPoint = null; 9 }
代碼路徑:ci
項目名稱string |
百度雲盤地址下/PIE示例程序/13.小工具集錦/地圖平移校訂/ MapPanCorrectionDemo |
數據路徑 |
百度雲盤地址下/PIE示例數據/柵格數據/04.World/World.tif |
視頻路徑 |
百度雲盤地址下/PIE視頻教程/13.小工具集錦/地圖平移校訂.avi |
注意:
在地圖初始化的時候,圖層樹控件須要手動綁定地圖控件:
tocControlMain.SetBuddyControl(mapControlMain as PIE.Carto.IPmdContents);
效果圖:
加載時,China.tif在World.tif之上,兩幅圖座標系一致,可是卻沒有重合
平移保存以後,再次加載: