c#调用gdalwarp

mac2025-09-07  11

通过命令行gdalwarp.exe通过gdal的c#binding通过c#调用gdal的python绑定 第一种直接使用Process类,写好py文件即可 第3种也可以new Process,或者用pythonnet之类的库也可(ps 我没试过) var inRasterDs = Gdal.Open(_inRasterFile, Access.GA_ReadOnly); string evWkt = inRasterDs.GetProjectionRef(); var cellSize = RasterUtil.GetCellSize(inRasterDs); //栅格6参数 double[] argout = new double[6]; inRasterDs.GetGeoTransform(argout); //calc extent var xmin = argout[0]; var ymax = argout[3]; var xmax = argout[0] + argout[1] * inRasterDs.RasterXSize + inRasterDs.RasterYSize * argout[2]; var ymin = argout[3] + argout[4] * inRasterDs.RasterXSize + inRasterDs.RasterYSize * argout[5]; inRasterDs.Dispose(); //重投影并指定像元大小和四至,重采样方式为nearest,并指定格式为ENVI .hdr Labelled Raster var options = Gdal.ParseCommandLine($"-t_srs {evWkt} -tr {cellSize.cellSizeX} {cellSize.cellSizeY} " + $"-te {xmin} {ymin} {xmax} {ymax} -r near -of ENVI "); //调用gdalwarp using (Dataset inputDataset = Gdal.Open(inraster, Access.GA_ReadOnly)) { IntPtr[] ptr = { Dataset.getCPtr(inputDataset).Handle }; GCHandle gcHandle = GCHandle.Alloc(ptr, GCHandleType.Pinned); Dataset resultDs = null; try { var dss = new SWIGTYPE_p_p_GDALDatasetShadow(gcHandle.AddrOfPinnedObject(), false, null); resultDs = Gdal.wrapper_GDALWarpDestName( outputRasterPath, 1, dss, new GDALWarpAppOptions(options), this.reprojectionProgressUC, null); } catch (Exception) { _logger.Error(Gdal.GetLastErrorMsg()); _logger.Error(e); throw; } finally { gcHandle.Free(); if (resultDs != null) { resultDs.Dispose(); } } } /// <summary> /// 默认进度信息 /// </summary> public int reprojectionProgressUC(double complete, IntPtr Message, IntPtr Data) { string msg = Marshal.PtrToStringAnsi(Message); string datastr = Marshal.PtrToStringAnsi(Data); Console.WriteLine(complete + " " + msg + " " + datastr); //_logger.Info(complete + " " + msg + " " + datastr); ProgressChanged?.Invoke(this, Tuple.Create(complete, msg)); //1 继续执行 0 停止执行 if (_tokenSource.IsCancellationRequested) { Console.WriteLine($"{_inRasterFile} resample canceled"); // _logger.Info($"{_inRasterFile} resample canceled"); return 0; } else { return 1; } }

C#在.NET上运行GDAL / gdalwarp … 2 Using gdalwarp with c# bindings?

最新回复(0)