⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ezimportbase.pas

📁 很管用的GIS控件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
unit EzImportBase;

{$I EZ_FLAG.PAS}
interface

Uses
  Controls, SysUtils, Classes, Windows, Db, Forms, Dialogs,
  EzLib, EzBase, EzBaseGIS, EzProjections ;

Type

  { TEzImportConverter }

  TEzImportConverter = Class
  Private
    FSourceCoordSystem: TEzCoordSystem;
    FDestinCoordSystem: TEzCoordSystem;
    FSourceProjector: TEzProjector;
    FDestinProjector: TEzProjector;
    Function EditProjector( Const Caption: String;
      Projector: TEzProjector; Var CoordSystem: TEzCoordSystem ): Boolean;
    Procedure Initialize;
  Public
    Constructor Create;
    Destructor Destroy; Override;
    Function Convert( Const SrcPt: TEzPoint ): TEzPoint;
    Function ConvertDistance( Const d: Double; const RefPt: TEzPoint ): Double;
    Function EditProjections( AutoChangeDestin: Boolean ): Boolean;
    Function DestinationUnits: TEzCoordsUnits;

    Property SourceCoordSystem: TEzCoordSystem Read FSourceCoordSystem Write FSourceCoordSystem;
    Property DestinCoordSystem: TEzCoordSystem Read FDestinCoordSystem Write FDestinCoordSystem;
    Property SourceProjector: TEzProjector Read FSourceProjector;
    Property DestinProjector: TEzProjector Read FDestinProjector;
  End;

  TEzFileProgressEvent = Procedure(Sender: TObject;
    Const Filename: String; Progress, NoEntities, BadEntities: Integer;
    Var CanContinue: Boolean) Of Object;

  TEzExternalFile = Class(TComponent)
  Private
    FDrawBox: TEzBaseDrawBox;
    FFileName: String;

    FOnFileProgress: TEzFileProgressEvent;
    Procedure SetDrawBox( Const Value: TEzBaseDrawBox );
    function GetAbout: TEzAbout;
    procedure SetAbout(const Value: TEzAbout);
  Protected
    Procedure Notification( AComponent: TComponent; Operation: TOperation ); Override;
  Published
    Property FileName: String Read FFileName Write FFileName;
    Property DrawBox: TEzBaseDrawBox Read FDrawBox Write SetDrawBox;

    Property OnFileProgress: TEzFileProgressEvent Read FOnFileProgress Write FOnFileProgress;
    Property About: TEzAbout read GetAbout write SetAbout;
  End;

  TEzBaseImport = Class(TEzExternalFile)
  Private
    FConverter: TEzImportConverter;
    FAutoFreeConverter: Boolean;
    FImportToCurrent: Boolean;
    FConfirmProjectionSystem: Boolean;
    FShowAllMapOnFinish: Boolean;
    Procedure SetConverter( Value: TEzImportConverter );
  {$IFDEF BCB}
    function GetConfirmProjectionSystem: Boolean;
    function GetConverter: TEzImportConverter;
    function GetOverwrite: Boolean;
    procedure SetConfirmProjectionSystem(const Value: Boolean);
    procedure SetOverwrite(const Value: Boolean);
  {$ENDIF}
  Public
    Constructor Create( AOwner: TComponent ); Override;
    Destructor Destroy; Override;
    Function Execute: Boolean;
    Procedure ImportInitialize; Dynamic;
    Procedure GetSourceFieldList( FieldList: TStrings ); Dynamic;
    Procedure ImportFirst; Dynamic;
    Procedure AddSourceFieldData(DestLayer: TEzBaseLayer; DestRecno: Integer); Dynamic;
    Function GetSourceExtension: TEzRect; Dynamic;
    Function ImportEof: Boolean; Dynamic;
    Function GetNextEntity(var progress,entno: Integer): TEzEntity; Dynamic;
    Procedure ImportNext; Dynamic;
    Procedure ImportEnd; Dynamic;
    Function ImportFiles( FileList: TStrings ): Boolean;

    Property Converter: TEzImportConverter {$IFDEF BCB} Read GetConverter {$ELSE} Read FConverter {$ENDIF} Write SetConverter;
  Published
    Property Overwrite: Boolean {$IFDEF BCB} Read GetOverwrite Write SetOverwrite {$ELSE} Read FImportToCurrent Write FImportToCurrent {$ENDIF};
    Property ConfirmProjectionSystem: Boolean {$IFDEF BCB} Read GetConfirmProjectionSystem Write SetConfirmProjectionSystem {$ELSE} Read FConfirmProjectionSystem Write FConfirmProjectionSystem {$ENDIF} Default true;
    Property ShowAllMapOnFinish: Boolean read FShowAllMapOnFinish write FShowAllMapOnFinish;
  End;

  TEzBaseExport = Class(TEzExternalFile)
  Private
    FLayername: String;
  {$IFDEF BCB}
    function GetLayerName: String;
    procedure SetLayerName(const Value: String);
  {$ENDIF}
  Public
    Function Execute: Boolean;
    Procedure ExportInitialize; Dynamic;
    Procedure ExportEntity( SourceLayer: TEzBaseLayer; Entity: TEzEntity ); Dynamic;
    Procedure ExportEnd; Dynamic;
  Published
    Property LayerName: String {$IFDEF BCB} Read GetLayerName Write SetLayerName {$ELSE} Read FLayerName Write FLayerName {$ENDIF};
  End;

