private static void ParseSatelliteSensor(ref HdrFile hdr) { if (string.IsNullOrEmpty(hdr.Description) || string.IsNullOrEmpty(hdr.SensorType)) { return; } Int16 satelliteType = -1; if (!Int16.TryParse(hdr.SensorType, out satelliteType)) { return; } string regStr = @"\S*\s*\S*filedatetime:(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})\s*(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2})\S*"; DateTime dt = DateTime.MinValue; Regex reg = new Regex(regStr); if (reg.IsMatch(hdr.Description.ToLower())) { Match m = reg.Match(hdr.Description.ToLower()); if (!DateTime.TryParse(m.Groups["year"].Value + "-" + m.Groups["month"].Value + "-" + m.Groups["day"].Value + " " + m.Groups["hour"].Value + ":" + m.Groups["minute"].Value + ":" + m.Groups["second"].Value, out dt)) { dt = DateTime.MinValue; } } }
public static HdrFile ParseFromHdrfile(string hdrfile) { string[] regexStrs = new string[] { @"^\s*\bdescription\b\s*=\s*{(?<Des>.*)}$", @"^\s*\bsamples\b\s*=\s*(?<samples>\d*)$", @"^\s*\blines\b\s*=\s*(?<lines>\d*)$", @"^\s*\bbands\b\s*=\s*(?<bands>\d*)$", @"^\s*\bheader\b\s*\boffset\b\s*=\s*(?<headerOffset>\d*)$", @"^\s*\bfile\b\s*\btype\b\s*=\s*(?<fileType>.*)$", @"^\s*\bdata\b\s*\btype\b\s*=\s*(?<dataType>\d*)$", @"^\s*\binterleave\b\s*=\s*(?<interleave>.*)$", @"^\s*\bsensor\b\s*\btype\b\s*=\s*(?<sensorType>.*)$", @"^\s*\bbyte\b\s*\border\b\s*=\s*(?<byteOder>\d*)$", @"^\s*\bmap\b\s*\binfo\b\s*=\s*{(?<prjName>.*),\s*(?<CX>\d+(\.\d+)?)\s*,\s*(?<CY>\d+(\.\d+)?)\s*,\s*(?<CLon>-?\d+(\.\d+)?)([E|e](?<ELon>\+?\d+))?\s*,\s*(?<CLat>-?\d+(\.\d+)?)([E|e](?<ELat>\+?\d+))?\s*,\s*(?<RLon>-?\d+(\.\d+)?)([E|e](?<ERLon>-?\d+))?\s*,\s*(?<RLat>-?\d+(\.\d+)?)([E|e](?<ERLat>-?\d+))?\s*,\s*(?<Project>\S*)\s*,\s*\bunits\b\s*=\s*(?<units>\S*)\s*}", @"^\s*\bband\b\s*\bnames\b\s*=\s*{\s*(?<bandNames>[^}]*)", @"^\s*\bgeo\b\s*\bpoints\b\s*=\s*{\s*(?<geoPoints>[^}]*)}$" }; HdrFile _hdrFile = new HdrFile(); string projExp = @"\bprojection\b\s*\binfo\b\s*=\s*{(?<ID>\d+),.+}"; string fileString = File.ReadAllText(hdrfile, Encoding.Default); Match prjM = Regex.Match(fileString, projExp, RegexOptions.IgnoreCase); if (prjM.Success) { _hdrFile.HdrProjectionInfo = GetHdrProjectionInfo(prjM.Value); } // string[] context = File.ReadAllLines(hdrfile, Encoding.Default); int j = 0; foreach (string str in context) { for (int i = 0; i < regexStrs.Length; i++) { Regex regex = new Regex(regexStrs[i]); Match m = regex.Match(str); if (m.Success) { switch (i) { case 0: _hdrFile.Description = m.Groups["Des"].Value; break; case 1: _hdrFile.Samples = Convert.ToInt32(string.IsNullOrEmpty(m.Groups["samples"].Value) ? "0" : m.Groups["samples"].Value); break; case 2: _hdrFile.Lines = Convert.ToInt32(string.IsNullOrEmpty(m.Groups["lines"].Value) ? "0" : m.Groups["lines"].Value); break; case 3: _hdrFile.Bands = Convert.ToInt32(string.IsNullOrEmpty(m.Groups["bands"].Value) ? "0" : m.Groups["bands"].Value); break; case 4: _hdrFile.HeaderOffset = Convert.ToInt32(string.IsNullOrEmpty(m.Groups["headerOffset"].Value) ? "0" : m.Groups["headerOffset"].Value); break; case 5: _hdrFile.FileType = m.Groups["fileType"].Value; break; case 6: _hdrFile.DataType = Convert.ToInt32(string.IsNullOrEmpty(m.Groups["dataType"].Value) ? "0" : m.Groups["dataType"].Value); break; case 7: _hdrFile.Intertleave = GetEnuminterleave(m.Groups["interleave"].Value); break; case 8: _hdrFile.SensorType = m.Groups["sensorType"].Value; break; case 9: _hdrFile.ByteOrder = GetEnumByteOrder(m.Groups["byteOder"].Value); break; case 10: _hdrFile.MapInfo = GetMapInfo(m.Groups["prjName"].Value, m.Groups["CX"].Value, m.Groups["CY"].Value, m.Groups["CLon"].Value, m.Groups["CLat"].Value, m.Groups["ELon"].Value, m.Groups["ELat"].Value, m.Groups["RLon"].Value, m.Groups["ERLon"].Value, m.Groups["RLat"].Value, m.Groups["ERLat"].Value, m.Groups["Project"].Value, m.Groups["units"].Value); break; case 11: _hdrFile.BandNames = GetBandNames(m.Groups["bandNames"].Value, str, context, j); break; case 12: _hdrFile.GeoPoints = GetGeoPoint(m.Groups["geoPoints"].Value); break; } break; } } j++; } ParseSatelliteSensor(ref _hdrFile); return(_hdrFile); }
public static void SaveTo(string hdrfilename, HdrFile hdrfile) { try { using (StreamWriter sw = new StreamWriter(hdrfilename, false, Encoding.Default)) { sw.WriteLine("ENVI"); sw.WriteLine("description = {" + hdrfile.Description + "}"); sw.WriteLine(string.Format("samples = {0}", hdrfile.Samples)); sw.WriteLine(string.Format("lines = {0}", hdrfile.Lines)); sw.WriteLine(string.Format("bands = {0}", hdrfile.Bands)); sw.WriteLine(string.Format("header offset = {0}", hdrfile.HeaderOffset)); sw.WriteLine("major frame offsets = {" + hdrfile.MajorFrameOffsets[0] + "," + hdrfile.MajorFrameOffsets[1] + "}"); sw.WriteLine(string.Format("file type = {0}", hdrfile.FileType)); sw.WriteLine(string.Format("data type = {0}", hdrfile.DataType)); sw.WriteLine(string.Format("interleave = {0}", hdrfile.Intertleave.ToString().ToLower())); sw.WriteLine(string.Format("sensor type = {0}", hdrfile.SensorType)); sw.WriteLine(string.Format("byte order = {0}", (int)hdrfile.ByteOrder)); if (hdrfile.MapInfo != null && !hdrfile.MapInfo.IsEmpty()) { string mapInfos = "map info = {" + hdrfile.MapInfo.Name + "," + hdrfile.MapInfo.BaseRowColNumber.X.ToString() + "," + hdrfile.MapInfo.BaseRowColNumber.Y.ToString() + "," + hdrfile.MapInfo.BaseMapCoordinateXY.Longitude.ToString() + "," + hdrfile.MapInfo.BaseMapCoordinateXY.Latitude.ToString() + "," + hdrfile.MapInfo.XYResolution.Longitude.ToString() + "," + hdrfile.MapInfo.XYResolution.Latitude.ToString() + "," + hdrfile.MapInfo.CoordinateType + ",units = " + hdrfile.MapInfo.Units + "}"; sw.WriteLine(mapInfos); } if (hdrfile.HdrProjectionInfo != null) { sw.WriteLine(hdrfile.HdrProjectionInfo.ToString()); } if (hdrfile.GeoPoints != null) { string sGeoPoints = "geo points = {"; foreach (HdrGeoPoint pt in hdrfile.GeoPoints) { sGeoPoints += (pt.PixelPoint.Y.ToString() + "," + pt.PixelPoint.X.ToString() + "," + pt.GeoPoint.Latitude.ToString() + pt.GeoPoint.Longitude.ToString() + ","); } if (sGeoPoints.EndsWith(",")) { sGeoPoints = sGeoPoints.Substring(0, sGeoPoints.Length - 1); } sGeoPoints += "}"; sw.WriteLine(sGeoPoints); } if (hdrfile.BandNames != null) { string bandNames = string.Empty; foreach (string name in hdrfile.BandNames) { bandNames += (name + ","); } if (bandNames.EndsWith(",")) { bandNames = bandNames.Substring(0, bandNames.Length - 1); } sw.WriteLine("band names = {" + bandNames + "}"); } } } catch (Exception e) { return; } }