public void ResetOffset() { double XMin = double.MaxValue; double YMin = double.MaxValue; double XMax = double.MinValue; double YMax = double.MinValue; foreach (Hdf4FileAttr fileAttr in this) { SEnvelope envelope = fileAttr.Envelope; if (XMin > envelope.XMin) { XMin = envelope.XMin; } if (YMin > envelope.YMin) { YMin = envelope.YMin; } if (XMax < envelope.XMax) { XMax = envelope.XMax; } if (YMax < envelope.YMax) { YMax = envelope.YMax; } } SEnvelope envelopef4 = Hdf4FileAttr.Envelope; envelopef4.XMin = XMin; envelopef4.YMin = YMin; envelopef4.XMax = XMax; envelopef4.YMax = YMax; RefreshPointString(envelopef4); foreach (Hdf4FileAttr fileAttr in this) { fileAttr.Offset(envelopef4); } double dx = envelopef4.Width; double dxcell = Math.Round(dx / Hdf4FileAttr.CellWidth); Hdf4FileAttr.XDim = Convert.ToInt32(dxcell); XDimNew = string.Format("XDim={0}", Hdf4FileAttr.XDim); double dy = envelopef4.Height; double dycell = Math.Round(dy / Hdf4FileAttr.CellHeight); Hdf4FileAttr.YDim = Convert.ToInt32(dycell); YDimNew = string.Format("YDim={0}", Hdf4FileAttr.YDim); }
public static void DoConvertHdf4To5(SHdf4To5 hdf4To5, Hdf4FileAttrs hdf4FileAttrs, PrjEnvelope outenvelope, float dstResolution, ISpatialReference dstSpatialReference) { SEnvelope tempse = new SEnvelope(); tempse.XMin = outenvelope.MinX; tempse.XMax = outenvelope.MaxX; tempse.YMin = outenvelope.MinY; tempse.YMax = outenvelope.MaxY; Action <string, int, int> messageAction = hdf4To5.MessageAction; ConvertHdf4To5(hdf4FileAttrs, hdf4To5.Hdf5Name, messageAction, tempse, dstResolution, dstSpatialReference); hdf4FileAttrs.Dispose(); }
public void AddHdf4File(string f4Name) { var hdf4FileAttr = new Hdf4FileAttr(f4Name); Add(hdf4FileAttr); if (Hdf4FileAttr == null) { Hdf4FileAttr = hdf4FileAttr.Clone(); SEnvelope envelope = Hdf4FileAttr.Envelope; UpperLeftPointOld = GetUpperLeftPointString(envelope); LowerRightPointOld = GetLowerRightPointString(envelope); XDimOld = string.Format("XDim={0}", Hdf4FileAttr.XDim); YDimOld = string.Format("YDim={0}", Hdf4FileAttr.YDim); } }
/// <summary> /// 计算偏移 /// </summary> /// <param name="parentXMin">计算拼接的父窗口x最小值</param> /// <param name="parentYMin">计算拼接的父窗口y最小值</param> /// <param name="parentXMax">计算拼接的父窗口x最大值</param> /// <param name="parentYMax">计算拼接的父窗口y最大值</param> //public void Offset(double parentXMin, double parentYMin, double parentXMax, double parentYMax) public void Offset(SEnvelope parentEnvelope) { //double dx = XMin - parentXMin; double dx = Envelope.XMin - parentEnvelope.XMin; double dxcell = Math.Round(dx / CellWidth); XOffset = Convert.ToInt32(dxcell); //double dy = YMin - parentYMin; //double dycell = Math.Round(dy / CellHeight); //YOffset = Convert.ToInt32(dycell); //double dy = parentYMax - YMax; double dy = parentEnvelope.YMax - Envelope.YMax; double dycell = Math.Round(dy / CellHeight); YOffset = Convert.ToInt32(dycell); }
private static void ConvertHdf4To5BySds(Hdf4FileAttrs hdf4FileAttrs, Action <string, int, int> messageAction, int k, int nyf5, int nxf5, int rank, H5FileId fileId, SEnvelope envelopeNew, float dstResolution, ISpatialReference dstSpatialReference, ref UInt16[] rows, ref UInt16[] cols, ref Size srcStepSize, ref Size outSize) { var dataField = hdf4FileAttrs.Hdf4FileAttr.DataFields[k]; string sdName = dataField.DataFieldName; string dataType = dataField.DataType; HDF4Helper.DataTypeDefinitions dtaDefinitions = Utility.EnumParse(dataType, HDF4Helper.DataTypeDefinitions.DFNT_UINT16); Type type = Utility.GetDataType(dtaDefinitions); if (type == typeof(byte)) { ConvertHdf4To5BySds <byte>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else if (type == typeof(char)) { ConvertHdf4To5BySds <char>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, '0', dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else if (type == typeof(double)) { ConvertHdf4To5BySds <double>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else if (type == typeof(short)) { ConvertHdf4To5BySds <short>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else if (type == typeof(ushort)) { ConvertHdf4To5BySds <ushort>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else if (type == typeof(long)) { ConvertHdf4To5BySds <long>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else if (type == typeof(float)) { ConvertHdf4To5BySds <float>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } else //(type == typeof(int)) { ConvertHdf4To5BySds <int>(hdf4FileAttrs, nyf5, nxf5, k, rank, fileId, sdName, dtaDefinitions, messageAction, envelopeNew, 0, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); } }
private Dictionary <string, PrjEnvelope> GetSrcFiles(Dictionary <string, PrjEnvelope> MosaicFiles, FileListItem[] fli) { Dictionary <string, PrjEnvelope> srcFiles = new Dictionary <string, PrjEnvelope>(); if (ckGCTPChina.Checked) { srcFiles = MosaicFiles; } else { foreach (FileListItem filename in fli) { SHdf4To5 shdf4To5 = new SHdf4To5(new string[] { filename.FileName }, filename.FileName); Hdf4FileAttrs hdf4FileAttrs = UtilHdf4To5.PreConvertHdf4To5(shdf4To5); SEnvelope envelopef4 = hdf4FileAttrs.Hdf4FileAttr.Envelope; srcFiles.Add(filename.FileName, new PrjEnvelope(envelopef4.XMin, envelopef4.XMax, envelopef4.YMin, envelopef4.YMax)); } } return(srcFiles.Count == 0 ? null : srcFiles); }
private static void ConvertHdf4To5(Hdf4FileAttrs hdf4FileAttrs, string f5name, Action <string, int, int> messageAction, SEnvelope outenvelope, float dstResolution, ISpatialReference dstSpatialReference) { try { // Create a new file using H5F_ACC_TRUNC access, // default file creation properties, and default file // access properties. H5FileId fileId = H5F.create(f5name, H5F.CreateMode.ACC_TRUNC); int nxf5 = hdf4FileAttrs.Hdf4FileAttr.XDim; int nyf5 = hdf4FileAttrs.Hdf4FileAttr.YDim; int rank = 2; //测试读取的科学数据集及其属性 int sdscount = hdf4FileAttrs.Hdf4FileAttr.DataFields.Count; SEnvelope envelopeNew = null; UInt16[] rows = null; //正向查找表 UInt16[] cols = null; //正向查找表 Size srcStepSize = Size.Empty; Size outSize = Size.Empty; try { for (int k = 0; k < sdscount; k++) { ConvertHdf4To5BySds(hdf4FileAttrs, messageAction, k, nyf5, nxf5, rank, fileId, outenvelope, dstResolution, dstSpatialReference, ref rows, ref cols, ref srcStepSize, ref outSize); if (envelopeNew == null && dstSpatialReference != null && outenvelope != null) { envelopeNew = outenvelope; } } if (envelopeNew != null) { hdf4FileAttrs.RefreshPointString(envelopeNew); } } finally { rows = null; cols = null; srcStepSize = Size.Empty; GC.Collect(); } HDFAttributeDef[] attributeDef5s = hdf4FileAttrs.GetHDFAttributeDefs(); List <HDFAttributeDef> updateFileAttrs = new List <HDFAttributeDef>(); updateFileAttrs.AddRange(attributeDef5s); if (dstSpatialReference != null && dstSpatialReference.IsSame(SpatialReference.GetDefault())) { //投影信息 string projtype = "Geopraphic Latitude longtitude"; HDFAttributeDef projecttype = new HDFAttributeDef("Projection Type", typeof(string), projtype.Length, projtype); updateFileAttrs.Add(projecttype); //写四角坐标信息 HDFAttributeDef lefttopY = new HDFAttributeDef("Left-Top Latitude", typeof(float), 1, new float[] { (float)outenvelope.YMax }); updateFileAttrs.Add(lefttopY); HDFAttributeDef lefttopX = new HDFAttributeDef("Left-Top Longtitude", typeof(float), 1, new float[] { (float)outenvelope.XMin }); updateFileAttrs.Add(lefttopX); HDFAttributeDef boottomrightY = new HDFAttributeDef("Bottom-Right Latitude", typeof(float), 1, new float[] { (float)outenvelope.YMin }); updateFileAttrs.Add(boottomrightY); HDFAttributeDef boottomrightX = new HDFAttributeDef("Bottom-Right Longtitude", typeof(float), 1, new float[] { (float)outenvelope.XMax }); updateFileAttrs.Add(boottomrightX); HDFAttributeDef xResolution = new HDFAttributeDef("Longtitude Resolution", typeof(float), 1, new float[] { (float)dstResolution }); updateFileAttrs.Add(xResolution); HDFAttributeDef yResolution = new HDFAttributeDef("Latitude Resolution", typeof(float), 1, new float[] { (float)dstResolution }); updateFileAttrs.Add(yResolution); } foreach (HDFAttributeDef attributeDef5 in updateFileAttrs) { WriteHdfAttributes.WriteHdfAttribute(fileId, attributeDef5); } H5F.close(fileId); } catch (Exception ex) { throw new Exception("拼接Hdf4时出错,具体信息:" + ex.Message, ex); } }
private static void ConvertHdf4To5BySds <T>(Hdf4FileAttrs hdf4FileAttrs, int nyf5, int nxf5, int k, int rank, H5FileId fileId, string sdName, HDF4Helper.DataTypeDefinitions dtaDefinitions, Action <string, int, int> messageAction, SEnvelope envelopeNew, T fillValue, float dstResolution, ISpatialReference dstSpatialReference, ref UInt16[] rows, ref UInt16[] cols, ref Size srcStepSize, ref Size outSize) { T[,] data = new T[nyf5, nxf5]; H5DataSetId dsetId = null; T[,] dataNew = null; SDataByProject <T> dataByProject = null; try { for (int i = 0; i < hdf4FileAttrs.Count; i++) { Hdf4FileAttr hdf4FileAttr = hdf4FileAttrs[i]; if (messageAction != null) { messageAction(String.Format("正在转换数据集 {0}", sdName), k, i); } H4SDS sd = hdf4FileAttr.H4File.Datasets[k]; GetDataByHdf4(sd, rank, hdf4FileAttr, data); } dataNew = data; if (outSize.IsEmpty) { outSize = new Size(nxf5, nyf5); } if (dstSpatialReference != null && dstSpatialReference.IsSame(SpatialReference.GetDefault())) { if (rows == null && cols == null) { PrjEnvelope srcEnvelope = new PrjEnvelope(hdf4FileAttrs.Hdf4FileAttr.Envelope.XMin, hdf4FileAttrs.Hdf4FileAttr.Envelope.XMax, hdf4FileAttrs.Hdf4FileAttr.Envelope.YMin, hdf4FileAttrs.Hdf4FileAttr.Envelope.YMax); PrjEnvelope outenvelpoe = new PrjEnvelope(envelopeNew.XMin, envelopeNew.XMax, envelopeNew.YMin, envelopeNew.YMax); FilePrjSettings prjSetting = new FilePrjSettings(); prjSetting.OutEnvelope = outenvelpoe; prjSetting.OutResolutionX = dstResolution; prjSetting.OutResolutionY = dstResolution; outSize = prjSetting.OutSize; Size inSize = new Size(hdf4FileAttrs.Hdf4FileAttr.XDim, hdf4FileAttrs.Hdf4FileAttr.YDim); float dstResoultion = dstResolution; float srcResoultionX = (float)(hdf4FileAttrs.Hdf4FileAttr.CellWidth); float srcResoultionY = (float)(hdf4FileAttrs.Hdf4FileAttr.CellHeight); dataByProject = GetDataByProject <T>(dataNew, srcEnvelope, outenvelpoe, inSize, outSize, dstResoultion, srcResoultionX, srcResoultionY, fillValue, dstSpatialReference, out rows, out cols, out srcStepSize); } else { T[,] dstData = new T[outSize.Height, outSize.Width]; dataByProject = DoProject <T>(dataNew, fillValue, ref outSize, ref srcStepSize, dstData, rows, cols); } if (dataByProject != null) { dataNew = dataByProject.Data; } } long[] dims = new long[rank]; dims[0] = Convert.ToInt64(outSize.Height); dims[1] = Convert.ToInt64(outSize.Width); H5DataSpaceId dspaceId = H5S.create_simple(rank, dims); H5T.H5Type h5Type = Utility.GetH5Type(dtaDefinitions); // Define datatype for the data in the file. H5DataTypeId dtypeId = H5T.copy(h5Type); // Create the data set DATASETNAME. dsetId = H5D.create(fileId, sdName, dtypeId, dspaceId); // Write the one-dimensional data set array H5D.write(dsetId, new H5DataTypeId(h5Type), new H5Array <T>(dataNew));//H5A HDFAttributeDef[] attributeDef5s = hdf4FileAttrs[0].DatasetsAttributeDefs[k]; foreach (HDFAttributeDef attributeDef5 in attributeDef5s) { WriteHdfAttributes.WriteHdfAttribute(dsetId, attributeDef5); } } finally { // Close dataset and file. if (dsetId != null) { H5D.close(dsetId); } if (data != null) { data = null; } if (dataNew != null) { dataNew = null; } GC.Collect(); } }
private string GetLowerRightPointString(SEnvelope envelope) { return(string.Format("LowerRightMtrs=({0},{1})", envelope.XMax.ToString("0.000000"), envelope.YMin.ToString("0.000000"))); }
private string GetUpperLeftPointString(SEnvelope envelope) { return(string.Format("UpperLeftPointMtrs=({0},{1})", envelope.XMin.ToString("0.000000"), envelope.YMax.ToString("0.000000"))); }
public void RefreshPointString(SEnvelope envelope) { UpperLeftPointNew = GetUpperLeftPointString(envelope); LowerRightPointNew = GetLowerRightPointString(envelope); }
private void GetAttr(Pair pair) { GridName = pair.GetAttrValue("GridName"); string xdimStr = pair.GetAttrValue("XDim"); int xdim = 0; if (int.TryParse(xdimStr, out xdim)) { XDim = xdim; } string ydimStr = pair.GetAttrValue("YDim"); int ydim = 0; if (int.TryParse(ydimStr, out ydim)) { YDim = ydim; } Envelope = new SEnvelope(); string upperLeftPointMtrs = pair.GetAttrValue("UpperLeftPointMtrs"); string[] strsUL = upperLeftPointMtrs.Split(new[] { '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries); double xmin = 0.0; if (double.TryParse(strsUL[0], out xmin)) { Envelope.XMin = xmin; } double ymax = 0.0; if (double.TryParse(strsUL[1], out ymax)) { Envelope.YMax = ymax; } string lowerRightMtrs = pair.GetAttrValue("LowerRightMtrs"); string[] strsLR = lowerRightMtrs.Split(new[] { '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries); double xmax = 0.0; if (double.TryParse(strsLR[0], out xmax)) { Envelope.XMax = xmax; } double ymin = 0.0; if (double.TryParse(strsLR[1], out ymin)) { Envelope.YMin = ymin; } CellWidth = Envelope.Width / XDim; CellHeight = Envelope.Height / YDim; Projection = pair.GetAttrValue("Projection"); ProjParams = pair.GetAttrValue("ProjParams"); Pair dataFields = pair.GetGroup("DataField"); DataFields = new DataFields(dataFields); }