ArcGIS栅格处理总结

更新时间:2023-11-26 15:04:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

ArcGIS栅格处理总结

RealStyle-赵晓鲁 2012年9月28日

处理栅格数据用到的新的接口有:iconditionalop,Ipnt,IRasterDescriptor,

IextractionOp,IrasterLayerExport,IRasterAnalysisEnvironment,ILogicalOp,IReclassOp,IMathOp,INumberRemap,IRemap,ISurfaceop,Irawpixels(很有用) 坐标值位置:

地图坐标最小X、Y值在左下角,IEnvelope最小值是左下角,最大值是右上角,栅格数据最小行列值是在左上角,最大行列值是在右下角。

IRasterAnalysisEnvironment:一般和IextractionOp一起用,用来设置提取出来的栅格数据的一些属性信息。

遍历栅格数据所有数据,将像素值存储在二维数组中

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass(); IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\栅格数据处理集\\Temp\\data\, 0) asIRasterWorkspace ;

IRaster pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster(); IRasterProps pRprops = pRaster asIRasterProps ;

IRasterBandCollection pRbcoll = pRaster asIRasterBandCollection ; int pRbcount = pRbcoll.Count ; IPixelBlock3 pPblock3 = null;

GC.Collect();//释放内存,强制回收垃圾内存

byte[,] pByte = newbyte[pRprops.Width, pRprops.Height * pRbcount];//内存占有量为byte类型所占的字节数*pRprops.Width*pRprops.Height * pRbcount

System.Array pArray = null;//定义一个数组,用来存储pPblock3.get_PixelData(pTindex)获得的像素块的值

for (int pTindex = 0; pTindex < pRbcount; pTindex++)//遍历每个波段 {

IRasterCursor pRcursor = pRaster.CreateCursor();//128行,全部列是一个

pRcursor.Next(), do {

pPblock3 = pRcursor.PixelBlock asIPixelBlock3;//将每个128行的值放在pPblock3中 pArray = pPblock3.get_PixelData(pTindex) as System.Array;//将相应波段的数值存储在数组中

for (int pW = 0; pW < pArray.GetLength(0); pW++)//列数 {

for (int pH = 0; pH < pArray.GetLength(1); pH++)//行数 {

pByte[pW, pH * (pTindex + 1)] = Convert.ToByte (pArray.GetValue(pW, pH));//给二维数组赋值 } }

} while (pRcursor.Next());//获得下一个128行栅格数据

ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(pRcursor );//对对象进行内存释放 }//遍历每个波段

for (int pT = 0; pT < pByte.GetLength(0); pT++)//遍历二维数组 {

for (int pTT = 0; pTT < pByte.GetLength(1); pTT++) {

int pp = Convert.ToInt32(pByte.GetValue(pT, pTT)); }

}//遍历二维数组

1、 如果想将Tif转换为bmp,仅仅需要用到的AO的接口是Isaveas。

IWorkspaceFactorypWksfact=newRasterWorkspaceFactoryClass(); IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0 ) asIRasterWorkspace ; IRasterDataset pRdataset = pRwks.OpenRasterDataset(\); IRaster2 pRaster = pRdataset.CreateDefaultRaster() asIRaster2 ;

IRasterWorkspace2pRwks2=pWksfact.OpenFromFile(@\,0)asIRasterWorkspace2; ISaveAs pSaveas = pRaster asISaveAs ;

pSaveas.SaveAs(\, pRwks2 asIWorkspace, \);

2、 修改栅格数据灰度值:没有用到isaveas

用到的接口是:IRasterCursor,IPixelBlock3,System.Array pArray ,IRasterEdit,IPnt

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass(); IRasterWorkspace2 pRwks = pWksfact.OpenFromFile(@\, 0) asIRasterWorkspace2 ;

IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2 ;

IRasterProps pRprops = pRaster asIRasterProps; IPoint pPoint = newPointClass () ; pPoint.PutCoords (0,0 ) ; IPnt pPnt = newDblPntClass();

pPnt.SetCoords(pRprops.Width,pRprops.Height );//确定范围

IRasterCursor pRcursor = pRaster.CreateCursorEx( pPnt );//确定一个栅格指针的范围,ex:extent,所有要用ppnt,

