/// <summary> /// Runs GdalWarp with passed parameters /// </summary> /// <param name="inputFilePath">Input GeoTiff's path</param> /// <param name="outputFilePath">Output file's path</param> /// <param name="options">Array of string parameters /// <remarks><para/>See <see cref="ConvertCoordinateSystemOptions"/> field for /// more info</remarks></param> /// <param name="progress">GdalWarp's progress /// <remarks><para/><see langword="null"/> by default</remarks></param> /// <exception cref="ArgumentNullException"/> public static Task WarpAsync(string inputFilePath, string outputFilePath, string[] options, IProgress <double> progress = null) { #region Preconditions checks CheckHelper.CheckFile(inputFilePath); CheckHelper.CheckFile(outputFilePath, false); CheckHelper.CheckDirectory(Path.GetDirectoryName(outputFilePath)); if (options == null || options.Length <= 0) { throw new ArgumentNullException(nameof(options)); } #endregion Gdal.GDALProgressFuncDelegate callback = GdalProgress; _gdalProgress = progress; // Initialize Gdal, if needed ConfigureGdal(); return(Task.Run(() => { using Dataset inputDataset = Gdal.Open(inputFilePath, Access.GA_ReadOnly); using Dataset resultDataset = Gdal.Warp(outputFilePath, new[] { inputDataset }, new GDALWarpAppOptions(options), callback, string.Empty); })); }
public static void Main(string[] args) { if (args.Length != 3) { usage(); } Gdal.AllRegister(); GDALWarpAppOptions options = new GDALWarpAppOptions(args[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); string[] dstNames = args[2].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Dataset[] ds = new Dataset[dstNames.Length]; for (int i = 0; i < dstNames.Length; i++) { ds[i] = Gdal.Open(dstNames[i], Access.GA_ReadOnly); } Dataset dso = Gdal.Warp(args[0], ds, options, new Gdal.GDALProgressFuncDelegate(ProgressFunc), "Sample Data"); if (dso == null) { Console.WriteLine("Can't create dest dataset " + args[1]); System.Environment.Exit(-1); } }
private void backgroundWorkerMain_DoWork(object sender, DoWorkEventArgs e) { progressInfo.SubTitle = "读取基本信息"; reportProgress(); List <long> codeList = CountryDao.FindAllCode(); if (codeList == null || codeList.Count == 0) { MessageBox.Show("矢量没有正确的code_new信息,请重新注册"); return; } DataSource dataSource = Ogr.Open("data\\shp\\dst\\country.shp", 0); int codeNum = codeList.Count; progressInfo.Info = string.Format("已完成:0/{0}", codeNum); for (int i = 0; i < codeNum; i++) { progressInfo.SubComplete = 0; progressInfo.SubTitle = "查询数据"; reportProgress(); long code = codeList[i]; List <string> nameList = ImgDao.findNameByYearAndCode(year, code); if (nameList == null || nameList.Count == 0) { continue; } Layer layer = dataSource.ExecuteSQL(string.Format("select * from country where Code_new={0}", code), null, null); if (layer.GetFeatureCount(1) == 0) { continue; } layer.ResetReading(); Feature feature = layer.GetNextFeature(); Geometry geometry = feature.GetGeometryRef(); Envelope envelope = new Envelope(); geometry.GetEnvelope(envelope); int nameCount = nameList.Count; Dataset[] inDss = new Dataset[nameCount]; for (int j = 0; j < nameCount; j++) { inDss[j] = Gdal.Open(string.Format("data\\img\\r{0}\\{1}", year, nameList[j]), Access.GA_ReadOnly); } string outFile = string.Format("{0}\\{1:D3}.tif", outPath, code); string mosaicFile = "/vsimem/mosaic.tif"; string maskFile = "/vsimem/mask.tif"; try { progressInfo.SubTitle = "正在镶嵌:"; string[] options = new string[] { "-t_srs", "data\\shp\\dst\\country.prj", "-tr", "30", "30", "-srcnodata", "128", "-dstnodata", "0", "-ot", "Byte", "-co", "COMPRESS=LZW", "-co", "TILED=YES", "-te", envelope.MinX.ToString(), envelope.MinY.ToString(), envelope.MaxX.ToString(), envelope.MaxY.ToString() }; GDALWarpAppOptions gdalOptions = new GDALWarpAppOptions(options); Dataset mosaicDs = Gdal.Warp(mosaicFile, inDss, gdalOptions, gdalProgressFunc, null); progressInfo.SubTitle = "正在栅格化"; progressInfo.SubInfo = ""; options = new string[] { "-burn", "1", "-where", string.Format("Code_new={0}", code), "-ot", "Byte", "-co", "COMPRESS=LZW", "-co", "TILED=YES", "-tr", "30", "30", "-te", envelope.MinX.ToString(), envelope.MinY.ToString(), envelope.MaxX.ToString(), envelope.MaxY.ToString() }; GDALRasterizeOptions rasterOptions = new GDALRasterizeOptions(options); Dataset vecDs = Gdal.OpenEx("data\\shp\\dst\\country.shp", 0, null, null, null); Dataset maskDs = Gdal.wrapper_GDALRasterizeDestName(maskFile, vecDs, rasterOptions, gdalProgressFunc, null); Dataset outDs = multi(mosaicDs, maskDs, outFile); outDs.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); continue; } progressInfo.Info = string.Format("已完成:{0}/{1}", i + 1, codeNum); totalProgress = (i + 1) * 100 / codeNum; reportProgress(); } MessageBox.Show("镶嵌完成"); }