private void button1_Click(object sender, EventArgs e) { try { //create raster dataset from the JPIP service url Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); IWorkspaceFactory wsFact = Activator.CreateInstance(factoryType) as IWorkspaceFactory; string tempPath = Path.GetTempPath(); IRasterWorkspace2 ws = wsFact.OpenFromFile(tempPath, 0) as IRasterWorkspace2; IRasterDataset rds = ws.OpenRasterDataset(txtJPIPUrl.Text); //create a layer from the raster dataset IRasterLayer rasterLayer = new RasterLayerClass(); rasterLayer.CreateFromDataset(rds); string layerName = txtLayerName.Text; if (layerName == "") { layerName = txtJPIPUrl.Text.Substring(txtJPIPUrl.Text.LastIndexOf("/") + 1, txtJPIPUrl.Text.Length - txtJPIPUrl.Text.LastIndexOf("/") - 1); } rasterLayer.Name = layerName; //add the JPIP layer to the current data frame of ArcMap ArcMap.Document.FocusMap.AddLayer(rasterLayer); this.Close(); } catch { MessageBox.Show("Couldn't connect to the specified URL, sample url: jpip://myserver:8080/JP2Server/imagealias"); } }
/// <summary> /// 获取该栅格图像的相关数据 /// <remarks> /// <list type="bullet"> /// <item> /// <term> /// 初始化_pDataset /// </term> /// <item> /// 获取栅格的Height和Width /// </item> /// </item> /// </list> /// </remarks> /// </summary> /// <returns></returns> private void Open() { //Open IWorkspaceFactory pRFactory = new RasterWorkspaceFactoryClass(); IRasterWorkspace2 rasterWorkspace = pRFactory.OpenFromFile(_rasterWorkSapce, 0) as IRasterWorkspace2; if (rasterWorkspace == null) { throw new ArgumentException("栅格文件无法打开"); } _pDataset = rasterWorkspace.OpenRasterDataset(_rasterName); //set height and width IRasterProps pRasterProps = (IRasterProps)GetRaster(); RasterInfo = new RasterInformation() { Width = pRasterProps.Width, Height = pRasterProps.Height, XCellSize = pRasterProps.MeanCellSize().X, YCellSize = pRasterProps.MeanCellSize().Y, OriginPoint = new PointClass() { X = pRasterProps.Extent.XMin, Y = pRasterProps.Extent.YMin }, SpatialReference = ((IGeoDataset)_pDataset).SpatialReference }; }
public bool CopyRaster(IRasterWorkspace2 pRW, string pFileName, IRasterWorkspaceEx pWorkSpace, string pDestName) { try { IRasterDataset pRds = pRW.OpenRasterDataset(pFileName); IRasterProps pRasterProps = (IRasterProps)pRds.CreateDefaultRaster(); IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass(); IRasterDef pRasterDef = new RasterDefClass(); pRasterDef.SpatialReference = pRasterProps.SpatialReference; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.AvgNumPoints_2 = 4; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.set_GridSize(0, 1000); pGeoDefEdit.SpatialReference_2 = pRasterProps.SpatialReference; IRasterDataset pRasterDataset = pWorkSpace.SaveAsRasterDataset(pDestName, pRds.CreateDefaultRaster(), pRasterStorageDef, "", pRasterDef, pGeoDef); return true; } catch (System.Exception ex) { return false; } }
//按照名称与类型打开数据集,针对featureDataset和rasterDataset public static object openByTypeAndName(string name, string type) { try { if (type.Equals("featureDataset")) { IFeatureWorkspace w = vec_workspace as IFeatureWorkspace; IFeatureDataset data = w.OpenFeatureDataset(name); return(data); } else if (type.Equals("rasterDataset")) { IRasterWorkspace2 w = ras_workspace as IRasterWorkspace2; IRasterDataset data = w.OpenRasterDataset(name); return(data); } else { LogHelper.writeLog("error openByTypeAndName wrong type"); MessageBox.Show("wrong type"); } } catch (System.Exception ex) { LogHelper.writeLog("error openByTypeAndName " + ex.Message); MessageBox.Show("error : " + ex.Message); } return(null); }
public bool CopyRaster(IRasterWorkspace2 pRW, string pFileName, IRasterWorkspaceEx pWorkSpace, string pDestName) { try { IRasterDataset pRds = pRW.OpenRasterDataset(pFileName); IRasterProps pRasterProps = (IRasterProps)pRds.CreateDefaultRaster(); IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass(); IRasterDef pRasterDef = new RasterDefClass(); pRasterDef.SpatialReference = pRasterProps.SpatialReference; IGeometryDef pGeoDef = new GeometryDefClass(); IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit; pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; pGeoDefEdit.AvgNumPoints_2 = 4; pGeoDefEdit.GridCount_2 = 1; pGeoDefEdit.set_GridSize(0, 1000); pGeoDefEdit.SpatialReference_2 = pRasterProps.SpatialReference; IRasterDataset pRasterDataset = pWorkSpace.SaveAsRasterDataset(pDestName, pRds.CreateDefaultRaster(), pRasterStorageDef, "", pRasterDef, pGeoDef); return(true); } catch (System.Exception ex) { return(false); } }
private IDataset TryOpenDataset(Moves.GisDataset dataset, IWorkspace workspace, IDatasetName datasetName) { if (dataset.DatasourceType.AsDatasetType() == null) { return(null); } var datasetType = dataset.DatasourceType.AsDatasetType().Value; if (datasetType == esriDatasetType.esriDTFeatureClass) { try { IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace; IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(datasetName.Name); return((IDataset)featureClass); } catch (Exception) { return(null); } } // For opening raster data see https://desktop.arcgis.com/en/arcobjects/10.5/net/webframe.htm#62937a09-b1c5-47d7-a1ac-f7a5daab3c89.htm if (datasetType == esriDatasetType.esriDTRasterDataset) { try { // ReSharper disable once SuspiciousTypeConversion.Global // Raster Workspace Class is in ESRI.ArcGIS.DataSourcesRaster IRasterWorkspace2 rasterWorkspace = (IRasterWorkspace2)workspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(datasetName.Name); // ReSharper disable once SuspiciousTypeConversion.Global // Three possible co-classes FunctionRasterDataset, RasterBand, RasterDataset are in ESRI.ArcGIS.DataSourcesRaster return((IDataset)rasterDataset); } catch (Exception) { return(null); } } if (datasetType == esriDatasetType.esriDTRasterCatalog || datasetType == esriDatasetType.esriDTMosaicDataset) { try { IRasterWorkspaceEx rasterWorkspace = (IRasterWorkspaceEx)workspace; IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(datasetName.Name); // ReSharper disable once SuspiciousTypeConversion.Global // Three possible co-classes FunctionRasterDataset, RasterBand, RasterDataset are in ESRI.ArcGIS.DataSourcesRaster return((IDataset)rasterDataset); } catch (Exception) { return(null); } } //TODO: Open additional types of data sources, support at least all in theme Manager return(null); }
/// <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); }
static void Main(string[] args) { //ESRI License Initializer generated code. m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeAdvanced }, new esriLicenseExtensionCode[] { }); //ESRI License Initializer generated code. //Get a path to the raster workspace and create a RasterWorkspace object Type factoryType = Type.GetTypeFromProgID("esriDataSourcesRaster.RasterWorkspaceFactory"); IWorkspaceFactory workspaceFactory = Activator.CreateInstance(factoryType) as IWorkspaceFactory; IRasterWorkspace2 rasterWorkspace = (IRasterWorkspace2)workspaceFactory.OpenFromFile("\\\\Filepath\\ToRaster\\Folder", 0); IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset("DEM_Resample.tif"); //Create a raster layer to get the raster object from it IRasterLayer rasterLayer = new RasterLayer(); rasterLayer.CreateFromDataset(rasterDataset); IRaster raster = rasterLayer.Raster; //Get the raster properties so we can modify them later and get details about them if we so choose. IRasterProps rasterProps = raster as IRasterProps; double cellSize = 60; //Declate a new spatial reference if you want to change the spatial reference used. ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironment(); ISpatialReference2 srReference = srFactory.CreateProjectedCoordinateSystem(26917) as ISpatialReference2; //Create an IRasterGeometryProc object as this has the projectFast method we are looking for. IRasterGeometryProc rasterGeometryProc = new RasterGeometryProc(); rasterGeometryProc.ProjectFast(rasterProps.SpatialReference, rstResamplingTypes.RSP_NearestNeighbor, ((object)cellSize), raster); //Create a new rasterBandCollection to store the raster in and save it there. IRasterBandCollection rasterBandCollection = raster as IRasterBandCollection; String outName = "NewImage.tif"; String outType = "TIFF"; rasterBandCollection.SaveAs(outName, ((IWorkspace)rasterWorkspace), outType); Console.WriteLine("DONE"); Console.ReadLine(); //Do not make any call to ArcObjects after ShutDownApplication() m_AOLicenseInitializer.ShutdownApplication(); }
public static ILayer RasterView(string gdb_Path, string feat_Name) { try { IWorkspaceFactory2 pWKF = new FileGDBWorkspaceFactoryClass(); IRasterWorkspace2 pRasterWKEx = (IRasterWorkspace2)pWKF.OpenFromFile(gdb_Path, 0); IRasterLayer featLyr = new RasterLayerClass(); IRasterDataset rstDs = pRasterWKEx.OpenRasterDataset(feat_Name) as IRasterDataset; IRaster rst = rstDs.CreateDefaultRaster(); featLyr.CreateFromRaster(rst); ILayer lyr = featLyr as ILayer; lyr.Name = feat_Name; return(lyr); } catch (Exception e) { return(null); } }
/// <summary> /// 导入数据(栅格数据集) /// </summary> /// <param name="RDDatasetName">栅格数据集名称</param> /// <param name="filePath">源数据路径</param> /// <param name="pWorkspace">栅格数据集工作空间</param> /// <param name="eError"></param> /// <returns></returns> private bool InputRasterDataset(string RDDatasetName, string filePath, IWorkspace pWorkspace, out Exception eError) { eError = null; FileInfo pFileInfo = new FileInfo(filePath); string pFileDic = pFileInfo.DirectoryName; //文件目录 string pFileName = pFileInfo.Name; //文件名 try { //IMosaicRaster pMosaicR = null; //目标栅格数据集 IRasterDataset pObjRasterDataset = GetRasterDataset(RDDatasetName, pWorkspace, out eError); if (pObjRasterDataset == null) { return(false); } //IRaster pObjRaster = pObjRasterDataset.CreateDefaultRaster(); //if(pObjRaster!=null) //{ // pMosaicR = pObjRaster as IMosaicRaster; //} //栅格数据工作空间 IWorkspaceFactory pOrgRasterWsFac = new RasterWorkspaceFactoryClass(); IWorkspace pWS = pOrgRasterWsFac.OpenFromFile(pFileDic, 0); IRasterWorkspace2 pRasterWS = pWS as IRasterWorkspace2; if (pRasterWS == null) { return(false); } IRasterDataset pOrgRDataset = pRasterWS.OpenRasterDataset(pFileName); IRaster pOrgRaster = pOrgRDataset.CreateDefaultRaster(); //load raster data to exist raster dataset IRasterLoader pRasterLoad = new RasterLoaderClass(); if (pOrgRaster != null) { //if(pMosaicR!=null) //{ // //若目标要素不为空,则进行拼接 // //pMosaicR.MosaicOperatorType = rstMosaicOperatorType.MT_LAST; // pMosaicR.OrderByField = "Name"; // pMosaicR.Ascending = true; // pMosaicR.MosaicColormapMode = rstMosaicColormapMode.MM_MATCH; // IMosaicOperator pMosaicOpe=pMosaicR.MosaicOperator; //} pRasterLoad.Background = 0; //background value be ignored when loading pRasterLoad.PixelAlignmentTolerance = 0; //重采样的容差 pRasterLoad.MosaicColormapMode = rstMosaicColormapMode.MM_LAST; //拼接的颜色采用 last map color pRasterLoad.Load(pObjRasterDataset, pOrgRaster); } Marshal.ReleaseComObject(pRasterLoad); } catch (System.Exception ex) { //******************************************************************* //guozheng added if (ModData.SysLog != null) { ModData.SysLog.Write(ex, null, DateTime.Now); } else { ModData.SysLog = new SysCommon.Log.clsWriteSystemFunctionLog(); ModData.SysLog.Write(ex, null, DateTime.Now); } //******************************************************************** eError = ex; return(false); } return(true); }
private void CopyRasterMethod() { try { double Size = 0; string gdb_Path = target.In_Directory; string feat_Name = target.In_FileName; string outputFolderPath = target.Out_Directory; string outputName = target.Out_FileName; ESRI.ArcGIS.DataManagementTools.CopyRaster cr = new ESRI.ArcGIS.DataManagementTools.CopyRaster(); cr.in_raster = gdb_Path + "\\" + feat_Name; if (System.IO.Path.GetExtension(outputFolderPath).ToUpper().Equals(".GDB")) { cr.out_rasterdataset = outputFolderPath + "\\" + GetNameGDB(outputFolderPath, outputName); } else { cr.out_rasterdataset = outputFolderPath + "\\" + GetNameRaster(outputFolderPath, outputName); } IWorkspaceFactory2 in_wsf = new FileGDBWorkspaceFactoryClass(); IRasterWorkspace2 in_rstWs = (IRasterWorkspace2)in_wsf.OpenFromFile(gdb_Path, 0); IRasterLayer in_rstLyr = new RasterLayerClass(); IRasterDataset in_rstDs = in_rstWs.OpenRasterDataset(feat_Name); in_rstLyr.CreateFromDataset(in_rstDs); IRasterProps in_rasterProps = (IRasterProps)in_rstLyr.Raster; int Height = in_rasterProps.Height; int Width = in_rasterProps.Width; rstPixelType in_rstPT = in_rasterProps.PixelType; int BandsCount = in_rstLyr.BandCount; Dictionary <rstPixelType, int> DictPT = new Dictionary <rstPixelType, int>(); DictPT.Clear(); DictPT.Add(rstPixelType.PT_DOUBLE, 64); DictPT.Add(rstPixelType.PT_FLOAT, 32); DictPT.Add(rstPixelType.PT_LONG, 32); DictPT.Add(rstPixelType.PT_SHORT, 32); DictPT.Add(rstPixelType.PT_UCHAR, 8); DictPT.Add(rstPixelType.PT_ULONG, 32); DictPT.Add(rstPixelType.PT_USHORT, 32); DictPT.Add(rstPixelType.PT_CHAR, 8); int Depth = 32; DictPT.TryGetValue(in_rasterProps.PixelType, out Depth); Size = 1.0 * Height * Width * BandsCount * Depth / 8.0 / 1024 / 1024; target.Size = Size; Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; IGeoProcessorResult gpr = gp.Execute(cr, null) as IGeoProcessorResult; if (gpr != null && gpr.Status == esriJobStatus.esriJobSucceeded) { DateTime tm2 = DateTime.Now; TimeSpan ts1 = new TimeSpan(startTime.Ticks); TimeSpan ts2 = new TimeSpan(tm2.Ticks); TimeSpan ts3 = ts2.Subtract(ts1); string out1, out2; GetConfig("Size", out out1); GetConfig("Time", out out2); double size = double.Parse(out1); double time = double.Parse(out2); SaveConfig("Size", (size + Size).ToString()); SaveConfig("Time", (time + ts3.TotalSeconds).ToString()); target.IsFinished = true; target.IsBusy = false; OnFinished(new LayerOperaEventArgs(target)); } else { } } catch (Exception e) { } }