IPixelBlock3 pPblock = pRcursor.PixelBlock asIPixelBlock3 ; System.Array pArray = pPblock.get_PixelData(0) as System.Array; for (int pWidth = 0 ;pWidth < pRprops.Width ;pWidth ++ ) {

for (int pHeight = 0; pHeight < pRprops.Height; pHeight ++ ) {

if (Convert.ToInt32(pPblock.GetVal(0, pWidth, pHeight)) == 0) {

pArray.SetValue(Convert.ToByte ( 100),pWidth,pHeight);//将像素值存在数组里面 } } }

pPblock.set_PixelData(0, pArray);//对像素块进行修改 IRasterEdit pRedit = pRaster asIRasterEdit; IPnt pDblpnt = newDblPntClass();

pDblpnt.SetCoords(0,0 );//pdblpnt指的是左上角的坐标

pRedit.Write(pDblpnt , pPblock asIPixelBlock);//将灰度块写入栅格数据

栅格数据二值化:目的:一个是数据处理源的需要,再一个目的是栅格数据的大小可以变得很小。

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\栅格数据处理集\\Temp\\data\,0) asIRasterWorkspace ;

IRaster pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster(); IGeoDataset pGeodataset = pRaster asIGeoDataset; IEnvelope pEnv = pGeodataset.Extent;

IRasterCursor pRcursor = pRaster.CreateCursor(); IRasterProps pRprops = pRaster asIRasterProps ; //创建一个二值的图像

IRasterWorkspace2 pRwks2 = pRwks asIRasterWorkspace2; IPoint pPoint = newPointClass () ;

pPoint.PutCoords (pEnv.XMin ,pEnv.YMin ) ;

if (Directory.Exists(@\栅格数据处理集\\Temp\\data\\TwoValue\)) {

IDataset pDds = pRwks2.OpenRasterDataset(\) asIDataset; if (pDds != null) pDds.Delete(); }

IRasterDataset pRdataset =

pRwks2.CreateRasterDataset( \,\,pPoint ,pRprops.Width,pRprops.Height ,pRprops.MeanCellSize().X,pRprops.MeanCellSize().Y ,1,rstPixelType.PT_U1 ,pRprops.SpatialReference,true );//创建一个栅格数据集

IRaster pcR = pRwks.OpenRasterDataset (\).CreateDefaultRaster () ; System.Array pChangearr = newbyte [pRprops.Width,pRprops.Height ];//定义一个二维数组,用来存储二值化的数据。 int pT = 0 ;

int Pa = pRprops.Height / 128; do {

IPixelBlock3 pPb = pRcursor.PixelBlock asIPixelBlock3;

System.Array pArray = pPb.get_PixelData(0) as System.Array ; int pHH = 0;

Pa = pRprops.Height / 128 -pT; pT = pT + 1; if (Pa >= 1) {

pHH = 128; } else {

pHH = pRprops.Height % 128; }

for (int pH = 0; pH < pHH ; pH++) {

for (int pW = 0; pW < pRprops.Width; pW++) {

if (Convert.ToByte ( pArray.GetValue(pW, pH)) == 1) {

pChangearr.SetValue(pArray.GetValue(pW, pH), pW, pH + 128 * (pT - 1));//赋值定义的数组 } } }

} while (pRcursor.Next () ) ;//每128行遍历一次 IRaster2 pR2 =pRaster asIRaster2 ; IPnt pPnt = newDblPntClass () ;

pPnt.SetCoords (pRprops.Width ,pRprops.Height ) ;

IRasterCursor pR2cursor = pR2.CreateCursorEx (pPnt ) ; IPixelBlock3 pSpb = pR2cursor.PixelBlock asIPixelBlock3; IPnt pSpnt = newDblPntClass(); pSpnt.SetCoords(0, 0);

pSpb.set_PixelData(0, pChangearr);//将数组赋值给pSpb IRasterEdit pRedit = pcR asIRasterEdit; pRedit.Write(pSpnt, pSpb asIPixelBlock);//编辑栅格数据集,写完后,栅格数据就会是现在的值

模板提取:

//找到模板,并将模板放在数组里

IWorkspaceFactory pRwksfact = newRasterWorkspaceFactoryClass(); IRasterWorkspace pRwks = pRwksfact.OpenFromFile(@\栅格数据处理集\\Temp\\data\, 0) asIRasterWorkspace ; IRaster pModelraster =

pRwks.OpenRasterDataset(\).CreateDefaultRaster(); IRaster2 pModelraster2 = pModelraster asIRaster2;

IRasterProps pModelRprops = pModelraster asIRasterProps; //pModelRprops.NoDataValue = 0; IPnt pModelpnt = newDblPntClass();

pModelpnt.SetCoords(pModelRprops.Width,pModelRprops.Height );

IRasterCursor pModelrcursor = pModelraster2.CreateCursorEx(pModelpnt); IPixelBlock3 pModelppb3 = pModelrcursor.PixelBlock asIPixelBlock3;

System.Array pModelarray = pModelppb3.get_PixelData(0) as System.Array; //找到模板,并将模板放在数组里

//找到源文件,将源文件存放到数组里

IRaster pOriraster = pRwks.OpenRasterDataset(\).CreateDefaultRaster(); IRaster2 pOriraster2 = pOriraster asIRaster2;

IRasterProps pOrirprops = pOriraster2 asIRasterProps; //pOrirprops.NoDataValue = 0; IPnt pOripnt = newDblPntClass();

pOripnt.SetCoords(pOrirprops.Width, pOrirprops.Height);

IRasterCursor pOrircursor = pOriraster2.CreateCursorEx(pOripnt); IPixelBlock3 pOripb3 = pOrircursor.PixelBlock asIPixelBlock3;

System.Array pOriarray = pOripb3.get_PixelData(0) as System.Array; //找到源文件,将源文件存放到数组里

//创建一个二值的栅格数据

if (Directory.Exists(@\栅格数据处理集\\Temp\\data\\PCreate\))

{

IDataset pTds = pRwks.OpenRasterDataset(\) asIDataset; pTds.Delete(); }

IGeoDataset pTgeodataset = pOriraster asIGeoDataset; IEnvelope pTenv = pTgeodataset.Extent; IPoint pCpoint = newPointClass();

pCpoint.PutCoords(pTenv.XMin, pTenv.YMin);

IRasterWorkspace2 pRwks2 = pRwks asIRasterWorkspace2 ; IRasterDataset pCrdataset =

pRwks2.CreateRasterDataset(\,\,pCpoint,pOrirprops.Width,pOrirprops.Height,pOrirprops.MeanCellSize().X ,pOrirprops.MeanCellSize().Y ,1,rstPixelType.PT_U1 ,pOrirprops.SpatialReference,true ); //创建一个二值的栅格数据 //创建一个二维数组

System.Array pCarray = newbyte [pOrirprops.Width ,pOrirprops.Height]; //计算模板中所有值平方的相加和 double pModelsun = 0; int pTemp = 0;

for (int pH = 0; pH < pModelRprops.Height; pH++) {

for (int pW = 0; pW < pModelRprops.Width;pW ++ ) {

if (Convert.ToSByte ( pModelarray.GetValue (pW,pH ))>=0 ) {

pTemp = Convert.ToInt16 ( pModelarray.GetValue(pW, pH)); pModelsun = pModelsun + (Math.Pow(pTemp, 2)); } } }

//遍历原始栅格数据的行列 double pMOmulti = 0; double pOrisun = 0; int pTMod = 0; int pTori = 0;

double pScale = 0;

for (int pH = 0; pH < pOrirprops.Height - pModelRprops.Height; pH++)//遍历每行 { try {

for (int pW = 0; pW < pOrirprops.Width - pModelRprops.Width; pW++)//遍历每列 {

if (Convert.ToSByte(pOriarray.GetValue(pW, pH)) >= 0)

{

pMOmulti = 0; pOrisun = 0; pTMod = 0; pTori = 0;

for (int pMh = 0; pMh < pModelRprops.Height; pMh++)//遍历模型行 {

for (int pMw = 0; pMw < pModelRprops.Width; pMw++)//遍历模型列 {

if (Convert.ToSByte(pModelarray.GetValue(pMw, pMh)) >= 0) {

pTori = Convert.ToSByte(pOriarray.GetValue(pW + pMw, pH + pMh)); pTMod = Convert.ToSByte(pModelarray.GetValue(pMw, pMh)); pMOmulti = pMOmulti + pTori * pTMod; pOrisun = pOrisun + Math.Pow(pTori, 2); } } } }

if (pOrisun > 0) {

pScale = pMOmulti / (Math.Sqrt(pModelsun) * Math.Sqrt(pOrisun)); if (pScale > 0.9) {

for (int pMh = 0; pMh < pModelRprops.Height; pMh++)//遍历模型行 {

for (int pMw = 0; pMw < pModelRprops.Width; pMw++)//遍历模型列 {

pCarray.SetValue(Convert.ToByte (pOriarray.GetValue(pW + pMw, pH + pMh)), pW + pMw, pH + pMh); } } } } } } catch {

continue ; } }

