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

📄 ezimportbase.pas

📁 很管用的GIS控件
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  Saved: TCursor;
  BadEntities, progress,entno: Integer;
  CanContinue: Boolean;
begin
  Result := False;

  If FDrawBox = Nil Then EzGisError( SWrongEzGIS );
  If FConverter = Nil Then EzGisError( SWrongProjector );
  If Length(Trim(FileName)) = 0 Then Exit;

  PrevLayerCount := FDrawBox.GIS.Layers.Count;

  FileWithoutExt:= ChangeFileExt(Filename, '') + '_';

  Self.ImportInitialize;    // do some initialization here and return the name of the file to generate

  { this method will try to configure the converter by guessing a coordinate
    system from the source file to import }

  If PrevLayerCount > 0 Then
  Begin
    FConverter.DestinCoordSystem := FDrawBox.GIS.MapInfo.CoordSystem;
    FConverter.DestinProjector.Params.Assign( FDrawBox.GIS.ProjectionParams );
  End;
  { Ask for the projection of the shapefile }
  If FConfirmProjectionSystem And
    Not FConverter.EditProjections( PrevLayerCount = 0 ) Then Exit;

  DestCoordSystem := FConverter.DestinCoordSystem;
  DestCoordsUnits := FConverter.DestinationUnits;
  If PrevLayerCount = 0 Then
  Begin
    With FDrawBox.GIS Do
    Begin
      MapInfo.CoordSystem := FConverter.DestinCoordSystem;
      MapInfo.CoordsUnits := DestCoordsUnits;
      ProjectionParams.Assign( FConverter.DestinProjector.Params );
      Modified := True;
    End;
  End;

  { Exists a layer with that name already ? }
  If Not FImportToCurrent Then
  Begin
    temps := EzSystem.GetValidLayerName( ExtractFileName( FileWithoutExt ) );
    Index := FDrawBox.GIS.Layers.IndexOfName( temps );
    If Index >= 0 Then
    Begin
      If MessageDlg( Format( SLayerAlreadyExists, [temps] ), mtConfirmation, [mbYes, mbNo], 0 ) = mrYes Then
      Begin
        // replace existing layer
        With FDrawBox.GIS Do
        Begin
          Layer := Layers[Index];
          wasActive := AnsiCompareText( MapInfo.CurrentLayer, Layer.Name ) = 0;
          Layers.Delete( Layer.Name, true );
          If wasActive And ( Layers.Count > 0 ) Then
            MapInfo.CurrentLayer := Layers[0].Name;
          If Layers.Count = 0 Then
            With MapInfo Do
            Begin
              Extension := INVALID_EXTENSION;
              CurrentLayer := '';
            End;
        End
      End Else
        Exit;
    End;

    FieldList := TStringList.Create;
    // copiar estructura de source file
    Try
      GetSourceFieldList( FieldList );

      Layer := FDrawBox.GIS.Layers.CreateNewEx(
        ExtractFilePath(FDrawBox.GIS.FileName) + ExtractFileName(FileWithoutExt),
        DestCoordSystem, DestCoordsUnits, FieldList);

    Finally
      FieldList.Free;
    End;
    If Layer = Nil Then
    Begin
      MessageToUser( SCannotCreateNewLayer, SMsgError, MB_ICONERROR );
      Exit;
    End;
  End
  Else
    Layer := FDrawBox.GIS.CurrentLayer;

  // set as active
  FDrawBox.GIS.MapInfo.CurrentLayer := Layer.Name;

  Layer.ForceOpened;
  Layer.StartBatchInsert;

  Self.ImportFirst;  // an initialization must be done here for importing

  If FDrawBox.GIS.ShowWaitCursor Then
  Begin
    Saved:= Screen.Cursor;
    Screen.Cursor := crHourglass;
  End;

  CanContinue:= True;
  BadEntities:= 0;

  Try

    While Not Self.ImportEof Do    // ImportEof must return true when finished importing
    Begin

      Try
        TmpEntity := Self.GetNextEntity( progress,entno );   // GetNextEntity must return the next entity from source files

        { show progress messages }
        If Assigned(FOnFileProgress) Then
          FOnFileProgress(Self, FFilename, progress, entno, BadEntities, CanContinue);

        If Not CanContinue Then
        Begin
          If Assigned(TmpEntity) Then
            FreeAndNil( TmpEntity );
          Break;
        End;

        Try
          If TmpEntity = Nil Then
          Begin
            Inc( BadEntities );
            Continue;
          End;
          { convert from source coordinate system }
          Try
            TmpEntity.BeginUpdate;
            For I := 0 To TmpEntity.Points.Count - 1 Do
              TmpEntity.Points[I] := FConverter.Convert( TmpEntity.Points[I] );
            TmpEntity.EndUpdate;
            NewRecno := Layer.AddEntity( TmpEntity );
            If Layer.DBTable <> Nil Then
            Begin
              // AddSourceFieldData must position on record, ex.: Layer.DBTable.Recno:= NewRecno;
              Self.AddSourceFieldData( Layer, NewRecno );   // this method must add source database field to the current record
            End;
          Finally
            FreeAndNil( TmpEntity );
          End;
        Except
          On E: Exception Do
          Begin
            Inc( BadEntities ); // count number of bad entities and ignore error
          End;
        End;
      Finally
        Self.ImportNext;   // this method must advance to next valid record on source file
      End;
    End;

    { this method must retrieve the extension of the file imported }
    SourceExt:= Self.GetSourceExtension;
    If Not EqualRect2D( SourceExt, INVALID_EXTENSION ) Then
    Begin
      With SourceExt Do
      Begin
        source_emax := FConverter.Convert( Emax );
        source_emin := FConverter.Convert( Emin );
      End;
      With FDrawBox.GIS.MapInfo Do
      Begin
        If PrevLayerCount = 0 Then
        Begin
          TmpExt.Emax := source_emax;
          TmpExt.Emin := source_emin;
          Extension := TmpExt;
        End
        Else
        Begin
          TmpExt := Extension;
          MaxBound( TmpExt.Emax, source_emax );
          MinBound( TmpExt.Emin, source_emin );
          Extension := TmpExt;
        End;
      End;
    End;

  Finally
    Layer.FinishBatchInsert;
    Self.ImportEnd;      // this method must close the source file and/or other finalization
    If FDrawBox.GIS.ShowWaitCursor Then
      Screen.Cursor := Saved;
  End;

  Layer.WriteHeaders(True);
  FDrawBox.GIS.QuickUpdateExtension;
  If (PrevLayerCount = 0) Or FShowAllMapOnFinish Then
    With FDrawBox.GIS Do
      For I := 0 To DrawBoxList.Count - 1 Do
        DrawBoxList[I].ZoomToExtension;

  FDrawBox.GIS.Modified := true;

  Result := true;

