internal AbstractWarpDataset MosaicToFile(string mosaicFilename) { AbstractWarpDataset mosaicFileRaster = null; try { if (File.Exists(mosaicFilename)) { var rDs = Gdal.Open(mosaicFilename, Access.GA_Update); mosaicFileRaster = new WarpDataset(rDs, mosaicFilename); if (mosaicFileRaster.BandCount != fileRaster.BandCount) { mosaicFileRaster.Dispose(); mosaicFileRaster = CreateMosaicFile(mosaicFilename, inArg); } } else { mosaicFileRaster = CreateMosaicFile(mosaicFilename, inArg); } if (mosaicFileRaster == null) { return(null); } RasterMoasicProcesser mo = new RasterMoasicProcesser(); //mo.Moasic(new IRasterDataProvider[] { fileRaster }, mosaicFileRaster, true, new string[] { "0" }, action); mo.MoasicSimple(fileRaster, mosaicFileRaster, true, new string[] { "0" }, action); return(mosaicFileRaster); } finally { } }
private void TryGetInfosFromRasterProvider(string fname) { string extension = Path.GetExtension(fname).ToUpper(); foreach (string ext in EXTENSIONS) { if (extension == ext) { return; } } if (!File.Exists(fname)) { return; } AbstractWarpDataset prd = null; //IGeoDataDriver id = null; try { prd = WarpDataset.Open(fname); if (prd != null) { _projectName = GetProjectionIdentify(prd.SpatialRef.GetAttrValue("GEOGCS", 0)); DataIdentify df = prd.DataIdentify; if (string.IsNullOrEmpty(Satellite) || Satellite.ToUpper() == "NUL") { Satellite = df.Satellite; } if (string.IsNullOrEmpty(Sensor) || Sensor.ToUpper() == "NUL") { Sensor = df.Sensor; } if (OrbitDateTime == DateTime.MinValue) { OrbitDateTime = df.OrbitDateTime; } if (string.IsNullOrWhiteSpace(Resolution) || Resolution == "NULL") { Resolution = TryGetResolution(prd.ResolutionX); } } } catch { } finally { if (prd != null) { prd.Dispose(); } } }
private void TryGetProject(string fname) { AbstractWarpDataset prd = WarpDataset.Open(fname); try { if (prd != null) { _projectName = GetProjectionIdentify(prd.SpatialRef.GetAttrValue("GEOGCS", 0)); } } finally { if (prd != null) { prd.Dispose(); } } }
public void Do(InputArg inArg) { CheckAtg(inArg); string projectionIdentify = inArg.ProjectionIdentify; OutputArg outArg = new OutputArg(); try { using (AbstractWarpDataset inputRaster = WarpDataset.Open(inArg.InputFilename)) { RasterDatasetInfo dsInfo = mRasterSourceManager.GetInstance().GetRasterDatasetInfo(inArg.InputFilename); DateTime? dateTime = mRasterSourceManager.GetInstance().GetImageTime(inArg.InputFilename); DataIdentify dataIdentify = new DataIdentify(); outArg.OrbitFilename = Path.GetFileName(inArg.InputFilename); outArg.Satellite = dsInfo.SatelliteID; outArg.Sensor = dsInfo.SensorID; outArg.Level = "L1"; outArg.ProjectionIdentify = projectionIdentify; outArg.ObservationDate = dateTime.HasValue ? dateTime.Value.ToString("yyyyMMdd") : ""; outArg.ObservationTime = dateTime.HasValue ? dateTime.Value.ToString("HHmm") : ""; outArg.Station = ParseStation(Path.GetFileName(inArg.InputFilename)); outArg.DayOrNight = DayOrNight(inputRaster); if (dateTime.HasValue) { outArg.OrbitIdentify = CalcOrbitIdentify(dateTime.Value, inArg.PervObservationDate, inArg.PervObservationTime, inArg.OrbitIdentify); } outArg.Length = new FileInfo(inArg.InputFilename).Length; string validEnvelopeMsg = ""; #region 日夜检查 if (!string.IsNullOrWhiteSpace(inArg.DayNight)) { if (inArg.DayNight != "daynight" && outArg.DayOrNight == "X") { outArg.LogLevel = "info"; outArg.LogInfo = "未设定处理白天和晚上数据,白天晚上标记未知:X"; } else if (inArg.DayNight == "day" && outArg.DayOrNight != "D") { outArg.LogLevel = "info"; outArg.LogInfo = "设定为只处理白天数据,当前数据标记为晚上"; } else if (inArg.DayNight == "night" && outArg.DayOrNight != "N") { outArg.LogLevel = "info"; outArg.LogInfo = "设定为只处理晚上数据,当前数据标记为白天"; } else if (inArg.DayNight == "notnight" && outArg.DayOrNight == "N") { outArg.LogLevel = "info"; outArg.LogInfo = "设定为不处理晚上数据,当前数据标记为晚上"; } else if (inArg.DayNight == "notday" && outArg.DayOrNight == "D") { outArg.LogLevel = "info"; outArg.LogInfo = "设定为不处理白天数据,当前数据标记为白天"; } } #endregion 日夜检查 if (inArg.ValidEnvelopes == null || inArg.ValidEnvelopes.Length == 0) { outArg.LogLevel = "error"; outArg.LogInfo = "参数错误:未正确设置ValidEnvelopes"; } else if (!ValidEnvelope(inputRaster, inArg.ValidEnvelopes, out validEnvelopeMsg)) { outArg.LogLevel = "info"; outArg.LogInfo = validEnvelopeMsg; } else { PrjOutArg prjArg; if (inArg.Envelopes == null || inArg.Envelopes.Length == 0) { prjArg = new PrjOutArg(projectionIdentify, null, inArg.ResolutionX, inArg.ResolutionY, inArg.OutputDir); } else { prjArg = new PrjOutArg(projectionIdentify, inArg.Envelopes, inArg.ResolutionX, inArg.ResolutionY, inArg.OutputDir); } //prjArg.Args = new string[] { "SolarZenith"}; if (inArg.Bands != null && inArg.Bands.Length != 0) { prjArg.SelectedBands = inArg.Bands; Console.WriteLine("SelectedBands:" + string.Join(",", prjArg.SelectedBands)); } //扩展参数 List <string> extArgs = new List <string>(); extArgs.Add("IsClearPrjCache"); if (inArg.ExtArgs != null) { extArgs.AddRange(inArg.ExtArgs); } prjArg.Args = extArgs.ToArray(); ProjectionFactory prjFactory = new ProjectionFactory(); string retMessage = ""; string[] files = prjFactory.Project(inputRaster, prjArg, new Action <int, string>(OnProgress), out retMessage); prjFactory = null; //投影结束,执行拼接,如果有拼接节点 List <OutFileArg> fileArgs = new List <OutFileArg>(); for (int i = 0; i < files.Length; i++) { string file = files[i]; if (string.IsNullOrWhiteSpace(file) || !File.Exists(file)) { continue; } OutFileArg fileArg = new OutFileArg(); Envelope env = null; float resolutionX; float resolutionY; string overViewFilename = ""; using (AbstractWarpDataset outfileRaster = WarpDataset.Open(file)) { Console.WriteLine("生成缩略图开始"); overViewFilename = OverViewHelper.OverView(outfileRaster, _prjPngSize); Console.WriteLine("生成缩略图结束"); env = outfileRaster.GetEnvelope(); resolutionX = outfileRaster.ResolutionX; resolutionY = outfileRaster.ResolutionY; var dt = dateTime.HasValue ? dateTime.Value : DateTime.Now; TryMosaicFile(inArg, outfileRaster, dsInfo, dt, outArg.DayOrNight); } fileArg.OutputFilename = Path.GetFileName(file); fileArg.Thumbnail = (string.IsNullOrWhiteSpace(overViewFilename) && File.Exists(overViewFilename) ? "" : Path.GetFileName(overViewFilename)); string solarZenithFile = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".SolarZenith.ldf"); string solarZenithHdrFile = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".SolarZenith.hdr"); fileArg.ExtendFiles = Path.ChangeExtension(Path.GetFileName(file), "hdr") + (string.IsNullOrWhiteSpace(solarZenithFile) && File.Exists(solarZenithFile) ? "" : "," + Path.GetFileName(solarZenithFile)) + (string.IsNullOrWhiteSpace(solarZenithHdrFile) && File.Exists(solarZenithHdrFile) ? "" : "," + Path.GetFileName(solarZenithHdrFile)); fileArg.Envelope = new PrjEnvelopeItem("GBAL", env == null ? null : new RasterProject.PrjEnvelope(env.MinX, env.MaxX, env.MinY, env.MaxY, SpatialReferenceFactory.CreateSpatialReference(4326))); fileArg.ResolutionX = resolutionX.ToString(); fileArg.ResolutionY = resolutionY.ToString(); fileArg.Length = new FileInfo(file).Length; fileArgs.Add(fileArg); if (inArg.IsOnlySaveMosaicFile) { TryDeleteFile(file); } } outArg.OutputFiles = fileArgs.ToArray(); outArg.LogLevel = "info"; if (string.IsNullOrWhiteSpace(retMessage)) { outArg.LogInfo = "投影成功"; } else { outArg.LogInfo = retMessage; } if (string.IsNullOrWhiteSpace(validEnvelopeMsg)) { outArg.LogInfo = outArg.LogInfo + validEnvelopeMsg; } } } } catch (Exception ex) { outArg.LogLevel = "error"; Console.WriteLine(ex.StackTrace); outArg.LogInfo = ex.Message + ex.StackTrace; Console.WriteLine("PIE.Meteo.ProjectTool.Execute()", ex); } finally { //输出参数文件重新命名 string inputFileName = Path.GetFileName(inArg.InputFilename); System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"_\d{4}M"); if (rex.IsMatch(inputFileName)) { string oldResStr = rex.Match(inputFileName).Groups[0].Value; if (inArg.ProjectionIdentify == "GLL") { inputFileName = inputFileName.Replace(oldResStr, $"_{PrjFileName.GLLResolutionIdentify(inArg.ResolutionX)}"); } else { inputFileName = inputFileName.Replace(oldResStr, $"_{PrjFileName.ResolutionIdentify(inArg.ResolutionX)}"); } } string outXmlFilename = Path.Combine(inArg.OutputDir, inputFileName + ".xml"); OutputArg.WriteXml(outArg, outXmlFilename); } }
public AbstractWarpDataset[] Clip(AbstractWarpDataset srcRaster, BlockDef[] blockDefs, int samplePercent, string driver, string outdir, Action <int, string> progressCallback, params object[] options) { AbstractWarpDataset[] tProviders = new AbstractWarpDataset[blockDefs.Length]; if (progressCallback != null) { progressCallback(0, "开始数据分幅"); } for (int blockNums = 0; blockNums < blockDefs.Length; blockNums++) { if (progressCallback != null) { progressCallback((int)((blockNums + 1.0) / blockDefs.Length * 100), string.Format("正在分幅第{0}/{1}个文件...", (blockNums + 1).ToString(), blockDefs.Length)); } //位置映射参数 int tBeginRow = -1, tEndRow = -1, tBeginCol = -1, tEndCol = -1; int oBeginRow = -1, oEndRow = -1, oBeginCol = -1, oEndCol = -1; Envelope oEnvelope = srcRaster.GetEnvelope(); Envelope tEnvelope = blockDefs[blockNums].ToEnvelope(); Size oSize = new Size(srcRaster.Width, srcRaster.Height); Size tSize = ClipCutHelper.GetTargetSize(blockDefs[blockNums], srcRaster.ResolutionX, srcRaster.ResolutionY); bool isInternal = new RasterMoasicClipHelper().ComputeBeginEndRowCol(oEnvelope, oSize, tEnvelope, tSize, ref oBeginRow, ref oBeginCol, ref oEndRow, ref oEndCol, ref tBeginRow, ref tBeginCol, ref tEndRow, ref tEndCol); string blockFilename = ClipCutHelper.GetBlockFilename(blockDefs[blockNums], srcRaster.fileName, outdir, driver); int oWidth = 0; int oHeight = 0; float tResolutionX; float tResolutionY; if (samplePercent > 0 && samplePercent < 100) { oHeight = (int)(tSize.Width * samplePercent * 1f / 100 + 0.5); oWidth = (int)(tSize.Width * samplePercent * 1f / 100 + 0.5); tResolutionX = srcRaster.ResolutionX * samplePercent * 1f / 100; tResolutionY = srcRaster.ResolutionY * samplePercent * 1f / 100; } else { oHeight = tSize.Height; oWidth = tSize.Width; tResolutionX = srcRaster.ResolutionX; tResolutionY = srcRaster.ResolutionY; } string[] optionString = new string[] { "INTERLEAVE=BSQ", "VERSION=LDF", "WITHHDR=TRUE", "SPATIALREF=" + srcRaster.SpatialRef.ExportToProj4(), "MAPINFO={" + 1 + "," + 1 + "}:{" + tEnvelope.MinX + "," + tEnvelope.MaxY + "}:{" + tResolutionX + "," + tResolutionY + "}" }; string[] _options = new string[] { "header_offset=128" }; double[] geoTrans = new double[] { tEnvelope.MinX, Convert.ToDouble(tResolutionX.ToString("f6")), 0, tEnvelope.MaxY, 0, -Convert.ToDouble(tResolutionY.ToString("f6")) }; var rDs = DatasetFactory.CreateRasterDataset(blockFilename, oWidth, oHeight, srcRaster.BandCount, srcRaster.DataType, "ENVI", null); rDs.SetGeoTransform(geoTrans); rDs.SetProjection(srcRaster.SpatialRef.ExportToWkt()); tProviders[blockNums] = new WarpDataset(rDs, blockFilename); int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); for (int oRow = oBeginRow; oRow < oEndRow; oRow += rowStep) { if (oRow + rowStep > oEndRow) { rowStep = oEndRow - oRow; } for (int bandIndex = 1; bandIndex <= srcRaster.BandCount; bandIndex++) { int sample = (oEndCol - oBeginCol); int typeSize = ClipCutHelper.GetSize(srcRaster.DataType); int bufferSize = sample * rowStep * typeSize; byte[] databuffer = new byte[bufferSize]; unsafe { fixed(byte *ptr = databuffer) { IntPtr buffer = new IntPtr(ptr); srcRaster.GetRasterBand(bandIndex).ReadRaster(oBeginCol, oRow, sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); if (samplePercent > 0 && samplePercent < 100) { tProviders[blockNums].GetRasterBand(bandIndex).WriteRaster( (int)(tBeginCol * samplePercent * 1f / 100 + 0.5), (int)((tBeginRow + (oRow - oBeginRow)) * samplePercent * 1f / 100 + 0.5), (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), buffer, (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), srcRaster.DataType, 0, 0); } else { tProviders[blockNums].GetRasterBand(bandIndex).WriteRaster(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); } } } } } } return(tProviders); }
public AbstractWarpDataset Clip(AbstractWarpDataset srcRaster, BlockDef blockDefs, int samplePercent, string driver, string outdir, Action <int, string> progressCallback, out double validPercent, params object[] options) { AbstractWarpDataset tProviders = null; if (progressCallback != null) { progressCallback(0, "开始数据分幅"); } //位置映射参数 int tBeginRow = -1, tEndRow = -1, tBeginCol = -1, tEndCol = -1; int oBeginRow = -1, oEndRow = -1, oBeginCol = -1, oEndCol = -1; Envelope oEnvelope = srcRaster.GetEnvelope(); Envelope tEnvelope = blockDefs.ToEnvelope(); Size oSize = new Size(srcRaster.Width, srcRaster.Height); Size tSize = ClipCutHelper.GetTargetSize(blockDefs, srcRaster.ResolutionX, srcRaster.ResolutionY); bool isInternal = new RasterMoasicClipHelper().ComputeBeginEndRowCol(oEnvelope, oSize, tEnvelope, tSize, ref oBeginRow, ref oBeginCol, ref oEndRow, ref oEndCol, ref tBeginRow, ref tBeginCol, ref tEndRow, ref tEndCol); string blockFilename = ClipCutHelper.GetBlockFilename(blockDefs, srcRaster.fileName, outdir, driver); int oWidth = 0; int oHeight = 0; float tResolutionX; float tResolutionY; if (samplePercent > 0 && samplePercent < 100) { oHeight = (int)(tSize.Width * samplePercent * 1f / 100 + 0.5); oWidth = (int)(tSize.Width * samplePercent * 1f / 100 + 0.5); tResolutionX = srcRaster.ResolutionX * samplePercent * 1f / 100; tResolutionY = srcRaster.ResolutionY * samplePercent * 1f / 100; } else { oHeight = tSize.Height; oWidth = tSize.Width; tResolutionX = srcRaster.ResolutionX; tResolutionY = srcRaster.ResolutionY; } string[] optionString = new string[] { "INTERLEAVE=BSQ", "VERSION=LDF", "WITHHDR=TRUE", "SPATIALREF=" + srcRaster.SpatialRef.ExportToProj4(), "MAPINFO={" + 1 + "," + 1 + "}:{" + tEnvelope.MinX + "," + tEnvelope.MaxY + "}:{" + tResolutionX + "," + tResolutionY + "}" }; string[] _options = new string[] { "header_offset=128" }; double[] geoTrans = new double[] { tEnvelope.MinX, Convert.ToDouble(tResolutionX.ToString("f6")), 0, tEnvelope.MaxY, 0, -Convert.ToDouble(tResolutionY.ToString("f6")) }; var rDs = DatasetFactory.CreateRasterDataset(blockFilename, oWidth, oHeight, srcRaster.BandCount, srcRaster.DataType, "ENVI", null); rDs.SetGeoTransform(geoTrans); rDs.SetProjection(srcRaster.SpatialRef.ExportToWkt()); tProviders = new WarpDataset(rDs, blockFilename); int rowStep = ClipCutHelper.ComputeRowStep(srcRaster, oBeginRow, oEndRow); int sample = (oEndCol - oBeginCol); int typeSize = ClipCutHelper.GetSize(srcRaster.DataType); long allPixelByte = sample * (oEndRow - oBeginRow) * typeSize * srcRaster.BandCount; long validPixelByte = 0; long validPer = (int)(allPixelByte * 0.1f); int stepCount = (int)((oEndRow - oBeginRow) / rowStep + 0.5) * srcRaster.BandCount; int step = 0; int percent = 0; for (int oRow = oBeginRow; oRow < oEndRow; oRow += rowStep) { if (oRow + rowStep > oEndRow) { rowStep = oEndRow - oRow; } for (int bandIndex = 0; bandIndex < srcRaster.BandCount; bandIndex++) { step++; percent = (int)(step * 1.0f / stepCount * 100); if (progressCallback != null) { progressCallback(percent, "完成数据分幅" + percent + "%"); } int bufferSize = sample * rowStep * typeSize; byte[] databuffer = new byte[bufferSize]; unsafe { fixed(byte *ptr = databuffer) { IntPtr buffer = new IntPtr(ptr); srcRaster.GetRasterBand(bandIndex).ReadRaster(oBeginCol, oRow, sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); if (validPixelByte < validPer) { foreach (byte b in databuffer) { if (b != 0) { validPixelByte++; } } } if (validPixelByte == 0) { continue; } if (samplePercent > 0 && samplePercent < 100) { tProviders.GetRasterBand(bandIndex).WriteRaster( (int)(tBeginCol * samplePercent * 1f / 100 + 0.5), (int)((tBeginRow + (oRow - oBeginRow)) * samplePercent * 1f / 100 + 0.5), (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), buffer, (int)(sample * samplePercent * 1f / 100 + 0.5), (int)(rowStep * samplePercent * 1f / 100 + 0.5), srcRaster.DataType, 0, 0); } else { tProviders.GetRasterBand(bandIndex).WriteRaster(tBeginCol, tBeginRow + (oRow - oBeginRow), sample, rowStep, buffer, sample, rowStep, srcRaster.DataType, 0, 0); } } } } } validPercent = validPixelByte * 1.0d / allPixelByte; if (progressCallback != null) { progressCallback(100, "完成数据分幅"); } return(tProviders); }