IRaster pCraster = pRwks.OpenRasterDataset(\).CreateDefaultRaster();

IRaster2 pCr2 = pCraster asIRaster2;

IRasterProps pCrprops = pCr2 asIRasterProps; IPnt pCpnt = newPntClass();

pCpnt.SetCoords(pCrprops.Width, pCrprops.Height); IRasterCursor pCrcur = pCr2.CreateCursorEx(pCpnt); IPixelBlock3 pCpb = pCrcur.PixelBlock asIPixelBlock3 ;

pCpb.set_PixelData(0, pCarray);

IRasterEdit pRedit = pCraster asIRasterEdit; IPnt pTlpnt = newPntClass(); pTlpnt.SetCoords(0, 0);

pRedit.Write(pTlpnt, pCpb asIPixelBlock);

pixel value是栅格象元的值,stretched value是该象元显示时用的值。因显示器显示时必须把象元值转换到0—255显示,所以就有了stretched value。

IRasterProps pRprops = pRaster asIRasterProps;

pRprops.NoDataValue = 2;为空白区域赋值,将nodata空白区域赋上pixel值,而不是stretched value。

单元值可正可负,可以是整型也可以是浮点型。在单元中,还可以使用 NoData 值来表示数据缺失。

显示具有 NoData 值的栅格时,所有渲染器可将 NoData 值设置为某个颜色或无颜色;然而,“拉伸”渲染器可识别特定背景值并显示颜色或无颜色。

