// Create a FileSystemConnectionPath using the folder path.FileSystemConnectionPathconnectionPath=newFileSystemConnectionPath(newSystem.Uri(@"C:\Temp"),FileSystemDatastoreType.Raster);// Create a new FileSystemDatastore using the FileSystemConnectionPath.FileSystemDatastoredataStore=newFileSystemDatastore(connectionPath);// Open the raster dataset.RasterDatasetfileRasterDataset=dataStore.OpenDataset<RasterDataset>("Sample.tif");
Open raster dataset in a geodatabase.
// Create a FileGeodatabaseConnectionPath using the path to the gdb. Note: This can be a path to a .sde file.FileGeodatabaseConnectionPathgeodatabaseConnectionPath=newFileGeodatabaseConnectionPath(newUri(@"C:\Temp\rasters.gdb"));// Create a new Geodatabase object using the FileGeodatabaseConnectionPath.Geodatabasegeodatabase=newGeodatabase(geodatabaseConnectionPath);// Open the raster dataset.RasterDatasetgdbRasterDataset=geodatabase.OpenDataset<RasterDataset>("sample");
Get the raster dataset definition from a raster dataset.
Create a raster cursor to iterate through the raster data.
awaitQueuedTask.Run(()=>{// Create a full raster from the raster dataset.ArcGIS.Core.Data.Raster.Rasterraster=rasterDataset.CreateFullRaster();// Calculate size of pixel blocks to process. Use 1000 or height/width of the raster, whichever is smaller.intpixelBlockHeight=raster.GetHeight()>1000?1000:raster.GetHeight();intpixelBlockWidth=raster.GetWidth()>1000?1000:raster.GetWidth();// Create the raster cursor using the height and width calculated.RasterCursorrasterCursor=raster.CreateCursor(pixelBlockWidth,pixelBlockHeight);// Use a do-while loop to iterate through the pixel blocks of the raster using the raster cursor.do{// Get the current pixel block from the cursor.using(PixelBlockcurrentPixelBlock=rasterCursor.Current){// Do something with the pixel block...}// Once you are done, move to the next pixel block.}while(rasterCursor.MoveNext());});
Read and Write pixels from and to a raster dataset using pixel blocks.
awaitQueuedTask.Run(()=>{// Create a full raster from the raster dataset.ArcGIS.Core.Data.Raster.Rasterraster=rasterDataset.CreateFullRaster();// Calculate size of pixel block to create. Use 128 or height/width of the raster, whichever is smaller.intpixelBlockHeight=raster.GetHeight()>128?128:raster.GetHeight();intpixelBlockWidth=raster.GetWidth()>128?128:raster.GetWidth();// Create a new (blank) pixel block.PixelBlockcurrentPixelBlock=raster.CreatePixelBlock(pixelBlockWidth,pixelBlockHeight);// Read pixel values from the raster dataset into the pixel block starting from the given top left corner.raster.Read(0,0,currentPixelBlock);// Do something with the pixel block...// Write the pixel block to the raster dataset starting from the given top left corner.raster.Write(0,0,currentPixelBlock);});
Process pixels using a pixel block
awaitQueuedTask.Run(()=>{// Read pixel values from the raster dataset into the pixel block starting from the given top left corner.raster.Read(0,0,currentPixelBlock);// For each plane (band) in the pixel blockfor(intplane=0;plane<currentPixelBlock.GetPlaneCount();plane++){// Get a copy of the array of pixels from the pixel block corresponding to the current plane.ArraysourcePixels=currentPixelBlock.GetPixelData(plane,true);// Get the height and width of the pixel block.intpBHeight=currentPixelBlock.GetHeight();intpBWidth=currentPixelBlock.GetWidth();// Iterate through the pixels in the array.for(inti=0;i<pBHeight;i++){for(intj=0;j<pBWidth;j++){// Get the NoData mask value to see if the pixel is a valid pixel.if(Convert.ToByte(currentPixelBlock.GetNoDataMaskValue(plane,j,i))==1){// Get the pixel value from the array and process it (add 5 to the value).// Note: This is assuming the pixel type is Unisigned 8bit.intpixelValue=Convert.ToInt16(sourcePixels.GetValue(j,i))+5;// Make sure the pixel value does not go above the range of the pixel type.pixelValue=pixelValue>254?254:pixelValue;// Set the new pixel value to the array.// Note: This is assuming the pixel type is Unisigned 8bit.sourcePixels.SetValue(Convert.ToByte(pixelValue),j,i);}}}// Set the modified array of pixels back to the pixel block.currentPixelBlock.SetPixelData(plane,sourcePixels);}// Write the pixel block to the raster dataset starting from the given top left corner.raster.Write(0,0,currentPixelBlock);});
Calculate Raster statistics
//If a raster dataset has statistics, you can create a raster layer and get these statistics by accessing the colorizer.awaitQueuedTask.Run(()=>{//Accessing the raster layervarlyr=MapView.Active.Map.GetLayersAsFlattenedList().OfType<BasicRasterLayer>().FirstOrDefault();//Getting the colorizervarcolorizer=lyr.GetColorizer()asCIMRasterStretchColorizer;//Accessing the statisticsvarstats=colorizer.StretchStats;varmax=stats.max;varmin=stats.min;});