end;

function TEzBaseImport.ImportFiles(FileList: TStrings): Boolean;
Var
  I: Integer;
  TmpFilename: String;
  TmpBool: Boolean;
Begin
  result := true;
  TmpBool := FConfirmProjectionSystem;
  TmpFilename := FFileName;
  Try
    For I := 0 To FileList.Count - 1 Do
    Begin
      FFileName := FileList[I];
      Execute;
      FConfirmProjectionSystem := False;
    End;
  Finally
    FFileName := TmpFilename;
    FConfirmProjectionSystem := TmpBool;
  End;
end;

procedure TEzBaseImport.SetConverter(Value: TEzImportConverter);
begin
  If ( FConverter <> Nil ) And FAutoFreeConverter Then
    FreeAndNil( FConverter );
  FConverter := Value;
  FAutoFreeConverter := False;
end;

procedure TEzBaseImport.ImportInitialize;
begin
  // nothing to do here
end;

Procedure TEzBaseImport.GetSourceFieldList( FieldList: TStrings );
Begin
End;

Procedure TEzBaseImport.ImportFirst;
begin
end;

Procedure TEzBaseImport.ImportEnd;
begin
end;

Function TEzBaseImport.ImportEof: Boolean;
Begin
  Result:= True;
End;

Procedure TEzBaseImport.ImportNext;
Begin
End;

Function TEzBaseImport.GetNextEntity(Var progress,entno: Integer): TEzEntity;
Begin
  Result:= Nil;
End;

Procedure TEzBaseImport.AddSourceFieldData(DestLayer: TEzBaseLayer; DestRecno: Integer);
Begin
End;

Function TEzBaseImport.GetSourceExtension: TEzRect;
Begin
  Result:= INVALID_EXTENSION;
End;

{ TEzBaseExport }

function TEzBaseExport.Execute: Boolean;
Var
  CanContinue: Boolean;
  Layer: TEzBaseLayer;
  I, nEntities: Integer;
  TmpEntity: TEzEntity;
  TmpClass: TEzEntityClass;
Begin
  If FDrawBox = Nil Then EzGisError( SWrongEzGIS );

  Layer := FDrawBox.GIS.Layers.LayerByName( FLayername );

  If Layer = Nil Then EzGisError( Format( SWrongLayername, [FLayername] ) );

  { create the shape file }
  ExportInitialize;
  Try
    Layer.ForceOpened;
    Layer.First;
    Layer.StartBuffering;
    Try
      nEntities := IMax( 1, Layer.RecordCount );
      I := 0;
      CanContinue := True;
      While Not Layer.Eof Do
      Begin
        Try
          With Layer Do
          Begin
            Inc( I );
            FOnFileProgress( Self, FFilename, Round( ( ( I + 1 ) / nEntities ) * 100 ), I, 0, CanContinue );
            If Not CanContinue Then Break;

            If RecIsDeleted Then Continue;

            TmpClass := GetClassFromID( RecEntityID );
            TmpEntity := TmpClass.Create( 4 );
            RecLoadEntity2( TmpEntity );
            Try
              ExportEntity(Layer, TmpEntity);
            Finally
              TmpEntity.Free;
            End;
          End;
        Finally
          Layer.Next;
        End;
      End;
    Finally
      Layer.EndBuffering;
    End;
  Finally
    Self.ExportEnd;
  End;
  result := true;
end;

{$IFDEF BCB}
function TEzBaseExport.GetLayerName: String;
begin
  Result:= FLayerName;
end;

procedure TEzBaseExport.SetLayerName(const Value: String);
begin
  FLayerName:= Value;
end;
{$ENDIF}

Procedure TEzBaseExport.ExportInitialize;
Begin
End;

Procedure TEzBaseExport.ExportEntity( SourceLayer: TEzBaseLayer; Entity: TEzEntity );
Begin
End;

Procedure TEzBaseExport.ExportEnd;
Begin
End;

end.

⌨️ 快捷键说明

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