Nodata相关操作

在栅格数据进行叠加运算时,如果有些区域的值为nodata那么这些区域的值和其他任何栅格数据相加的在值都是nodata,所以有时候需要将nodata区域设置为0。

可以通过SetNull将有些值设置为nodata,也可以通过IsNull来确定哪些区域是nodata区域。

ILogicalOp pLogicalop = newRasterMathOpsClass();//获得ILogicalop接口,用于数学运算

IRaster pOutr = pLogicalop.IsNull(pRaster asIGeoDataset ) asIRaster ;//如果数据缺失,那么输出为1,否则输出为0.

Con可以将栅格数据中的某些值(通过条件查询)设置为想要的区域,将某些不想要的区域设置为其它值(可

以是自己设置的也可以是输入的固定值)

IConditionalOp:方法Con, IConditionalOp.

Con(IGeoDatasetconditionalRaster,IGeoDatasettrueRaster, ref objectfalseRaster)方法:

不管conditionalRaster

是仅有0和1的值,还是有多个值的栅格数据,

0的栅格像

只要是值大于0的栅格像素值,赋的值都是trueRaster,凡是值等于

素值那么赋值为falseRaster,falseRaster可以为一个常数,也可以为一个栅格数据集(Raster)对应的像素值。

IConditionalOp. Con代码

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass(); IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\栅格数据处理集\\Temp\\data\, 0)

asIRasterWorkspace ;

IRaster pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster(); IRaster pSraster = newRasterClass(); //object pint = 100;//object为常数

object pRa = pRaster;//object为栅格数据集

IConditionalOp pConditionalop = newRasterConditionalOpClass();

//pSraster = pConditionalop.Con(pRaster as IGeoDataset, pRaster as IGeoDataset , ref pint) as IRaster ;//object为常数

pSraster = pConditionalop.Con(pRaster asIGeoDataset, pRaster asIGeoDataset, ref pRa) asIRaster;//object为栅格数据集 ISaveAs pSa = pSraster asISaveAs;

pSa.SaveAs(\, pRwks asIWorkspace, \);

使用“设为空函数”将值设置为 NoData:在spatial analyst下的conditional下的set null。

要将值大于 5 的像元设置为 NoData,并且保留其他像元的原始值,请在工具对话框中使用以下参数: 输入条件栅格:InRas1 表达式:\条件)

求值结果为 False 的输入栅格或常数值:InRas1(即不符合条件的像素值设置为输入栅格对应的值) 输出栅格:setnull_1

IConditionalOp:只是针对栅格数据只有两个值的0和1的栅格,如果只有0和1,那么将值为1的设置为空,值为0的仍然为0,或者是其它栅格(False Raster)数据相应位置的值。或者栅格数据有多个值,0和大于0两类,当栅格数据有多个值时,值大于0的设置为空,值等于0的仍然是0或者是其它栅格数据(False Raster)相应位置的值。和conditional下的set null理解差不多,但是有区别,不能设置”sql语句”。将大于0的值变为nodata,将等于0的值变为falseraster的栅格像素值。 用到的接口是 IConditionalOp方法是:SetNull

相应 的代码:

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster()

asIRaster2;

IRaster2 pR2 = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2; IConditionalOp pCondi = newRasterConditionalOpClass(); IGeoDataset pDataset = pRaster asIGeoDataset; IGeoDataset pFdataset = pR2 asIGeoDataset;

IGeoDataset pOutdataset = pCondi.SetNull(pDataset ,pFdataset ); ISaveAs pSa = pOutdataset asISaveAs;

pSa.SaveAs( \,pRwks asIWorkspace ,\ );

通过一个面的范围来提取栅格数据:

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2;

IRasterProps pRprops = pRaster asIRasterProps ;

IExtractionOp pExtra = newRasterExtractionOpClass();

IWorkspaceFactory pSwksfact = newShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFwks = pSwksfact.OpenFromFile(@\, 0) asIFeatureWorkspace ;

IFeatureClass pFclass = pFwks.OpenFeatureClass(\); IFeatureCursor pFcur = pFclass.Search(null,false ); IFeature pFeature = pFcur.NextFeature();

IPolygon pPolygon = pFeature.Shape asIPolygon ;

