private void 加载高光谱文件ToolStripMenuItem_Click(object sender, EventArgs e) { string fullFilePath; OpenFileDialog openFile = new OpenFileDialog(); //openFile.Filter = "数据集|datasets"; openFile.Multiselect = true; if (openFile.ShowDialog() == DialogResult.OK) { fullFilePath = openFile.FileName; //获得文件路径 int index = fullFilePath.LastIndexOf("\\"); string filePath = fullFilePath.Substring(0, index); //获得文件名称 string fileName = fullFilePath.Substring(index + 1); IWorkspaceFactory workspcFac = new RasterWorkspaceFactory(); IRasterWorkspace rasterWorkspc; IRasterDataset rasterDatset = new RasterDatasetClass(); IRasterLayer rasterLay = new RasterLayerClass(); rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace; rasterDatset = rasterWorkspc.OpenRasterDataset(fileName); rasterLay.CreateFromDataset(rasterDatset); //axMapControl1.ClearLayers(); rasterLay.Name = (num).ToString(); num++; axMapControl1.AddLayer(rasterLay); axMapControl1.Refresh(); } }
public static ILayer OpenRasterFile(string spacePath, string fileName) { IWorkspace myWorkspace = rWorkspaceFactory.OpenFromFile(spacePath, 0); IRasterWorkspace rasterWorkspace = myWorkspace as IRasterWorkspace; IRasterDataset rasterDataset = new RasterDatasetClass(); IRasterLayer rasterLayer = new RasterLayerClass(); rasterDataset = rasterWorkspace.OpenRasterDataset(fileName); rasterLayer.CreateFromDataset(rasterDataset); return(rasterLayer); }
private void 加载图像ToolStripMenuItem_Click(object sender, EventArgs e) { //打开选择文件对话框用于选取图形文件 string fullFilePath; //存储打开文件的全路径 //设置OpenFileDialog的属性,使其能打开多种类型文件 OpenFileDialog openFile = new OpenFileDialog(); openFile.Multiselect = true; openFile.Filter = "shape文件(*.shp)|*.shp"; openFile.Filter += "|栅格数据(*.jpg,*.bmp,*.tiff)|*.jpg;*.bmp;*.tiff;*.tif;*.img"; openFile.Title = "打开文件"; if (openFile.ShowDialog() == DialogResult.OK) { fullFilePath = openFile.FileName; //获得文件路径 int index = fullFilePath.LastIndexOf("\\"); string filePath = fullFilePath.Substring(0, index); //获得文件名称 string fileName = fullFilePath.Substring(index + 1); //加载shape文件 if (openFile.FilterIndex == 1) { //打开工作空间工厂 IWorkspaceFactory workspcFac = new ShapefileWorkspaceFactory(); IFeatureWorkspace featureWorkspc; IFeatureLayer featureLay = new FeatureLayerClass(); //打开路径 featureWorkspc = workspcFac.OpenFromFile(filePath, 0) as IFeatureWorkspace; //打开类要素 featureLay.FeatureClass = featureWorkspc.OpenFeatureClass(fileName); featureLay.Name = fileName; //添加图层 axMapControl1.AddLayer(featureLay); axMapControl1.Refresh(); } //加载栅格图像 else if (openFile.FilterIndex == 2) { IWorkspaceFactory workspcFac = new RasterWorkspaceFactory(); IRasterWorkspace rasterWorkspc; IRasterDataset rasterDatset = new RasterDatasetClass(); IRasterLayer rasterLay = new RasterLayerClass(); rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace; rasterDatset = rasterWorkspc.OpenRasterDataset(fileName); rasterLay.CreateFromDataset(rasterDatset); axMapControl1.ClearLayers(); rasterLay.Name = fileName; axMapControl1.AddLayer(rasterLay); axMapControl1.Refresh(); } } }
public static ILayer OpenRasterFile(string fullFilePath) { String path = System.IO.Path.GetDirectoryName(fullFilePath); //路径 String _name = System.IO.Path.GetFileName(fullFilePath); //文件名 IWorkspace myWorkspace = rWorkspaceFactory.OpenFromFile(path, 0); IRasterWorkspace rasterWorkspace = myWorkspace as IRasterWorkspace; IRasterDataset rasterDataset = new RasterDatasetClass(); IRasterLayer rasterLayer = new RasterLayerClass(); rasterDataset = rasterWorkspace.OpenRasterDataset(_name); rasterLayer.CreateFromDataset(rasterDataset); return(rasterLayer); }
public void RasterReadTest() { new ArcEngineLicense(); //new SpatialAdjust().Georef(); IRasterDataset rasterDataset = new RasterDatasetClass(); rasterDataset.OpenFromFile(@"C:\test\v2\wsiearth.tif"); //rasterDataset.Format; var format = rasterDataset.Format; Trace.WriteLine(format); }
/// <summary> /// 获取栅格元数据 /// </summary> private void GetRasterMeta() { textBoxLayerName.Text = pRasterLayer.Name; textBoxDataType.Text = "栅格数据"; textBoxCreateTime.Text = DateTime.Now.ToString("yyyy年MM月dd日 hh时mm分ss秒"); IRaster pRaster = pRasterLayer.Raster; IRasterDataset pRasterDataset = new RasterDatasetClass(); IRasterProps pRasterProps = (IRasterProps)pRaster; textBoxRHeight.Text = pRasterProps.Height.ToString(); textBoxProjection.Text = pRasterProps.SpatialReference.Name; textBoxRWidth.Text = pRasterProps.Width.ToString(); //投影到经纬度 IEnvelope pExtent = pRasterProps.Extent; try { ISpatialReferenceFactory spatialrefFactory = new SpatialReferenceEnvironmentClass(); ISpatialReference pSR = spatialrefFactory.CreateGeographicCoordinateSystem((int)(esriSRGeoCSType.esriSRGeoCS_WGS1984)); pExtent.Project(pSR); } catch { MessageBox.Show("输入文件空间参考有误"); } textBoxRMinX.Text = pExtent.XMin.ToString(); textBoxRMinY.Text = pExtent.YMin.ToString(); textBoxRMaxX.Text = pExtent.XMax.ToString(); textBoxRMaxY.Text = pExtent.YMax.ToString(); IPnt pPnt = pRasterProps.MeanCellSize(); textBoxRResolution.Text = Math.Min(pPnt.X, pPnt.Y).ToString(); try { object nodata = pRasterProps.NoDataValue; if (nodata.GetType().IsArray) { Array a = ((Array)nodata); textBoxRNoDataValue.Text = a.GetValue(0).ToString(); } } catch { MessageBox.Show("无效值获取失败"); } textBoxRBandCount.Text = pRasterLayer.BandCount.ToString(); }
private IRasterLayer SetViewShedRenderer(IRaster pInRaster, string sField, string sPath,double[,] dValue) { try { if (dValue == null) return null; if (sField.Trim() == "") return null; string path=""; string fileName=""; int iPath=sPath.LastIndexOf('\\'); path=sPath.Substring(0,iPath); fileName=sPath.Substring(iPath+1,sPath.Length-iPath-1); IRasterDescriptor pRD = new RasterDescriptorClass(); pRD.Create(pInRaster, null, sField); IReclassOp pReclassOp = new RasterReclassOpClass(); IGeoDataset pGeodataset = pInRaster as IGeoDataset; IRasterAnalysisEnvironment pEnv = pReclassOp as IRasterAnalysisEnvironment; IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); IWorkspace pWS = pWSF.OpenFromFile(path, 0); //pEnv.OutWorkspace = pWS; //object objSnap = null; //object objExtent = pGeodataset.Extent; //pEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref objExtent, ref objSnap); //pEnv.OutSpatialReference = pGeodataset.SpatialReference; IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection; IRasterBand pRasterBand = pRsBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic = pRasterBand.Statistics; double dMaxValue = pRasterStatistic.Maximum; double dMinValue = pRasterStatistic.Minimum; INumberRemap pNumRemap = new NumberRemapClass(); for (int i = 0; i < (dValue.Length/3); i++) { pNumRemap.MapRange(dValue[i, 0], dValue[i, 1], (Int32)dValue[i, 2]); } IRemap pRemap = pNumRemap as IRemap; IGeoDataset pGeoDs = new RasterDatasetClass(); pGeoDs=pReclassOp.ReclassByRemap(pGeodataset, pRemap, false); IRasterBandCollection pRasBandCol = (IRasterBandCollection)pGeoDs; pRasBandCol.SaveAs(fileName, pWS, "GRID");//"IMAGINE Image" IRaster pOutRaster =pGeoDs as IRaster; IRasterLayer pRLayer = new RasterLayerClass(); pRLayer.CreateFromRaster(pOutRaster); pRLayer.Name = fileName; return pRLayer; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return null; } }
private IRasterLayer calWSCons(string _outPath) { //��դ�� pMapAlgebraOp try { if (pMapAlgebraOp==null) { pMapAlgebraOp = new RasterMapAlgebraOpClass(); } string strExp = ""; string sRainPath = ""; //��ֱ���ý�����ʴ�����ݻ��ǽ������ݼ��� IRaster pRasterR = null; if (rbtnR.Checked) { pRasterR = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbR.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterR as IGeoDataset, "R"); } else { sRainPath = this.txtPcpPath.Text; // IWorkspaceFactory pWsF = new RasterWorkspaceFactoryClass(); IRasterWorkspace pRWs; IRasterDataset pRDs = new RasterDatasetClass(); IRasterLayer pRLyr = new RasterLayerClass(); pRWs = pWsF.OpenFromFile(sRainPath, 0) as IRasterWorkspace; string sFileName=""; for (int i = 1; i < 13; i++) { sFileName = this.txtPcpPrefix.Text + i.ToString() + this.txtPcpSuffix.Text; pRDs = pRWs.OpenRasterDataset(sFileName); pMapAlgebraOp.BindRaster(pRDs as IGeoDataset, sFileName); strExp = strExp + "[" + sFileName + "] + "; } //cal total pcp strExp = strExp.TrimEnd("+ ".ToCharArray()); IGeoDataset pGeoDsPcp = pMapAlgebraOp.Execute(strExp); pMapAlgebraOp.BindRaster(pGeoDsPcp, "Pcp"); //cal Ri strExp = ""; string sFileNameE = "",sDsName="",strExpR=""; for (int i = 1; i < 13; i++) { sFileName = this.txtPcpPrefix.Text + i.ToString() + this.txtPcpSuffix.Text; sFileNameE="["+sFileName+"]"; strExp = "1.735 * pow(10,1.5 * log10((" + sFileNameE + " * " + sFileNameE + ") / [Pcp]) - 0.08188)"; IGeoDataset pGeoDsRi = pMapAlgebraOp.Execute(strExp); sDsName = "Pcp" + i.ToString(); pMapAlgebraOp.BindRaster(pGeoDsRi, sDsName); pMapAlgebraOp.UnbindRaster(sFileName); strExpR = strExpR + "[" + sDsName + "] + "; } //cal R strExpR = strExpR.TrimEnd("+ ".ToCharArray()); IGeoDataset pGeoDsR = pMapAlgebraOp.Execute(strExpR); pMapAlgebraOp.BindRaster(pGeoDsR, "R"); for (int i = 1; i < 13; i++) { sDsName = "Pcp" + i.ToString(); pMapAlgebraOp.UnbindRaster(sDsName); } } //����K���� IRaster pRasterSclay = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilClay.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterSclay as IGeoDataset, "clay"); IRaster pRasterSsand = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilSand.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterSsand as IGeoDataset, "sand"); IRaster pRasterSslit = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilSlit.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterSslit as IGeoDataset, "slit"); IRaster pRasterSOrg = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbSoilOrganic.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterSOrg as IGeoDataset, "org"); //cal K //K =(0.2 + 0.3 * exp(-0.0256 * soil_sand * (1.0 - soil_silt / 100.0))) * pow((soil_silt * 1.0 / (soil_clay * 1.0 + soil_silt * 1.0)),0.3) * (1.0 - 0.25 * soil_oc / (soil_oc * 1.0 + exp(3.72 - 2.95 * soil_oc))) * (1.0 - (0.7 * ksd) / (ksd + exp(-5.51 + 22.9 * ksd))) strExp = "(0.2 + 0.3 * Exp(-0.0256 * [sand] * (1.0 - [slit] / 100.0))) * Pow(([slit] * 1.0 / ([clay] * 1.0 + [slit] * 1.0)), 0.3) * (1.0 - 0.25 * [org] * 0.58 / ([org] * 0.58 + Exp(3.72 - 2.95 * [org] * 0.58))) * (1.0 - (0.7 * (1.0 - [sand] / 100.0)) / ((1.0 - [sand] / 100.0) + Exp(-5.51 + 22.9 * (1.0 - [sand] / 100.0))))"; IGeoDataset pGeoDsK = pMapAlgebraOp.Execute(strExp); pMapAlgebraOp.BindRaster(pGeoDsK, "K"); pMapAlgebraOp.UnbindRaster("clay"); pMapAlgebraOp.UnbindRaster("sand"); pMapAlgebraOp.UnbindRaster("slit"); pMapAlgebraOp.UnbindRaster("org"); //cal L*S IHydrologyOp pHydrologyOp = new RasterHydrologyOpClass(); object objZLimit = System.Type.Missing; IRaster pRasterDem = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbDem.Text)) as IRasterLayer).Raster; //Fill Dem IGeoDataset pGeoDsDemFill =pHydrologyOp.Fill(pRasterDem as IGeoDataset,ref objZLimit); // cal FlowDirection IGeoDataset pGeoDsFlowDir = pHydrologyOp.Fill(pGeoDsDemFill, ref objZLimit); //cal FlowAccumulation IGeoDataset pGeoDsFlowAcc = pHydrologyOp.Fill(pGeoDsFlowDir, ref objZLimit); pMapAlgebraOp.BindRaster(pGeoDsFlowAcc, "FlowAcc"); //cal Slope with Deg ISurfaceOp pSurfaceOp = new RasterSurfaceOpClass(); object objZFactor = System.Type.Missing; IGeoDataset pGeoDsSlope = pSurfaceOp.Slope(pGeoDsDemFill, esriGeoAnalysisSlopeEnum.esriGeoAnalysisSlopeDegrees, ref objZFactor); // bind raster data "Slope" pMapAlgebraOp.BindRaster(pGeoDsSlope, "Slope"); //cal S strExp = "Con([Slope] < 5,10.8 * Sin([Slope] * 3.14 / 180) + 0.03,[Slope] >= 10,21.9 * Sin([Slope] * 3.14 / 180) - 0.96,16.8 * Sin([Slope] * 3.14 / 180) - 0.5)"; IGeoDataset pGeoDsS = pMapAlgebraOp.Execute(strExp); pMapAlgebraOp.BindRaster(pGeoDsS, "S"); //cal m strExp = "Con([Slope] <= 1,0.2,([Slope] > 1 & [Slope] <= 3),0.3,[Slope] >= 5,0.5,0.4)"; IGeoDataset pGeoDsM = pMapAlgebraOp.Execute(strExp); pMapAlgebraOp.BindRaster(pGeoDsM, "m"); //cal ls strExp = "[S] * Pow(([FlowAcc] * "+this.txtCellSize.Text+" / 22.1),[m])"; IGeoDataset pGeoDsLS = pMapAlgebraOp.Execute(strExp); pMapAlgebraOp.BindRaster(pGeoDsLS, "LS"); pMapAlgebraOp.UnbindRaster("m"); pMapAlgebraOp.UnbindRaster("S"); pMapAlgebraOp.UnbindRaster("Slope"); pMapAlgebraOp.UnbindRaster("FlowAcc"); IRaster pRasterC = null; if (rbtnVegCover.Checked) { pRasterC = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbC.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterC as IGeoDataset, "C"); } else { //cal vegetation cover IRaster pRasterNDVI = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbC.Text)) as IRasterLayer).Raster; IRasterBandCollection pRasterBandCollection = pRasterNDVI as IRasterBandCollection; IRasterBand pRasterBand = pRasterBandCollection.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistics = pRasterBand.Statistics; double dMax = pRasterStatistics.Maximum; double dMin = pRasterStatistics.Minimum; pMapAlgebraOp.BindRaster(pRasterNDVI as IGeoDataset, "NDVI"); if (dMin < 0) { dMin = 0; } //veg%yr% = (ndvi%yr% - ndvi%yr%min) / (ndvi%yr%max - ndvi%yr%min) strExp = "([NDVI] - "+dMin+") / ("+dMax +" - "+dMin+")"; IGeoDataset pGeoDsC = pMapAlgebraOp.Execute(strExp); pMapAlgebraOp.BindRaster(pGeoDsC, "C"); pMapAlgebraOp.UnbindRaster("NDVI"); } //����P���� IRaster pRasterP = (pMap.get_Layer(LayerOprate.getLayerIndexByName(pMap, this.cmbP.Text)) as IRasterLayer).Raster; pMapAlgebraOp.BindRaster(pRasterP as IGeoDataset, "P"); if (_outPath != "") { strExp = _outPath + " = [R] * [K] * [LS] * (1 - [C]) * [P]"; } else { strExp = "[R] * [K] * [LS] * (1 - [C]) * [P]"; } IGeoDataset pGeoDsSr = pMapAlgebraOp.Execute(strExp); IRaster pOutRaster = pGeoDsSr as IRaster; IRasterLayer pOutRasterLayer = new RasterLayerClass(); pOutRasterLayer.CreateFromRaster(pOutRaster); //�������� //string strOutDir = _outPath.Substring(0, _outPath.LastIndexOf("\\")); int startX = _outPath.LastIndexOf("\\"); int endX = _outPath.Length; string sRLyrName = _outPath.Substring(startX + 1, endX - startX - 1); pOutRasterLayer.Name = sRLyrName; pMapAlgebraOp = null; return pOutRasterLayer; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return null; } }
//դ���ط��� private IRasterLayer calMValve(IRaster pInRaster, string sField) { try { if (sField.Trim() == "") return null; IRasterDescriptor pRD = new RasterDescriptorClass(); pRD.Create(pInRaster, null, sField); IReclassOp pReclassOp = new RasterReclassOpClass(); IGeoDataset pGeodataset = pInRaster as IGeoDataset; IRasterAnalysisEnvironment pEnv = pReclassOp as IRasterAnalysisEnvironment; IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection; IRasterBand pRasterBand = pRsBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic = pRasterBand.Statistics; double dMaxValue = pRasterStatistic.Maximum; double dMinValue = pRasterStatistic.Minimum; INumberRemap pNumRemap = new NumberRemapClass(); //m���������� ���¶�Ϊ 0-7.5; 7.5-12.5; 12.5-17.5; 17.5--22.5; >22.5; //mֵ�ֱ�Ϊ�� 0.1;0.15;0.2;0.25,0.3 //��ֵ������100�� pNumRemap.MapRange(dMinValue, 7.5, 10); pNumRemap.MapRange(7.5, 12.5, 15); pNumRemap.MapRange(12.5, 17.5, 20); pNumRemap.MapRange(17.5, 22.5, 25); pNumRemap.MapRange(22.5, dMaxValue, 30); IRemap pRemap = pNumRemap as IRemap; IGeoDataset pGeoDs = new RasterDatasetClass(); pGeoDs = pReclassOp.ReclassByRemap(pGeodataset, pRemap, false); IRaster pOutRaster = pGeoDs as IRaster; IRasterLayer pRLayer = new RasterLayerClass(); pRLayer.CreateFromRaster(pOutRaster); return pRLayer; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return null; } }
private IRasterLayer SetViewShedRenderer(IRaster pInRaster, string sField, string sPath, double[,] dValue) { try { if (dValue == null) { return(null); } if (sField.Trim() == "") { return(null); } string path = ""; string fileName = ""; int iPath = sPath.LastIndexOf('\\'); path = sPath.Substring(0, iPath); fileName = sPath.Substring(iPath + 1, sPath.Length - iPath - 1); IRasterDescriptor pRD = new RasterDescriptorClass(); pRD.Create(pInRaster, null, sField); IReclassOp pReclassOp = new RasterReclassOpClass(); IGeoDataset pGeodataset = pInRaster as IGeoDataset; IRasterAnalysisEnvironment pEnv = pReclassOp as IRasterAnalysisEnvironment; IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); IWorkspace pWS = pWSF.OpenFromFile(path, 0); //pEnv.OutWorkspace = pWS; //object objSnap = null; //object objExtent = pGeodataset.Extent; //pEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref objExtent, ref objSnap); //pEnv.OutSpatialReference = pGeodataset.SpatialReference; IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection; IRasterBand pRasterBand = pRsBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic = pRasterBand.Statistics; double dMaxValue = pRasterStatistic.Maximum; double dMinValue = pRasterStatistic.Minimum; INumberRemap pNumRemap = new NumberRemapClass(); for (int i = 0; i < (dValue.Length / 3); i++) { pNumRemap.MapRange(dValue[i, 0], dValue[i, 1], (Int32)dValue[i, 2]); } IRemap pRemap = pNumRemap as IRemap; IGeoDataset pGeoDs = new RasterDatasetClass(); pGeoDs = pReclassOp.ReclassByRemap(pGeodataset, pRemap, false); IRasterBandCollection pRasBandCol = (IRasterBandCollection)pGeoDs; pRasBandCol.SaveAs(fileName, pWS, "GRID");//"IMAGINE Image" IRaster pOutRaster = pGeoDs as IRaster; IRasterLayer pRLayer = new RasterLayerClass(); pRLayer.CreateFromRaster(pOutRaster); pRLayer.Name = fileName; return(pRLayer); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return(null); } }
private void button1_Click(object sender, EventArgs e) { //存储打开文件的全路径 string fullFilePath; //设置OpenFileDialog的属性,使其能打开多种类型文件 OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "shape文件(*.shp)|*.shp|栅格数据(*.img,*.tiff)|*.img;*.tiff|Personal Geodatabase(*.mdb)|*.mdb|地图文档(*.mxd)|*.mxd|All Files(*.*)|*.*"; openFile.Title = "打开文件"; try { if (openFile.ShowDialog() == DialogResult.OK) { fullFilePath = openFile.FileName; //获得文件路径 int index = fullFilePath.LastIndexOf("\\"); string filePath = fullFilePath.Substring(0, index); int loc2 = fullFilePath.LastIndexOf("."); //获得文件名称 string fileNam = fullFilePath.Substring(index + 1); //加载shape文件 if (openFile.FilterIndex == 1) { //打开工作空间工厂 IWorkspaceFactory workspcFac = new ShapefileWorkspaceFactory(); IFeatureWorkspace featureWorkspc; IFeatureLayer featureLay = new FeatureLayerClass(); //打开路径 featureWorkspc = workspcFac.OpenFromFile(filePath, 0) as IFeatureWorkspace; //打开类要素 featureLay.FeatureClass = featureWorkspc.OpenFeatureClass(fileNam); String fname; fname = fullFilePath.Substring(index + 1, loc2 - index - 1); listBox1.Items.Insert(0, fname); frm.mainMapControl.ClearLayers(); //////注意与主函数MainForm中区别,此处要加"frm." //添加图层 frm.mainMapControl.AddLayer(featureLay); //////注意与主函数MainForm中区别,此处要加"frm." frm.mainMapControl.Refresh(); //////注意与主函数MainForm中区别,此处要加"frm." } //加载栅格图像 else if (openFile.FilterIndex == 2) { IWorkspaceFactory workspcFac = new RasterWorkspaceFactory(); IRasterWorkspace rasterWorkspc; IRasterDataset rasterDatst = new RasterDatasetClass(); IRasterLayer rasterLay = new RasterLayerClass(); rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace; rasterDatst = rasterWorkspc.OpenRasterDataset(fileNam); rasterLay.CreateFromDataset(rasterDatst); String fname = fullFilePath.Substring(index + 1, loc2 - index - 1); listBox1.Items.Insert(0, fname); ////////frm.axMapControl1.ClearLayers();//////注意与主函数MainForm中区别,此处要加"frm." //添加图层 frm.mainMapControl.AddLayer(rasterLay); //////注意与主函数MainForm中区别,此处要加"frm." frm.mainMapControl.Refresh(); //////注意与主函数MainForm中区别,此处要加"frm." } //加载地图文档 else if (openFile.FilterIndex == 3) { IMapDocument mapDoc = new MapDocumentClass(); mapDoc.Open(filePath, ""); frm.mainMapControl.ClearLayers();//////注意与主函数MainForm中区别,此处axMapControl1前要加"frm." for (int i = 0; i < mapDoc.MapCount - 1; i++) { frm.mainMapControl.Map = mapDoc.get_Map(i); } IActiveView activeViw = frm.mainMapControl.Map as IActiveView; activeViw.Extent = frm.mainMapControl.FullExtent; frm.mainMapControl.Refresh(); } } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }