ArcEngine 栅格数据 总结

Posted 杨千羽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArcEngine 栅格数据 总结相关的知识,希望对你有一定的参考价值。

<header id="header">

				<nav class="nav-container group" id="nav-mobile">
			<div class="nav-toggle"><i class="fa fa-bars"></i></div>
			<div class="nav-text"><!-- put your mobile menu text here --></div>
			<div class="nav-wrap container"><ul id="menu-gis%e4%b8%bb%e8%a6%81%e7%9b%ae%e5%bd%95" class="nav container-inner group"><li id="menu-item-35143" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-35143"><a href="https://www.giserdqy.com/gis/gisknowledge/">GIS理论</a></li>
  • 商务合作
  • 								<div class="container">
    					<div class="container-inner">
    						<div class="toggle-search"><i class="fa fa-search"></i></div>
    						<div class="search-expand">
    							<div class="search-expand-inner">
    								<form method="get" class="searchform themeform" action="https://www.giserdqy.com/">
    <div>
    	<input type="text" class="search" name="s" onblur="if(this.value=='')this.value='To search type and hit enter';" onfocus="if(this.value=='To search type and hit enter')this.value='';" value="To search type and hit enter">
    </div>
    
    		</nav><!--/#nav-mobile-->
    	
    	
    	
    	<div class="container-inner group">
    
    						<div class="group pad">
    				<p class="site-title"><a href="https://www.giserdqy.com/" rel="home">GIS开发者</a></p>
    										<p class="site-description">gis,openlayers,leaflet,gis应用,geoai,geoserver,cesium,python,arcpy,arcmap,webgis</p>
    													</div>
    		
    		
    						<nav class="nav-container group" id="nav-header">
    				<div class="nav-toggle"><i class="fa fa-bars"></i></div>
    				<div class="nav-text"><!-- put your mobile menu text here --></div>
    				<div class="nav-wrap container"><ul id="menu-%e6%a0%b8%e5%bf%83%e7%9b%ae%e5%bd%95" class="nav group"><li id="menu-item-35187" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-home menu-item-35187"><a href="https://www.giserdqy.com/">首页</a></li>
    
  • GIS论坛
  • 	</div><!--/.container-->
    	<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    
    </header><!--/#header-->
    
    <div class="container" id="page">
    	<div class="container-inner">
    		<div class="main">
    			<div class="main-inner group">
    
    <div class="page-title pad group">
    
    		<ul class="meta-single group">
    		<li class="category"><a href="https://www.giserdqy.com/arcengine/" rel="category tag">ArcEngine</a></li>
    				</ul>
    
    				<article class="post-26805 post type-post status-publish format-standard has-post-thumbnail hentry category-arcengine tag-arcengine tag-arcengine--">
    			<div class="post-inner group">
    				<p id="breadcrumbs"><span><span><a href="https://www.giserdqy.com/">首页</a> » <span><a href="https://www.giserdqy.com/arcengine/">ArcEngine</a> » <span class="breadcrumb_last" aria-current="page">ArcEngine 栅格数据 总结</span></span></span></span></p>					<h1 class="post-title">ArcEngine 栅格数据 总结</h1>
    				
    				<div class="bdsharebuttonbox bdshare-button-style0-24" data-bd-bind="1563003962454"><a href="#" class="bds_more" data-cmd="more"></a><a href="#" class="bds_mshare" data-cmd="mshare" title="分享到一键分享"></a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信"></a><a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友"></a><a href="#" class="bds_youdao" data-cmd="youdao" title="分享到有道云笔记"></a><a href="#" class="bds_twi" data-cmd="twi" title="分享到Twitter"></a><a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook"></a><a href="#" class="bds_linkedin" data-cmd="linkedin" title="分享到linkedin"></a><a href="#" class="bds_copy" data-cmd="copy" title="分享到复制网址"></a><a href="#" class="bds_print" data-cmd="print" title="分享到打印"></a></div>
    
    				<p class="post-byline">by <a href="https://www.giserdqy.com/author/dqy/" title="由giser发布" rel="author">giser</a> · 2019-04-05</p>
    
    				
    				<div class="clear"></div>
    
    				<div class="entry ">
    					<div class="entry-inner">
    						<div id="ez-toc-container" class="counter-hierarchy counter-decimal">
    

    目录


    本文大部分内容参考AO帮助文档和ArcMap帮助文档,大家实际使用中遇到的问题也可以在本帖下方留言交流,谢谢!
    欢迎浏览,拒绝转载!

    基础知识

    栅格数据基础知识:传送门

    栅格数据是由一系列等间距的格网矩阵组成,用来表达完整的主题、光谱、图像信息。
    栅格数据模型分为栅格数据集(Raster dataset)、栅格目录(Raster catalog)、镶嵌数据集(Mosaic dataset)。他们以文件系统、个人地理数据库、文件地理数据库、企业地理数据库为数据源进行存储。

    栅格数据集也就是我们经常所得的jpg、tif文件等, ArcGIS 将这些栅格数据抽象为 RasterDataset,栅格数据集就代表了磁盘中的一个文件,它由一个或多个波段组成。在使用栅格数据集的时候,栅格数据会被转换成 IMG 文件存储在数据库中。我们可以对栅格数据集进行一些操作,如改变空间参考,建立影像金字塔等。

    栅格目录,正如其名字一样是一个目录,跟书的目录相似,它记录了一个或者多个栅格数据集,每一个栅格数据集都作为一条记录存储在栅格目录中。栅格目录对栅格数据集的管理有两种方式,托管和非托管。托管方式的时候,栅格数据是存储在数据库中, 非托管的时候,栅格目录记录了栅格数据集的路径,也就是栅格数据并没有存储在数据库中。当我们删除一条记录的时候,对我们的栅格数据没有任何影响。

    镶嵌数据集可以说是栅格数据集和栅格目录的混合技术,它的存储方式和栅格目录类似,但是在使用的时候和普通的栅格数据集是一样的,镶嵌数据集用于管理和发布海量多分辨率,多传感器影像,对栅格数据提供了动态镶嵌和实时处理的功能。
    对文件地理数据库、个人地理数据库和 ArcSDE 地理数据库中的栅格存储加以比较:

    类图

    在线地址:传送门
    本地路径:本地路径:如 C:\\Program Files (x86)\\ArcGIS\\DeveloperKit10.2\\Diagrams\\DataSourcesRasterObjectModel.pdf
    ##和栅格数据集有关的GP工具

    栅格数据接口使用示例

    打开栅格数据工作空间

     
    1. //Open a file geodatabase workspace as RasterWorkspace.
    2. static IRasterWorkspaceEx OpenFGDB(string FGDBPath)
    3. //FGDBPath string example: c:\\data\\raster.gdb.
    4. IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactoryClass();
    5. return (IRasterWorkspaceEx)workspaceFactory.OpenFromFile(FGDBPath, 0);
    6. //Open an ArcSDE workspace as RasterWorkspace.
    7. static IRasterWorkspaceEx OpenSDE(string conString)
    8. //conString example: SERVER=ais;INSTANCE=9200;VERSION=sde.DEFAULT;USER=raster;PASSWORD=raster.
    9. IWorkspaceFactory2 workspaceFactory = new SdeWorkspaceFactoryClass();
    10. return (IRasterWorkspaceEx)workspaceFactory.OpenFromString(conString, 0);
    11. //Open an accessed workspace as RasterWorkspace.
    12. static IRasterWorkspaceEx OpenAccess(string PGDBPath)
    13. //FGDBPath string example: c:\\data\\rasters.mdb.
    14. IWorkspaceFactory2 workspaceFactory = new AccessWorkspaceFactoryClass();
    15. return (IRasterWorkspaceEx)workspaceFactory.OpenFromFile(PGDBPath, 0);
    16. //Open a file workspace as RasterWorkspace.
    17. static IRasterWorkspace OpenFileWorkspace(string wsPath)
    18. //wsPath example: c:\\data\\rasters.
    19. IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
    20. return (IRasterWorkspace)workspaceFact.OpenFromFile(wsPath, 0);

    打开栅格数据集

     
    1. IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(datasetName);
     
    1. IRasterDataset rasterDataset = rasterWorkspaceEx.OpenRasterDataset(datasetName);

    创建栅格数据集

    1. Creating a raster dataset(创建以文件存储的栅格数据集,如TIFF)
      1)Create a workspace.创建工作空间
      2)Create a TIFF file with specified width, height, pixel type, cell size, and other necessary dimensions.创建一个TIFF文件,并指定它的宽度,高度,像素类型、像元大小和其他必要的维度。
      3)Use the IPixelBlock and IRasterEdit interfaces to edit the pixel values.使用IPixelBlock 和 IRasterEdit 接口来编辑像元的值。
     
    1. public static IRasterDataset CreateRasterDataset(string Path, string FileName)
    2. try
    3. IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path);
    4. //Define the spatial reference of the raster dataset.
    5. ISpatialReference sr = new UnknownCoordinateSystemClass();
    6. //Define the origin for the raster dataset, which is the lower left corner of the raster.
    7. IPoint origin = new PointClass();
    8. origin.PutCoords(15.0, 15.0);
    9. //Define the dimensions of the raster dataset.
    10. int width = 100; //This is the width of the raster dataset.
    11. int height = 100; //This is the height of the raster dataset.
    12. double xCell = 30; //This is the cell size in x direction.
    13. double yCell = 30; //This is the cell size in y direction.
    14. int NumBand = 1; // This is the number of bands the raster dataset contains.
    15. //Create a raster dataset in TIFF format.
    16. IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "TIFF",
    17. origin, width, height, xCell, yCell, NumBand, rstPixelType.PT_UCHAR, sr,
    18. true);
    19. //If you need to set NoData for some of the pixels, you need to set it on band
    20. //to get the raster band.
    21. IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset;
    22. IRasterBand rasterBand;
    23. IRasterProps rasterProps;
    24. rasterBand = rasterBands.Item(0);
    25. rasterProps = (IRasterProps)rasterBand;
    26. //Set NoData if necessary. For a multiband image, a NoData value needs to be set for each band.
    27. rasterProps.NoDataValue = 255;
    28. //Create a raster from the dataset.
    29. IRaster raster = rasterDataset.CreateFullRaster();
    30. //Create a pixel block using the weight and height of the raster dataset.
    31. //If the raster dataset is large, a smaller pixel block should be used.
    32. //Refer to the topic "How to access pixel data using a raster cursor".
    33. IPnt blocksize = new PntClass();
    34. blocksize.SetCoords(width, height);
    35. IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize)as IPixelBlock3;
    36. //Populate some pixel values to the pixel block.
    37. System.Array pixels;
    38. pixels = (System.Array)pixelblock.get_PixelData(0);
    39. for (int i = 0; i < width; i++)
    40. for (int j = 0; j < height; j++)
    41. if (i == j)
    42. pixels.SetValue(Convert.ToByte(255), i, j);
    43. else
    44. pixels.SetValue(Convert.ToByte((i * j) / 255), i, j);
    45. pixelblock.set_PixelData(0, (System.Array)pixels);
    46. //Define the location that the upper left corner of the pixel block is to write.
    47. IPnt upperLeft = new PntClass();
    48. upperLeft.SetCoords(0, 0);
    49. //Write the pixel block.
    50. IRasterEdit rasterEdit = (IRasterEdit)raster;
    51. rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock);
    52. //Release rasterEdit explicitly.
    53. System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit);
    54. return rasterDataset;
    55. catch (Exception ex)
    56. System.Diagnostics.Debug.WriteLine(ex.Message);
    57. return null;
    58. public static IRasterWorkspace2 OpenRasterWorkspace(string PathName)
    59. //This function opens a raster workspace.
    60. try
    61. IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass();
    62. return workspaceFact.OpenFromFile(PathName, 0)as IRasterWorkspace2;
    63. catch (Exception ex)
    64. System.Diagnostics.Debug.WriteLine(ex.Message);
    65. return null;
    1. Creating a raster dataset in a geodatabase(创建以GDB存储的栅格数据集,如GRID)
      1)Create a file geodatabase workspace factory.创建哥FGDB的工作工厂对象。
      2)Create a raster workspace and query IRasterWorkspaceEx.创建一个栅格工作空间对象并且转换到IRasterWorkspaceEx对象上
      3)Define the storage and raster properties using the RasterStorageDef and RasterDef classes. 利用RasterStorageDef 和RasterDef 类定义栅格数据集的存储和栅格属性。
      4)Create the dataset using the CreateRasterDataset method.利用CreateRasterDataset方法创建栅格数据集。
     
    1. IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
    2. IRasterWorkspaceEx ws = (IRasterWorkspaceEx)wsf.OpenFromFile(@"c:\\temp\\fgdb.gdb", 0);
    3. //Define the raster storage.
    4. IRasterStorageDef storage = new RasterStorageDefClass();
    5. storage.Tiled = true;
    6. storage.TileHeight = 128;
    7. storage.TileWidth = 128;
    8. //Define the spatial reference.
    9. IRasterDef rasterDef = new RasterDefClass();
    10. rasterDef.SpatialReference = new UnknownCoordinateSystemClass();
    11. //Create data.
    12. IRasterDataset rasterDs = ws.CreateRasterDataset("newraster", 3,
    13. rstPixelType.PT_SHORT, storage, "sde.DEFAULT", rasterDef, null);

    访问HDF和NIFF数据的子集

    一些栅格格式,如分层数据格式(HDF),可以在单个文件中包含多个subdatasets。访问HDF subdatasets使用IRasterDatasetJukebox接口,请参见下面的代码示例:

     
    1. IWorkspaceFactory wsf = new FileGDBWorkspaceFactoryClass();
    2. IRasterWorkspaceEx ws = (IRasterWorkspaceEx)wsf.OpenFromFile(@"c:\\temp\\fgdb.gdb", 0);
    3. //Define the raster storage.
    4. IRasterStorageDef storage = new RasterStorageDefClass();
    5. storage.Tiled = true;
    6. storage.TileHeight = 128;
    7. storage.TileWidth = 128;
    8. //Define the spatial reference.
    9. IRasterDef rasterDef = new RasterDefClass();
    10. rasterDef.SpatialReference = new UnknownCoordinateSystemClass();
    11. //Create data.
    12. IRasterDataset rasterDs = ws.CreateRasterDataset("newraster", 3,
    13. rstPixelType.PT_SHORT, storage, "sde.DEFAULT", rasterDef, null);

    读取JPEG EXIF扩展标记信息

     
    1. public static void JPEG_EXIFtag(IRasterDataset exifDataset)
    2. //exifDataset represents a raster dataset opened from a JPEG file that has EXIF tags.
    3. IDataset dataset = (IDataset)exifDataset;
    4. //Get the EXIF tags and the associated values.
    5. IPropertySet propertySet = dataset.PropertySet;
    6. System.Object tag_names;
    7. System.Object tag_values;
    8. propertySet.GetAllProperties(out tag_names, out tag_values);
    9. string[] stringNames = (string[])tag_names;
    10. object[] stringValues = (object[])tag_values;
    11. for (int i = 0; i < stringNames.Length - 1; i++)
    12. System.Console.WriteLine(stringNames[i]);
    13. System.Console.WriteLine(stringValues[i]);

    读取像素数据

    • 使用光栅游标访问像素数据
     
    1. public static void UsingRasterCursorWithPixelBlock(IRasterDataset2 rasterDs)
    2. try
    3. //Create a raster.
    4. IRaster2 raster2 = rasterDs.CreateFullRaster()as IRaster2;
    5. //Create a raster cursor with a system-optimized pixel block size by passing a null.
    6. IRasterCursor rasterCursor = raster2.CreateCursorEx(null);
    7. //Use the IRasterEdit interface.
    8. IRasterEdit rasterEdit = raster2 as IRasterEdit;
    9. //Loop through each band and pixel block.
    10. IRasterBandCollection bands = rasterDs as IRasterBandCollection;
    11. IPixelBlock3 pixelblock3 = null;
    12. long blockwidth = 0;
    13. long blockheight = 0;
    14. System.Array pixels;
    15. IPnt tlc = null;
    16. object v;
    17. long bandCount = bands.Count;
    18. do
    19. 以上是关于ArcEngine 栅格数据 总结的主要内容,如果未能解决你的问题,请参考以下文章

      如何平均单个 PostGIS 栅格表中的所有波段?

      arcEngine开发之加载栅格数据

      R语言入门——读取栅格数据参数解读

      栅格那点儿事(四B)---多波段栅格数据的显示

      C#+arcengine获得栅格数据的属性表

      C#+arcengine获得栅格数据的属性表