转自原文 栅格数据AE

两个星期以来一直与栅格数据打交道,对AO的栅格部分应该有了一定的理解,下面是自己的一点体会,希望高手指教:-)

1、栅格数据的存储类型

栅格数据一般可以存储为ESRI GRID(由一系列文件组成),TIFF格式(包括一个TIF文件和一个AUX文件),IMAGINE Image格式 在AE中一般调用ISaveAs接口来保存栅格数据

2、栅格数据集和栅格编目的区别

一个栅格数据集由一个或者多个波段(RasterBand)的数据组成,一个波段就是一个数据矩阵。对于格网数据(DEM数据)和单波段的影像数据,表现为仅仅只有一个波段数据的栅格数据集,而对于多光谱影像数据则表现为具有多个波段的栅格数据集

栅格编目(RasterCatalog)用于显示某个研究区域内各种相邻的栅格数据,这些相邻的栅格数据没有经过拼接处理合成一副大的影像图

3、IRasterWorkspaceEx与IRasterWorkspace ,IRsterWorkspace2的区别

1).IRasteWorkspaceEx接口主要是用来读取GeoDatabase中的栅格数据集和栅格编目

2) . IRasterWorkspace ,IRsterWorkspace2主要是用来读取以文件格式存储在本地的栅格数据

4、加载栅格数据(以存储在本地的栅格数据文件为例)

1.直接用IRasterLayer接口打开一个栅格文件并加载到地图控件

IRasterLayer rasterLayer = new RasterLayerClass();
rasterLayer.CreateFromFilePath(fileName); // fileName指存本地的栅格文件路径
axMapControl1.AddLayer(rasterLayer, );

2. 用IRasterDataset接口打开一个栅格数据集

IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
IWorkspace workspace;
workspace = workspaceFactory.OpenFromFile(inPath, ); //inPath栅格数据存储路径
if (workspace == null)
{
Console.WriteLine("Could not open the workspace.");
return;
}
IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
IRasterDataset rastDataset;
rastDataset= rastWork.OpenRasterDataset(inName);//inName栅格文件名
if (rastDataset == null)
{
Console.WriteLine("Could not open the raster dataset.");
return;
}

5、如何读取栅格数据的属性和遍历栅格数据

栅格数据的属性包括栅格大小,行数,列数,投影信息,栅格范围等等,见下面代码

(假设当前加载的栅格文件栅格值存储方式为:UShort类型)

IRasterProps rasterProps = (IRasterProps)clipRaster;
int dHeight = rasterProps.Height;//当前栅格数据集的行数
int dWidth = rasterProps.Width; //当前栅格数据集的列数
double dX = rasterProps.MeanCellSize().X; //栅格的宽度
double dY = rasterProps.MeanCellSize().Y; //栅格的高度
IEnvelope extent=rasterProps.Extent; //当前栅格数据集的范围
rstPixelType pixelType=rasterProps.PixelType; //当前栅格像素类型
IPnt pntSize = new PntClass();
pntSize.SetCoords(dX, dY);
IPixelBlock pixelBlock = clipRaster.CreatePixelBlock(pntSize);
IPnt pnt = new PntClass();
for (int i = ; i < dHeight; i++)
for (int j = ; j < dWidth; j++)
{
pnt.SetCoords(i, j);
clipRaster.Read(pnt, pixelBlock);
if (pixelBlock != null)
{
object obj = pixelBlock.GetVal(, , );
MessageBox.Show( Convert.ToUInt32(obj).ToString());
}
}

6、如何提取指定的范围的栅格数据

提取指定范围内的栅格数据通常用两种方法IRasterLayerExport(esriCarto), IExtractionOp, IExtractionOp2 (esriSpatialAnalyst),IRasterLayerExport接口提供的栅格数据提取功能有限,只能以矩形范围作为提取范围,而IExtractionOp接口提供了多边形,圆,属性,矩形等几种形式作为提取栅格数据.

1).IRasterLayerExport接口

