Developing with ArcGIS Raster APIs
Hong Xu, Qian Liu and Peng Gao
Presentation Outline • Introduction of raster data model • Access and display of raster data • Operations on raster data – SaveAs,filtering,transformation,mosaic etc. • Creating raster data and writing pixels • Raster attribute table • Loading raster to a geodatabase
Characteristics of Raster Data pixel size pixel value pixel type
•
columns
Stored in many formats – – – –
GRID, TIFF, IMG, JPEG, JP2000, etc. ArcSDE, PGDB FGDB 9.2 PCI, ECW, USGS ASCII DEM, BSB, PCRaster, HDF4 9.2
bands rows
• • • • • •
Pyramids Statistics Colormap Raster attribute table 9.2 Spatial reference Geodata transformation
9.2
Common APIs for Raster Data Access •
Same interface to access data in all supported formats. – Auxiliary file (aux) to store information that are not handled
by formats – .rrd file is to store pyramids – Geodatabase raster data support those properties internally •
Open structure for adding new raster format support – GDAL driver – Format dll
9.2
Raster Data Model •
Raster datasets (RasterDataset) –
•
Arrays of pixels and the associated information stored in a file system or geodatabase.
Raster catalogs(RasterCatalog) –
A collection of raster datasets
RasterDataset
RasterCatalog
Raster Data Objects Workspace
RasterCatalog
Selection DefinitionQuery
GdbRasterCatalog Layer RasterRender
* RasterDataset
Raster
PixelFilter
MosaicRaster
PixelBlock
Raster GeometryProc
GeodataXForm
RasterLayer
Presentation Outline • Introduction of raster data model • Access and display of raster data • Operations on raster data – SaveAs,filtering,transformation,mosaic etc. • Raster attribute table • Creating raster data and writing pixels • Loading raster to a geodatabase
Open Raster Data Workspace
•
IRasterWorkspaceEx (optional)
OpenRasterCatalog (Name):IRasterCatalog OpenRasterDataset (Name):IRasterDataset ……
IRasterWorkspace2 (optional)
OpenRasterDataset (Name): IRasterDataset …….
Access GDB raster dataset and raster catalog IRasterWorkspaceEx workspace; IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass(); workspace = (IRasterWorkspaceEx) workspaceFactory.OpenFromFile(@"c:\temp\data.gdb", 0); IRasterDataset rasterDataset = workspace.OpenRasterDataset("airphoto"); IRasterCatalog rasterCatalog = workspace.OpenRasterCatalog("DRGs");
•
Access raster dataset from file system IRasterWorkspace workspace; IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass (); workspace = (IRasterWorkspace) workspaceFactory.OpenFromFile(@"c:\temp", 0); IRasterDataset rasterDataset = workspace.OpenRasterDataset("redlands.tif");
Raster Dataset Display RasterLayer IRasterLayer
•
CreateFromDataset (RasterDataset):IRasterLayer CreateFromRaster(Raster): IRasterLayer Renderer:IRenderer ……
Set a raster renderer or use the default IRasterLayer rasterlayer = new RasterLayerClass(); rasterlayer.CreateFromDataset rasterDataset; rasterlayer.Renderer = renderer; mapControl.FocusMap.AddLayer rasterlayer
•
Raster Renderers: –
•
RGB renderer, Stretch renderer,Unique value renderer,Classify renderer, Colormap renderer
Use RasterRendererMakerDefault to custom your own default rendering behavior
Raster Catalog Display GdbRasterCatalogLayer IRasterLayer
•
Setup (Table as ITable): IGdbRasterCatalogLayer Renderers : IArray ……
Set available renderers or use default IGdbRasterCatalogLayer catalogLayer = new GdbRasterCatalogLayerClass(); catalogLayer.Setup(RasterCatalog); catalogLayer.Renderers = RendererArray; mapControl.FocusMap.AddLayer catalogLayer;
•
RasterCatalogRendererPickerDefault to customize default renderer picker
•
RasterCatalogLayer is for displaying a 8.x table based raster catalog.
Presentation Outline • Introduction of raster data model • Access and display of raster data • Operations on raster data – SaveAs,filtering,transformation,mosaic etc. • Creating raster data and writing pixels • Raster attribute table • Loading raster to a geodatabase
Raster – Setting Raster Properties IRasterProps
ISaveAs
Raster Extent: IEnvelope Height:Long NoDataValue:Variant PixelType:RstPixelType SpatialReference: ISpatialReference Width: Long MeanCellSize:IPnt SaveAs (Name as String, ws as IWorkspace,format as String)
IRasterDataset.CreateDefaultRaster creates Raster • Set Extent, column/row •
– Width of extent = cell size x #column – Cell size is derived
•
Persist using SaveAs
Raster - SaveAs Raster or RasterDataset ISaveAs
SaveAs (Name, workspace, Format): IRasterDataset
ISaveAs2
•
SaveAsRasterDataset (Name, workspace, Format, StorageDef) IRasterDataset
ISaveAs is supported by both Raster and RasterDataset – Mainly perform format conversion for RasterDataset case – Resampled and persist the change if used with Raster
•
Support SaveAs to GRID, IMAGINE, TIFF and geodatabases and more in 9.2 – FGDB
9.2
– JPEG, JP2, PNG, BMP, PCI, DEM, PCRaster, HDF4 – Set compressions using RasterStorageDef
9.2
9.2
– Control pyramids building when saveas to GRID, IMG and TIFF
9.2
Raster – Pixel vs. Map Space column row
Raster 9.2
IRaster2
MapToPixel (x, y, column, row) PixelToMap (column, row, x, y) GetPixelValue(Band, Column, Row)
Y
x
•
Given (x,y) in map, find column and row as well as the pixel value IRaster2 raster = (IRaster2)rasterDataset.CreateDefaultRaster; int iColumn = raster.ToPixelColumn(mapX); int iRow = raster.ToPixelRow (mapY); double value = raster.GetPixelValue(0, iColumn, iRow);
At 9.1 IRasterGeometryProc2::WKSPointsMap2PixelTransform IPixelBlock::GetVal
New Geodata Transformations
9.2
GeodataXform IGeodataXform IClone IPersist IPersistStream
IGeodataXform : IUnknown Domains: IGeometryCollection IsIdentity: Boolean SpatialReference: ISpatialReference Transform (in Direction: esriTransformDirection, in npoints: Long, in points: WKSPoint) TransformCellsize (in Direction: esriTransformDirection, in dx: Double, in dy: Double, in pAreaOfInterest: IEnvelope) TransformExtent (in Direction: esriTransformDirection, in pExtent: IEnvelope) TransformPoints (in Direction: esriTransformDirection, in pPoints: IPointCollection)
RPCXform
PolynomialXform
AdjustXform
CoordinateXform
SplineXform
……
CustomXform
Support polynomial (RasterGeometryProc in 9.1), Spline, rubbersheeting transformations • Raster re-projection • Image orthorectification • Create your own Xform class •
Performing Transformations RasterDataset IGeoDatasetSchemaEdit2 AlterGeodataXform (IGeodataXform xform)
•
GeodataXform
9.2
Raster IRaster2 : IUnknown GeodataXform: IGeodataXform GeoTransformations: IGeoTransformationOperationSet RasterDataset: IRasterDataset RasterXformer: IRasterXformer
Persist with dataset using IGeoDataSchemaEdit2 – Auxiliary file based raster, internal for GDB raster
•
Apply to a Raster using IRaster2::GeodataXform – Transform extent, the set – Transform cell size, then set width/height – SaveAs
•
Project raster with datum transformation – IRaster2::GeoTransformations
Pixel Filters
Raster
PixelFilter IPixelFilter : IUnknown Filter (in pBlock: IPixelBlock)
Pansharpening Filter
RemapFilter
RasterConvolution Filter
•
•
IPixelOperrtion : IUnknown Filter
CustomFilter
……
ConvolutionFilter – 3x3, 5x5 low pass, high pass, and etc.
-1 2 -1
– Define your own kernel (PutCoefficients)
-1 2 -1 -1 2 -1
PanSharpeningFilter 9.2 – Mean, Brovey, IHS, and ESRI methods
•
Create your own PixelFilter class
•
Apply to Raster using IPixelOperation::Filter
Mosaicking Raster Data MosaicRaster IRasterCollection IMosaicRaster ISaveAs
IMosaicRaster2
•
Using MosaicRaster to create new dataset – – – –
•
Append (IRaster:raster) …… RasterCatalog (IRasterCatalog rastercatalog) WhereClause (String whereclause) MosaicColormapMode …… RasterCatalogTable (IRasterCatalogTable catalogTable) ……
Mosaic from many raster datasets Mosaic from RasterCatalog & WhereClause Mosaic from RasterCatalogTable ISaveAs
Mosaic to an existing dataset (appending) IRasterDatasetEdit::Mosaic RasterLoader – Designed for mosaicking SDE/FGDB raster dataset – –
Presentation Outline • Introduction of raster data model • Access and visualization of raster data • Operations on raster data – SaveAs,filtering,transformation, mosaic etc. • Creating raster data and writing pixels • Raster attribute table • Loading raster to a geodatabase
Creating Raster Dataset IRasterWorkspaceEx (optional)
IRasterWorkspace2 (optional)
•
CreateRasterDataset (Name, Format, origin, column, row, cellX, cellY, #Band, PixelType, spatialreference, perment ): IRasterDataset …….
•
Create file raster dataset – –
•
Workspace CreateRasterCatalog (Name, Fields, ShapeName, RasterName, Configkey):IRasterCatalog CreateRasterDataset (Name, #Band, PixelType, StorageDef, ConfigKey, RasterDef, GeometryDef):IRasterDataset ……
Empty raster – Mosaic or write using pixel blocks
With dimension Populated with default values
–
Create in-memory raster dataset –
Stores the pixels in memory
Create GDB raster dataset
9.2
Working with PixelBlocks Raster IRaster
CreatePixelBlock(size):IPixelBlock
PixelBlock IPixelBlock3 PixelData(#band)
IRaster raster = rasterDataset.CreateDefaultRaster; IRasterProps rasterProps = (IRasterProps)raster; rasterProps.Extent = extent; rasterProps.Width = 512; rasterProps.Height = 512; IPixelBlock3 pixelblock= (IPixelBlock3)raster.CreatePixelBlock(blockSize); System.Array pixelArray = (System.Array)pixelblock.get_PixelData(0); //edit pixel values pixelblock.set_PixelData(0, pixelArray); IRasterEdit rasterEdit = (IRasterEdit)raster; rasterEdit.Write (tlcorner, pixelblock);
Using PixelBlock with RasterCursor Raster IRaster IRaster2
CreateCursor(): IRasterCursor CreateCursorEx(Ipnt: Size) :IRasterCursor
RasterCursor IRasterCursor PixelBlock: IPixelBlock Next TopLeft: IPnt Reset
IRaster2 raster2 = (IRaster2 ) raster; IRasterEdit rasterEdit = (IRasterEdit)raster; IRasterCursor cursor = raster2.CreateCursorEx(blockSize); IPixelBlock3 pixelBlock; IPnt tlCorner; do { pixelBlock = (IPixelBlock3)cursor.PixelBlock; //do something with the pixel block tlCorner = cursor.TopLeft; rasterEdit.Write(tlCorner, pixelblock); } While (cursor.Next == true)
PixelBlock
Presentation Outline • Introduction of raster data model • Access and display of raster data • Operations on raster data – Conversion,mosaic,transformation etc. • Creating raster data and writing pixels • Raster attribute table • Loading raster to a geodatabase
Raster Attribute Table
9.2
• Raster of all formats support raster attribute
table
RAT for SDE/FGDB raster is stored as a table with a special name and joined internally – GRID still uses VAT – .dbf format for other file raster formats –
• Build raster attribute table – Single band of integer type – Build attribute table if not exists (65563) IRasterDatasetEdit2 rasterdatasetEdit = (IRasterDatasetEdit2)rasterDataset; rasterdatasetEdit2.BuildAttributeTable
Raster Attribute Table •
Access raster attribute table IRasterBandCollection bandCol = rasterDataset; IRasterBand rasterBand = bandCol.Item(0); ITable rasterTable = rasterBand.AttributeTable;
IRaster2 raster = rasterDataset.CreateDefaultRaster; rasterTable = raster.AttributeTable;
•
9.2
Raster table is a generic table (ITable) in 9.2 and supports editing – Add a field – Modify a value in the table
Presentation Outline
• • • • • •
Introduction of raster data model Access and display of raster data Operations on raster data – SaveAs,filtering,transformation, mosaic etc. Raster attribute table Creating raster data and writing pixels Loading rasters to a geodatabase
Create Large Raster Mosaic • Create an empty raster dataset – Set pyramid reference point – Define spatial reference – Specify pyramid level and resample method – Specify storage parameter • Tile size, compression type and quality IRasterStorageDef rasterStorageDef; //set pyramid origin IPoint origin = new PointClass(); origin.PutCoords(-180, 90); rasterStorageDef.Origin = origin; //set compression to JPEG 25 rasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG; rasterStorageDef.CompressionQuality = 25; //set spatial reference to GCS_WGS_1984 ISpatialReference spatialReference = CreateSpatialReference(4326); rasterDef.SpatialReference = spatialReference;
Mosaic To Large Raster Mosaic • Mosaic raster dataset – Resolve overlapping area – Process background – Shift/resample if cells are not aligned void MosaicRaster(IRasterDataset dataset, IRasterDataset input) { IRasterLoader rasterLoader = new RasterLoaderClass(); //set background to ignore rasterLoader.Background = 0; //set tolerance so that it shifts if there is a cell misalignment rasterLoader.PixelAlignmentTolerance = 0.5; //mosaic rasterLoader.Load(dataset, input.CreateDefaultRaster()); }
Raster Catalog GeometryDef IRasterCatalog IFeatureClass
RasterDef
RasterCatalog
...
IRasterCatalogItem IRow IRowBuffer
RasterCatalogItem
...
RasterStorageDef
•
RasterValue
RasterDef defines properties for Raster column Spatial reference, managed or unmanaged – 9.2 raster values can persist its own spatial reference –
GeometryDef defines spatial reference for Geometry field • RasterStorageDef is used to set storage properties – Compression, pyramid building, pyramid origin, tile size, etc. •
Create Raster Catalog • Specify raster column and geometry column
properties when creating fields –
IRasterDef • Spatial reference, management type
–
IGeometryDef • Spatial reference, spatial index grids //shape field IGeometryDef geometryDef = new GeometryDefClass(); IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; (IGeometryDefEdit)geometryDef; geometryDefEdit.SpatialReference_2 geometryDefEdit.SpatialReference_2 = spatialReference; spatialReference; //… //… set other properties
//raster field IField2 field2 = new FieldClass(); IFieldEdit2 field2Edit = (IFieldEdit2)field2; IRasterDef rasterDef = new RasterDefClass();
fieldEdit.Name_2 = "SHAPE"; fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; fieldEdit.GeometryDef_2 = geometryDef;
field2Edit.Type_2 = esriFieldType.esriFieldTypeRaster; field2Edit.Name_2 = "RASTER"; rasterDef.SpatialReference =CreateSpatialReference(29714); field2Edit.RasterDef = rasterDef; field2Edit.IsManaged = true;
Load Raster Dataset To Raster Catalog • Batch loading – Load a directory or a list of raster datasets – Control projection on-the-fly – Control geotransformation – Specify storage parameters for all the rasters IRasterCatalogLoader catalogLoader = new RasterCatalogLoaderClass(); //set workspace where raster catalog resides catalogLoader.Workspace = workspace; //maintain spatial reference of the input raster catalogLoader.Projected = false; //set storage parameter catalogLoader.StorageDef = storageDef; //load catalogLoader.Load(catalogName, folder, null);
9.2
Load Raster Dataset To Raster Catalog • Individual loading – Is similar to inserting feature to a featureclass – Create a Feature using IFeature – Set raster field with IRasterValue – Specify storage parameter with IRasterStorageDef void LoadRasterCatalog(IRasterCatalog catalog, IRasterDataset dataset) { IFeatureClass featureClass = (IFeatureClass)catalog; IFeature feature = featureClass.CreateFeature(); //create raster value and set IRasterValue rasterValue = new RasterValueClass(); rasterValue.RasterDataset = dataset; feature.set_Value(catalog.RasterFieldIndex, rasterValue); feature.Store(); }
Access Raster Dataset in Raster Catalog • Open raster catalog • Get raster catalog item • Get raster dataset IRasterDataset GetRasterCatalogItem(IRasterCatalog catalog, int oid) { IFeatureClass featureClass = (IFeatureClass)catalog; IRasterCatalogItem rasterCatalogItem = featureClass.GetFeature(oid); return rasterCatalogItem.RasterDataset; }
Distributed Raster Catalog Loading • Designed for an ArcGIS server environment • Support parallel loading using available server
containers • Load a directory or a list of rasters DrLoader IDrLoader
9.2
IDrLoader : IUnknown InvalidDatasets: String IsBusy: Boolean LastError: IErrorInfo RasterCatalogLoader: IRasterCatalogLoader ServerName: String Cancel GetStats (out pName: String, out pMillis: Double) GetThreadHandle: Long Load (in catalog: String, in Directory: String) LoadDatasets (in catalog: String, in namelist: String)
Session Evaluations Reminder
Session Attendees: Please turn in your session evaluations. . . . Thank you