环境:Windows10专业版 + ArcGIS10.2 + ArcEngine10.2 + Visual Studio 2019
因每个人电脑版本和软件版本不同,运行的结果可能不同
系列文章:
目录
[3.6 打开地图文档](#3.6 打开地图文档)
[3.6.1 实现思想](#3.6.1 实现思想)
[3.6.2 实现的主体代码及注释](#3.6.2 实现的主体代码及注释)
[3.7 保存地图文档](#3.7 保存地图文档)
[3.7.1 实现思想](#3.7.1 实现思想)
[3.7.2 实现的主体代码及注释](#3.7.2 实现的主体代码及注释)
[3.8 另存为地图文档](#3.8 另存为地图文档)
[3.8.1 实现思想](#3.8.1 实现思想)
[3.8.2 实现的主体代码及注释](#3.8.2 实现的主体代码及注释)
[3.9 添加.shp](#3.9 添加.shp)
[3.9.1 实现思想](#3.9.1 实现思想)
[3.9.2 实现的主体代码及注释](#3.9.2 实现的主体代码及注释)
[3.10 添加.lyr](#3.10 添加.lyr)
[3.10.1 实现思想](#3.10.1 实现思想)
[3.10.2 实现的主体代码及注释](#3.10.2 实现的主体代码及注释)
[3.11 添加栅格数据](#3.11 添加栅格数据)
[3.11.1 实现思想](#3.11.1 实现思想)
[3.11.2 实现的主体代码及注释](#3.11.2 实现的主体代码及注释)
[3.12 退出](#3.12 退出)
[3.12.1 实现思想](#3.12.1 实现思想)
[3.12.2 实现的主体代码及注释](#3.12.2 实现的主体代码及注释)
[3.13 创建栅格数据集](#3.13 创建栅格数据集)
[3.13.1 实现思想](#3.13.1 实现思想)
[3.13.2 实现的主体代码及注](#3.13.2 实现的主体代码及注)
三、功能实现
3.6 打开地图文档
3.6.1 实现思想
(1)主窗口添加"打开地图文档"控件。
(2)新建类MxdOperator,打开地图文档对话框,读取数据源类型,用LoadMxFile()方法打开地图文档。
(3)为"打开地图文档"控件生成点击事件响应函数
3.6.2 实现的主体代码及注释
cs
public void OpenMxd(ESRI.ArcGIS.Controls.AxMapControl basicControl)
{
//打开地图文档对话框,读取数据源类型
OpenFileDialog pOpenFileDialog = new OpenFileDialog();
pOpenFileDialog.Filter = "ArcMap文档(*.mxd)|*.mxd|All Files (*.*)|*.*||";
pOpenFileDialog.Title = "打开地图文档";
pOpenFileDialog.RestoreDirectory = true;
if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
{
if (System.IO.Path.GetExtension(pOpenFileDialog.FileName) == ".mxd")
{
if (basicControl.CheckMxFile(pOpenFileDialog.FileName)) //检查地图文档有效性
{
basicControl.LoadMxFile(pOpenFileDialog.FileName);
}
else
{
MessageBox.Show(pOpenFileDialog.FileName + "是无效的地图文档!", "信息提示");
return;
}
}
}
else
{
return;
}
}
//打开地图文档
private void openMxd_Click(object sender, EventArgs e)
{
MxdOperator mxdOperator = new MxdOperator();
mxdOperator.OpenMxd(axMapControl1);
}
3.7 保存地图文档
3.7.1 实现思想
(1)主窗口添加"保存"控件。
(2)新建类MxdOperator,用Save()方法保存地图文档。
(3)为"保存"控件生成点击事件响应函数。
3.7.2 实现的主体代码及注释
cs
//保存
public void SaveMxd(ESRI.ArcGIS.Controls.AxMapControl basicControl)
{
try
{
if (basicControl.LayerCount == 0)
{
MessageBox.Show("无可用的地图文档!");
return;
}
string sMxdFileName = basicControl.DocumentFilename;
string fvg = sMxdFileName;
if (sMxdFileName == null)
{
IMapDocument pMapDocument = new MapDocumentClass();
if (sMxdFileName != null && basicControl.CheckMxFile(sMxdFileName))
{
if (pMapDocument.get_IsReadOnly(sMxdFileName))
MessageBox.Show("本地图文档是只读的,不能保存!");
pMapDocument.Close();
return;
}
else
{
SaveFileDialog pSavedlg = new SaveFileDialog();
pSavedlg.Title = "请选择保存路径";
pSavedlg.OverwritePrompt = true;
pSavedlg.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
pSavedlg.RestoreDirectory = true;
if (pSavedlg.ShowDialog() == DialogResult.OK)
{
sMxdFileName = pSavedlg.FileName;
}
else
{
return;
}
}
pMapDocument.New(sMxdFileName);
pMapDocument.ReplaceContents(basicControl.Map as IMxdContents);
pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
pMapDocument.Close();
MessageBox.Show("另存为地图文档成功!");
}
else
{
IMxdContents pMxdC;
pMxdC = basicControl.Map as IMxdContents;
IMapDocument pMapDocument = new MapDocumentClass();
pMapDocument.Open(basicControl.DocumentFilename, "");
IActiveView pActiveView = basicControl.Map as IActiveView;
pMapDocument.ReplaceContents(pMxdC);
if (pMapDocument == null) return;
//检查地图文档是否是只读
if (pMapDocument.get_IsReadOnly(pMapDocument.DocumentFilename) == true)
{
MessageBox.Show("本地图文档是只读的,不能保存!");
return;
}
//根据相对的路径保存地图文档
pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
MessageBox.Show("地图文档保存成功!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//保存
private void saveMxd_Click(object sender, EventArgs e)
{
MxdOperator mxdOperator = new MxdOperator();
mxdOperator.SaveMxd(axMapControl1);
}
3.8 另存为地图文档
3.8.1 实现思想
(1)主窗口添加"保存"控件。
(2)新建类MxdOperator,用MapDocumentClass()新建地图文档,用SaveFileDialog()方法确定保存的路径和结果,用Save()方法保存地图文档。
(3)为"另存为"控件生成点击事件响应函数。
3.8.2 实现的主体代码及注释
cs
//另存为
public void SaveAs(ESRI.ArcGIS.Controls.AxMapControl basicControl)
{
try
{
if (basicControl.LayerCount == 0)
{
MessageBox.Show("无可用的地图文档!");
return;
}
string sMxdFileName = basicControl.DocumentFilename;
IMapDocument pMapDocument = new MapDocumentClass();
if (sMxdFileName != null && basicControl.CheckMxFile(sMxdFileName))
{
if (pMapDocument.get_IsReadOnly(sMxdFileName))
{
MessageBox.Show("本地图文档是只读的,不能保存!");
pMapDocument.Close();
return;
}
else
{
SaveFileDialog pSavedlg = new SaveFileDialog();
pSavedlg.Title = "请选择保存路径";
pSavedlg.OverwritePrompt = true;
pSavedlg.Filter = "ArcMap文档(*.mxd)|*.mxd|ArcMap模板(*.mxt)|*.mxt";
pSavedlg.RestoreDirectory = true;
if (pSavedlg.ShowDialog() == DialogResult.OK)
{
sMxdFileName = pSavedlg.FileName;
}
else
{
return;
}
}
}
pMapDocument.New(sMxdFileName);
pMapDocument.ReplaceContents(basicControl.Map as IMxdContents);
pMapDocument.Save(pMapDocument.UsesRelativePaths, true);
pMapDocument.Close();
MessageBox.Show("另存为地图文档成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//另存为
private void saveAsMxd_Click(object sender, EventArgs e)
{
MxdOperator mxdOperator = new MxdOperator();
mxdOperator.SaveAs(axMapControl1);
}
3.9 添加.shp
3.9.1 实现思想
(1)在窗口添加"添加.shp"控件。
(2)为"添加.shp"控件生成点击事件响应函数,用AddShapeFile()方法添加shape文件。
3.9.2 实现的主体代码及注释
cs
//shape文件加载
private void loadShape_Click(object sender, EventArgs e)
{
//利用文件对话框的方式选择加载地图
OpenFileDialog openFdlg = new OpenFileDialog();
openFdlg.Title = "选择需要加载的地理数据文件";
openFdlg.Filter = "Shape格式文件(*.shp)|*.shp";
openFdlg.ShowDialog();
String strFileName = openFdlg.FileName;
if (strFileName == String.Empty)//用户没有进行文件选择,则返回
{
return;
}
//用户选择了shape文件
String pathName = System.IO.Path.GetDirectoryName(strFileName);
String fileName = System.IO.Path.GetFileNameWithoutExtension(strFileName);
//往axMapControl1中加载地图
axMapControl1.AddShapeFile(pathName, fileName);
//在鹰眼中加载地图
axMapControl2.ClearLayers();
axMapControl2.AddShapeFile(pathName, fileName);
//显示全图为鸟瞰图
axMapControl2.Extent = axMapControl2.FullExtent;
}
3.10 添加.lyr
3.10.1 实现思想
(1)在窗口添加"添加.lyr"控件。
(2)为"添加.lyr"控件生成点击事件响应函数,用AddLayerFromFile()方法添加lyr文件。
3.10.2 实现的主体代码及注释
cs
//lyr文件加载
private void layerLoad_Click(object sender, EventArgs e)
{
OpenFileDialog OpenFdlg = new OpenFileDialog();
OpenFdlg.Filter = "lyr文件(*.lyr)|*.lyr";
if (OpenFdlg.ShowDialog() == DialogResult.OK)
{
string pFileName = OpenFdlg.FileName;
axMapControl1.AddLayerFromFile(pFileName);
axMapControl1.ActiveView.Refresh();
axMapControl2.ClearLayers();
axMapControl2.AddLayerFromFile(pFileName);
//显示全图为鸟瞰图
axMapControl2.Extent = axMapControl2.FullExtent;
}
}
3.11 添加栅格数据
3.11.1 实现思想
(1)新建"AddRaster"类,使用AddRasterFile()加载栅格文件,使用AddRasterDataset()加载栅格数据集中的栅格文件。
(2)添加"添加栅格数据","添加栅格数据集"控件。
(3)为"添加栅格数据","添加栅格数据集"生成点击事件响应函数。
3.11.2 实现的主体代码及注释
cs
//加载栅格文件
private void loadRasterFile_Click(object sender, EventArgs e)
{
AddRaster addRaster = new AddRaster();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择栅格文件";
openFileDialog.Filter = "TIFF(*.tif)|*.tif";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
addRaster.AddRasterFile(openFileDialog.FileName, axMapControl1);
axMapControl2.ClearLayers();
addRaster.AddRasterFile(openFileDialog.FileName, axMapControl2);
//显示全图为鸟瞰图
axMapControl2.Extent = axMapControl2.FullExtent;
}
}
//加载栅格数据集
public void AddRasterDataset(string directory,ESRI.ArcGIS.Controls.AxMapControl axMapControl1)
{
IWorkspaceFactory pWorkspaceFactory = new FileGDBWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(directory, 0);
IRasterWorkspaceEx pRasterWorkspaceEx = pWorkspace as IRasterWorkspaceEx;
// 获取栅格数据集
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(esriDatasetType.esriDTRasterDataset);
pEnumDataset.Reset();
IDataset pDataset = pEnumDataset.Next();
// 遍历数据集
while (pDataset != null)
{
// 创建金字塔
IRasterDataset pRasterDataset = pRasterWorkspaceEx.OpenRasterDataset(pDataset.Name);
IRasterPyramid pRasterPyramid = pRasterDataset as IRasterPyramid;
if (!pRasterPyramid.Present)
{
pRasterPyramid.Create();
}
// 栅格图层
IRasterLayer pRasterLayer = new RasterLayer();
pRasterLayer.CreateFromDataset(pRasterDataset);
// 刷新视图
axMapControl1.AddLayer(pRasterLayer, 0);
axMapControl1.Refresh();
// 下一数据集
pDataset = pEnumDataset.Next();
}
}
//加载栅格文件
private void loadRasterFile_Click(object sender, EventArgs e)
{
AddRaster addRaster = new AddRaster();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择栅格文件";
openFileDialog.Filter = "TIFF(*.tif)|*.tif";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
addRaster.AddRasterFile(openFileDialog.FileName, axMapControl1);
axMapControl2.ClearLayers();
addRaster.AddRasterFile(openFileDialog.FileName, axMapControl2);
//显示全图为鸟瞰图
axMapControl2.Extent = axMapControl2.FullExtent;
}
}
//加载栅格数据集
private void loadRasterDataset_Click(object sender, EventArgs e)
{
AddRaster addRaster = new AddRaster();
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = "请选择文件地理数据库";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
if (!folderBrowserDialog.SelectedPath.EndsWith(".gdb"))
{
MessageBox.Show("请选择文件地理数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
addRaster.AddRasterDataset(folderBrowserDialog.SelectedPath, axMapControl1);
}
}
}
3.12 退出
3.12.1 实现思想
(1)添加"退出"控件。
(2)为"退出"控件生成点击事件响应函数,调用Application.Exit()方法退出任务。
3.12.2 实现的主体代码及注释
cs
//退出功能
private void Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
3.13 创建栅格数据集
3.13.1 实现思想
(1)添加"创建栅格数据集"控件。
(2)添加"栅格工具类",利用CreateRasterDataset()方法创建栅格数据集。
(3)为"创建栅格数据集"控件生成点击事件响应函数。
3.13.2 实现的主体代码及注释
cs
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DataSourcesRaster;
//打开栅格数据集工作空间
public IRasterWorkspaceEx OpenRasterWorkspaceFromFileGDB(string filePath)
{
IWorkspaceFactory wsFactory = new FileGDBWorkspaceFactoryClass();
IRasterWorkspaceEx ws = (IRasterWorkspaceEx)wsFactory.OpenFromFile(filePath, 0);
return ws;
}
//创建栅格数据集函数
public bool CreateRaster(string filePath, string rasterName)
{
IRasterWorkspaceEx rasterWorksapceEx;
//打开工作空间
rasterWorksapceEx = OpenRasterWorkspaceFromFileGDB(filePath);
//设置存储参数
IRasterStorageDef storageDef = new RasterStorageDef();
storageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG;
//设置栅格列属性
IRasterDef rasterDef = new RasterDef();
//定义空间参考
ISpatialReferenceFactory2 srFactotry = new SpatialReferenceEnvironmentClass();
int gcsType = (int)esriSRGeoCSType.esriSRGeoCS_WGS1984;
IGeographicCoordinateSystem geoCoordSystem = srFactotry.CreateGeographicCoordinateSystem(gcsType);
ISpatialReference spatialRef = (ISpatialReference)geoCoordSystem;
rasterDef.SpatialReference = spatialRef;
//创建栅格数据集
IRasterDataset rasterDataset;
rasterDataset = rasterWorksapceEx.CreateRasterDataset(rasterName, 3, rstPixelType.PT_FLOAT, storageDef, null, rasterDef, null);
return true;
}
//创建栅格数据集
private void miCreateRaster_Click(object sender, EventArgs e)
{
RasterUtil rasterUtil = new RasterUtil();
rasterUtil.CreateRaster("D:\\raster\\Raster.gdb", "rasterForTest");
}
3.14 格式转换
3.14.1 实现思想
(1)添加"格式转换"控件。
(2)在"栅格工具类"中,利用SaveAs()方法另存为图像文件。
(3)为"格式转换"控件生成点击事件响应函数。
3.14.2 实现的主体代码及注释
cs
//打开栅格工作空间
public IRasterWorkspace OpenRasterWorkspaceFromFile(string filePath)
{
IWorkspaceFactory wsFactory = new RasterWorkspaceFactoryClass();
IRasterWorkspace ws = (IRasterWorkspace)wsFactory.OpenFromFile(filePath, 0);
return ws;
}
//栅格数据格式转换
public bool RasterConvert(string fileGDB, string oldRasterName, string filePath, string newRasterName)
{
IWorkspace workspace;
IRasterWorkspaceEx rasterWorksapceEx;
//打开输入工作空间
rasterWorksapceEx = OpenRasterWorkspaceFromFileGDB(fileGDB);
//打开栅格数据集
IRasterDataset rasterDataset = rasterWorksapceEx.OpenRasterDataset(oldRasterName);
//得到栅格波段
IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
//打开输出工作空间
workspace = OpenRasterWorkspaceFromFile(filePath) as IWorkspace;
//另存为给定文件名的图像文件
rasterBands.SaveAs(newRasterName, workspace, "TIFF");
return true;
}
//格式转换
private void miRasterConvert_Click(object sender, EventArgs e)
{
RasterUtil rasterUtil = new RasterUtil();
rasterUtil.RasterConvert("D:\\raster\\Raster.gdb", "rasterForTest", "D:\\raster", "newRaster.tif");
}
3.15 影像镶嵌
3.15.1 实现思想
(1)添加"影像镶嵌"控件。
(2)在"栅格工具类"中,将所有影像到一个输出栅格数据集。
(3)为"影像镶嵌"控件生成点击事件响应函数。
3.15.2 实现的主体代码及注释
cs
//影像镶嵌,将catalogName中的所有栅格影像镶嵌成单个影像,并以outputName为文件
//名保存至outputFolder
public void Mosaic(string GDBName, string catalogName, string outputFolder, string outputName)
{
//打开个人数据库
IWorkspaceFactory workspaceGDBFactory = new AccessWorkspaceFactoryClass();
IWorkspace GDBworkspace = workspaceGDBFactory.OpenFromFile(GDBName, 0);
//打开要被镶嵌的影像所在的栅格目录
IRasterWorkspaceEx rasterWorkspaceEx = (IRasterWorkspaceEx)GDBworkspace;
IRasterCatalog rasterCatalog;
rasterCatalog = rasterWorkspaceEx.OpenRasterCatalog(catalogName);
//定义一个影像镶嵌对象
IMosaicRaster mosaicRaster = new MosaicRasterClass();
//镶嵌栅格目录中的所有影像到一个输出栅格数据集
mosaicRaster.RasterCatalog = rasterCatalog;
//设置镶嵌选项
mosaicRaster.MosaicColormapMode = rstMosaicColormapMode.MM_MATCH;
mosaicRaster.MosaicOperatorType = rstMosaicOperatorType.MT_LAST;
//打开输出栅格数据集所在的工作空间
IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
IWorkspace workspace = workspaceFactory.OpenFromFile(outputFolder, 0);
//保存到目标栅格数据集,数据格式可以是TIFE,ING,GRID,BMP,GIF,JPEG2O00,
//JPEG,Geodatabase等
ISaveAs saveas = (ISaveAs)mosaicRaster;
saveas.SaveAs(outputName, workspace, "TIFF");
}
//图像镶嵌
private void miRasterMosaic_Click(object sender, EventArgs e)
{
RasterUtil rasterUtil = new RasterUtil();
rasterUtil.Mosaic("D:\\raster\\RasterDatabase.mdb", "RasterCatalog", "D:\\raster", "MosaicRaster.tif");
}