IRasterLayerExport rLayerExport = new RasterLayerExportClass();
rLayerExport.RasterLayer = rasterLayer;// rasterLayer指当前加载的栅格图层
rLayerExport.Extent = clipExtent;//clipExtent指提取栅格数据的范围
if (proSpatialRef != null)
rLayerExport.SpatialReference = proSpatialRef;// proSpatialRef当前栅格数据的投影信息
IWorkspaceFactory pWF = new RasterWorkspaceFactoryClass();
try
{
IWorkspace pRasterWorkspace = pWF.OpenFromFile(_folder, );// _folder指栅格文件保存路径
IRasterDataset outGeoDataset = rLayerExport.Export(pRasterWorkspace, code, strRasterType);
//调用ISaveAs接口将导出的数据集保存
……………………..
}
Catch(Exception ex)
{
Throw new Argumention(ex.Message);
}

2).IExtractionOp接口(调用此接口前,应该先检查空间许可)

IExtractionOp extraction = new RasterExtractionOpClass();
try
{
IGeoDataset geoDataset = extraction.Rectangle((IGeoDataset)clipRaster, clipExtent, true);
IRaster raster = geoDataset as IRaster;
if (raster != null)
{
IWorkspaceFactory WF = new RasterWorkspaceFactoryClass();
IWorkspace rasterWorkspace = WF.OpenFromFile(_folder, );
ISaveAs saveAs = (ISaveAs)raster;
saveAs.SaveAs(“Result.tif”, rasterWorkspace, "TIFF");
}
}
catch (Exception ex)
{
MessageBox..Show(Ex.message);
}

7.栅格数据重采样

栅格数据的重采样主要基于三种方法:最邻近采样(NEAREST),双线性

ILINEAR)和三次卷积采样(CUBIC)。

(1).最邻近采样:它用输入栅格数据中最临近栅格值作为输出值。因此,在重采

样后的输出栅格中的每个栅格值, 都是输入栅格数据中真实存在而未加任何改变的值。这种方法简单易用,计算量小,重采样的速度最快。

(2).双线性采样:此重采样法取待采样点(x,y)点周围四个邻点,在y方向(或X方向)内插两次,再在x方向(或y方向)内插一次,得到(x,y)点的栅格值。

(3).三次卷积采样:这是进一步提高内插精度的一种方法。它的基本思想是增加邻点来获

得最佳插值函数。取待计算点周围相邻的16个点,与双线性采样类似,可先在某一方向上内插,如先在x方向上,每四个值依次内插四次,再根据四次的计算结果在y方上内插,最终得到内插结果

代码示例:采用双线性采样

IRasterGeometryProc rasterGeometryProc = new RasterGeometryProcClass();
rasterGeometryProc.Resample(rstResamplingTypes.RSP_CubicConvolution, newCellSize, clipRaster);
public static IRasterLayer SetViewShedRenderer(IRaster pInRaster,string sField,string sPath)
{
IRasterDescriptor pRD = new RasterDescriptorClass();
pRD.Create(pInRaster, new QueryFilterClass(), sField);
IReclassOp pReclassOp = new RasterReclassOpClass();
IGeoDataset pGeodataset=pInRaster as IGeoDataset;
IRasterAnalysisEnvironment pEnv = pReclassOp as IRasterAnalysisEnvironment;
IWorkspaceFactory pWSF=new RasterWorkspaceFactoryClass();
IWorkspace pWS = pWSF.OpenFromFile(sPath, );
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 RasterLayerClass();
IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection;
IRasterBand pRasterBand = pRsBandCol.Item();
pRasterBand.ComputeStatsAndHist();
IRasterStatistics pRasterStatistic = pRasterBand.Statistics;
double dMaxValue = pRasterStatistic.Maximum ;
double dMinValue = pRasterStatistic.Minimum ;
INumberRemap pNumRemap = new NumberRemapClass();
pNumRemap.MapRange(dMinValue, , );
pNumRemap.MapRange(, dMaxValue, );
IRemap pRemap = pNumRemap as IRemap;
IRaster pOutRaster = pReclassOp.ReclassByRemap(pGeodataset, pRemap, false) as IRaster ;
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 == esriGeometryType.esriGeometryPoint)
shape = "Point";
else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolyline)
shape = "Polyline";
else if (pFCls.ShapeType == esriGeometryType.esriGeometryPolygon)
shape = "Polygon";
for (int i = ; i < pFCls.Fields.FieldCount; i++)
{
pTable.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 = ; i < pFCls.Fields.FieldCount; 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;
IRaster 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();
ITable pRTable = pBand.AttributeTable;
DataTable pTable = new DataTable();
for (int i = ; 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 != null)
{
DataRow pRow = pTable.NewRow();
for (int i = ;i<pRrow .Fields .FieldCount ;i++)
{
pRow[i] = pRrow.get_Value(i).ToString () ;
}
pTable.Rows.Add(pRow);
pRrow = pCursor.NextRow();
}
dataGridView1.DataSource = pTable;
}
}

