MXDの差分確認


レイヤの名称一意として、追加/削除/表示縮尺/フィルタの変更点をコンソールに書く。
MXDがJSONにでもならないかな、特殊なシンボルとかはバイナリで表現されてもいいから。

private struct LayerInfo
{
  public string DefinitionExpression;
  public double MaximumScale;
  public double MinimumScale;

  public override string ToString()
  {
    return string.Format("{0},{1}, \"{2}\"", this.MinimumScale, this.MaximumScale, this.DefinitionExpression);
  }
}//end struct
if (args.Length != 2)
{
Console.WriteLine("引数1 MXD1 引数2 MXD2");
return;
}//end if

string mxd1 = args[0];
string mxd2 = args[1];

Dictionary<string, LayerInfo> dictLayerInfo1 = new Dictionary<string, LayerInfo>();
Dictionary<string, LayerInfo> dictLayerInfo2 = new Dictionary<string, LayerInfo>();
List<string> diff = new List<string>();

using (ComReleaser com = new ComReleaser())
{
UID uid = new UIDClass();
com.ManageLifetime(uid);

uid.Value = "{" + typeof(IFeatureLayer).GUID.ToString() + "}";


IMapDocument mapDoc_1 = new MapDocumentClass();
com.ManageLifetime(mapDoc_1);

IMapDocument mapDoc_2 = new MapDocumentClass();
com.ManageLifetime(mapDoc_2);

mapDoc_1.Open(mxd1);
mapDoc_2.Open(mxd2);

IMap map1 = mapDoc_1.get_Map(0);
com.ManageLifetime(map1);

IMap map2 = mapDoc_2.get_Map(0);
com.ManageLifetime(map2);

IEnumLayer enumLayers1 = map1.get_Layers(uid, true);
com.ManageLifetime(enumLayers1);

IEnumLayer enumLayers2 = map2.get_Layers(uid, true);
com.ManageLifetime(enumLayers2);

enumLayers1.Reset();
enumLayers2.Reset();

ILayer layer1 = null;
ILayer layer2 = null;

while ((layer1 = enumLayers1.Next()) != null)
{
  LayerInfo info = new LayerInfo();
  info.DefinitionExpression = ((IFeatureLayerDefinition)layer1).DefinitionExpression;
  info.MinimumScale = layer1.MinimumScale;
  info.MaximumScale = layer1.MaximumScale;
  dictLayerInfo1.Add(layer1.Name, info);
}//end layer1 loop

while ((layer2 = enumLayers2.Next()) != null)
{
  LayerInfo info = new LayerInfo();
  info.DefinitionExpression = ((IFeatureLayerDefinition)layer2).DefinitionExpression;
  info.MinimumScale = layer2.MinimumScale;
  info.MaximumScale = layer2.MaximumScale;
  dictLayerInfo2.Add(layer2.Name, info);

  if (!dictLayerInfo1.ContainsKey(layer2.Name) ||
      !dictLayerInfo1[layer2.Name].Equals(info))
  {
    diff.Add(layer2.Name);
  }//end if
}//end layer2 loop

}//end com
foreach (var layerName in diff)
{
LayerInfo info1 = new LayerInfo
{
  DefinitionExpression = "N/A",
  MinimumScale = double.NaN,
  MaximumScale = double.NaN
};
LayerInfo info2 = new LayerInfo
{
  DefinitionExpression = "N/A",
  MinimumScale = double.NaN,
  MaximumScale = double.NaN
};

if (dictLayerInfo1.ContainsKey(layerName) &&
    dictLayerInfo2.ContainsKey(layerName))
{

  info1 = dictLayerInfo1[layerName];
  info2 = dictLayerInfo2[layerName];
}
else if (dictLayerInfo1.ContainsKey(layerName))
{
  info1 = dictLayerInfo1[layerName];
}
else if (dictLayerInfo2.ContainsKey(layerName))
{
  info2 = dictLayerInfo2[layerName];
}
カテゴリー: 開発 タグ: パーマリンク