public IRasterDataset saveRasterToDatasetM(object inRaster, string outName, IWorkspace wks, rasterType rastertype, object noDataVl=null, int IntBlockWidth= 512, int IntBlockHeight=512) { IFunctionRasterDataset fDset = null; if ((rastertype == rasterType.GRID) && (((IRasterProps)inRaster).PixelType == rstPixelType.PT_DOUBLE)) { fDset = createIdentityRaster(inRaster, rstPixelType.PT_FLOAT); } else { fDset = createIdentityRaster(inRaster); } IRasterDataset newRasterDataset = createNewRaster(inRaster, wks, outName,rastertype); IRasterBandCollection rsbc = (IRasterBandCollection)newRasterDataset; int bndCnt = rsbc.Count; IRasterFunctionHelper fHelp = new RasterFunctionHelperClass(); fHelp.Bind(fDset); IRaster nRs = ((IRasterDataset3)newRasterDataset).CreateFullRaster(); int tRasterWidth = fDset.RasterInfo.Width; int tRasterHeight = fDset.RasterInfo.Height; IPnt pntSize = new PntClass(); IPnt topLeft = new PntClass(); int intW = IntBlockWidth; int intH = IntBlockHeight; int nw = intW; int nh = intH; IRasterEdit nRsE = (IRasterEdit)nRs; if (rastertype == rasterType.GDB) { IRasterProps rsPropOut = (IRasterProps)nRs; IEnvelope env = fDset.RasterInfo.Extent; IPnt mcellSize = fDset.RasterInfo.CellSize; rsPropOut.Extent = env; rsPropOut.Width = (int)(env.Width / mcellSize.X); rsPropOut.Height = (int)(env.Height / mcellSize.Y); for (int pbh = 0; pbh < tRasterHeight; pbh += intW) { for (int pbw = 0; pbw < tRasterWidth; pbw += intH) { topLeft.SetCoords(pbw, pbh); getPbWidthHeight(tRasterWidth, tRasterHeight, topLeft, intW, intH, out nw, out nh); //Console.WriteLine("PBTL = " + pbh.ToString() + ", " + pbw.ToString() + ", PBH = " + nh.ToString() + ", PBW = " + nw.ToString()); pntSize.SetCoords(nw, nh); IPixelBlock3 inPb = (IPixelBlock3)fHelp.Raster.CreatePixelBlock(pntSize); fHelp.Raster.Read(topLeft, (IPixelBlock)inPb); IPixelBlock3 outPb = (IPixelBlock3)nRs.CreatePixelBlock(pntSize); for (int b = 0; b < bndCnt; b++) { outPb.set_PixelData(b, inPb.get_PixelDataByRef(b)); outPb.set_NoDataMask(b, inPb.get_NoDataMaskByRef(b)); } nRsE.Write(topLeft, (IPixelBlock)outPb); } } } else { object uNoDataVl = convertToActualNoDataVl(noDataVl,fDset.RasterInfo.PixelType); try { double nDv; if (!(Double.TryParse(noDataVl.ToString(), out nDv))) { uNoDataVl=nDv; } } catch(Exception e) { Console.WriteLine(e.ToString()); } try { for (int b = 0; b < bndCnt; b++) { IRasterProps rsPropsOut = (IRasterProps)rsbc.Item(b); rsPropsOut.NoDataValue = uNoDataVl; } } catch (Exception e) { Console.WriteLine(e.ToString()); } for (int pbh = 0; pbh < tRasterHeight; pbh += intH) { for (int pbw = 0; pbw < tRasterWidth; pbw += intW) { topLeft.SetCoords(pbw, pbh); getPbWidthHeight(tRasterWidth, tRasterHeight, topLeft, intW, intH, out nw, out nh); Console.WriteLine("PBTL = " + pbh.ToString() + ", " + pbw.ToString() + ", PBH = " + nh.ToString() + ", PBW = " + nw.ToString()); pntSize.SetCoords(nw, nh); IPixelBlock3 inPb = (IPixelBlock3)fHelp.Raster.CreatePixelBlock(pntSize); fHelp.Raster.Read(topLeft, (IPixelBlock)inPb); IPixelBlock3 outPb = (IPixelBlock3)nRs.CreatePixelBlock(pntSize); for (int b = 0; b < bndCnt; b++) { System.Array outSArr = (System.Array)outPb.get_PixelData(b); for (int r = 0; r < nh; r++) { for (int c = 0; c < nw; c++) { object objVl = inPb.GetVal(b, c, r); if (objVl == null) { objVl = uNoDataVl; } outSArr.SetValue(objVl, c, r); } } outPb.set_PixelData(b, outSArr); } nRsE.Write(topLeft, (IPixelBlock)outPb); } } } System.Runtime.InteropServices.Marshal.ReleaseComObject(nRsE); return newRasterDataset; }
/// <summary> /// Creates a new raster dataset based on the template Raster. If a raster with the same outRaster name exist it will be overwritten /// </summary> /// <param name="templateRaster">a raster that has the size and shape desired</param> /// <param name="outWks">the output workspace</param> /// <param name="outRasterName">the name of the raster</param> /// <param name="numBands">the number of raster bands</param> /// <param name="pixelType">the pixel type</param> /// <param name="env">the extent</param> /// <param name="meanCellSize"> the mean Cell Size of the new raster</param> /// <param name="spRf"> the spatial reference of the raster</param> /// <returns></returns> public IRasterDataset createNewRaster(IEnvelope env, IPnt meanCellSize,IWorkspace outWks, string outRasterName, int numBands, rstPixelType pixelType, rasterType rType, ISpatialReference spRf) { outRasterName = getSafeOutputName(outWks, outRasterName); IRasterDataset3 newRstDset = null; if (outWks.Type == esriWorkspaceType.esriFileSystemWorkspace) { outRasterName = getSafeOutputName(outWks, outRasterName); string rasterTypeStr = rType.ToString(); if (rType== rasterType.IMAGINE) { rasterTypeStr = "IMAGINE Image"; outRasterName = outRasterName + ".img"; } else if (rType == rasterType.TIFF) { outRasterName = outRasterName + ".tif"; } else if (rType == rasterType.GRID) { } else if (rType == rasterType.BMP) { outRasterName = outRasterName + ".bmp"; } else if (rType == rasterType.RST) { outRasterName = outRasterName + ".rst"; } else { rasterTypeStr = "IMAGINE Image"; outRasterName = outRasterName + ".img"; } double dX = meanCellSize.X; double dY = meanCellSize.Y; IRasterWorkspace2 rsWks = (IRasterWorkspace2)outWks; newRstDset = (IRasterDataset3)rsWks.CreateRasterDataset(outRasterName, rasterTypeStr, env.LowerLeft, System.Convert.ToInt32(env.Width / dX), System.Convert.ToInt32(env.Height / dY), dX, dY, numBands, pixelType, spRf, true); } else { IRasterWorkspaceEx rsWks = (IRasterWorkspaceEx)outWks; IRasterDef rsDef = new RasterDefClass(); IRasterStorageDef rsStDef = new RasterStorageDefClass(); rsStDef.Origin = env.LowerLeft; rsStDef.CellSize = meanCellSize; rsDef.SpatialReference = spRf; newRstDset = (IRasterDataset3)rsWks.CreateRasterDataset(outRasterName, numBands, pixelType, rsStDef, null, rsDef, null); } return newRstDset; }
public IRasterDataset saveRasterToDataset(IRaster inRaster, string outName, IWorkspace wks,rasterType rastertype) { string rsTypeStr = rastertype.ToString(); string ext = ""; if (rastertype== rasterType.IMAGINE) { rsTypeStr = "IMAGINE Image"; ext = ".img"; } else if (rastertype == rasterType.HDF4) { ext = ".hdf"; } else if (rastertype == rasterType.ENV) { ext = ".hdr"; } else { ext = "." + rastertype.ToString().ToLower(); } esriWorkspaceType tp = wks.Type; if (tp == esriWorkspaceType.esriLocalDatabaseWorkspace) { rsTypeStr = rasterType.GDB.ToString(); } if (rastertype == rasterType.GRID || rastertype == rasterType.GDB) { outName = getSafeOutputName(wks, outName); if (outName.Length > 12) { outName.Substring(12); } if ((rastertype==rasterType.GRID)&&(((IRasterProps)inRaster).PixelType == rstPixelType.PT_FLOAT)) { inRaster = returnRaster(inRaster,rstPixelType.PT_FLOAT); } } else { if (outName.IndexOf(ext) == -1) { outName = outName + ext; } } if (geoUtil.ftrExists(wks, outName)) { outName = "_" + outName; } IRasterDataset rsDset = null; try { IRasterStorageDef2 rsStorDef = new RasterStorageDefClass(); rsStorDef.PyramidLevel = 0; ISaveAs2 sv = (ISaveAs2)inRaster; rsDset = (IRasterDataset)sv.SaveAsRasterDataset(outName, wks, rsTypeStr,rsStorDef); IRaster2 rs2 = (IRaster2)calcStatsAndHist(rsDset); ITable vat = rs2.AttributeTable; int rwCnt = 0; try { rwCnt = vat.RowCount(null); } catch { rwCnt = 0; } if (rwCnt > 0) { IRasterDatasetEdit2 rsDsetE = (IRasterDatasetEdit2)rsDset; rsDsetE.DeleteAttributeTable(); if (((IRasterBandCollection)rs2).Count == 1) { rsDsetE.BuildAttributeTable(); } } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.ToString()); Console.WriteLine(e.ToString()); rsDset = ((IRaster2)returnRaster(wks.PathName + "\\" + outName)).RasterDataset; } return rsDset; }
/// <summary> /// Creates a new raster dataset based on the template Raster. If a raster with the same outRaster name exist it will be overwritten /// </summary> /// <param name="templateRaster">a raster that has the size and shape desired</param> /// <param name="outWks">the output workspace</param> /// <param name="outRasterName">the name of the raster</param> /// <param name="numBands">the number of raster bands</param> /// <param name="pixelType">the pixel type</param> /// <returns></returns> public IRasterDataset createNewRaster(object templateRaster, IWorkspace outWks, string outRasterName, rasterType rType) { IFunctionRasterDataset fDset = createIdentityRaster(templateRaster); //IRasterBandCollection rsBC = (IRasterBandCollection)fDset; IEnvelope env = fDset.RasterInfo.Extent; IPnt meanCellSize = fDset.RasterInfo.CellSize; int numBands = fDset.RasterInfo.BandCount; rstPixelType pType = fDset.RasterInfo.PixelType; ISpatialReference spRef = fDset.RasterInfo.SpatialReference; IRasterDataset rsDset = createNewRaster(env, meanCellSize, outWks, outRasterName, numBands, pType, rType, spRef); return rsDset; }