创建栅格数据集

CreateRasterDataset C#
public IRasterDataset CreateFileRasterDataset(string directoryName, string fileName)
{
// This function creates a new img file in the given workspace
// and then assigns pixel values
try
{
IRasterDataset rasterDataset = null;
IPoint originPoint = new PointClass();
originPoint.PutCoords(, );
// Create the dataset
IRasterWorkspace2 rasterWorkspace2 = null;
rasterWorkspace2 = CreateRasterWorkspace(directoryName);
rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, , , , , , rstPixelType.PT_UCHAR, new UnknownCoordinateSystemClass(), true);
IRawPixels rawPixels = null;
IPixelBlock3 pixelBlock3 = null;
IPnt pixelBlockOrigin = null;
IPnt pixelBlockSize = null;
IRasterBandCollection rasterBandCollection;
IRasterProps rasterProps;
// QI for IRawPixels and IRasterProps
rasterBandCollection = (IRasterBandCollection)rasterDataset;
rawPixels = (IRawPixels)rasterBandCollection.Item();
rasterProps = (IRasterProps)rawPixels;
// Create pixelblock
pixelBlockOrigin = new DblPntClass();
pixelBlockOrigin.SetCoords(, );
pixelBlockSize = new DblPntClass();
pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height);
pixelBlock3 = (IPixelBlock3)rawPixels.CreatePixelBlock(pixelBlockSize);
// Read pixelblock
rawPixels.Read(pixelBlockOrigin, (IPixelBlock)pixelBlock3);
// Get pixeldata array
System.Object[,] pixelData;
pixelData = (System.Object[,])pixelBlock3.get_PixelDataByRef();
// Loop through all the pixels and assign value
for (int i = ; i < rasterProps.Width; i++)
for (int j = ; j < rasterProps.Height; j++)
pixelData[i, j] = (i * j) % ;
// Write the pixeldata back
System.Object cachePointer;
cachePointer = rawPixels.AcquireCache();
rawPixels.Write(pixelBlockOrigin, (IPixelBlock)pixelBlock3);
rawPixels.ReturnCache(cachePointer);
// Return raster dataset
return rasterDataset;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
return null;
}
}
public IRasterWorkspace2 CreateRasterWorkspace(string pathName)
{
// Create RasterWorkspace
IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(pathName, ) as IRasterWorkspace2;
}
public IRasterDataset tin2raster(string tempBathyTIN,string geoPath, string gridName)
{
string tinFolder = System.IO.Path.GetDirectoryName(tempBathyTIN);
string tinName = System.IO.Path.GetFileName(tempBathyTIN);
IRasterDataset rasterDataset = new RasterDatasetClass();
try
{
string rasterPath = System.IO.Path.GetDirectoryName(geoPath);
IWorkspaceFactory TinWF = new TinWorkspaceFactory();
ITinWorkspace TinWK = TinWF.OpenFromFile(tinFolder,)as ITinWorkspace;
ITinAdvanced2 tinAd = TinWK.OpenTin(tinName) as ITinAdvanced2;
IEnvelope extent = tinAd.Extent;
IPoint origin = extent.LowerLeft;
origin.X = origin.X - ( * 0.5);
origin.Y = origin.Y - ( * 0.5);
int nCol = (int)Math.Round(extent.Width / ) + ;
int nRow = (int)Math.Round(extent.Height / ) +;
ISpatialReference2 spatialRef = (ISpatialReference2)extent.SpatialReference;
IWorkspaceFactory rasterWF = new RasterWorkspaceFactoryClass();
IRasterWorkspace2 workSpace = (IRasterWorkspace2)rasterWF.OpenFromFile(rasterPath,);
rasterDataset = workSpace.CreateRasterDataset(gridName, "GRID", origin,nCol,nRow,,,,ESRI.ArcGIS.Geodatabase.rstPixelType.PT_FLOAT, spatialRef,true);
IRasterBandCollection bandColl = (IRasterBandCollection) rasterDataset;
IRasterBand rasterBand = bandColl.Item();
IRawPixels rawPixels = (IRawPixels)rasterBand;
IPnt blockSize = new DblPntClass();
blockSize.X = nCol;
blockSize.Y = nRow;
IPixelBlock3 pixelBlock = (IPixelBlock3)rawPixels.CreatePixelBlock(blockSize);
ITinSurface tinSurface = (ITinSurface)tinAd;
IRasterProps rasterProps = (IRasterProps)rawPixels;
object nodataFloat;
//long nodataInt;
object val = pixelBlock.get_PixelDataByRef();
MessageBox.Show(val.ToString());
double cellsize = ;
origin.X = origin.X + ( * 0.5);
origin.Y = origin.Y + ( * nRow) - ( * 0.5);
nodataFloat = Convert.ToDouble(rasterProps.NoDataValue.ToString());
tinSurface.QueryPixelBlock(origin.X,origin.Y,cellsize,cellsize,esriRasterizationType.esriElevationAsRaster,nodataFloat,val);
IPnt offset = new DblPntClass();
offset.X = ;
offset.Y = ;
rawPixels.Write(offset,pixelBlock as IPixelBlock);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
return rasterDataset;
}

最新文章

  1. 根据起止日期构建指定查询条件:第N周(yyyy-MM-dd/yyyy-MM-dd)
  2. 【python】错误/异常处理,调试,测试
  3. jQuery.ajax()调用asp.net后台方法
  4. Sql Server数据的加密与解密
  5. ☀【Grunt】插件
  6. hunnu Sum of f(x)
  7. SharePoint 命令使用集锦 (持续更新中...)
  8. 使用教程 - BestSync同步软件 - SQL2008R2 数据库定时备份解决方案
  9. window系统查看端口被哪个进程占用
  10. padding-bottom布局解析;
  11. Git——git 上传时 遗漏文件解决办法
  12. Angular 选项卡
  13. Hive基础概念、安装部署与基本使用
  14. nginx_ssl安装
  15. LInux下(centos7.2)更新 python3.7
  16. Spark Scheduler内部原理剖析
  17. oracle rac 常见安装、管理错误
  18. 一步一步学习IdentityServer4 (2) 开始一个简单的事例
  19. CentOS7安装.NET Core运行环境
  20. 多网卡下,vlc发送IGMP组播报告包

热门文章

  1. PasswordHelper 对user对象的password进行加密重设
  2. 前端中url、href、src的详细含义
  3. Linux远程远程控制程序TeamViewer
  4. Windows 一键关闭UAC、防火墙、IE配置脚本
  5. 控制div固定在页面的某个位置 ,用js感觉很麻烦 CSS更好一些
  6. Swift学习笔记(11)--类与结构体
  7. [Python] Find available methods and help in REPL
  8. server环境信息【C#代码获取】
  9. 淘宝在hbase中的应用和优化
  10. worldpress 的 GPG 加密插件