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

📄 mapxcontainer.pas

📁 此代码是关于mapgis的在
💻 PAS
📖 第 1 页 / 共 5 页
字号:
  try
    InitComFields(Flds, MapFields);
    LyrInfo.type_ :=miLayerInfoTypeNewTable;
    LyrInfo.AddParameter('FileSpec', FileName);
    LyrInfo.AddParameter('NAME', LayerName);
    LyrInfo.AddParameter('Fields', Flds);
    Result:=MapX.Layers.Add(LyrInfo, Position);
    if bAddData then
      MapX.Datasets.Add(miDataSetLayer, Result, Result.Name,
                        EmptyParam, EmptyParam, EmptyParam,
                        EmptyParam, EmptyParam);
  finally
    LyrInfo:=nil;
    Flds:=nil;
  end;
end;

function TLayersManager.CreateCustomTempLayer(const LayerName: string;
  MapFields: TMapFieldList; const Index:Integer;
  const bAddData:Boolean): CMapXLayer;
var
  LyrInfo:LayerInfo;
  Flds:Fields;
  Position:OleVariant;
begin
  if Index=-1 then
    Position:=MapX.Layers.Count+1
  else
    Position:=Index;
  LyrInfo:=CoLayerInfo.Create;
  Flds:=CoFields.Create;
  try
    InitComFields(Flds, MapFields);
    LyrInfo.type_ :=miLayerInfoTypeTemp;
    LyrInfo.AddParameter('FileSpec', LayerName+'.TAB');
    LyrInfo.AddParameter('NAME', LayerName);
    LyrInfo.AddParameter('Fields', Flds);
    Result:=MapX.Layers.Add(LyrInfo, Position);
    if bAddData then
      MapX.Datasets.Add(miDataSetLayer, Result, Result.Name,
                        EmptyParam, EmptyParam, EmptyParam,
                        EmptyParam, EmptyParam);
  finally
    LyrInfo:=nil;
    Flds:=nil;
  end;
end;

procedure TLayersManager.InitComFields(Flds: Fields;
  MapFields: TMapFieldList);
var
  i:Integer;
  MapFld:TMapField;
begin
  for i:=0 to MapFields.ItemCount-1 do
  begin
    MapFld:=MapFields.Items[i];
    case MapFld.FieldType of
      mftString   : Flds.AddStringField(MapFld.FieldName, MapFld.Length, EmptyParam);
      mftInteger  : Flds.AddIntegerField(MapFld.FieldName, EmptyParam);
      mftSmallint : Flds.AddSmallIntField(MapFld.FieldName, EmptyParam);
      mftBoolean  : Flds.AddLogicalField(MapFld.FieldName, EmptyParam);
      mftFloat    : Flds.AddFloatField(MapFld.FieldName, EmptyParam);
      mftDateTime : Flds.AddDateField(MapFld.FieldName, EmptyParam);
      mftBCD      : Flds.AddNumericField(MapFld.FieldName, MapFld.Prec, MapFld.Scale, EmptyParam);
    end;
  end;
end;

function TLayersManager.CreateNormalLayerFromDataSetAndLayer(const LayerName,
  DataSetName, KeyFieldName: string; DS: TCustomADODataSet): CMapXLayer;
var
  oBLayer : BindLayer;
  MapDS:DataSet;
begin
  if not DS.Active then
    raise Exception.Create('系统基础表没有打开!');
  if DS.FindField(KeyFieldName)=nil then
    raise Exception.Create('编号字段不存在!');
  //创建BindLayer//
  oBLayer := coBindLayer.Create;
  oBLayer.LayerName := LayerName;
  oBLayer.LayerType := miBindLayerTypeNormal;
  //添加数据集//
  MapDS:=MapX.Datasets.Add(miDataSetADO,//数据集类型,这是miDataSetADO,即ADO专用的
                           DS.Recordset,//使用这个方法获得ADO中的_Recordset类型
                           DataSetName,//数据集名称
                           KeyFieldName,//传入的是Xunit表中的字段ID的名称
                           EmptyParam,
                           oBLayer,//BindLayer
                           EmptyParam,
                           EmptyParam);
end;

function TLayersManager.CreateSymbolLayerFromDataSetAndLayer(
  const LayerName, DataSetName, RefColumn, KeyFieldName, TabFileName: string;
  DS: TCustomADODataSet): CMapXLayer;
var
  oBLayer : BindLayer;
  MapDS:DataSet;
