在MapXtreme中导出FeatureLayer时,一般人的做法我不太清楚,我之前是SearchAll得到FeatureCollection,然后outTable.InsertFeatures(ftrs),但是这种方法慢得让人吐血,今天多方搜索,终于得到一个方法,那就是利用table.ExecuteReader直接读取,然后再转存,效率高得不得了。
贴段代码给有需要的同学:
/// <summary>
/// 将图元列表导出图层到指定文件夹
/// </summary>
/// <param name="layer">MapInfo图层</param>
/// <param name="outLayerId">图层ID</param>
/// <param name="outPath">输出文件夹</param>
internal static bool ExportToNativeLayer(IMapLayer layer, string outLayerId, string outPath)
{
var flayer = layer as FeatureLayer;
if (flayer != null)
{
var clone = flayer.Table.TableInfo.Columns.Clone();
var ti = new TableInfoNative(outLayerId);
for(var i=0; i<clone.Count;i++)
{
ti.Columns.Add(clone[i]);
}
if (!Directory.Exists(outPath))
Directory.CreateDirectory(outPath);
// tab文件的全路径
ti.Alias = outLayerId + "_export";
ti.TablePath = string.Format(@"{0}.tab", Path.Combine(outPath, outLayerId));
ti.Temporary = false;
ti.WriteTabFile();
//打开本地化的TableInfo
var table = MapInfo.Engine.Session.Current.Catalog.CreateTable(ti);
var rdr = flayer.Table.ExecuteReader();
var list = new List<Feature>();
while (rdr.Read())
{
var ftr = rdr.Current;
list.Add( ftr);
}
rdr.Close();
table.BeginAccess(TableAccessMode.Write);
try
{
foreach (var ftr in list)
table.InsertFeature(ftr);
}
finally
{
table.EndAccess();
}
ti.WriteTabFile(); //写入tabFile并关闭TableInfo
table.Close();
return true;
}
return false;
}