IWorkspace pOutwks = pWksfact.OpenFromFile(@\, 0); IGeoDataset pGeodataset = pRaster asIGeoDataset;

IGeoDataset pOutgd = pExtra.Polygon(pGeodataset ,pPolygon,true ) asIGeoDataset ;//true指的是面内的栅格提取出来,false指的是面外的栅格数据提取出来, IRaster pOutr = pOutgd asIRaster;//将Geodataset转换为iraster,而不是转换为irasterdataset,用isaveas来保存存储在内存中的栅格数据。 ISaveAs pSa = (ISaveAs ) pOutr ;

pSa.SaveAs(\, pOutwks, \);//poutwks一定要是栅格工作空间工厂打开的工作空间,不能是其它工作空间工厂打开的工作空间。

通过属性提取栅格数据

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2;

IRasterProps pRprops = pRaster asIRasterProps ;

IExtractionOp pExtra = newRasterExtractionOpClass();

IQueryFilter pQueryfilter = newQueryFilterClass () ;//属性查询,查询到属性值是多少的像素。

pQueryfilter.WhereClause = \;//设置像素查询的条件。

IRasterDescriptor pRdescriptor = newRasterDescriptorClass();//栅格描述接口。 pRdescriptor.Create(pRaster asIRaster, pQueryfilter, \); IWorkspaceFactory pRwks2 = newRasterWorkspaceFactoryClass(); IWorkspace pWks2 = pRwks2.OpenFromFile(@\, 0);

IRaster pOut2 = pExtra.Attribute(pRdescriptor) asIRaster;//将获得的栅格数据给iraster

ISaveAs poUTas = pOut2 asISaveAs ;

poUTas.SaveAs(\,pWks2 ,\);

通过范围(Rectangle)来提取栅格数据

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2;

IRasterProps pRprops = pRaster asIRasterProps ;

IWorkspaceFactory pSwksfact = newShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFwks = pSwksfact.OpenFromFile(@\, 0) asIFeatureWorkspace ;

IFeatureClass pFclass = pFwks.OpenFeatureClass(\); IFeatureCursor pFcur = pFclass.Search(null, false); IFeature pFeature = pFcur.NextFeature(); IEnvelope pEnv = pFeature.Extent; //获取范围

IExtractionOp pEx = newRasterExtractionOpClass();

IRaster pOraster = pEx.Rectangle(pRaster asIGeoDataset, pEnv, true) asIRaster ; ISaveAs pSa = pOraster asISaveAs;//保存

pSa.SaveAs(\,pRwks asIWorkspace ,\);

通过矩形(Rectangle)来导出

地图坐标最小X、Y值在左下角,IEnvelope最小值是左下角,最大值是右上角,栅格数据最小行列值是在左上角,最大行列值是在右下角。 double pXmin, pXmax, pYmin, pYmax; IEnvelope pEnv = newEnvelopeClass();

pR2.PixelToMap(pW, pH + pRprops.Height, out pXmin, out pYmin);//获得xy值的最小值 pR2.PixelToMap(pW + pRprops.Width ,pH,out pXmax ,out pYmax );//获得xy值得最大值 pEnv.PutCoords(pXmin, pYmin, pXmax, pYmax);//设置IEnvelope的范围 IExtractionOp pExtractop = newRasterExtractionOpClass();

IRaster pSraster = pExtractop.Rectangle(pR asIGeoDataset, pEnv, true) asIRaster; ISaveAs pSa = pSraster asISaveAs;

pSa.SaveAs(\ + Pc , pRwks asIWorkspace, \);

通过栅格数据提取栅格数据

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2;//获得待提取的栅格数据

IRasterProps pRprops = pRaster asIRasterProps ; IRaster pMaskraster =

pRwks.OpenRasterDataset(\).CreateDefaultRaster();//获取掩膜的栅格数据

IExtractionOp pEx = newRasterExtractionOpClass();

IRaster pO = pEx.Raster(pRaster asIGeoDataset ,pMaskraster asIGeoDataset ) asIRaster ;

ISaveAs pSA = pO asISaveAs;//保存

pSA.SaveAs(\ ,pRwks asIWorkspace ,\ );

通过圆来提取栅格数据:

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2;//获得待提取的栅格数据

IRasterProps pRprops = pRaster asIRasterProps ;

IWorkspaceFactory pShpwksfact = newShapefileWorkspaceFactoryClass(); IFeatureWorkspace pFwks = pShpwksfact.OpenFromFile(@\, 0) asIFeatureWorkspace ;

IFeatureClass pFclass = pFwks.OpenFeatureClass(\); IFeatureCursor pFcur = pFclass.Search(null, false); IFeature pFeature = pFcur.NextFeature();

IPolygon pPolygon = pFeature.Shape asIPolygon ; IArea pArea = pPolygon asIArea;

