Einzelnen Beitrag anzeigen

Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: String auf bestimmtest Format (Geokoordinaten) prüfen und verbessern

  Alt 26. Mär 2013, 10:40
Kleiner Test ohne jegliche Fehlerbehandlung:
Delphi-Quellcode:
uses StrUtils;

procedure TForm24.Button1Click(Sender: TObject);
type
  TGeoData = record
    NSOrientation: char;
    NSDegree: integer;
    NSMinutes: double;
    EWOrientation: char;
    EWDegree: integer;
    EWMinutes: double;
  end;
const
  DEGREESIGN = '°';
  ORIGINAL = 'N 49° 41.766E8 °31.67 2';
var
  CurrentPos, NextPos: integer;
  Data: TGeoData;
  Settings: TFormatSettings;
  GeoData, s: string;
begin
  Settings := TFormatSettings.Create;
  Settings.DecimalSeparator := '.';
  ZeroMemory(@Data, SizeOf(Data));
  GeoData := StringReplace(ORIGINAL, ' ', '', [rfReplaceAll]);
  Data.NSOrientation := GeoData[1];
  CurrentPos := 2;
  NextPos := Pos(DEGREESIGN, GeoData);
  Data.NSDegree := StrToInt(Copy(GeoData, CurrentPos, NextPos - CurrentPos));
  CurrentPos := NextPos + 1;
  NextPos := PosEx('E', AnsiUpperCase(GeoData), CurrentPos);
  if NextPos = 0 then
    NextPos := PosEx('W', AnsiUpperCase(GeoData), CurrentPos);
  Data.NSMinutes := StrToFloat(Copy(GeoData, CurrentPos, NextPos - CurrentPos), Settings);
  CurrentPos := NextPos + 1;
  Data.EWOrientation := GeoData[NextPos];
  NextPos := PosEx(DEGREESIGN, GeoData, CurrentPos);
  Data.EWDegree := StrToInt(Copy(GeoData, CurrentPos, NextPos - CurrentPos));
  CurrentPos := NextPos + 1;
  Data.EWMinutes := StrToFloat(Copy(GeoData, CurrentPos, MAXINT), Settings);
  s := Format('%s %.2d%s %.3f %s %.3d%s %.3f', [
    Data.NSOrientation,
    Data.NSDegree,
    DEGREESIGN,
    Data.NSMinutes,
    Data.EWOrientation,
    Data.EWDegree,
    DEGREESIGN,
    Data.EWMinutes], Settings);
  ShowMessage(s);
end;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat