public static void LoadGeoData(AxMapControl axMapControl1, AxMapControl axMapControl2, string strFileN) { string strFExtenN = System.IO.Path.GetExtension(strFileN); switch (strFExtenN) { case ".shp": { string strPath = System.IO.Path.GetDirectoryName(strFileN); string strFile = System.IO.Path.GetFileNameWithoutExtension(strFileN); axMapControl1.AddShapeFile(strPath, strFile); axMapControl2.ClearLayers(); axMapControl2.AddShapeFile(strPath, strFile); axMapControl2.Extent = axMapControl2.FullExtent; break; } case ".bmp": case ".tif": case ".jpg": case ".img": { IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); string pathName = System.IO.Path.GetDirectoryName(strFileN); string fileName = System.IO.Path.GetFileName(strFileN); IWorkspace pWS = pWSF.OpenFromFile(pathName, 0); IRasterWorkspace pRWS = pWS as IRasterWorkspace; IRasterDataset pRasterDataSet = pRWS.OpenRasterDataset(fileName); IRasterPyramid pRasPyramid = pRasterDataSet as IRasterPyramid; if (pRasPyramid != null) { if (!(pRasPyramid.Present)) { pRasPyramid.Create(); } } IRaster pRaster = pRasterDataSet.CreateDefaultRaster(); IRasterLayer pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromRaster(pRaster); ILayer pLayer = pRasterLayer as ILayer; axMapControl1.AddLayer(pLayer, 0); axMapControl2.ClearLayers(); axMapControl2.AddLayer(pLayer, 0); axMapControl2.Extent = axMapControl2.FullExtent; break; } case ".mxd": { if (axMapControl1.CheckMxFile(strFExtenN)) { axMapControl1.LoadMxFile(strFExtenN); } else MessageBox.Show("所选择的文件不是Mxd文件!", "提示信息"); break; } default: break; } }
/// <summary> /// IMapDocument接口的open方法加载地图文档 /// </summary> /// <param name="mapControl">加载地图文档的地图控件</param> public void IMapDocumentLoadMxd(AxMapControl mapControl) { OpenFileDialog openDialog = new OpenFileDialog(); openDialog.RestoreDirectory = true; openDialog.CheckFileExists = true; openDialog.Multiselect = false; openDialog.Title = "打开地图文档"; openDialog.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf"; if (openDialog.ShowDialog() == DialogResult.OK) { string fileName = openDialog.FileName; if (fileName == "") { return; } if (mapControl.CheckMxFile(fileName)) { IMapDocument mapDocument = new MapDocument(); mapDocument.Open(fileName); mapControl.Map = mapDocument.ActiveView.FocusMap; mapControl.ActiveView.Refresh(); } } }
/// <summary> /// 控件的加载地图文档 /// </summary> /// <param name="mapControl">要加载的地图控件</param> public void LoadMxFile(AxMapControl mapControl) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "打开MXD"; openFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd;|ArcMap模板(*.mxt)|*.mxt|发布地图文件(*.pmf)|*.pmf|所有地图格式(*.mxd;*.mxt;*.pmf)|*.mxd;*.mxt;*.pmf"; openFileDialog.Multiselect = false; openFileDialog.CheckFileExists = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { string fileName = openFileDialog.FileName; if (fileName == "") { return; } if (mapControl.CheckMxFile(fileName)) { mapControl.LoadMxFile(fileName); } else { MessageBox.Show(fileName + "是无效的地图文档,请检查后重新加载"); return; } } }
private void bttImport_Click(object sender, EventArgs e) { if (txtPath.Text == "") { MessageBox.Show(" 请导入鹰眼图文件位置!", "提示!"); return; } string strPath = txtPath.Text; if (!System.IO.File.Exists(strPath)) { MessageBox.Show("未找到指定路径下的文件!", "提示!"); return; } if (!m_AxMapControl.CheckMxFile(strPath)) { MessageBox.Show("该mxd文件不是合法文件!", "提示!"); return; } bool pIsImport = ImportEagleEyset(m_Workspace, strPath); if (pIsImport) { MessageBox.Show("鹰眼图导入成功!", "提示!"); this.Dispose(); this.Close(); } if (!pIsImport) { MessageBox.Show("鹰眼图导入失败!", "提示!"); return; } }
/// <summary> /// 打开地图文档 /// </summary> /// <param name="axMapControl"></param> public void loadMapDoc(AxMapControl axMapControl) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "打开地图文档"; openFileDialog1.Filter = "地图文档(*.mxd)|*.mxd";//设置过滤属性 if (openFileDialog1.ShowDialog() != DialogResult.OK) { return; //未选择文件return } string filePath = openFileDialog1.FileName; //获取到文件路径 if (axMapControl.CheckMxFile(filePath)) //检查路径是否合法 { try { axMapControl.LoadMxFile(filePath, 0, Type.Missing); } catch (Exception e) { MessageBox.Show("该地图已损坏或者受保护不能被打开"); } } else { MessageBox.Show(filePath + "不是有效的地图文档路径"); return; } axMapControl.Refresh(); }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { if (!ModMxd._MxdPath.Equals("")) { DialogResult pResult = MessageBox.Show("是否保存当前的地图文档?", "询问", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); switch (pResult) { case DialogResult.Cancel: return; case DialogResult.Yes: { IMxdContents pMxdC; pMxdC = m_hookHelper.FocusMap as IMxdContents; IMapDocument pMapDocument = new MapDocumentClass(); //打开地图文档 if (File.Exists(ModMxd._MxdPath)) { pMapDocument.Open(ModMxd._MxdPath, ""); } else { pMapDocument.New(ModMxd._MxdPath); } //保存信息 IActiveView pActiveView = m_hookHelper.ActiveView; pMapDocument.ReplaceContents(pMxdC); pMapDocument.Save(true, true); break; } case DialogResult.No: break; } } OpenFileDialog pOpendlg = new OpenFileDialog(); pOpendlg.Title = "打开地图文档"; pOpendlg.Filter = "(*.mxd)|*.mxd"; if (pOpendlg.ShowDialog() != DialogResult.OK) { return; } string strMxdName = pOpendlg.FileName; if (m_AxMapControl.CheckMxFile(strMxdName)) { m_AxMapControl.LoadMxFile(strMxdName, "", ""); } ModMxd._MxdPath = strMxdName; }
/// <summary> /// 加载地图mxd /// </summary> /// <param name="mapControl"></param> /// <param name="mapFileName"></param> /// <returns></returns> public static bool LoadMxd(AxMapControl mapControl, string mapFileName) { if (mapControl.CheckMxFile(mapFileName)) { mapControl.LoadMxFile(mapFileName); return(true); } return(false); }
private string LoadMapFileInPreviewControl(string fileName) { string msg = string.Empty; if (File.Exists(fileName)) { string ext = Path.GetExtension(fileName).ToLower(); if (ext == ".mxd") { if (mapControl.CheckMxFile(fileName)) { try { mapControl.LoadMxFile(fileName); mapControl.Extent = mapControl.FullExtent; } catch (Exception ex) { msg = "ESRI Map Control generated an error.\nFile: " + fileName + "\nError: " + ex; } } else { msg = "Map document not valid: " + fileName; } } else if (ext == ".lyr") { try { mapControl.ClearLayers(); mapControl.SpatialReference = null; mapControl.AddLayerFromFile(fileName); mapControl.get_Layer(0).Visible = true; //Make sure the layer is visible //Set the Spatial Ref to match the current layer, not the previous layer. //mapControl.SpatialReference = mapControl.get_Layer(0).SpatialReference; mapControl.Extent = mapControl.FullExtent; } catch (Exception ex) { msg = "ESRI Map Control generated an error.\nFile: " + fileName + "\nError: " + ex; } } else { msg = "File must be a map document (.mxd) or a layer file (.lyr): " + fileName; } } else { msg = "File not found: " + fileName; } return(msg); }
//打开地图文档 public void OpenMapDocument(AxMapControl axMapControl1) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Title = "打开地图文档"; openFileDialog.Filter = "地图文档(*.mxd)|*.mxd"; openFileDialog.ShowDialog(); string sFilePath = openFileDialog.FileName; if (axMapControl1.CheckMxFile(sFilePath)) { axMapControl1.LoadMxFile(sFilePath, 0, Type.Missing); } else { MessageBox.Show(sFilePath + "不是有效的地图文档路径"); return; } axMapControl1.Refresh(); }
//加载地图文档 private void loadMapDocument() { System.Windows.Forms.OpenFileDialog openFileDialog; openFileDialog = new OpenFileDialog(); openFileDialog.Title = "打开地图文档"; openFileDialog.Filter = "map documents(*.mxd)|*.mxd"; openFileDialog.ShowDialog(); string filePath = openFileDialog.FileName; if (axMapControl1.CheckMxFile(filePath)) { axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass; axMapControl1.LoadMxFile(filePath, 0, Type.Missing); axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; } else { MessageBox.Show(filePath + "不是有效的地图文档"); } }
//加载Mxd文件函数 private void addmxdfile(string fullfilepath, AxMapControl axMapControl1) { if (fullfilepath == "") { return; } if (axMapControl1.CheckMxFile(fullfilepath)) { axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass; axMapControl1.LoadMxFile(fullfilepath, 0, Type.Missing); IActiveView activeViw = axMapControl1.Map as IActiveView; activeViw.Extent = axMapControl1.FullExtent; axMapControl1.Refresh(); axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault; } else if (fullfilepath != "") { MessageBox.Show(fullfilepath + "是无效的地图文档"); } }
//是否保存当前地图 public void IfSaveTheCurrentMap(AxMapControl axMapControl1) { if (axMapControl1.LayerCount > 0) { DialogResult result = MessageBox.Show("是否保存当前地图?", "警告", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Cancel) { return; } if (result == DialogResult.Yes) { if (null != axMapControl1.DocumentFilename && axMapControl1.CheckMxFile(axMapControl1.DocumentFilename)) { // 创建一个新的地图文档实例 IMapDocument mapDoc = new MapDocumentClass(); // 打开当前地图文档 mapDoc.Open(axMapControl1.DocumentFilename, string.Empty); // 保存地图文档 mapDoc.Save(mapDoc.UsesRelativePaths, false); mapDoc.Close(); } } } }
//打开文件函数 public static void OpenFile(AxMapControl mapControl, AxMapControl mapControl2, AxPageLayoutControl pageLayoutControl) { OpenFileDialog OpenFdlg = new OpenFileDialog(); OpenFdlg.Title = "选择需要打开的文件"; OpenFdlg.Filter = "mxd文件|*.mxd|Shape文件|*.shp|IMG文件|*.img|TIF文件|*.tif|所有文件|*.*"; OpenFdlg.RestoreDirectory = true; if (OpenFdlg.ShowDialog() == DialogResult.OK) { string strFileName = OpenFdlg.FileName;//完全名 if (strFileName == string.Empty) { return; } string pathName = System.IO.Path.GetDirectoryName(strFileName); //位置 string strFExtendN = System.IO.Path.GetExtension(strFileName); //后缀名 string fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName); //单独的文件名 string fileNameE = System.IO.Path.GetFileName(strFileName); //文件名和扩展名 switch (strFExtendN) { case ".shp": mapControl.AddShapeFile(pathName, fileName); OperateFile.CopyAndOverwriteMap(mapControl, pageLayoutControl); break; case ".mxd": if (mapControl.CheckMxFile(strFileName)) { mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass; mapControl.LoadMxFile(strFileName, 0, Type.Missing); mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault; OperateFile.CopyAndOverwriteMap(mapControl, pageLayoutControl); } else { MessageBox.Show("所选文件不是地图文档文件!", "信息提示"); return; } break; case ".bmp": case ".BMP": case ".tif": case ".TIF": case ".jpg": case ".JPG": case ".img": case ".IMG": case ".png": case ".PNG": IWorkspaceFactory pWSF; pWSF = new RasterWorkspaceFactory(); IWorkspace pWS; pWS = pWSF.OpenFromFile(pathName, 0); IRasterWorkspace pRWS; pRWS = pWS as IRasterWorkspace; IRasterDataset pRasterDataset; pRasterDataset = pRWS.OpenRasterDataset(fileNameE); //影像金字塔判断与创建 IRasterPyramid pRasPyrmid; pRasPyrmid = pRasterDataset as IRasterDataset as IRasterPyramid; if (pRasPyrmid != null) { if (!(pRasPyrmid.Present)) { pRasPyrmid.Create(); //在进度条中说明正在创建金字塔 } } IRaster pRaster; pRaster = pRasterDataset.CreateDefaultRaster(); IRasterLayer pRasterLayer; pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromRaster(pRaster); ILayer pLayer = pRasterLayer as ILayer; mapControl.AddLayer(pLayer, 0); OperateFile.CopyAndOverwriteMap(mapControl, pageLayoutControl); OperateFile.ArchieveEagleEyeWhenAddRaster(mapControl2, strFileName); break; } } }
//IMapDocument pMapDocument; /// <summary> /// 打开工程文档 /// </summary> /// <param name="pAxMapControl">axMapControl</param> private void loadMapDocument(AxMapControl pAxMapControl) { OpenFileDialog openFileDialog; openFileDialog = new OpenFileDialog(); openFileDialog.Title = "打开工程"; openFileDialog.Filter = "工程文档(*.mxd)|*.mxd"; if (openFileDialog.ShowDialog() == DialogResult.OK) { string sFilePath = openFileDialog.FileName; //需要添加 判断原来的AxMapControl中是不是有地图 if (pAxMapControl.CheckMxFile(sFilePath)) { pAxMapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass; pAxMapControl.LoadMxFile(sFilePath, 0, Type.Missing); pAxMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault; //加载鹰眼地图 //loadEagleEyeDocument(filePath); //pAxMapControl.Extent = pAxMapControl.FullExtent; } else { MessageBox.Show(sFilePath + "不是有效的工程文档!"); } } }
public static void LoadGeoData(AxMapControl axMapControl1, AxMapControl axMapControl2, string strFileN) { string strFExtenN = System.IO.Path.GetExtension(strFileN); switch (strFExtenN) { case ".shp": { string strPath = System.IO.Path.GetDirectoryName(strFileN); string strFile = System.IO.Path.GetFileNameWithoutExtension(strFileN); axMapControl1.AddShapeFile(strPath, strFile); axMapControl2.ClearLayers(); axMapControl2.AddShapeFile(strPath, strFile); axMapControl2.Extent = axMapControl2.FullExtent; break; } case ".bmp": case ".tif": case ".jpg": case ".img": { IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); string pathName = System.IO.Path.GetDirectoryName(strFileN); string fileName = System.IO.Path.GetFileName(strFileN); IWorkspace pWS = pWSF.OpenFromFile(pathName, 0); IRasterWorkspace pRWS = pWS as IRasterWorkspace; IRasterDataset pRasterDataSet = pRWS.OpenRasterDataset(fileName); IRasterPyramid pRasPyramid = pRasterDataSet as IRasterPyramid; if (pRasPyramid != null) { if (!(pRasPyramid.Present)) { pRasPyramid.Create(); } } IRaster pRaster = pRasterDataSet.CreateDefaultRaster(); IRasterLayer pRasterLayer = new RasterLayerClass(); pRasterLayer.CreateFromRaster(pRaster); ILayer pLayer = pRasterLayer as ILayer; axMapControl1.AddLayer(pLayer, 0); axMapControl2.ClearLayers(); axMapControl2.AddLayer(pLayer, 0); axMapControl2.Extent = axMapControl2.FullExtent; break; } case ".mxd": { if (axMapControl1.CheckMxFile(strFExtenN)) { axMapControl1.LoadMxFile(strFExtenN); } else { MessageBox.Show("所选择的文件不是Mxd文件!", "提示信息"); } break; } default: break; } }