IPoint pPoint = pArea.Centroid;//获得面的中心点。 ICircularArc pCircular = newCircularArcClass();

pCircular.PutCoordsByAngle( pPoint ,0,Math.PI*2 ,pPolygon.Length /4 );//通过弧度创建一个圆,不能是角度(0-180),只能是弧度,

IExtractionOp pEx = newRasterExtractionOpClass();//通过圆来提取

IRaster pO = pEx.Circle(pRaster asIGeoDataset, pCircular, true )asIRaster ; ISaveAs pSA = pO asISaveAs;//保存

pSA.SaveAs(\ ,pRwks asIWorkspace ,\ );

IRaster,IRasterDataset的区别:

IRasterDataset是数据库里存在的数据集,有点类似FeatureClass的意思。 而IRaster是内存当中的栅格对象。

使用IRasterDataset.CreateDefaultRaster即可在内存中生成IRaster对象。

波段合成:说明:合成转换为img格式,不要求有几个波段,但是合成转换为Tif文件,

则Tif文件必须至少有三个,多个可以。

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass(); IRasterWorkspace2 pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace2 ;

IRasterBandCollection pRcoll = pRwks.OpenRasterDataset(\) asIRasterBandCollection ;

IRasterBand pRband = pRcoll.Item(1) ;//获得波段 IRasterBand prband2 = pRcoll.Item(2);

IRasterBand pRband3 = pRcoll.Item (3) ;

IRasterBandCollection pOutbandcoll = newRasterClass(); pOutbandcoll.Add (pRband ,0) ; pOutbandcoll.Add (prband2,1);

pOutbandcoll.SaveAs( \,pRwks asIWorkspace ,\ ); //pOutbandcoll.SaveAs(\

//保存为tif文件时,必须有三个以上的波段即irasterband,而img格式则不要求有多个波段。

栅格图层以范围(ienvlope)导出(VBA)。

Dim pRasterlayerEX As IRasterLayerExport Dim pMxdoc As IMxDocument Dim pMap As IMap

Dim pRlayer As IRasterLayer Set pMxdoc = ThisDocument Set pMap = pMxdoc.FocusMap Set pRlayer = pMap.Layer(0)

Set pRasterlayerEX = New RasterLayerExport

Set pRasterlayerEX.RasterLayer = pRlayer '获得praseterlayerex的栅格图层 Dim pWksfact As IWorkspaceFactory

Set pWksfact = New RasterWorkspaceFactory Dim pRwks As IRasterWorkspace

Set pRwks = pWksfact.OpenFromFile(\

pRasterlayerEX.Extent = pMxdoc.ActiveView.Extent '获得输出栅格的范围(范围是矩形形状)

pRasterlayerEX.Export pRwks, \

C#代码:以范围(ienvlope)导出

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass();

IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\, 0) asIRasterWorkspace; IRaster2 pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster() asIRaster2;

IRasterLayer pRasterlayer = newRasterLayerClass(); pRasterlayer.CreateFromRaster(pRaster asIRaster);

IWorkspaceFactory pShpwksfact = newShapefileWorkspaceFactoryClass();

IFeatureWorkspace pFwks = pShpwksfact.OpenFromFile(@\, 0)asIFeatureWorkspace ;

IFeatureClass pFclass = pFwks.OpenFeatureClass(\); IFeatureCursor pFcur = pFclass.Search(null, false); IFeature pFeature = pFcur.NextFeature(); IEnvelope pEnv = pFeature.Extent;

IRasterLayerExport pRLex = newRasterLayerExportClass(); pRLex.Extent = pEnv;

pRLex.RasterLayer = pRasterlayer;

pRLex.Export(pRwks asIWorkspace, \, \);

栅格数据重分类:

重新分类用到的接口是:INumberRemap,IRemap,IReclassOp。

IReclassOp pRop = newRasterReclassOpClass();

IWorkspaceFactory pWksfact = newRasterWorkspaceFactoryClass(); IRasterWorkspace pRwks = pWksfact.OpenFromFile(@\,0) asIRasterWorkspace ;

IRaster pRaster = pRwks.OpenRasterDataset(\).CreateDefaultRaster(); INumberRemap pNumberremap = newNumberRemapClass();

pNumberremap.MapRange(0, 138.457031, 1);//一类的最小值和最大值和分类后的值 pNumberremap.MapRange(138.457031, 202.207031, 2);

pNumberremap.MapRangeToNoData(202.207031,255);//将一批数值变为nodata(数据缺省) IRemap pRemap = pNumberremap asIRemap ; pRemap.DeleteRecord(2);

IRaster pR = newRasterClass();

pR= pRop.ReclassByRemap(pRaster asIGeoDataset, pRemap, true) asIRaster ; ISaveAs pSa = pR asISaveAs;

pSa.SaveAs(\,pRwks asIWorkspace ,\); MessageBox.Show(\运行完成\,\提示\);

局域操作、邻域操作、区域操作、距离操作都有对应的接口。局域操作:ilocalop,邻域操作:ineighborhoodop接口,区域操作:izonalop,距离操作:idistanceop。

在ArcMAP中有时候会生成一个临时的栅格数据,而要将临时的栅格数据变成存储介质中的栅格数据,需要用到的接口是:Itemporarydataset。接口只是针对临时性的文

C# 处理bitmap位图总结。

位图的像素大小就是一个屏幕像素,不是ArcGIS中说的每个像素代表多少米的距离。

如果用到进度条,有时候会很慢,很慢

Dpi指的是

对于电脑,一般显示器设置的都是96dpi,好象是指每英寸可以显示96个象素吧。这个96,在何时会用到呢?显示的分辨率有专门的设置呀,比如设置为:1280*1024,即横向有1280象素,纵向有1024个象素。1280除以屏的宽度,就是dpi吧。我试了一下,屏的宽度为13.3465英寸,1280/13.3465,确实等于95.9,可以认为是96吧。这与dpi设置中的”96“是一致的。 修改像素值,很慢,用到的是SetPixel方法。

Bitmap pBmap = Bitmap.FromFile(@\栅格数据处理集\\Temp\\data\\chuli.bmp\) asBitmap;