begin
  if not DS.Active then
    raise Exception.Create('系统基础表没有打开!');
  if DS.FindField(RefColumn)=nil then
    raise Exception.Create('参照字段不存在!');
  if DS.FindField(KeyFieldName)=nil then
    raise Exception.Create('编号字段不存在!');
  //创建BindLayer//
  oBLayer := coBindLayer.Create;
  oBLayer.LayerName := LayerName;
  oBLayer.FileSpec := TabFileName;
  oBLayer.LayerType := miBindLayerTypePointRef;
  oBLayer.RefColumn1 := RefColumn;
  oBLayer.ReferenceLayer := LayerName;
  //添加数据集//
  MapDS:=MapX.Datasets.Add(miDataSetADO,//数据集类型,这是miDataSetADO,即ADO专用的
                           DS.Recordset,//使用这个方法获得ADO中的_Recordset类型
                           DataSetName,//数据集名称
                           KeyFieldName,//传入的是Xunit表中的字段ID的名称
                           oBLayer,
                           EmptyParam,//BindLayer
                           EmptyParam,
                           EmptyParam);
end;

function TLayersManager.IndexByLayer(ALyr: Layer): Integer;
var
  i:Integer;
begin
  Result:=-1;
  for i:=1 to MapX.Layers.Count do
    if MapX.Layers.Item[i]=ALyr then
    begin
      Result:=i;
      Exit;
    end;
end;

function TLayersManager.CopyLayer(ALyr: Layer; const DataSetIndex:Word;
  const FileName: string; const StructOnly:Boolean;
  const Index:Integer; const bAddData:Boolean;
  const AddFtsType:Integer):CMapXLayer;
var
  MapFields:TMapFieldList;
  LayerName, FilePath:string;
begin
  {创建空白图层}
  LayerName:=ExtractFileNameNoExt(FileName);
  FilePath:=ExtractFilePath(FileName);
  MapFields:=TMapFieldList.Create;
  try
    LoadMapFields(ALyr.DataSets.Item[DataSetIndex].Fields, MapFields);
    Result:=CreateCustomTableLayer(LayerName, FilePath, MapFields, Index, bAddData);
  finally
    MapFields.Free;
  end;
  {复制图层数据,包括图形和图形的记录}
  if not StructOnly then
    UnionFeature(Result, ALyr, DataSetIndex, '', AddFtsType);
end;

procedure TLayersManager.UnionFeature(ToLayer, FromLayer: Layer;
  const DataSetIndex: Word; const strWhere:string;
  const AddFtsType:Integer);
var
  i,j:Integer;
  ft:Feature;
  Fts:Features;
  ds:CMapXDataSet;
  rvs:RowValues;
  newrvs:RowValues;
begin
  Fts:=nil;
  case AddFtsType of
    UNION_FEATURE_ALL:begin
      Fts:=FromLayer.AllFeatures;
    end;
    UNION_FEATURE_SELECTED:begin
      Fts:=FromLayer.Selection.Clone;
    end;
    UNION_FEATURE_QUERY:begin
      Fts:=FromLayer.Search(strWhere, EmptyParam);
    end;
  end;

  if Fts=nil then Exit;

  ToLayer.Editable:=True;
  if DataSetIndex=0 then
    for i:=1 to Fts.Count do
    begin
      ft:=Fts.Item[i];
      ToLayer.AddFeature(ft, EmptyParam);    
    end
  else
  begin
    ds:=FromLayer.DataSets.Item[DataSetIndex];
    newrvs:=CreateRowValuesFromStruct(ds.Fields, ToLayer.DataSets.Item[1]);
    for i:=1 to Fts.Count do
    begin
      ft:=Fts.Item[i];
      rvs:=ds.RowValues[ft];
      if rvs=nil then
        ToLayer.AddFeature(ft, EmptyParam)
      else
      begin
        for j:=1 to ds.Fields.Count do
          newrvs.Item[j].Value:=rvs.Item[j].Value;
        ToLayer.AddFeature(ft, newrvs);
      end;
    end;
  end;
end;

{ TMapCtrlTools }

constructor TMapCtrlTools.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  Fm_Map:=TMapManager.Create;
  Fm_Layer:=TLayerManager.Create;
  Fm_Layer.MapManager:=Fm_Map;
  Fm_Layers:=TLayersManager.Create;

  RegisterSymbols;
end;

destructor TMapCtrlTools.Destroy;
begin
  Fm_Map.Free;
  Fm_Layer.Free;
  Fm_Layers.Free;
  inherited Destroy;
end;

procedure TMapCtrlTools.RegisterSymbols;
var
  i:Integer;
  aItem:TBaseStyleInfo;
begin
  for i:=0 to StyleClasses.Count-1 do
  begin
    aItem:=TBaseStyleInfo(TStyleInfoClass(StyleClasses.Items[i]).NewInstance);
    aItem.Create;
    aItem.UserID:=i;
    aItem.Caption:='<默认>';
    Fm_Map.UserStyles.Add(aItem);
  end;
end;

procedure TMapCtrlTools.SetMapX(const Value: TMapXObject);
begin
  FMapX := Value;
  Fm_Map.MapX := Value;
  Fm_Layers.MapX := Value;
end;

{ TLayerManager }

function TLayerManager.AddCircle(const x, y, r: Double;
  mstyle:OleVariant; const Standalone:Boolean; ActionId:Integer): feature;