implementation

Uses
  ezimpl, ezConsts, EzSystem, EzBasicCtrls, EzRtree, fProj ;

function TEzExternalFile.GetAbout: TEzAbout;
begin
  Result:= SEz_GisVersion;
end;

procedure TEzExternalFile.SetAbout(const Value: TEzAbout);
begin
end;

Procedure TEzExternalFile.Notification( AComponent: TComponent; Operation: TOperation );
Begin
  Inherited Notification( AComponent, Operation );
  If ( Operation = opRemove ) And ( AComponent = FDrawBox ) Then
    FDrawBox := Nil;
End;

Procedure TEzExternalFile.SetDrawBox( Const Value: TEzBaseDrawBox );
Begin
{$IFDEF LEVEL5}
  if Assigned( FDrawBox ) then FDrawBox.RemoveFreeNotification( Self );
{$ENDIF}
  If Value <> Nil Then
    Value.FreeNotification( Self );
  FDrawBox := Value;
End;

{ TEzImportConverter }
Constructor TEzImportConverter.Create;
Begin
  Inherited Create;
  FSourceProjector := TEzProjector.Create( Nil );
  FDestinProjector := TEzProjector.Create( Nil );
End;

Destructor TEzImportConverter.Destroy;
Begin
  Inherited Destroy;
  FSourceProjector.free;
  FDestinProjector.free;
End;

Procedure TEzImportConverter.Initialize;
Begin
  If FSourceProjector.Params.Count = 0 Then
    FSourceProjector.InitDefault;
  If FDestinProjector.Params.Count = 0 Then
    FDestinProjector.InitDefault;
End;

Function TEzImportConverter.Convert( Const SrcPt: TEzPoint ): TEzPoint;
Var
  Long, Lat: Double;
Begin
  Case FSourceCoordSystem Of
    csCartesian:
      result := SrcPt;
    { if the source is actually a projection, then later can be changed in EzGIS }
    csLatLon:
      Case FDestinCoordSystem Of
        csCartesian, csLatLon:
          result := srcPt;
        csProjection:
          Begin
            If Not FDestinProjector.HasProjection Then
              EzGisError( SLatLonMismatch );
            FDestinProjector.CoordSysFromLatLong( SrcPt.X, SrcPt.Y, result.X, result.Y );
          End;
      End;
    csProjection:
      Case FDestinCoordSystem Of
        csCartesian: result := SrcPt; // no change
        csLatLon:
          FSourceProjector.CoordSysToLatLong( SrcPt.X, SrcPt.Y, Result.X, Result.Y );
        csProjection:
          Begin
            { convert first to lat/lon }
            FSourceProjector.CoordSysToLatLong( SrcPt.X, SrcPt.Y, Long, Lat );
            FDestinProjector.CoordSysFromLatLong( Long, Lat, Result.X, Result.Y );
          End;
      End;
  End;
End;

Function TEzImportConverter.ConvertDistance( Const d: Double; const RefPt: TEzPoint ): Double;
Var
  Long1, Lat1, Long2, Lat2: double;
  p1, p2: TEzPoint;
