Create File raster Dataset
Description:
<Text> This sample shows how to create a file raster dataset and populate pixel values using ipixelblock. </Text>
Products: Engine: VBA, VB6, C #, VB. NET, Java Platforms:Windows, Solaris, LinuxMinimum ArcGIS release:9.0 |
How to use:
- Add this function to your project.
- 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 PPS = 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 PPS = 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;
}
Send feedback on this sample