/// <summary> /// 矢量点转栅格 /// </summary> /// <param name="xjFeatureLayer">矢量图层</param> /// <param name="RasterPath">栅格绝对路径</param> /// <param name="CellSize">栅格边长</param> /// <param name="SecletctedField">所选字段(高程等)</param> /// <returns>返回栅格图层</returns> private ILayer xjShpPointToRaster(IFeatureLayer xjFeatureLayer, string RasterPath, double CellSize, string SecletctedField) { IFeatureClass xjFeatureClass = xjFeatureLayer.FeatureClass; IFeatureClassDescriptor xjFeatureClassDescriptor = new FeatureClassDescriptorClass();//using ESRI.ArcGIS.GeoAnalyst; xjFeatureClassDescriptor.Create(xjFeatureClass, null, SecletctedField); IGeoDataset xjGeoDataset = xjFeatureClassDescriptor as IGeoDataset; IWorkspaceFactory xjwsf = new RasterWorkspaceFactoryClass(); //using ESRI.ArcGIS.DataSourcesRaster; string xjRasterFolder = System.IO.Path.GetDirectoryName(RasterPath); IWorkspace xjws = xjwsf.OpenFromFile(xjRasterFolder, 0); IConversionOp xjConversionOp = new RasterConversionOpClass(); IRasterAnalysisEnvironment xjRasteren = xjConversionOp as IRasterAnalysisEnvironment; object xjCellSize = CellSize as object; xjRasteren.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref xjCellSize); string xjFileName = System.IO.Path.GetFileName(RasterPath); IRasterDataset xjdaset2 = xjConversionOp.ToRasterDataset(xjGeoDataset, "TIFF", xjws, xjFileName); IRasterLayer xjRasterLayer = new RasterLayerClass(); xjRasterLayer.CreateFromDataset(xjdaset2); ILayer xjLayer = xjRasterLayer; xjRasterLayer.Name = xjFileName; return(xjLayer); }
/// <summary> /// 执行矢量转栅格 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FtoR_backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; //设置CancellationPending可以退出任务线程 if (worker.CancellationPending == true) { e.Cancel = true; } else { try { FileInfo inputinfo = new FileInfo(featuretoraster_inputcombox.Text); FileInfo outputinfo = new FileInfo(featuretoraster_output.Text); IWorkspaceFactory outputworkspaceFactory = new RasterWorkspaceFactoryClass(); IWorkspace outputworkspaceop = outputworkspaceFactory.OpenFromFile(outputinfo.DirectoryName, 0);//创建输出工作空间 IWorkspaceFactory workspacefactory = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace featureworkspace = workspacefactory.OpenFromFile(inputinfo.DirectoryName, 0) as IFeatureWorkspace; IFeatureClass featureclass = featureworkspace.OpenFeatureClass(inputinfo.Name); IFeatureClassDescriptor featureClassDescriptor = new FeatureClassDescriptorClass(); featureClassDescriptor.Create(featureclass, null, featuretoraster_fieldcombox.Text); IGeoDataset geodatasetop = featureClassDescriptor as IGeoDataset; IConversionOp conversionOp = new RasterConversionOpClass(); //转换设置 IRasterAnalysisEnvironment rasterAnalysisEnvironment = conversionOp as IRasterAnalysisEnvironment; //栅格的大小 double dCellSize = Convert.ToDouble(featuretoraster_cellsize.Text); object oCellSize = dCellSize as object; //设置栅格大小 rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref oCellSize); string format = ""; if (outputinfo.Extension == ".tif") { format = "TIFF"; } else if (outputinfo.Extension == ".img") { format = "IMAGINE Image"; } else { format = "GRID"; } //启动预处理计时 watch = new Stopwatch(); watch.Start(); //执行转换 conversionOp.ToRasterDataset(geodatasetop, format, outputworkspaceop, outputinfo.Name); } catch (Exception erro) { MessageBox.Show(erro.Message, "错误"); } } }
/// <summary> /// 转换 /// </summary> /// <param name="pFeatureClass">要素类</param> /// <param name="fieldName">栅格采用的字段</param> /// <param name="rasterWorkSpace">输出栅格的工作空间</param> /// <param name="newRasterName">新的栅格名称</param> public void Convert(IFeatureClass pFeatureClass, string fieldName, string rasterWorkSpace, string newRasterName) { FileHelper.DeleteFile(rasterWorkSpace, newRasterName, ".tif", ".tfw",".tif.aux"); IFeatureClassDescriptor featureClassDescriptor = new FeatureClassDescriptorClass(); featureClassDescriptor.Create(pFeatureClass, null, fieldName); IGeoDataset geoDataset = (IGeoDataset)featureClassDescriptor; IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IWorkspace workspace = workspaceFactory.OpenFromFile(rasterWorkSpace, 0); IConversionOp conversionOp = new RasterConversionOpClass(); IRasterAnalysisEnvironment rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)conversionOp; rasterAnalysisEnvironment.OutWorkspace = workspace; //set cell size rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref _cellSize); //set output extent object objectMissing = Type.Missing; rasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref _extentEnvelope, ref objectMissing); //set output spatial reference rasterAnalysisEnvironment.OutSpatialReference = ((IGeoDataset)pFeatureClass).SpatialReference; //convertion conversionOp.ToRasterDataset(geoDataset, _rasterType, workspace, newRasterName); }
/// <summary> /// 转换 /// </summary> /// <param name="pFeatureClass">要素类</param> /// <param name="fieldName">栅格采用的字段</param> /// <param name="rasterWorkSpace">输出栅格的工作空间</param> /// <param name="newRasterName">新的栅格名称</param> public void Convert(IFeatureClass pFeatureClass, string fieldName, string rasterWorkSpace, string newRasterName) { FileHelper.DeleteFile(rasterWorkSpace, newRasterName, ".tif", ".tfw", ".tif.aux"); IFeatureClassDescriptor featureClassDescriptor = new FeatureClassDescriptorClass(); featureClassDescriptor.Create(pFeatureClass, null, fieldName); IGeoDataset geoDataset = (IGeoDataset)featureClassDescriptor; IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IWorkspace workspace = workspaceFactory.OpenFromFile(rasterWorkSpace, 0); IConversionOp conversionOp = new RasterConversionOpClass(); IRasterAnalysisEnvironment rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)conversionOp; rasterAnalysisEnvironment.OutWorkspace = workspace; //set cell size rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref _cellSize); //set output extent object objectMissing = Type.Missing; rasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref _extentEnvelope, ref objectMissing); //set output spatial reference rasterAnalysisEnvironment.OutSpatialReference = ((IGeoDataset)pFeatureClass).SpatialReference; //convertion conversionOp.ToRasterDataset(geoDataset, _rasterType, workspace, newRasterName); }
private IFunctionRasterDataset createBarrierRaster() { IFeatureClassDescriptor ftrDesc = new FeatureClassDescriptorClass(); IQueryFilter qf = new QueryFilterClass(); ftrDesc.Create(BarriersFeatureClass, qf, BarriersFeatureClass.OIDFieldName); IConversionOp convOp = new RasterConversionOpClass(); IRasterAnalysisEnvironment rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)convOp; IRasterAnalysisGlobalEnvironment rasterAnalysisGlobalEnvironment = (IRasterAnalysisGlobalEnvironment)convOp; rasterAnalysisGlobalEnvironment.AvoidDataConversion = true; rasterAnalysisEnvironment.OutSpatialReference = sp; rasterAnalysisEnvironment.OutWorkspace = OutWorkspace; //object cells = Dem.RasterInfo.CellSize; object ext = ((IGeoDataset)Dem).Extent; object snap = ((IGeoDataset)Dem); rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, Dem); rasterAnalysisEnvironment.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ext, Dem); string outRdName = rsUtil.getSafeOutputName(OutWorkspace, "sBarrier"); IRasterDataset geoDset = convOp.ToRasterDataset((IGeoDataset)ftrDesc, "GRID", OutWorkspace, outRdName); IGeoDatasetSchemaEdit2 geoSch = (IGeoDatasetSchemaEdit2)geoDset; if (geoSch.CanAlterSpatialReference) { geoSch.AlterSpatialReference(rasterAnalysisEnvironment.OutSpatialReference); } return(rsUtil.createIdentityRaster(geoDset)); }
/// <summary> /// 矢量转栅格 /// </summary> /// <param name="IFeatureClass">要转换的矢量数据</param> /// <param name="eleField">转换的字段名</param> /// <param name="cellsize">栅格大小,默认为null</param> ///<param name="rasterSavePath">保存路径</param> ///<param name="demName">dem名称</param> /// <returns>返回ILayer类型的图层</returns> public ILayer FeatureToRaster(IFeatureClass pFeatureClass, string eleField, object cellSize, string rasterSavePath, string demName) { IFeatureClassDescriptor pFeatureClassDescriptor = new FeatureClassDescriptorClass(); //获取转化的字段 pFeatureClassDescriptor.Create(pFeatureClass, null, eleField); //转换字段 IGeoDataset pGeoDataset = (IGeoDataset)pFeatureClassDescriptor; //获取第一个参数 //默认栅格大小 if (Convert.ToDouble(cellSize) <= 0) { IEnvelope envelope = pGeoDataset.Extent; if (envelope.Width > envelope.Height) { cellSize = envelope.Height / 250; } else { cellSize = envelope.Width / 250; } } IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace2 rasterWorkspace2 = workspaceFactory.OpenFromFile(rasterSavePath, 0) as IRasterWorkspace2; //in case that there is already an existing raster with the raster name, try to delete it if (System.IO.Directory.Exists(System.IO.Path.Combine(rasterSavePath, demName))) { IDataset dataset = rasterWorkspace2.OpenRasterDataset(demName) as IDataset; dataset.Delete(); } IConversionOp conversionOp = new RasterConversionOpClass(); IRasterAnalysisEnvironment rasterAnalysisEnvironment = conversionOp as IRasterAnalysisEnvironment; rasterAnalysisEnvironment.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSize); IRasterDataset rasterDataset = null; try { rasterDataset = conversionOp.ToRasterDataset(pGeoDataset, "TIFF", (IWorkspace)rasterWorkspace2, demName); } catch (Exception ee) { MessageBox.Show(ee.Message); } ILayer layer = null; if (rasterDataset != null) { IRasterLayer pRlyr = new RasterLayerClass(); pRlyr.CreateFromDataset(rasterDataset); layer = pRlyr as ILayer; } return(layer); }
private void _PunchDEM() { OnProgress("Punching DEM with drainage points..."); IConversionOp conversionOp = new RasterConversionOpClass(); ILogicalOp logicalOp = new RasterMathOpsClass(); IGeoDataset inletLocations = null; IWorkspace tempWorkspace = null; try { tempWorkspace = GetTempRasterWorkspace(); SetAnalysisEnvironment((IRasterAnalysisEnvironment)conversionOp); SetAnalysisEnvironment((IRasterAnalysisEnvironment)logicalOp); IFeatureClassDescriptor sourceDescriptor = new FeatureClassDescriptorClass(); sourceDescriptor.Create(_drainClass, null, _drainClass.OIDFieldName); string gridPath = SetupOp.CreateTempFileName(_GetTempDir(), "TmpInlets", null); string gridName = System.IO.Path.GetFileName(gridPath); IRasterDataset pRasterDataset = null; try { pRasterDataset = conversionOp.ToRasterDataset((IGeoDataset)sourceDescriptor, "GRID", tempWorkspace, gridName); inletLocations = logicalOp.BooleanNot(logicalOp.IsNull((IGeoDataset)pRasterDataset)); string outputPath = CreateTempFileName(_GetResultDir(), "punchdem", ""); _punchedDEM = GeoprocessingTools.SetNull((IRaster)inletLocations, _dem, outputPath); } finally { _MarkForDisposal((IDataset)pRasterDataset); } } finally { UrbanDelineationExtension.ReleaseComObject(tempWorkspace); UrbanDelineationExtension.ReleaseComObject(conversionOp); UrbanDelineationExtension.ReleaseComObject(logicalOp); } OnProgress("DEM punched."); }
// // // public static void ConvertShape2Raster(string fullPathToShape, double inCellSize, string outRasterName) { //��������������õ��ļ�����·�� string pathToWorkspace = System.IO.Path.GetDirectoryName(fullPathToShape); string shapefileName = System.IO.Path.GetFileNameWithoutExtension(fullPathToShape); IFeatureClass pFClass = OpenFeatureClassFromShapefile(pathToWorkspace, shapefileName); IGeoDataset geoDataset = (IGeoDataset)pFClass; IWorkspace pWSpace = setRasterWorkspace(pathToWorkspace); // Create raster conversion operator and set up analysis environment, // then use it to make the output raster. IConversionOp rasterConversionOp = new RasterConversionOpClass(); try { // Again, an interface that has to be present on the object. IRasterAnalysisEnvironment rasterAnalysisEnvironment = (IRasterAnalysisEnvironment)rasterConversionOp; rasterAnalysisEnvironment.OutWorkspace = pWSpace; esriRasterEnvSettingEnum envType = esriRasterEnvSettingEnum.esriRasterEnvValue; System.Object inCellSizeObj = inCellSize; rasterAnalysisEnvironment.SetCellSize(envType, ref inCellSizeObj); IEnvelope rasterExtent = geoDataset.Extent; // SetExtent can take an Envelope or another Raster as the // second parameter, C# does this by passing the generic Object with // the keyword "ref" in front. Same for the Raster to be used for // the grid cell registration - which in this case is null, i.e. a void* System.Object inExtent = rasterExtent; System.Object inSnapRaster = 0; rasterAnalysisEnvironment.SetExtent(envType, ref inExtent, ref inSnapRaster); } catch { } rasterConversionOp.ToRasterDataset(geoDataset, "TIFF", pWSpace, outRasterName); }
private void btnGO_Click(object sender, EventArgs e) { string fileName; string shpFile; int startX, endX; string shpDir; IFeatureClass pFClass = null; if (bFeatDataPath == true) { fileName = comboBoxInData.Text; shpDir = fileName.Substring(0, fileName.LastIndexOf("\\")); startX = fileName.LastIndexOf("\\"); endX = fileName.Length; shpFile = fileName.Substring(startX + 1, endX - startX - 1); pFClass = UtilityFunction.OpenFeatureClassFromShapefile(shpDir, shpFile); } else { pFClass = GetFeatureFromMapLyr(comboBoxInData.Text); } if (bRasterDataPath == true) { fileName = txtOutPath.Text; shpDir = fileName.Substring(0, fileName.LastIndexOf("\\")); startX = fileName.LastIndexOf("\\"); endX = fileName.Length; shpFile = fileName.Substring(startX + 1, endX - startX - 1); } else { shpDir = txtOutPath.Text; shpFile = "����תդ��"; } IGeoDataset pTempDS = pFClass as IGeoDataset; IFeatureLayer pFeatLayer = new FeatureLayerClass(); pFeatLayer.FeatureClass = pFClass; IFeatureClassDescriptor pFeatClsDes = new FeatureClassDescriptorClass(); if (comboBoxField.Text != "��") pFeatClsDes.Create(pFClass, null, comboBoxField.Text); else pFeatClsDes.Create(pFClass, null, ""); pTempDS = pFeatClsDes as IGeoDataset; try { IConversionOp pConversionOp = new RasterConversionOpClass(); string sCellSize = txtCellSize.Text; double dCellSize = Convert.ToDouble(sCellSize); pConversionOp = UtilityFunction.SetFeatToRasterAnalysisEnv(shpDir, dCellSize, pFeatLayer); if (File.Exists(shpDir + "\\" + shpFile + ".img") == true) File.Delete(shpDir + "\\" + shpFile + ".img"); IWorkspace pWS = UtilityFunction.setRasterWorkspace(shpDir); IRasterDataset pRasterDs = pConversionOp.ToRasterDataset(pTempDS, "IMAGINE Image", pWS, shpFile); ITemporaryDataset pTempRaster = pRasterDs as ITemporaryDataset; if (pTempRaster.IsTemporary() == true) pTempRaster.MakePermanent(); IRaster pOutRaster = pRasterDs.CreateDefaultRaster(); IRasterLayer pRasterLayer = UtilityFunction.SetStretchRenderer(pOutRaster); pMap.AddLayer(pRasterLayer); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnGO_Click(object sender, EventArgs e) { string fileName; string shpFile; int startX, endX; string shpDir; IFeatureClass pFClass = null; if (bFeatDataPath == true) { fileName = comboBoxInData.Text; shpDir = fileName.Substring(0, fileName.LastIndexOf("\\")); startX = fileName.LastIndexOf("\\"); endX = fileName.Length; shpFile = fileName.Substring(startX + 1, endX - startX - 1); pFClass = Utility.OpenFeatureClassFromShapefile(shpDir, shpFile); } else { pFClass = GetFeatureFromMapLyr(comboBoxInData.Text); } if (bRasterDataPath == true) { fileName = txtOutPath.Text; shpDir = fileName.Substring(0, fileName.LastIndexOf("\\")); startX = fileName.LastIndexOf("\\"); endX = fileName.Length; shpFile = fileName.Substring(startX + 1, endX - startX - 1); } else { shpDir = txtOutPath.Text; shpFile = "特征转栅格"; } IGeoDataset pTempDS = pFClass as IGeoDataset; IFeatureLayer pFeatLayer = new FeatureLayerClass(); pFeatLayer.FeatureClass = pFClass; IFeatureClassDescriptor pFeatClsDes = new FeatureClassDescriptorClass(); if (comboBoxField.Text != "无") { pFeatClsDes.Create(pFClass, null, comboBoxField.Text); } else { pFeatClsDes.Create(pFClass, null, ""); } pTempDS = pFeatClsDes as IGeoDataset; try { IConversionOp pConversionOp = new RasterConversionOpClass(); string sCellSize = txtCellSize.Text; double dCellSize = Convert.ToDouble(sCellSize); pConversionOp = Utility.SetFeatToRasterAnalysisEnv(shpDir, dCellSize, pFeatLayer); if (File.Exists(shpDir + "\\" + shpFile + ".img") == true) { File.Delete(shpDir + "\\" + shpFile + ".img"); } IWorkspace pWS = Utility.setRasterWorkspace(shpDir); IRasterDataset pRasterDs = pConversionOp.ToRasterDataset(pTempDS, "IMAGINE Image", pWS, shpFile); ITemporaryDataset pTempRaster = pRasterDs as ITemporaryDataset; if (pTempRaster.IsTemporary() == true) { pTempRaster.MakePermanent(); } IRaster pOutRaster = pRasterDs.CreateDefaultRaster(); IRasterLayer pRasterLayer = Utility.SetStretchRenderer(pOutRaster); pMainFrm.getMapControl().AddLayer(pRasterLayer, 0); } catch (Exception ex) { MessageBox.Show(ex.Message); } }