Bitmap pBmap2 = newBitmap(pBmap.Width,

pBmap.Height,PixelFormat.Format24bppRgb ); for (int pW = 0; pW

for (int pH = 0; pH < pBmap.Height; pH++) {

pBmap2.SetPixel(pW, pH, pBmap.GetPixel(pW, pH));//修改像素值 } }

pBmap2.Save(@\栅格数据处理集

\\Temp\\data\\Newbmp1.bmp\,System.Drawing.Imaging.ImageFormat.Bmp );//保存

修改栅格像素值,这个方法是在内存中处理,很快。

Bitmap pBitmap = Bitmap.FromFile(@\栅格数据处理集\\Temp\\data\\chuli.bmp\) asBitmap;

int pW = pBitmap.Width; int pH = pBitmap.Height; Bitmap pNewbmap =

newBitmap(pBitmap.Width,pBitmap.Height,PixelFormat.Format24bppRgb ); Rectangle pRect = newRectangle(0, 0, pW, pH);

//将bitmap锁定到系统内,获得bitmapdata,using System.Drawing.Imaging来引用Bitmapdata

BitmapData pBmdata = pBitmap.LockBits(pRect, System.Drawing.Imaging.ImageLockMode.ReadWrite , System.Drawing.Imaging.PixelFormat.Format24bppRgb ); BitmapData pNewbmdata = pNewbmap.LockBits(pRect, System.Drawing.Imaging.ImageLockMode.ReadWrite,

System.Drawing.Imaging.PixelFormat.Format24bppRgb ); //位图中第一个像素的位置

System.IntPtr pbmptr = pBmdata.Scan0; System.IntPtr pNewptr = pNewbmdata.Scan0;

//扫描宽度:pBmdata.Stride解释,如果pBitmap.Width可以被4整除,那么pBitmap.Width和pBmdata.Stride值是相同的,如果pBitmap.Width不能被4整除,那么pBmdata.Stride就取大于pBitmap.Width,并且可以被4整数的最小整数 int pBte = pBmdata.Stride * pH; byte[] pByte = newbyte[pBte];

int pNbte = pNewbmdata.Stride * pH; byte [] pNewbyte = newbyte [pNbte ] ;

//复制RGB信息,将bitmapdata数据复制到数组中

System.Runtime.InteropServices.Marshal.Copy(pbmptr, pByte, 0, pBte);//将数据拷贝到数组中,是按照左上角顺序开始拷贝的,而不像MemoryStream那样,是从左下角开始 pBitmap.UnlockBits(pBmdata);

System.Runtime.InteropServices.Marshal.Copy(pNewptr, pNewbyte, 0, pNbte); for (int pT = 0; pT < pByte.Length; pT++) {

pNewbyte[pT] = pByte[pT]; }

//将数组内容复制到内存中

System.Runtime.InteropServices.Marshal.Copy(pNewbyte, 0, pNewptr, pNbte); pNewbmap.UnlockBits(pNewbmdata);

pNewbmap.Save(@\栅格数据处理集\\Temp\\data\\Newbmp2.bmp\, ImageFormat.Bmp); 图片旋转:

//旋转Image图片

Image pImage = Image.FromFile(@\栅格数据处理集\\Temp\\data\\chuli.bmp\); pImage.RotateFlip(RotateFlipType.Rotate270FlipNone);//将图片旋转270°,此方法只能旋转固定的角度,即90,180,270 ,不能随意的旋转。

if (File.Exists(@\栅格数据处理集\\Temp\\data\\chuli.bmp\)) {//设置文件属性

File.SetAttributes(@\栅格数据处理集\\Temp\\data\\chuli.bmp\,

FileAttributes.Hidden);//将图片设置为隐藏,FileAttributes是设置文件的属性,可以是只读,好多属性 }

pImage.Save(@\栅格数据处理集\\Temp\\data\\chuli2.bmp\, ImageFormat.Bmp);//将图像存储为bmp格式

pImage.Dispose();//释放由pImage使用的所有资源 //旋转Image图片

//旋转Bitmap图片

Bitmap pBitmap = newBitmap(@\栅格数据处理集\\Temp\\data\\chuli.bmp\);//bmp也可以进行旋转

pBitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);

pBitmap.Save(@\栅格数据处理集\\Temp\\data\\chuli0.bmp\,ImageFormat.Bmp ); //旋转Bitmap图片

图片缩放:

原理:栅格数据重采样。

Bitmap pBm = newBitmap (@\栅格数据处理集\\Temp\\data\\chuli.bmp\) ; //也可以用Bitmap srcBitmap = Bitmap.FromFile(@\栅格数据处理集\\Temp\\data\\chuli.bmp\

Bitmap pSavebm = newBitmap( pBm,pBm.Width /2,pBm.Height /2 );//图片缩放 pSavebm.Save(@\栅格数据处理集\\Temp\\data\\chuli00.bmp\,ImageFormat.Bmp );

图片旋转:

Bitmap pBm = newBitmap(@\栅格数据处理集\\Temp\\data\\chuli.bmp\);

GraphicsPath pGrapath = newGraphicsPath();//引用using System.Drawing.Drawing2D; pGrapath.AddRectangle(newRectangleF (0,0,pBm.Width ,pBm.Height ) );//添加一个矩阵

Matrix pMatrix = newMatrix();//定义3×3放射矩阵,//引用using System.Drawing.Drawing2D;

pMatrix.Rotate (45) ;//按照指定的角度对矩阵进行顺时针旋转

RectangleF pRectf = pGrapath.GetBounds(pMatrix );//旋转后的矩阵,高和宽不是整数 Bitmap pNewbm =

newBitmap((int )pRectf.Width ,(int )pRectf.Height,PixelFormat.Format24bppRgb );

Graphics pGra = Graphics.FromImage(pNewbm);

pGra.Clear(System.Drawing.Color.White );//将背景色填充为白色

pGra.TranslateTransform(-pRectf.X ,-pRectf.Y );//定义转换后的左上角坐标 pGra.RotateTransform(45);//新的图像转换45°

pGra.InterpolationMode = InterpolationMode.HighQualityBilinear ;//内插算法 pGra.DrawImageUnscaled(pBm , 0, 0);//在指定的位置使用图像的原始物理大小绘制指定

的图像

pNewbm.Save(@\栅格数据处理集\\Temp\\data\\chuliFF3.bmp\,ImageFormat.Bmp );//保存图像

图片保存Image与Byte数组的转换:

可以将图片保存到数组中,这样可以就操作数组了。 //Image与Byte数组的转换

Bitmap pBm = newBitmap(@\栅格数据处理集\\Temp\\data\\chuli.bmp\MemoryStream pMs = newMemoryStream(); pBm.Save( pMs ,ImageFormat.Bmp );//保存时,将图像保存到内存流中。其中数据流的存储是从左下角的坐标开始的,而不是从左上角坐标开始

byte[] pBmdata = pMs.GetBuffer();//将内存流中的数据放到数组中,这样就可以操作数组了。

MemoryStream pM = newMemoryStream(pBmdata);//从数组中获得内存流 Bitmap pb = Bitmap.FromStream(pM ) asBitmap ;//从内存流中获得位图 pb.Save(@\栅格数据处理集\\Temp\\data\\temp2.bmp\

C/C++与C#数据类型对应

C/C++ C# ---------------------------------------- charsbyte / Sbyte

unsigned char byte / Byte shortshort / Int16

unsigned short ushort / UInt16 intint / Int32

unsignedintuint / UInt32 longint / Int32 longuint / UInt32 floatfloat / Single

doubledouble / Double

char* (字符串) string / String void* (数组) IntPtr

注意:c#中的char是2字节,表达UNICODE字符,long是8字节整数。

数组操作:

? C#中使用IntPtr来对用C/C++中分配的内存空间。

? 使用System.Runtime.InteropServices.Marshal类中的Copy方法可实现IntPtr与C#一

维数组的相互转换。

本文来源:https://www.bwwdw.com/article/gslt.html

Top