Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1279807
  • 博文数量: 135
  • 博客积分: 10588
  • 博客等级: 上将
  • 技术积分: 1325
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-18 11:12
文章分类

全部博文(135)

文章存档

2013年(6)

2012年(3)

2011年(11)

2010年(7)

2009年(14)

2008年(6)

2007年(42)

2006年(46)

分类:

2009-06-05 15:55:57

两个星期以来一直与栅格数据打交道,对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, 0);

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

IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();

IWorkspace workspace;

workspace = workspaceFactory.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栅格文件名

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 = 0; i < dHeight; i++)

for (int j = 0; j < dWidth; j++)

{

pnt.SetCoords(i, j);

clipRaster.Read(pnt, pixelBlock);

if (pixelBlock != null)

{

object obj = pixelBlock.GetVal(0, 0, 0);

MessageBox.Show( Convert.ToUInt32(obj).ToString());

}

}

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

提取指定范围内的栅格数据通常用两种方法IRasterLayerExport(esriCarto), IExtractionOp, IExtractionOp2 (),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, 0);// _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, 0);

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);


栅格数据重分类 (2009-01-10 10:10:09)


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, 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 RasterLayerClass();
IRasterBandCollection pRsBandCol = pGeodataset as IRasterBandCollection;
IRasterBand pRasterBand = pRsBandCol.Item(0);
pRasterBand.ComputeStatsAndHist();
IRasterStatistics pRasterStatistic = pRasterBand.Statistics;
double dMaxValue = pRasterStatistic.Maximum ;
double 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 ;
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 = 0; 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 = 0; 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(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 != null)
{
DataRow pRow = pTable.NewRow();
for (int i =0 ;i{
pRow[i] = pRrow.get_Value(i).ToString () ;
}
pTable.Rows.Add(pRow);
pRrow = pCursor.NextRow();
}
dataGridView1.DataSource = pTable;
}

}

  创建栅格数据集 (2008-04-20 14:14:05)






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(0, 0);

// Create the dataset
IRasterWorkspace2 rasterWorkspace2 = null;
rasterWorkspace2 = CreateRasterWorkspace(directoryName);

rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, 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(0);
rasterProps = (IRasterProps)rawPixels;



// Create pixelblock
pixelBlockOrigin = new DblPntClass();
pixelBlockOrigin.SetCoords(0, 0);

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(0);

// Loop through all the pixels and assign value
for (int i = 0; i < rasterProps.Width; i++)
for (int j = 0; j < rasterProps.Height; j++)
pixelData[i, j] = (i * j) % 255;



// 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, 0) 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,0)as ITinWorkspace;
ITinAdvanced2 tinAd = TinWK.OpenTin(tinName) as ITinAdvanced2;
IEnvelope extent = tinAd.Extent;
IPoint origin = extent.LowerLeft;
origin.X = origin.X - (5 * 0.5);
origin.Y = origin.Y - (5 * 0.5);
int nCol = (int)Math.Round(extent.Width / 5) + 1;
int nRow = (int)Math.Round(extent.Height / 5) +1;

ISpatialReference2 spatialRef = (ISpatialReference2)extent.SpatialReference;

IWorkspaceFactory rasterWF = new RasterWorkspaceFactoryClass();
IRasterWorkspace2 workSpace = (IRasterWorkspace2)rasterWF.OpenFromFile(rasterPath,0);

rasterDataset = workSpace.CreateRasterDataset(gridName, "GRID", origin,nCol,nRow,5,5,1,ESRI.ArcGIS.Geodatabase.rstPixelType.PT_FLOAT, spatialRef,true);


IRasterBandCollection bandColl = (IRasterBandCollection) rasterDataset;
IRasterBand rasterBand = bandColl.Item(0);
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(0);
MessageBox.Show(val.ToString());
double cellsize = 5;
origin.X = origin.X + (5 * 0.5);
origin.Y = origin.Y + (5 * nRow) - (5 * 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 = 0;
offset.Y = 0;
rawPixels.Write(offset,pixelBlock as IPixelBlock);


}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());

}
return rasterDataset;

}
阅读(6245) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~