1、 1、栅格数据的存储类型 栅格数据一般可以存储为ESRI GRID(由一系列文件组成),TIFF格式(包括一个TIF文件和一个AUX文件),IMAGINE Image格式 在AE中一般调用ISaveAs接口来保存栅格数据 2、栅格数据集和栅格编目的区别 一个栅格数据集由一个或者多个波段(RasterBand)的数据组成,一个波段就是一个数据矩阵。对于格网数据(DEM数据)和单波段的影像数据,表现为仅仅只有一个波段数据的栅格数据集,而对于多光谱影像数据则表现为具有多个波段的栅格数据集 栅格编目(RasterCatalog)用于显示某个研究区域内各种相邻的栅格数据,这些相
2、邻的栅格数据没有经过拼接处理合成一副大的影像图 3、IRasterWorkspaceEx与IRasterWorkspace ,IRsterWorkspace2的区别 1).IRasteWorkspaceEx接口主要是用来读取GeoDatabase中的栅格数据集和栅格编目 2) . IRasterWorkspace ,IRsterWorkspace2主要是用来读取以文件格式存储在本地的栅格数据 4、加载栅格数据(以存储在本地的栅格数据文件为例) 1.直接用IRasterLayer接口打开一个栅格文件并加载到地图控件 IRasterLayer rasterLay
3、er = new RasterLayerClass(); rasterLayer.CreateFromFilePath(fileName); // fileName指存本地的栅格文件路径 axMapControl1.AddLayer(rasterLayer, 0); 2. 用IRasterDataset接口打开一个栅格数据集 IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory(); IWorkspace workspace; workspace = workspaceFactory.
4、OpenFromFile(inPath, 0); //inPath栅格数据存储路径 if (workspace == null) { Console.WriteLine("Could not open the workspace."); return; } IRasterWorkspace rastWork = (IRasterWorkspace)workspace; IRasterDataset rastDataset; rastDataset= rastWork.OpenRasterDataset(inName);//inName栅格文件名
5、 if (rastDataset == null) { Console.WriteLine("Could not open the raster dataset."); return; } 5、如何读取栅格数据的属性和遍历栅格数据 栅格数据的属性包括栅格大小,行数,列数,投影信息,栅格范围等等,见下面代码 (假设当前加载的栅格文件栅格值存储方式为:UShort类型) IRasterProps rasterProps = (IRasterProps)clipRaster; int dHeight = rasterProps.Height;
6、//当前栅格数据集的行数 int dWidth = rasterProps.Width; //当前栅格数据集的列数 double dX = rasterProps.MeanCellSize().X; //栅格的宽度 double dY = rasterProps.MeanCellSize().Y; //栅格的高度 IEnvelope extent=rasterProps.Extent; //当前栅格数据集的范围 rstPixelType pixelType=rasterProps.PixelType; //当前栅格像素类型 IPnt pntSize =
7、new PntClass(); pntSize.SetCoords(dX, dY); IPixelBlock pixelBlock = clipRaster.CreatePixelBlock(pntSize); IPnt pnt = new PntClass(); for (int i = 0; i < dHeight; i++) for (int j = 0; j < dWidth; j++) { pnt.SetCoords(i, j); clipRaster.Read(pnt, pixelBlock); if (pixelBlock !
8、 null) { object obj = pixelBlock.GetVal(0, 0, 0); MessageBox.Show( Convert.ToUInt32(obj).ToString()); } } 6、如何提取指定的范围的栅格数据 提取指定范围内的栅格数据通常用两种方法IRasterLayerExport(esriCarto), IExtractionOp, IExtractionOp2 (esriSpatialAnalyst),IRasterLayerExport接口提供的栅格数据提取功能有限,只能以矩形范围作为提取范围,而IExtr
9、actionOp接口提供了多边形,圆,属性,矩形等几种形式作为提取栅格数据. 1).IRasterLayerExport接口 IRasterLayerExport rLayerExport = new RasterLayerExportClass(); rLayerExport.RasterLayer = rasterLayer;// rasterLayer指当前加载的栅格图层 rLayerExport.Extent = clipExtent;//clipExtent指提取栅格数据的范围 if (proSpatialRef != null) rLayerEx
10、port.SpatialReference = proSpatialRef;// proSpatialRef当前栅格数据的投影信息 IWorkspaceFactory pWF = new RasterWorkspaceFactoryClass(); try { IWorkspace pRasterWorkspace = pWF.OpenFromFile(_folder, 0);// _folder指栅格文件保存路径 IRasterDataset outGeoDataset = rLayerExport.Export(pRasterWorkspace, code,
11、strRasterType); //调用ISaveAs接口将导出的数据集保存 …………………….. } Catch(Exception ex) { Throw new Argumention(ex.Message); } 2.IExtractionOp接口(调用此接口前,应该先检查空间许可) IExtractionOp extraction = new RasterExtractionOpClass(); try { IGeoDataset geoDataset = extraction.Rectangle((IGeoDa
12、taset)clipRaster, clipExtent, true); IRaster raster = geoDataset as IRaster; if (raster != null) { IWorkspaceFactory WF = new RasterWorkspaceFactoryClass(); IWorkspace rasterWorkspace = WF.OpenFromFile(_folder, 0); ISaveAs saveAs = (ISaveAs)raster; saveAs.SaveAs(“Result.tif”, ra
13、sterWorkspace, "TIFF"); } } catch (Exception ex) { MessageBox..Show(Ex.message); } 6.2如何创建一个栅格数据 public IRasterDataset CreateFileRasterDataset(string directoryName, string fileName) { // This function creates a new img file in the given workspace
14、 // and then assigns pixel values try { IRasterDataset rasterDataset = null; IPoint originPoint = new PointClass(); originPoint.PutCoords(0, 0); // Create the dataset IRasterWorkspace2 r
15、asterWorkspace2 = null; rasterWorkspace2 = CreateRasterWorkspace(directoryName); rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, rstPixelType.PT_UCHAR, new UnknownCoordinateSystemClass(), true);
16、 IRawPixels rawPixels = null; //之后他代表第一波段(实际只有一个波段)纵横代码 IPixelBlock3 pixelBlock3 = null; //承载栅格影像数据的块(内存块吗。。) IPnt pixelBlockOrigin = null; IPnt pixelBlockSize = null; IRasterBandCollection rasterBandCollection;
17、 IRasterProps rasterProps; // QI for IRawPixels and IRasterProps rasterBandCollection = (IRasterBandCollection)rasterDataset; rawPixels = (IRawPixels)rasterBandCollection.Item(0); rasterProps = (IRasterProps)rawPixels;
18、 // Create pixelblock 创建像素块 pixelBlockOrigin = new DblPntClass(); pixelBlockOrigin.SetCoords(0, 0); pixelBlockSize = new DblPntClass(); pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);
19、pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize); // 上述函数为参数为IPnt Size,返回值为IPixelBlock // Read pixelblock 读取像素块,虽然它是由rawPixels创造的 rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3); // Get pixeldata array
20、 System.Object[,] pixelData; pixelData = (System.Object[,])pixelBlock3.get_PixelDataByRef(0); //参数int plane // Loop through all the pixels and assign value for (int i = 0; i < rasterProps.Width; i++) for (int j = 0; j < rast
21、erProps.Height; j++) pixelData[i, j] = (i * j) % 255; // Write the pixeldata back System.Object cachePointer; cachePointer = rawPixels.AcquireCache(); rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelB
22、lock3); rawPixels.ReturnCache(cachePointer); // Return raster dataset return rasterDataset; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); return null
23、 } } public IRasterWorkspace2 CreateRasterWorkspace(string pathName) { // Create RasterWorkspace IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass(); return workspaceFactory.OpenFromFile(pathName, 0
24、) as IRasterWorkspace2; } 6.3依旧是生成栅格文件 public IRasterDataset CreateRasterDataset(string Path, string FileName) { try { //Create raster workspace. This example also works with any other workspaces that support raster data such as
25、a file geodatabase (FGDB) workspace. //Access the workspace and the SDE workspace. IRasterWorkspace2 rasterWs = OpenRasterWorkspace(Path); //Define the spatial reference of the raster dataset. ISpatialReference sr = new UnknownCoord
26、inateSystemClass(); //Define the origin for the raster dataset, which is the lower left corner of the raster. IPoint origin = new PointClass(); origin.PutCoords(15.0, 15.0); //Define the dimension of the raster dataset.
27、 int width = 100; //This is the width of the raster dataset. int height = 100; //This is the height of the raster dataset. double xCell = 30; //This is the cell size in x direction. double yCell = 30; //This is the cell size in y direction.
28、 int NumBand = 1; // This is the number of bands the raster dataset contains. //Create a raster dataset in grid format. IRasterDataset rasterDataset = rasterWs.CreateRasterDataset(FileName, "GRID", origin, width, height, xCell, yCell
29、 NumBand, rstPixelType.PT_UCHAR, sr, true); //Get the raster band. IRasterBandCollection rasterBands = (IRasterBandCollection)rasterDataset; IRasterBand rasterBand; IRasterProps rasterProps; ra
30、sterBand = rasterBands.Item(0); rasterProps = (IRasterProps)rasterBand; //Set NoData if necessary. For a multiband image, NoData value needs to be set for each band. rasterProps.NoDataValue = 255; //Create a raster from the dataset.
31、 IRaster raster = rasterDataset.CreateDefaultRaster(); //Create a pixel block. IPnt blocksize = new PntClass(); blocksize.SetCoords(width, height); IPixelBlock3 pixelblock = raster.CreatePixelBlock(blocksize) as I
32、PixelBlock3; //Populate some pixel values to the pixel block. System.Array pixels; pixels = (System.Array)pixelblock.get_PixelData(0); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++)
33、 if (i == j) pixels.SetValue(Convert.ToByte(255), i, j); else pixels.SetValue(Convert.ToByte((i * j) / 255), i, j); pixelblock.set_PixelData(0, (System.Array)pixels);
34、 //Define the location that the upper left corner of the pixel block is to write. IPnt upperLeft = new PntClass(); upperLeft.SetCoords(0, 0); //Write the pixel block. IRasterEdit rasterEdit = (IRasterEdit)raster;
35、 rasterEdit.Write(upperLeft, (IPixelBlock)pixelblock); //Release rasterEdit explicitly. System.Runtime.InteropServices.Marshal.ReleaseComObject(rasterEdit); return rasterDataset; } catch (Exception ex) {
36、 System.Diagnostics.Debug.WriteLine(ex.Message); return null; } } public IRasterWorkspace2 OpenRasterWorkspace(string PathName) { //This function opens a raster workspace. try {
37、 IWorkspaceFactory workspaceFact = new RasterWorkspaceFactoryClass(); return workspaceFact.OpenFromFile(PathName, 0) as IRasterWorkspace2; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message);
38、 return null; } } 来自http://bbs.esrichina- 7.栅格数据重采样 栅格数据的重采样主要基于三种方法:最邻近采样(NEAREST),双线性 ILINEAR)和三次卷积采样(CUBIC)。 (1).最邻近采样:它用输入栅格数据中最临近栅格值作为输出值。因此,在重采 样后的输出栅格中的每个栅格值, 都是输入栅格数据中真实存在而未加任何改变的值。这种方法简单易用,计算量小,重采样的速度最快。 (2).双线性采样:此重采样法取待采样点(x,y)点周围四个邻点,在y方向(或X
39、方向)内插两次,再在x方向(或y方向)内插一次,得到(x,y)点的栅格值。 (3).三次卷积采样:这是进一步提高内插精度的一种方法。它的基本思想是增加邻点来获 得最佳插值函数。取待计算点周围相邻的16个点,与双线性采样类似,可先在某一方向上内插,如先在x方向上,每四个值依次内插四次,再根据四次的计算结果在y方上内插,最终得到内插结果 代码示例:采用双线性采样 IRasterGeometryProc rasterGeometryProc = new RasterGeometryProcClass(); rasterGeometryProc.Resample(rstR
40、esamplingTypes.RSP_CubicConvolution, newCellSize, clipRaster); 栅格数据重分类 (2009-01-10 10:10:09) 标签:栅格 重分类 分类:AE二次开发 public static IRasterLayer SetViewShedRenderer(IRaster pInRaster,string sField,string sPath) { IRasterDescriptor pRD = new RasterDescriptorClass(); pRD.Create(pInRaste
41、r, new QueryFilterClass(), sField); IReclassOp pReclassOp = new RasterReclassOpClass(); IGeoDataset pGeodataset=pInRaster as IGeoDataset; IRasterAnalysisEnvironment pEnv = pReclassOp as IRasterAnalysisEnvironment; IWorkspaceFactory pWSF=new RasterWorkspaceFactoryClass(); IWorkspace pWS = p
42、WSF.OpenFromFile(sPath, 0); pEnv.OutWorkspace = pWS; object objSnap = null; object objExtent = pGeodataset.Extent; pEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref objExtent, ref objSnap); pEnv.OutSpatialReference = pGeodataset.SpatialReference; IRasterLayer pRLayer = new
43、RasterLayerClass(); IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection; IRasterBand pRasterBand = pRsBandCol.Item(0); pRasterBand.ComputeStatsAndHist(); IRasterStatistics pRasterStatistic = pRasterBand.Statistics; double dMaxValue = pRasterStatistic.Maximum ; double
44、dMinValue = pRasterStatistic.Minimum ; INumberRemap pNumRemap = new NumberRemapClass(); pNumRemap.MapRange(dMinValue, 0, 0); pNumRemap.MapRange(0, dMaxValue, 1); IRemap pRemap = pNumRemap as IRemap; IRaster pOutRaster = pReclassOp.ReclassByRemap(pGeodataset, pRemap, false) as IRaster ;
45、 pRLayer.CreateFromRaster(pOutRaster); return pRLayer; } 栅格图层和矢量图层的属性表浏览 if (pLyr is IFeatureLayer) { DataTable pTable = new DataTable(); IFeatureLayer pFealyr = pLyr as IFeatureLayer; IFeatureClass pFCls = pFealyr.FeatureClass; string shape = ""; if (pFCls.ShapeType == es
46、riGeometryType.esriGeometryPoint) shape = "Point"; else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolyline) shape = "Polyline"; else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolygon) shape = "Polygon"; for (int i = 0; i < pFCls.Fields.FieldCount; i++) { pTable.
47、Columns.Add(pFCls.Fields.get_Field(i).Name); } IFeatureCursor pCursor = pFCls.Search(null, false); int ishape = pFCls.Fields.FindField("Shape"); IFeature pFea = pCursor.NextFeature(); while (pFea != null) { DataRow pRow = pTable.NewRow(); for (int i = 0; i < pFCls.Fields.FieldCount;
48、i++) { if (i == ishape) { pRow[i] = shape; continue; } pRow[i] = pFea.get_Value(i).ToString(); } pTable.Rows.Add(pRow); pFea = pCursor.NextFeature(); } dataGridView1.DataSource = pTable; } else if (pLyr is IRasterLayer) { IRasterLayer pRlyr = pLyr as IRasterLayer; I
49、Raster pRaster = pRlyr.Raster; IRasterProps pProp = pRaster as IRasterProps; pProp.PixelType = rstPixelType.PT_LONG; if (pProp.PixelType == rstPixelType.PT_LONG) { IRasterBandCollection pBcol = pRaster as IRasterBandCollection; IRasterBand pBand = pBcol.Item(0); ITable pRTable = pBand.AttributeTable; DataTable pTable = new DataTable(); for (int i = 0; i < pRTable.Fields.FieldCount; i++) pTable.Columns.Add(pRTable.Fields.get_Field(i).Name); ICursor pCursor= pRTable.Search(null, false); IRow pRrow= pCursor.NextRow(); while (pRrow !=