Begin
  result := d;
  Case FSourceCoordSystem Of
    csLatLon:
      If FDestinCoordSystem = csProjection Then
      Begin
        If Not FDestinProjector.HasProjection Then
          EzGisError( SLatLonMismatch );
        FDestinProjector.CoordSysFromLatLong( Long1, Lat1, p1.X, p1.Y );
        FDestinProjector.CoordSysFromLatLong( Long1, Lat1 + d, p2.X, p2.Y );
        result := abs( p1.y - p2.y );
      End;
    csProjection:
      If FDestinCoordSystem = csLatLon Then
      Begin
        { must convert first from meters to degrees }
        FSourceProjector.CoordSysToLatLong( RefPt.X, RefPt.Y, Long1, Lat1 );
        FSourceProjector.CoordSysToLatLong( RefPt.X, RefPt.Y + d, Long2, Lat2 );
        result := abs( Lat2 - lat1 );
      End;
  End;
End;

Function TEzImportConverter.EditProjector( Const Caption: String;
  Projector: TEzProjector; Var CoordSystem: TEzCoordSystem ): Boolean;
Var
  sl1, sl2: TStringList;
  I: Integer;
Begin
  result := false;
  sl1 := TStringList.create;
  sl2 := TStringList.create;
  Try
    If Projector.Params.Count = 0 Then
      Projector.InitDefault;
    If Projector.Params.Count > 2 Then
      For I := 0 To 2 Do
        sl1.Add( Projector.Params[I] );
    For I := 3 To Projector.Params.count - 1 Do
      sl2.Add( Projector.Params[I] );
    If fProj.SelectCoordSystem(Caption, CoordSystem, sl1, sl2, true) Then
    Begin
      If CoordSystem = csProjection Then
      Begin
        Projector.Params.Clear;
        Projector.Params.AddStrings( sl1 );
        Projector.Params.AddStrings( sl2 );
        Projector.CoordSysInit;
        { were the parameters given correctly ? }
        If Not Projector.HasProjection Then
        Begin
          MessageToUser( SWrongProjParams, SMsgError, MB_ICONERROR );
          Exit;
        End;
      End;
    End
    Else
      Exit;
  Finally
    sl1.free;
    sl2.free;
  End;
  result := true;
End;

Function TEzImportConverter.EditProjections( AutoChangeDestin: Boolean ): Boolean;
Begin
  Initialize;
  result := EditProjector( SProjEditSource, FSourceProjector, FSourceCoordSystem );
  If result = false Then
    exit;
  If AutoChangeDestin Then
  Begin
    FDestinCoordSystem := FSourceCoordSystem;
    FDestinProjector.Params.Assign( FSourceProjector.Params );
    FDestinProjector.CoordSysInit;
  End;
  result := EditProjector( SProjEditDestin, FDestinProjector, FDestinCoordSystem );
End;

Function TEzImportConverter.DestinationUnits: TEzCoordsUnits;
Var
  u: String;
Begin
  If FDestinCoordSystem = csLatLon Then
    Result := cuDeg
  Else
  Begin
    u := FDestinProjector.GC.pj_param.AsString( 'units' );
    If Length( u ) = 0 Then
      result := cuM
    Else
      Result := EzBase.UnitCodeFromID( u );
  End;
End;


{ TEzBaseImport }

constructor TEzBaseImport.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FConfirmProjectionSystem := True;
  FConverter := TEzImportConverter.Create;
  FAutoFreeConverter := True;
end;

destructor TEzBaseImport.Destroy;
begin
  inherited Destroy;
  If FAutoFreeConverter And ( FConverter <> Nil ) Then
    FConverter.Free;
end;

{$IFDEF BCB}
function TEzBaseImport.GetConfirmProjectionSystem: Boolean;
begin
  Result := FConfirmProjectionSystem;
end;

function TEzBaseImport.GetConverter: TEzImportConverter;
begin
  Result := Converter;
end;

function TEzBaseImport.GetOverwrite: Boolean;
begin
  Result := FImportToCurrent;
end;

procedure TEzBaseImport.SetConfirmProjectionSystem(const Value: Boolean);
begin
  FConfirmProjectionSystem := Value;
end;

procedure TEzBaseImport.SetOverwrite(const Value: Boolean);
begin
  FImportToCurrent := Value;
end;
{$ENDIF}

Function TEzBaseImport.Execute: Boolean;
Var
  SourceExt, TmpExt: TEzRect;
  Source_emax: TEzPoint;
  Source_emin: TEzPoint;
  PrevLayerCount: Integer;
  FileWithoutExt, temps: String;
  DestCoordSystem: TEzCoordSystem;
  DestCoordsUnits: TEzCoordsUnits;
  Index: Integer;
  Layer: TEzBaseLayer;
  TmpEntity: TEzEntity;
  wasActive: boolean;
  FieldList: TStringList;
  I, NewRecno: Integer;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -