重写FeatureStyleModifier实现更新一个图层中指定的几个图元的样式

本文档介绍如何重写FeatureStyleModifier,用于更新地图图层中特定图元的样式。SelectedPointModifier类允许根据需求修改单个或多个点图元的样式,如调整点的大小。此外,提供了两种不同的方法来更改图元样式:一种是通过SQL更新图层,另一种是创建临时图层并应用新的样式。示例代码展示了具体的实现过程。

重写FeatureStyleModifier


using System;
using MapInfo.Styles;

namespace GIS.MapInfoModify
{
/// <summary>
/// SelectedPointModifier 的摘要说明。
/// 重写FeatureStyleModifier,可以通过SelectedPointModifier,来修改单个的或是多个选中的点图元
/// </summary>
internal class SelectedPointModifier:MapInfo.Mapping. {
private BasePointStyle _pointStyle;
private System.Collections.Hashtable features;
/// <summary>
/// 重写FeatureStyleModifier,实现对图层中单个或是多个图元的样式的修改
/// </summary>
/// <param name="name">样式的名称</param>
/// <param name="alias">样式别名</param>
/// <param name="dSelection">要修改样式的集合</param>
/// <param name="layerAlias">图层别名</param>
// /// <param name="PointStyle">想使选中的点显示的样式</param>
public SelectedPointModifier(string name, string alias, MapInfo.Engine.Selection dSelection, string layerAlias): base(name, alias)
{
// this._pointStyle=PointStyle;
features = new System.Collections.Hashtable();
string[] exp = new string[]{"MI_Key"};
this.Expressions = exp;
foreach(MapInfo.Data.IResultSetFeatureCollection irfc in dSelection)
{
// Only save the selected features from the layer to apply the modifier too.
// The DefaultSelection could have features from several layers.

if(irfc.BaseTable.Alias == layerAlias)
{
foreach(MapInfo.Data.Feature f in irfc)
{
features.Add(f.Key.Value, f.Key.Value);
}
}
}
}
protected override bool Modify(MapInfo.Styles.FeatureStyleStack styles, object[] values)
{
MapInfo.Styles.CompositeStyle cs = styles.Current;
if (features.Contains((values[0] as MapInfo.Data.Key).Value))
{
// Set the style for region features because the modifier is being applied to a region layer.
// BitmapPointStyle vs=new BitmapPointStyle("PINB-64.BMP");
// (vs.PointSize=20;
//cs = new CompositeStyle(null, null, null, vs);
// cs.SymbolStyle=this._pointStyle;
cs.SymbolStyle.PointSize=20;
//(cs.AreaStyle.Interior as MapInfo.Styles.SimpleInterior).ForeColor = System.Drawing.Color.Orange;
//(cs.AreaStyle.Border as MapInfo.Styles.SimpleLineStyle).Color = System.Drawing.Color.RoyalBlue;
//(cs.AreaStyle.Border as MapInfo.Styles.SimpleLineStyle).Width = new MapInfo.Styles.LineWidth(3, MapInfo.Styles.LineWidthUnit.Pixel);

return true;
}
return false;
}
}
}
下面是调用

#region 高亮显示选中的图元通过改变其样式
private void ShowShi(string LayerName)
{
MapInfo.Mapping.FeatureLayer fl = this.mapControl1.Map.Layers[LayerName] as MapInfo.Mapping.FeatureLayer;
//The FeatureLayer can be any Region layer desired to change
//其中只是改变选中的图元的大小来表示闪烁
if(fl!=null)
{
string ModifierName=LayerName+"Modifier";
SelectedPointModifier scm = new SelectedPointModifier(ModifierName, ModifierName, MapInfo.Engine.Session.Current.Selections.DefaultSelection, fl.Alias);
int ModifiCount=fl.Modifiers.Count;
if(ModifiCount!=0)
{
fl.Modifiers.Remove(ModifierName);
this.ShanCi++;
}
else
{
fl.Modifiers.Append(scm);
this.ShanCi++;
}
if(this.ShanCi%6==0)//闪几次就不再闪
{
this.timerSelect.Enabled=false;
this._shanShuoLayerName=null;
this.ShanCi=0;
}

}
}
 

————————————————————————————————————————————————

左懿代码一

foreach(Feature ftr in fc)
{
MapInfo.Data.MIConnection connection = new MapInfo.Data.MIConnection();
connection.Open();

MapInfo.Data.MICommand command = connection.CreateCommand();
command.CommandText = "update " + ftr.Table.Alias + " set obj = @obj,MI_Style=@style where MI_Key = @MIKEY";
command.Parameters.Add("@obj", ftr.Geometry );
command.Parameters.Add("@MIKEY", ftr.Key);
command.Parameters.Add("@style",cs);
command.ExecuteNonQuery();

command.Dispose();
connection.Close();
connection.Dispose();

ftr.Update();
}

——————————————————————————————————————————————

左懿代码二

string tempLayerName = "tempStreet";
MapInfo.Data.Table newTable = CreateTempLayer(tempLayerName);

//MapInfo.Data.Table newTable = MapInfo.Engine.Session.Current.Catalog.OpenTable(tempLayerName);

MapInfo.Data.MIConnection connection = new MapInfo.Data.MIConnection();
connection.Open();

MapInfo.Data.MICommand command = connection.CreateCommand();

command.CommandText = "delete "+tempLayerName ;
command.Prepare();
command.ExecuteNonQuery();

command.CommandText = "Insert into "+tempLayerName + " Select * from street where MI_Key = @MIKEY";
command.Parameters.Add("@MIKEY", ftr.Key);
command.Prepare();
command.ExecuteNonQuery();

command.Dispose();
connection.Close();

(mapControl.Map.Layers[tempLayerName] as FeatureLayer).Modifiers.Clear();
MapInfo.Mapping.FeatureOverrideStyleModifier fsmtemp = new MapInfo.Mapping.FeatureOverrideStyleModifier(null,cs);
(mapControl.Map.Layers[tempLayerName] as FeatureLayer).Modifiers.Append(fsmtemp);

——————————————————————————————————————————————

C/S下替换图片

Feature feature=this.FindFeature(this.tableName,"siteId","4");

((BitmapPointStyle)feature.Style).Name="TRUC-64";
feature.Update();

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值