begin
  Result:=AddCircularRegion(0, x, y, r, 100, mstyle, Standalone, ActionId);
end;

function TLayerManager.AddText(const x, y: Double;
  const Caption: string; mstyle:OleVariant; const Standalone:Boolean;
  ActionId:Integer): feature;
var
  pt:Point;//点
  f:Feature;
begin
  mstyle:=GetStyleComObject(mstyle);
  if not Standalone then begin
    f:=CoFeature.Create;
    f.Attach(MapX.OleObject);
    f.type_:=miFeatureTypeText;
    f.Point.Set_(x, y);
    f.Caption := WideString(Caption);
    f.Style:= CMapXStyle(IDispatch(mstyle));
    Result:=AddFeature(f, EmptyParam, ActionId);
  end
  else begin
    pt := CoPoint.Create;
    pt.Set_(x,y);
    Result := MapX.FeatureFactory.CreateText(pt,Caption, miPositionBR, mstyle);//创建特征
    Result.KeyValue := WideString(Caption);//标注
    AddFeature(Result, EmptyParam, ActionId);
  end;
end;

function TLayerManager.AddLine(const x1, y1, x2, y2: Double;
  mstyle:OleVariant; const Standalone:Boolean; ActionId:Integer): feature;
var
  Parts: TMapPartList;
  Pts:TMapPointList;
begin
  Parts:=TMapPartList.Create;
  try
    Pts:=Parts.Add;
    Pts.AddXY(x1, y1);
    Pts.AddXY(x2, y2);
    Result:=AddLine(Parts, mstyle, Standalone, ActionId);
  finally
    Parts.Free;
  end;
end;

function TLayerManager.AddLine(Parts:TMapPartList;
  mstyle:OleVariant; const Standalone:Boolean; ActionId:Integer): feature;
var
  Prts:CMapXParts;
begin
  Result:=nil;
  Prts:=CoParts.Create;
  try
    Parts.ConfigParts(Prts, True);
    Result:=AddLine(Prts, mstyle, Standalone, ActionId);
  finally
    Prts:=nil;
  end;
end;

function TLayerManager.AddSymbol(const x, y: Double; 
  mstyle:OleVariant; const Standalone:Boolean; ActionId:Integer): feature;
var
  pt:Point;//点
  f:Feature;
begin
  mstyle:=GetStyleComObject(mstyle);

  pt := CoPoint.Create;
  pt.Set_(x,y);
  try
    if Standalone then
    begin
      f := MapManager.MapX.FeatureFactory.CreateSymbol(pt,mstyle);//创建特征
      f.type_:=miFeatureTypeSymbol;
    end
    else
    begin
      f:=CoFeature.Create;
      f.Attach(MapX.OleObject);
      f.type_:=miFeatureTypeSymbol;
      f.Point:=pt;
      f.Style:=CMapXStyle(IDispatch(mstyle));
    end;
    Result:=AddFeature(f,EmptyParam, ActionId);//将特征添加到层
    f:=nil;
  finally
    pt:=nil;
  end;
end;

function TLayerManager.AddRectangle(const x1, y1, x2, y2: Double;
  const Caption: string; mstyle:OleVariant;
  const Standalone:Boolean; ActionId:Integer): feature;
var
  Parts: TMapPartList;
  Pts:TMapPointList;
begin
  Parts:=TMapPartList.Create;
  try
    Pts:=Parts.Add;
    Pts.AddXY(x1, y1);
    Pts.AddXY(x2, y1);
    Pts.AddXY(x2, y2);
    Pts.AddXY(x1, y2);
    Pts.AddXY(x1, y1);
    Result:=AddRegion(Parts, mstyle, Standalone, ActionId);
  finally
    Parts.Free;
  end;
end;

function TLayerManager.AddRegion(Parts:TMapPartList;
  mstyle:OleVariant; const Standalone:Boolean; ActionId:Integer): feature;
var
  Prts:CMapXParts;
begin
  Result:=nil;
  Prts:=CoParts.Create;
  try
    Parts.ConfigParts(Prts, True);
    Result:=AddRegion(Prts, mstyle, Standalone, ActionId);
  finally
    Prts:=nil;
  end;
end;

function TLayerManager.CombineSelectedShapes:Feature;
var
  i:Integer;
  Cancel:Boolean;
begin
  Cancel:=False;
  if Assigned(FBeforeDeleteFeature) then
    for i:=1 to Layer.Selection.Count do
    begin
      FBeforeDeleteFeature(Self, Layer.Selection.Item[i], ID_ACTION_COMBINE, Cancel);
      if Cancel then Exit;
    end;
  Result:=MapXAPIs.CombineSelectedShapes(MapX, Layer);
  if (Result<>nil) and Assigned(FOnFeatureCreate) then
    FOnFeatureCreate(Self, ID_ACTION_COMBINE, Result);
end;

procedure TLayerManager.Copy;

⌨️ 快捷键说明

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