public static int CreateOverview(Dataset _dataset, string resampling = "NEAREST", int[] overviewlist = null) { int value = -1; if (_dataset == null || _dataset.RasterCount <= 0) { return(value); } if (overviewlist == null) { List <int> intList = new List <int>(); int width = _dataset.RasterXSize; int height = _dataset.RasterYSize; int k = 1; while (width > 256 && height > 256) { k *= 2; intList.Add(k); width /= 2; height /= 2; } overviewlist = intList.ToArray(); } value = _dataset.BuildOverviews(resampling, overviewlist); return(value); }
/** * Downsampling 피라미드 형식으로 생성 */ private void BuildOverview(int[] levels) { if (dataset.BuildOverviews("NEAREST", levels, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Sample Data") != (int)CPLErr.CE_None) { Console.WriteLine("The BuildOverviews operation doesn't work"); Environment.Exit(-1); } /* -------------------------------------------------------------------- */ /* Displaying the raster parameters */ /* -------------------------------------------------------------------- */ for (int iBand = 1; iBand <= dataset.RasterCount; iBand++) { Band band = dataset.GetRasterBand(iBand); Console.WriteLine("Band " + iBand + " :"); Console.WriteLine(" DataType: " + band.DataType); Console.WriteLine(" Size (" + band.XSize + "," + band.YSize + ")"); Console.WriteLine(" PaletteInterp: " + band.GetRasterColorInterpretation().ToString()); for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); Console.WriteLine(" OverView " + iOver + " :"); Console.WriteLine(" DataType: " + over.DataType); Console.WriteLine(" Size (" + over.XSize + "," + over.YSize + ")"); Console.WriteLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString()); } } Console.WriteLine("Completed."); }
private bool CreatePyramids(string filename) { Gdal.SetConfigOption("USE_RRD", "YES"); Dataset ds = Gdal.Open(filename, Access.GA_Update); Driver drv = ds.GetDriver(); // System.Type szDriver = drv.ShortName.GetType(); int iWidth = ds.RasterXSize; int iHeight = ds.RasterYSize; int iPixelNum = iWidth * iHeight; //图像中的总像元个数 int iTopNum = 4096; //顶层金字塔大小,64*64 int iCurNum = iPixelNum / 4; int[] anLevels = new int[1024]; int nLevelCount = 0; //金字塔级数 do { anLevels[nLevelCount] = Convert.ToInt32(Math.Pow(2.0, nLevelCount + 2)); nLevelCount++; iCurNum /= 4; } while (iCurNum > iTopNum); int[] levels = new int[nLevelCount]; for (int a = 0; a < nLevelCount; a++) { levels[a] = anLevels[a]; } int ret = ds.BuildOverviews("nearest", levels); ds.Dispose(); drv.Dispose(); return(true); }
private Dataset _CreateDSWrite(Driver drv, string sPathFileName) { double[] _gt = new double[6]; _ds.GetGeoTransform(_gt); int xSize = 0, ySize = 0; if (_opt.HaveOverview) { _ds.BuildOverviews("NEAREST", new int[] { _opt.NumOverview }); xSize = _ds.GetRasterBand(1).GetOverview(0).XSize; ySize = _ds.GetRasterBand(1).GetOverview(0).YSize; _gt[1] *= _opt.NumOverview; _gt[5] *= _opt.NumOverview; } else { xSize = _ds.RasterXSize; ySize = _ds.RasterYSize; } string[] aryOption = { "" }; int nBand = _opt.HaveAlphaBand ? _ds.RasterCount + 1 : _ds.RasterCount; Dataset dsOut = drv.Create(sPathFileName, xSize, ySize, nBand, _ds.GetRasterBand(1).DataType, aryOption); dsOut.SetProjection(_ds.GetProjection()); dsOut.SetGeoTransform(_gt); return(dsOut); }
private void _SetOverviewSameLevel(int nLevel) { if (_ds.GetRasterBand(1).GetOverviewCount() < 1) { int[] aryOverview = new int[nLevel]; for (int i = 0; i < nLevel; i++) { aryOverview[i] = (int)Math.Pow(2.0, (double)(i + 1)); } // Example: // Level = 3 // aryOverview = { 2, 4, 8 } // Common resolutions are 2 4 8 16, meaning an overview at 1/2, 1/4, 1/8, and 1/16 resolution are created. _ds.BuildOverviews("NEAREST", aryOverview); } else if (_ds.GetRasterBand(1).GetOverviewCount() != nLevel) { string sMsg = string.Format( "{0}/{1}: Number overview({2}) in image is different for Level({3})", this.ToString(), "_SetOverviewSameLevel", _ds.GetRasterBand(1).GetOverviewCount(), nLevel); throw (new Exception(sMsg)); } }
private void _SetOverviewSameLevel(ref int nLevel) { nLevel = ImageCalculus.GetTotalLevelForPyramid(_sizeTile, _ds.RasterXSize, _ds.RasterYSize); if (_ds.GetRasterBand(1).GetOverviewCount() < 1) { int[] aryOverview = new int[nLevel]; for (int i = 0; i < nLevel; i++) { aryOverview[i] = (int)Math.Pow(2.0, (double)(i + 1)); } // Example: // Level = 3 // aryOverview = { 2, 4, 8 } _ds.BuildOverviews("NEAREST", aryOverview); } else if (_ds.GetRasterBand(1).GetOverviewCount() != nLevel) { string sMsg = string.Format( "{0}/{1}: Number overview({2}) in image is different for Level({3})", this.ToString(), "_SetOverviewSameLevel", _ds.GetRasterBand(1).GetOverviewCount(), nLevel); throw (new Exception(sMsg)); } }
public static bool CreateOverviews(OverviewCreateInfo info, string inputFilePath, Gdal.GDALProgressFuncDelegate progressFuncDelegate) { bool result = false; Dataset dataset = null; try { Access access = OverviewOptionEnum.OverviewForamt.External == info.OverviewFormat ? Access.GA_ReadOnly : Access.GA_Update; Gdal.PushErrorHandler(GdalConfig.GdalHandlers.ErrorHandler); dataset = Gdal.Open(inputFilePath, access); Gdal.PopErrorHandler(); if (dataset == null) { return(result); } List <int> levels = info.LevelList; if (info.IsNeedCalcLevel) { levels = CalculateOverViewLevels(dataset); } if (levels.Count < 1) { return(result); } if (OverviewOptionEnum.OverviewForamt.External == info.OverviewFormat) { DeleteExternalOverviews(inputFilePath); } Gdal.PushErrorHandler(GdalConfig.GdalHandlers.ErrorHandler); if (dataset.BuildOverviews(info.OverviewResamplingOption.ToString(), levels.ToArray(), progressFuncDelegate, null) == (int)CPLErr.CE_None) { result = true; } Gdal.PopErrorHandler(); } catch (Exception ex) { //LoggerManager.WriteErrorLog(ex.Message); } finally { if (dataset != null) { dataset.FlushCache(); dataset.Dispose(); } } return(result); }
public override void BuildOverviews(int[] levels, Action <int, string> progressTracker) { if (_dataset == null) { return; } if (levels == null) { levels = GetLevels(); } if (levels.Length == 0) { return; } _progressTracker = progressTracker; Gdal.SetConfigOption("USE_RRD", "YES"); _dataset.BuildOverviews("NEAREST", levels, new Gdal.GDALProgressFuncDelegate(ProgressFunc), string.Empty); }
/// <summary> /// /// </summary> /// <param name="method">"nearest", "gauss", "cubic", "average", "mode", "average_magphase" or "none" </param> public void BuildPyramids(string method) { // ArcGIS does not use Pyramids when the raster has less than 1024 rows or columns // http://desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/build-pyramids.htm if (Extent.Rows < 1024 || Extent.Cols < 1024) { return; } Open(); int iPixelNum = Extent.Rows * Extent.Rows; int iTopNum = 4096; int iCurNum = iPixelNum / 4; int[] anLevels = new int[1024]; int nLevelCount = 0; do { anLevels[nLevelCount] = Convert.ToInt32(Math.Pow(2.0, nLevelCount + 2)); nLevelCount++; iCurNum /= 4; } while (iCurNum > iTopNum); int[] levels = new int[nLevelCount]; for (int a = 0; a < nLevelCount; a++) { levels[a] = anLevels[a]; } if (_ds.BuildOverviews(method, levels, null, null) == (int)CPLErr.CE_Failure) { throw new InvalidDataException("Pyramids could not be built for this dataset"); } UnloadDS(); }
public static void Main(string[] args) { if (args.Length <= 2) { usage(); } Console.WriteLine(""); try { /* -------------------------------------------------------------------- */ /* Register driver(s). */ /* -------------------------------------------------------------------- */ Gdal.AllRegister(); /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ Dataset ds = Gdal.Open(args[0], Access.GA_Update); if (ds == null) { Console.WriteLine("Can't open " + args[0]); System.Environment.Exit(-1); } Console.WriteLine("Raster dataset parameters:"); Console.WriteLine(" Projection: " + ds.GetProjectionRef()); Console.WriteLine(" RasterCount: " + ds.RasterCount); Console.WriteLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); int[] levels = new int[args.Length - 2]; Console.WriteLine(levels.Length); for (int i = 2; i < args.Length; i++) { levels[i - 2] = int.Parse(args[i]); } if (ds.BuildOverviews(args[1], levels, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Sample Data") != (int)CPLErr.CE_None) { Console.WriteLine("The BuildOverviews operation doesn't work"); System.Environment.Exit(-1); } /* -------------------------------------------------------------------- */ /* Displaying the raster parameters */ /* -------------------------------------------------------------------- */ for (int iBand = 1; iBand <= ds.RasterCount; iBand++) { Band band = ds.GetRasterBand(iBand); Console.WriteLine("Band " + iBand + " :"); Console.WriteLine(" DataType: " + band.DataType); Console.WriteLine(" Size (" + band.XSize + "," + band.YSize + ")"); Console.WriteLine(" PaletteInterp: " + band.GetRasterColorInterpretation().ToString()); for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); Console.WriteLine(" OverView " + iOver + " :"); Console.WriteLine(" DataType: " + over.DataType); Console.WriteLine(" Size (" + over.XSize + "," + over.YSize + ")"); Console.WriteLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString()); } } Console.WriteLine("Completed."); Console.WriteLine("Use: gdalread " + args[0] + " outfile.png [overview] to extract a particular overview!"); } catch (Exception e) { Console.WriteLine("Application error: " + e.Message); } }