private void WriteLatLon(double d, bool fLatitude) { string sRef = fLatitude ? ((d < 0) ? "S" : "N") : ((d < 0) ? "W" : "E"); DMSAngle dmsa = new DMSAngle(d); FractionStruct[] f = new FractionStruct[3]; f[0] = (FractionStruct) new Fraction(dmsa.Degrees); f[1] = (FractionStruct) new Fraction(dmsa.Minutes); f[2] = (FractionStruct) new Fraction(dmsa.Seconds, 5); // Write reference PropertyItem pi = _image.PropertyItems[0]; // use an existing property pi.Type = (short)PropertyTagType.ASCII; pi.Id = (int)(fLatitude ? PropertyTagId.GpsLatitudeRef : PropertyTagId.GpsLongitudeRef); pi.Len = Encoding.ASCII.GetByteCount(sRef) + 1; pi.Value = new byte[pi.Len]; byte[] rgbSz = Encoding.ASCII.GetBytes(sRef); rgbSz.CopyTo(pi.Value, 0); pi.Value[pi.Len - 1] = 0; _image.SetPropertyItem(pi); // Now write the value pi = _image.PropertyItems[0]; pi.Type = (short)PropertyTagType.Rational; pi.Id = (int)(fLatitude ? PropertyTagId.GpsLatitude : PropertyTagId.GpsLongitude); pi.Len = Marshal.SizeOf(f[0]) * f.Length; pi.Value = new byte[pi.Len]; IntPtr ptr = Marshal.AllocHGlobal(pi.Len); for (int i = 0; i < f.Length; i++) { Marshal.StructureToPtr(f[i], new IntPtr(ptr.ToInt64() + i * Marshal.SizeOf(f[0])), true); } Marshal.Copy(ptr, pi.Value, 0, pi.Len); Marshal.FreeHGlobal(ptr); _image.SetPropertyItem(pi); }
public static IEnumerable <airportImportCandidate> Candidates(Stream s) { if (s == null) { throw new ArgumentNullException(nameof(s)); } ImportAirportContext ic = new ImportAirportContext(); List <airportImportCandidate> lst = new List <airportImportCandidate>(); using (CSVReader csvReader = new CSVReader(s)) { ic.InitFromHeader(csvReader.GetCSVLine(true)); string[] rgCols = null; while ((rgCols = csvReader.GetCSVLine()) != null) { airportImportCandidate aic = new airportImportCandidate() { FAA = GetCol(rgCols, ic.iColFAA).Replace("-", ""), IATA = GetCol(rgCols, ic.iColIATA).Replace("-", ""), ICAO = GetCol(rgCols, ic.iColICAO).Replace("-", ""), Name = GetCol(rgCols, ic.iColName), FacilityTypeCode = GetCol(rgCols, ic.iColType), Country = GetCol(rgCols, ic.iColCountry), Admin1 = GetCol(rgCols, ic.iColAdmin1) }; if (String.IsNullOrEmpty(aic.FacilityTypeCode)) { aic.FacilityTypeCode = "A"; // default to airport } aic.Name = GetCol(rgCols, ic.iColName); aic.Code = "(TBD)"; string szLat = GetCol(rgCols, ic.iColLatitude); string szLon = GetCol(rgCols, ic.iColLongitude); string szLatLong = GetCol(rgCols, ic.iColLatLong); aic.LatLong = null; if (!String.IsNullOrEmpty(szLatLong)) { // see if it is decimal; if so, we'll fall through. if (Regex.IsMatch(szLatLong, "[NEWS]", RegexOptions.IgnoreCase)) { aic.LatLong = DMSAngle.LatLonFromDMSString(GetCol(rgCols, ic.iColLatLong)); } else { string[] rgsz = szLatLong.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (rgsz.Length == 2) { szLat = rgsz[0]; szLon = rgsz[1]; } } } if (aic.LatLong == null) { aic.LatLong = new LatLong { Latitude = double.TryParse(szLat, out double d) ? d : new DMSAngle(szLat).Value, Longitude = double.TryParse(szLon, out d) ? d : new DMSAngle(szLon).Value }; } lst.Add(aic); } return(lst); } }
protected void btnImport_Click(object sender, EventArgs e) { if (!fileUploadAirportList.HasFile) { return; } if (!ckShowAllUserAirports.Checked) { gvMyAirports.DataSource = null; gvMyAirports.DataBind(); } ImportContext ic = new ImportContext(); using (CSVReader csvReader = new CSVReader(fileUploadAirportList.FileContent)) { try { ic.InitFromHeader(csvReader.GetCSVLine(true)); } catch (CSVReaderInvalidCSVException ex) { lblUploadErr.Text = ex.Message; return; } catch (MyFlightbookException ex) { lblUploadErr.Text = ex.Message; return; } List <airportImportCandidate> lst = ImportedAirportCandidates; lst.Clear(); string[] rgCols = null; while ((rgCols = csvReader.GetCSVLine()) != null) { airportImportCandidate aic = new airportImportCandidate(); aic.FAA = GetCol(rgCols, ic.iColFAA).Replace("-", ""); aic.IATA = GetCol(rgCols, ic.iColIATA).Replace("-", ""); aic.ICAO = GetCol(rgCols, ic.iColICAO).Replace("-", ""); aic.Name = GetCol(rgCols, ic.iColName); aic.FacilityTypeCode = GetCol(rgCols, ic.iColType); if (String.IsNullOrEmpty(aic.FacilityTypeCode)) { aic.FacilityTypeCode = "A"; // default to airport } aic.Name = GetCol(rgCols, ic.iColName); aic.Code = "(TBD)"; string szLat = GetCol(rgCols, ic.iColLatitude); string szLon = GetCol(rgCols, ic.iColLongitude); string szLatLong = GetCol(rgCols, ic.iColLatLong); aic.LatLong = null; if (!String.IsNullOrEmpty(szLatLong)) { // see if it is decimal; if so, we'll fall through. if (Regex.IsMatch(szLatLong, "[NEWS]", RegexOptions.IgnoreCase)) { aic.LatLong = DMSAngle.LatLonFromDMSString(GetCol(rgCols, ic.iColLatLong)); } else { string[] rgsz = szLatLong.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (rgsz.Length == 2) { szLat = rgsz[0]; szLon = rgsz[1]; } } } if (aic.LatLong == null) { aic.LatLong = new LatLong(); double d; if (double.TryParse(szLat, out d)) { aic.LatLong.Latitude = d; } else { aic.LatLong.Latitude = new DMSAngle(szLat).Value; } if (double.TryParse(szLon, out d)) { aic.LatLong.Longitude = d; } else { aic.LatLong.Longitude = new DMSAngle(szLon).Value; } } lst.Add(aic); } UpdateCandidateStatus(lst); bool fHideKHack = util.GetIntParam(Request, "khack", 0) == 0; lst.RemoveAll(aic => aic.IsOK || (fHideKHack && aic.IsKHack)); gvImportResults.DataSource = lst; gvImportResults.DataBind(); pnlImportResults.Visible = true; } }