GIS的积累
It is never to late to learn

导航

 

Create File Raster Dataset

Description:

This sample shows how to create a file raster dataset and populate pixel values using IPixelBlock.
Products:

Engine: VBA, VB6, C#, VB.NET, Java

Platforms: Windows, Solaris, LinuxMinimum ArcGIS Release: 9.0

How to use:
  1. Add this function to your project.
  2. Call this function from your code.
[VBA and VB6]
          Public Function createFileRasterDataset(sDir As String, sFile As String) As IRasterDataset      

' This function creates a new img file in the given workspace
' and then assigns pixel values
On Error GoTo er
Dim pWs As IRasterWorkspace2
Dim pDs As IRasterDataset
Dim pPoint As IPoint
Set pPoint = New Point
pPoint.PutCoords 0, 0

' Create the dataset
Set pWs = createRasterWorkspace(sDir)
Set pDs = pWs.CreateRasterDataset(sFile, "IMAGINE Image", pPoint, _
200, 100, 1, 1, 1, PT_UCHAR, New UnknownCoordinateSystem, True)

Dim pRawpixel As IRawPixels
Dim pPixelBlock As IPixelBlock3
Dim pPnt As IPnt, pSize As IPnt
Dim pPixelData
Dim pBands As IRasterBandCollection
Dim pRasterProps As IRasterProps
Dim pCache

' QI for IRawPixels and IRasterProps
Set pBands = pDs
Set pRawpixel = pBands.Item(0)
Set pRasterProps = pRawpixel

' Create pixelblock
Set pPnt = New DblPnt
pPnt.SetCoords 0, 0
Set pSize = New DblPnt
pSize.SetCoords pRasterProps.Width, pRasterProps.Height
Set pPixelBlock = pRawpixel.CreatePixelBlock(pSize)

' Read pixelblock
pRawpixel.Read pPnt, pPixelBlock

' Get pixeldata array
pPixelData = pPixelBlock.PixelDataByRef(0)

' Loop through all the pixels and assign value
Dim i As Long, j As Long
For i = 0 To pRasterProps.Width - 1
For j = 0 To pRasterProps.Height - 1
pPixelData(i, j) = (i * j) Mod 255
Next j
Next i

' Write the pixeldata back
Set pCache = pRawpixel.AcquireCache
pRawpixel.Write pPnt, pPixelBlock
pRawpixel.ReturnCache pCache

' Return
Set createFileRasterDataset = pDs

' Clean up
Set pWs = Nothing
Set pDs = Nothing
Set pPoint = Nothing
Set pRawpixel = Nothing
Set pPixelBlock = Nothing
Set pPnt = Nothing
Set pSize = Nothing
Set pRasterProps = Nothing
Set pBands = Nothing
Set pCache = Nothing
Exit Function

er:
MsgBox "Data creation failed: " + Err.Description

End Function


Public Function createRasterWorkspace(sPath As String) As IRasterWorkspace2
' Create RasterWorkspace

Dim pWsFact As IWorkspaceFactory
Set pWsFact = New RasterWorkspaceFactory

If pWsFact.IsWorkspace(sPath) Then
Set createRasterWorkspace = pWsFact.OpenFromFile(sPath, 0)
End If

Set pWsFact = Nothing

End Function


[C#]
          // Libraries needed to run this code:
// ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geometry
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.Array pixelData;
pixelData = (System.Array) 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.SetValue(Convert.ToByte((i * j) % 255), i, j);

pixelBlock3.set_PixelData(0, (System.Object) pixelData);

// 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;
}


[VB.NET]
          ' Imports: ESRI.ArcGIS.DataSourcesRaster, ESRI.ArcGIS.Geodatabase, ESRI.ArcGIS.Geometry 

Public Function createFileRasterDataset(ByVal directoryName As String, ByVal fileName As String) As IRasterDataset

' This function creates a new img file in the given workspace
' and then assign pixel values
Try
Dim rasterDataset As IRasterDataset = Nothing
Dim originPoint As IPoint = New PointClass
originPoint.PutCoords(0, 0)

' Create the dataset
Dim rasterWorkspace2 As IRasterWorkspace2 = Nothing
rasterWorkspace2 = createRasterWorkspace(directoryName)
rasterDataset = rasterWorkspace2.CreateRasterDataset(fileName, "IMAGINE Image", originPoint, 200, 100, 1, 1, 1, rstPixelType.PT_UCHAR, New UnknownCoordinateSystemClass, True)

