private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { OpenFileDialog openDLG = new OpenFileDialog(); //创建打开文件对话框 openDLG.Title = "open Shape File"; // 给对话框的标题赋值 openDLG.Filter = "Raster File(*.*)|*.bmp;*.tif;*.jpg;*.img|(*.bmp)|*.bmp|(*.jpg)|*.jpg|(*.tif)|*.tif|(*.img)|*.img"; // 设定对话框可以打开的文件类型,文件后缀分别为*.bmp;*.tif;*.jpg;*.img if (openDLG.ShowDialog() == DialogResult.OK) // 打开对话框,若对话框操作选择文件完成后选择的是ok按钮,则执行下面的代码 { string sFilePath = openDLG.FileName; // 获取选择的文件的路径和名称 int index = sFilePath.LastIndexOf("\\"); // 判定文件名字符串中的最后一个“\”符号的位置 string sPath = sFilePath.Substring(0, index); //截取文件路径 string sFileName = sFilePath.Substring(index + 1); //截取文件名称 IWorkspaceFactory pWorkspaceFectory = new RasterWorkspaceFactory(); //创建能打开栅格数据文件的工作空间工厂对象 IWorkspace pWorkspace = pWorkspaceFectory.OpenFromFile(sPath, 0); // 用工作空间工厂对象将栅格文件所在的文件夹打开,作为工作空间对象 IRasterWorkspace pRasterWorkspace = pWorkspace as IRasterWorkspace; // 将工作空间接口转换为 栅格工作空间接口 IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(sFileName); // 调用栅格工作空间接口的OpenRasterDataset方法打开指定的栅格数据集 ILayer pLayer = new RasterLayer(); // 创建一个栅格层 IRasterLayer pRasterLayer = pLayer as IRasterLayer; // 获得栅格层接口对象 pRasterLayer.CreateFromDataset(pRasterDataset); //根据刚才打开的栅格数据集进行栅格图层的创建 axMapControl1.Map.AddLayer(pLayer); //将栅格层添加到地图控件的地图中 axMapControl1.ActiveView.Refresh(); //刷新地图 } //添加栅格数据 }
public override void OnDblClick() { IMapControl3 pMapCtrl = ClsGlobal.GetMapControl(m_hookHelper); if (pMapCtrl == null) { if (m_NewPolygonFeedback != null) { m_NewPolygonFeedback.Stop(); m_NewPolygonFeedback = null; return; } } IGeometry pGeometry = m_NewPolygonFeedback.Stop(); m_NewPolygonFeedback = null; if (pGeometry == null) { return; } m_pClipPolygon = pGeometry as IPolygon; double dbNoDataValue = m_pFrmSetRasterNoDataValue.NoDataValue; //获取到无效值区域、无效值、栅格数据之后,开始设置 ClsSetRasterRegionToDataValue pSetRasterToNoDataValue = new ClsSetRasterRegionToDataValue(m_pRasterLayer.Raster, m_pClipPolygon, dbNoDataValue); if (!pSetRasterToNoDataValue.SetRegionToNoDataValue()) { MessageBox.Show("设置无效值出错!"); } else { //MessageBox.Show("设置无效值成功!"); //重新加载栅格数据 try { IRaster2 pSrcRaster2 = m_pRasterLayer.Raster as IRaster2; IRasterDataset pDstRasterDataset = pSrcRaster2.RasterDataset; IRasterPyramid3 pDstRasterPyramid3 = pDstRasterDataset as IRasterPyramid3; if (pDstRasterPyramid3.Present) { IDataLayer2 pDataLayer = m_pRasterLayer as IDataLayer2; pDataLayer.Disconnect(); pDstRasterPyramid3.DeletePyramid(); pDstRasterPyramid3.Create(); m_pRasterLayer.CreateFromDataset(pDstRasterDataset); } IMapControl3 pMapControl3 = ClsGlobal.GetMapControl(m_hookHelper); pMapControl3.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } base.OnDblClick(); }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { // TODO: Add CmdRasterResample.OnClick implementation IMapControl2 pMapCtr = (((IToolbarControl)m_hookHelper.Hook).Buddy) as IMapControl2; if (pMapCtr != null) { m_frmRasterResample = new FrmRasterResample(); IRasterProps pRasterProps = pRasterLayer.Raster as IRasterProps; m_frmRasterResample.Cellsize = pRasterProps.MeanCellSize().X; if (m_frmRasterResample.ShowDialog() == DialogResult.OK) { IGeoDataset pGeodataset = pRasterLayer.Raster as IGeoDataset; IRasterGeometryProc pRasterGeometryProc = new RasterGeometryProcClass(); ITransformationOp pTransOP = new RasterTransformationOpClass(); string name = pRasterLayer.Name; if (m_frmRasterResample.resampleType == 0) { IGeoDataset pGeodataset2 = pTransOP.Resample(pGeodataset, m_frmRasterResample.Cellsize, esriGeoAnalysisResampleEnum.esriGeoAnalysisResampleNearest); pRasterLayer.CreateFromDataset(pGeodataset2 as IRasterDataset); pRasterLayer.Name = name; } if (m_frmRasterResample.resampleType == 1) { IGeoDataset pGeodataset2 = pTransOP.Resample(pGeodataset, m_frmRasterResample.Cellsize, esriGeoAnalysisResampleEnum.esriGeoAnalysisResampleBilinear); pRasterLayer.CreateFromDataset(pGeodataset2 as IRasterDataset); pRasterLayer.Name = name; } if (m_frmRasterResample.resampleType == 2) { IGeoDataset pGeodataset2 = pTransOP.Resample(pGeodataset, m_frmRasterResample.Cellsize, esriGeoAnalysisResampleEnum.esriGeoAnalysisResampleCubic); pRasterLayer.CreateFromDataset(pGeodataset2 as IRasterDataset); pRasterLayer.Name = name; } pMapCtr.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, null, null); } } }
/// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { // TODO: Add CmdRasterTransZ.OnClick implementation IMapControl3 pMapCtr = (((IToolbarControl)m_hookHelper.Hook).Buddy) as IMapControl3; if (pMapCtr != null) { FrmRasterTransZ m_frmRasterTranZ = new FrmRasterTransZ(); if (m_frmRasterTranZ.ShowDialog() == DialogResult.OK) { if (m_frmRasterTranZ.istrue == true) { ClsRasterOp pRasterOp = new ClsRasterOp(); IRaster2 pRaster2 = pRasterLayer.Raster as IRaster2; IRasterDataset2 pRasterDataset = pRaster2.RasterDataset as IRasterDataset2; pRasterOp.ChangeRasterValue(pRasterDataset, m_frmRasterTranZ.a, m_frmRasterTranZ.b); //此处要判断图层是否存在金字塔,如果存在则需要重新创建 IRasterPyramid3 pDstRasterPyramid3 = pRasterDataset as IRasterPyramid3; if (pDstRasterPyramid3.Present) { IDataLayer2 pDataLayer = pRasterLayer as IDataLayer2; pDataLayer.Disconnect(); pDstRasterPyramid3.DeletePyramid(); pDstRasterPyramid3.Create(); pRasterLayer.CreateFromDataset(pRasterDataset); } //更新图层渲染方式 ChangeStrechRender(pRasterLayer); pMapCtr.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); if (m_axTocControl != null) { m_axTocControl.SetBuddyControl(pMapCtr); m_axTocControl.Update(); m_axTocControl.ActiveView.Refresh(); } } } } }
private bool ExportLayers(IMapDocument mapDoc, string strGdbName, bool bLayerSpatial) { //打开GDB工作空间 ClsGDBDataCommon cls = new ClsGDBDataCommon(); //IWorkspace wks = (IWorkspace)cls.OpenFromFileGDB(strGdbName); IWorkspace2 wksFeature = (IWorkspace2)cls.OpenFromFileGDB(strGdbName); if (wksFeature == null) { return(false); } IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass(); IWorkspace wksRaster = pWSF.OpenFromFile(System.IO.Path.GetDirectoryName(mapDoc.DocumentFilename), 0); if (wksRaster == null) { return(false); } for (int i = 0; i < mapDoc.MapCount; i++) { IMap map = mapDoc.get_Map(i); if (map == null) { continue; } ILayer layer = null; IEnumLayer pEnumLayer = map.get_Layers(null, true); pEnumLayer.Reset(); while ((layer = pEnumLayer.Next()) != null) { //只导出可见图层 if (chkExportVisible.Checked == true) { if (layer.Visible == false) { map.DeleteLayer(layer); continue; } } this.labelExport.Text = "导出:" + layer.Name; this.labelExport.Refresh(); if (layer is IFeatureLayer) { IFeatureClass featureClassNew = CopyFeatureClassToWorkspace(map, layer, wksFeature, bLayerSpatial); if (featureClassNew != null) { //拷贝完成重设数据源 cls.SetdataSourceOfFeatureLayer(layer, featureClassNew); } else { continue; } } else if (layer is IRasterLayer) { IRasterDataset rasterDatasetNew = CopyRasterToWorkspace(map, layer, wksRaster, bLayerSpatial); if (rasterDatasetNew != null) { //拷贝完成重设数据源 IRasterLayer rasterLayer = layer as IRasterLayer; rasterLayer.CreateFromDataset(rasterDatasetNew); } else { continue; } } } } this.labelExport.Text = "导出完成!"; return(true); }
private void method_3(object object_0) { frmOpenFile file = new frmOpenFile { Text = "添加数据", AllowMultiSelect = true }; file.AddFilter(new MyGxFilterDatasets(), true); if (file.DoModalOpen() == DialogResult.OK) { IFeatureClass featureClass; IFeatureLayer layer; System.Windows.Forms.Cursor.Current = Cursors.WaitCursor; IArray array = new ArrayClass(); IArray items = file.Items; int index = 0; while (true) { if (index >= items.Count) { break; } IGxDataset dataset = items.get_Element(index) as IGxDataset; if (dataset != null) { IDataset dataset2 = dataset.Dataset; if (dataset2 != null) { if (dataset2.Type == esriDatasetType.esriDTFeatureClass) { featureClass = (IFeatureClass)dataset2; if (featureClass.FeatureType == esriFeatureType.esriFTAnnotation) { layer = new FDOGraphicsLayerClass { FeatureClass = featureClass, Name = featureClass.AliasName }; } else { layer = new FeatureLayerClass { FeatureClass = featureClass, Name = featureClass.AliasName }; } array.Add(layer); } else { IFDOGraphicsLayerFactory factory; if (dataset2.Type == esriDatasetType.esriDTCadDrawing) { IEnumGxObject children = (dataset as IGxObjectContainer).Children; children.Reset(); for (IGxDataset dataset3 = children.Next() as IGxDataset; dataset3 != null; dataset3 = children.Next() as IGxDataset) { featureClass = dataset3.Dataset as IFeatureClass; if (featureClass.FeatureType == esriFeatureType.esriFTAnnotation) { factory = new FDOGraphicsLayerFactoryClass(); layer = (IFeatureLayer) factory.OpenGraphicsLayer( (IFeatureWorkspace)featureClass.FeatureDataset.Workspace, featureClass.FeatureDataset, featureClass.AliasName); } else { layer = new FeatureLayerClass { FeatureClass = featureClass, Name = featureClass.AliasName }; } array.Add(layer); } } else if (dataset2.Type == esriDatasetType.esriDTFeatureDataset) { IFeatureClassContainer container = (IFeatureClassContainer)dataset2; index = 0; while (index < (container.ClassCount - 1)) { featureClass = container.get_Class(index); if (featureClass.FeatureType == esriFeatureType.esriFTAnnotation) { factory = new FDOGraphicsLayerFactoryClass(); layer = (IFeatureLayer) factory.OpenGraphicsLayer( (IFeatureWorkspace)featureClass.FeatureDataset.Workspace, featureClass.FeatureDataset, featureClass.AliasName); } else { layer = new FeatureLayerClass { FeatureClass = featureClass, Name = featureClass.AliasName }; } array.Add(layer); index++; } } else if (dataset2.Type == esriDatasetType.esriDTTin) { ITinLayer unk = new TinLayerClass { Dataset = (ITin)dataset2, Name = dataset2.Name }; array.Add(unk); } else if ((dataset2.Type == esriDatasetType.esriDTRasterDataset) || (dataset2.Type == esriDatasetType.esriDTRasterBand)) { bool flag = true; if (!((IRasterPyramid)dataset2).Present) { if (ApplicationRef.PyramidPromptType == PyramidPromptType.AlwaysBuildNoPrompt) { ((IRasterPyramid)dataset2).Create(); } else if (ApplicationRef.PyramidPromptType == PyramidPromptType.AlwaysPrompt) { switch (BuildPyramidSet.Show()) { case DialogResult.Yes: ((IRasterPyramid)dataset2).Create(); break; case DialogResult.Cancel: flag = false; break; } } } if (flag) { RasterLayerClass class3 = new RasterLayerClass { Cached = true }; IRasterLayer layer3 = class3; layer3.CreateFromDataset((IRasterDataset)dataset2); layer3.Name = dataset2.Name; array.Add(layer3); } } else if (dataset2.Type == esriDatasetType.esriDTTable) { try { IRasterCatalogTable pCatalog = new RasterCatalogTableClass { Table = (ITable)dataset2 }; pCatalog.Update(); IRasterCatalogLayer layer4 = new RasterCatalogLayerClass(); layer4.Create(pCatalog); layer4.Name = dataset2.BrowseName; array.Add(layer4); } catch { } } } } } index++; } int count = array.Count; ILayer layer5 = null; for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (layer5 is IRasterCatalogLayer) { if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); count--; } } for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (layer5 is IRasterLayer) { if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); count--; } } for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (layer5 is ITinLayer) { if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); count--; } } for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (layer5 is IFeatureLayer) { layer = layer5 as IFeatureLayer; featureClass = layer.FeatureClass; if ((featureClass.ShapeType == esriGeometryType.esriGeometryPolygon) || (featureClass.ShapeType == esriGeometryType.esriGeometryEnvelope)) { if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); count--; } } } for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (layer5 is IFeatureLayer) { layer = layer5 as IFeatureLayer; featureClass = layer.FeatureClass; if (((((featureClass.ShapeType == esriGeometryType.esriGeometryLine) || (featureClass.ShapeType == esriGeometryType.esriGeometryBezier3Curve)) || ((featureClass.ShapeType == esriGeometryType.esriGeometryCircularArc) || (featureClass.ShapeType == esriGeometryType.esriGeometryEllipticArc))) || (featureClass.ShapeType == esriGeometryType.esriGeometryPath)) || (featureClass.ShapeType == esriGeometryType.esriGeometryPolyline)) { if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); count--; } } } for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (layer5 is IFeatureLayer) { layer = layer5 as IFeatureLayer; featureClass = layer.FeatureClass; if ((featureClass.ShapeType == esriGeometryType.esriGeometryMultipoint) || (featureClass.ShapeType == esriGeometryType.esriGeometryPoint)) { if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); count--; } } } for (index = count - 1; index >= 0; index--) { layer5 = array.get_Element(index) as ILayer; if (object_0 is IMap) { (object_0 as IMap).AddLayer(layer5); } else if (object_0 is IGroupLayer) { (object_0 as IGroupLayer).Add(layer5); } array.Remove(index); } System.Windows.Forms.Cursor.Current = Cursors.Default; } }
/// <summary> /// Create Bitmap From ESRI Dataset /// </summary> /// <param name="dataset">Dataset to generate an image from</param> /// <param name="imageFormat">Output image format</param> /// <param name="size">Size of output image</param> /// <param name="resolution">Resolution of output image (dpi)</param> /// <param name="background">Background color</param> /// <param name="filename">Ouput filename</param> public static void CreateBitmap(IDataset dataset, esriImageFormat imageFormat, Size size, ushort resolution, Color background, string filename) { ILayer layer = null; switch (dataset.Type) { case esriDatasetType.esriDTFeatureClass: IFeatureClass featureClass = (IFeatureClass)dataset; switch (featureClass.FeatureType) { case esriFeatureType.esriFTDimension: layer = new DimensionLayerClass(); break; case esriFeatureType.esriFTAnnotation: layer = new FeatureLayerClass(); IGeoFeatureLayer geoFeaureLayer = (IGeoFeatureLayer)layer; geoFeaureLayer.DisplayAnnotation = true; break; case esriFeatureType.esriFTComplexEdge: case esriFeatureType.esriFTComplexJunction: case esriFeatureType.esriFTSimple: case esriFeatureType.esriFTSimpleEdge: case esriFeatureType.esriFTSimpleJunction: layer = new FeatureLayerClass(); break; } if (layer == null) { return; } IFeatureLayer featureLayer = (IFeatureLayer)layer; featureLayer.FeatureClass = featureClass; break; case esriDatasetType.esriDTRasterDataset: layer = new RasterLayerClass(); IRasterLayer rasterLayer = (IRasterLayer)layer; rasterLayer.CreateFromDataset((IRasterDataset)dataset); break; default: string message = string.Format("[{0}] is not supported", dataset.Type.ToString()); throw new Exception(message); } if (layer == null) { return; } // Create In-memory Map IMap map = new MapClass(); map.AddLayer(layer); IActiveView activeView = (IActiveView)map; IExport export = null; tagRECT rect = new tagRECT(); // Set Format Specific Properties switch (imageFormat) { case esriImageFormat.esriImageJPG: export = new ExportJPEGClass(); IExportJPEG exportJpeg = (IExportJPEG)export; exportJpeg.ProgressiveMode = false; exportJpeg.Quality = 100; break; default: throw new Exception("[" + imageFormat.ToString() + "] is not supported"); } if (export == null) { throw new Exception("Failed to Created Exporter"); } // Set Background if ((export is IExportBMP) || (export is IExportGIF) || (export is IExportJPEG) || (export is IExportPNG) || (export is IExportTIFF)) { IExportImage exportImage = (IExportImage)export; exportImage.ImageType = esriExportImageType.esriExportImageTypeTrueColor; exportImage.BackgroundColor = GeodatabaseUtility.ToESRIColor(background); } // Set Export Frame rect = activeView.ExportFrame; rect.left = 0; rect.top = 0; rect.right = size.Width; rect.bottom = size.Height; // Set Output Extent IEnvelope envelope = new EnvelopeClass(); envelope.PutCoords(rect.left, rect.top, rect.right, rect.bottom); export.PixelBounds = envelope; export.Resolution = resolution; export.ExportFileName = filename; // Export map to image int intHdc = export.StartExporting(); activeView.Output(intHdc, resolution, ref rect, null, null); export.FinishExporting(); export.Cleanup(); // Clear Layers map.ClearLayers(); // Release COM Objects GeodatabaseUtility.ReleaseComObject(layer); GeodatabaseUtility.ReleaseComObject(envelope); GeodatabaseUtility.ReleaseComObject(map); GeodatabaseUtility.ReleaseComObject(activeView); GeodatabaseUtility.ReleaseComObject(export); GC.Collect(); GC.WaitForPendingFinalizers(); }
//直接更改图层坐标系统,并不重投影数据值 private void btnChangeCoordinate_Click(object sender, EventArgs e) { ISpatialReference pSpatialReference = null; FrmCoordinateSystem frm = new FrmCoordinateSystem(m_pLayer); if (frm.ShowDialog() == DialogResult.OK) { pSpatialReference = frm.pSpaReference; if (m_pLayer is IFeatureLayer) { IFeatureLayer pFLayer = m_pLayer as IFeatureLayer; IFeatureClass pFClass = pFLayer.FeatureClass; if (pFClass == null) { return; } IFeatureDataset pFDataset = pFClass.FeatureDataset; if (pFDataset == null) { IGeoDatasetSchemaEdit geoSchemaEdit = pFClass as IGeoDatasetSchemaEdit; if (geoSchemaEdit == null) { return; } if (geoSchemaEdit.CanAlterSpatialReference == true) { geoSchemaEdit.AlterSpatialReference(pSpatialReference); } } else { IGeoDatasetSchemaEdit geoSchemaEdit = pFDataset as IGeoDatasetSchemaEdit; if (geoSchemaEdit == null) { return; } if (geoSchemaEdit.CanAlterSpatialReference == true) { geoSchemaEdit.AlterSpatialReference(pSpatialReference); } } } else if (m_pLayer is IRasterLayer) { IRasterLayer pRLayer = m_pLayer as IRasterLayer; IRaster pRaster = pRLayer.Raster; IRaster2 pRaster2 = pRaster as IRaster2; IRasterDataset pRDataset = pRaster2.RasterDataset; IGeoDatasetSchemaEdit geoSchemaEdit = pRDataset as IGeoDatasetSchemaEdit; if (geoSchemaEdit == null) { return; } if (geoSchemaEdit.CanAlterSpatialReference == true) { geoSchemaEdit.AlterSpatialReference(pSpatialReference); pRLayer.CreateFromDataset(pRDataset); } } string strCoord = string.Empty; if (m_pLayer is IGeoDataset) { IGeoDataset geoDataset = m_pLayer as IGeoDataset; if (geoDataset == null) { this.txtCoordinateSystem.Text = string.Empty; } else { ISpatialReference reference = geoDataset.SpatialReference; strCoord = ClsGDBDataCommon.GetReferenceString(reference); txtCoordinateSystem.Text = strCoord; txtCoordinateSystem.Update(); } } } }