Dim rawPixels As IRawPixels = Nothing
Dim pixelBlock3 As IPixelBlock3 = Nothing
Dim pixelBlockOrigin As IPnt = Nothing
Dim pixelBlockSize As IPnt = Nothing
Dim rasterBandCollection As IRasterBandCollection
Dim rasterProps As IRasterProps

' QI for IRawPixels and IRasterProps
rasterBandCollection = CType(rasterDataset, IRasterBandCollection)
rawPixels = CType(rasterBandCollection.Item(0), IRawPixels)
rasterProps = CType(rawPixels, IRasterProps)

' Create pixelblock
pixelBlockOrigin = New DblPntClass
pixelBlockOrigin.SetCoords(0, 0)

pixelBlockSize = New DblPntClass
pixelBlockSize.SetCoords(rasterProps.Width, rasterProps.Height)

pixelBlock3 = CType(rawPixels.CreatePixelBlock(pixelBlockSize), IPixelBlock3)

' Read pixelblock
rawPixels.Read(pixelBlockOrigin, CType(pixelBlock3, IPixelBlock))

' Get pixeldata array
Dim pixelData As System.Array
pixelData = CType(pixelBlock3.PixelDataByRef(0), System.Array)

' Loop through all the pixels and assign value
Dim i As Integer
Dim j As Integer
For i = 0 To rasterProps.Width - 1
For j = 0 To rasterProps.Height - 1
pixelData.SetValue(Convert.ToByte((i * j) Mod 255), i, j)
Next j
Next i

pixelBlock3.PixelData(0) = CType(pixelData, System.Object)

' Write the pixeldata back
Dim cachePointer As System.Object
cachePointer = rawPixels.AcquireCache()
rawPixels.Write(pixelBlockOrigin, CType(pixelBlock3, IPixelBlock))
rawPixels.ReturnCache(cachePointer)

Return rasterDataset

Catch ex As Exception
System.Diagnostics.Debug.WriteLine(ex.Message)
Return Nothing
End Try

End Function

Public Function createRasterWorkspace(ByVal pathName As String) As IRasterWorkspace2

' Create RasterWorkspace
Dim workspaceFactory As IWorkspaceFactory = New RasterWorkspaceFactoryClass

Return CType(workspaceFactory.OpenFromFile(pathName, 0), IRasterWorkspace2)

End Function

[Java]
          
/**
* Description: creates a new img file in the given workspace
* and then assign pixel values
*
* @param aPath
* @param aFile
* @return
*/

public static IRasterDataset createFileRasterDataset(String aPath, String aFile) throws IOException{
IPoint point = new Point();
point.putCoords(0, 0);
UnknownCoordinateSystem sr = new UnknownCoordinateSystem();
/*create the dataset*/
IRasterWorkspace2 rasWksp2 = createRasterWorkspace(aPath);
IRasterDataset rasDS = rasWksp2.createRasterDataset(aFile, "IMAGINE Image", point, 200, 100, 1, 1, 1, rstPixelType.PT_CHAR, sr, true);
/*QI for IRawPixels and IRasterProps*/
IRasterBandCollection bands = new IRasterBandCollectionProxy(rasDS);
IRawPixels rawPix = new IRawPixelsProxy(bands.item(0));
IRasterProps rasProps = new IRasterPropsProxy(rawPix);
/*create pixelblock*/
IPnt pnt = new DblPnt();
pnt.setCoords(0, 0);
IPnt size = new DblPnt();
size.setCoords(rasProps.getWidth(), rasProps.getHeight());
IPixelBlock pixelBlk = rawPix.createPixelBlock(size);
/*read pixelblock*/
rawPix.read(pnt, pixelBlk);
IPixelBlock3 pixelBlock = new IPixelBlock3Proxy(pixelBlk);
int[][] pixelData = (int[][])pixelBlock.getPixelDataByRef(0);
/*loop through all the pixels and assign value*/
for(int i = 0; i < rasProps.getWidth() - 1; i++){
for(int j = 0; j < rasProps.getHeight() - 1; i++){
pixelData[i][j] = (i * j) % 255;
}
}
/*write the pixeldata back*/
Object cache = rawPix.acquireCache();
rawPix.write(pnt, new IPixelBlockProxy(pixelBlock));
rawPix.returnCache(cache);

return rasDS;
}


Feedback Send feedback on this sample
posted on 2010-03-02 15:21  GIS的学习  阅读(1176)  评论(0编辑  收藏  举报