MXDレイヤ


MXDをラスタのように描画するという意味不明な行為。

メリット
・ラベルエンジンの描画回避にかからない?
・ラベルが浮いてこない(描画順序があと)

つまりは背景レイヤ用
ベースマップレイヤはそうなってるのかもしれないが、妙に起動が遅くなるので。(そのうち検証?)
10.1 / 10.2ではなおっているといいなぁ。

下記がベース
レイヤ改に永続化機能を付与
地図画像Byte配列


下記が呼出し部分

string mxdPath = null;
using (OpenFileDialog open = new OpenFileDialog())
{
    open.Filter = "マップドキュメント|*.mxd";
    if (open.ShowDialog() != System.Windows.Forms.DialogResult.OK)
        return;
    mxdPath = open.FileName;
}//end dialog

CustomMxdLayer mxdLayer = new CustomMxdLayer(mxdPath);
this.axMapControl1.Map.AddLayer(mxdLayer);

((IViewRefresh)this.axMapControl1.ActiveView).AnimationRefresh(esriViewDrawPhase.esriViewAll, null, this.axMapControl1.ActiveView.Extent);

実装
get_DeviceFrameではなくBoudsとかとってTransformRectメソッドをesriDisplayTransformationEnum.esriTransformToDevice
で呼び出しが適当かも、緯度経度が世界を超えるとずれ始めるので、TopLeftをとる方が小さい画像になりメモリも節約できるかも?
速度調整可能な点はいくつかある。

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;

using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;

[ComVisible(true)]
[Guid("822F48B7-0C1F-43C0-A2B6-839525EC3D8B")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Custom.CustomMxdLayer")]
public class CustomMxdLayer : PersistBase, IPersitBaseChild, ILayer
{
    [Persist()]
    public string m_MapDocPath = null;

    //[Persist()]
    public IMap m_Map = null;

    [Persist()]
    private bool m_IsVisble = true;

    public CustomMxdLayer()
    {
    }

    public CustomMxdLayer(string mxdPath)
    {
        this.m_MapDocPath = mxdPath;

        this.InitMap();
    }

    private void InitMap()
    {
        using (ComReleaser com = new ComReleaser())
        {
            IMapDocument mapDoc = new MapDocumentClass();
            com.ManageLifetime(mapDoc);

            mapDoc.Open(this.m_MapDocPath, null);

            //リテラル 0
            this.m_Map = mapDoc.get_Map(0);

            mapDoc.Close();
        }//end com
    }//end method

    //---------------------------------------------------------------
    #region IPersitBaseChild実c1-30523?
    //---------------------------------------------------------------

    public void LoadCore(IVariantStream stream)
    {
        //読み込み時マップを読み直し
        this.InitMap();
    }

    public void SaveCore(IVariantStream stream)
    { 
    }

    //---------------------------------------------------------------
    #endregion IPersitBaseChild実c1-30523?
    //---------------------------------------------------------------

    //---------------------------------------------------------------
    #region ILayer実c1-30523?
    //---------------------------------------------------------------

    public void Draw(esriDrawPhase DrawPhase, IDisplay Display, ITrackCancel TrackCancel)
    {

        //c選択と注記は無視
        if (DrawPhase != esriDrawPhase.esriDPGeography)
            return;
        using (ComReleaser com = new ComReleaser())
        {
            tagRECT rect = Display.DisplayTransformation.get_DeviceFrame();
            int scWidth = rect.right;
            int scHeight = rect.bottom;

            IPoint leftU = Display.DisplayTransformation.ToMapPoint(0, 0);
            com.ManageLifetime(leftU);

            IPoint rightB = Display.DisplayTransformation.ToMapPoint(scWidth, scHeight);
            com.ManageLifetime(rightB);

            IEnvelope env = new EnvelopeClass();
            com.ManageLifetime(env);
            env.PutCoords(leftU.X, leftU.Y, rightB.X, rightB.Y);

            using (Bitmap img = this.getImg(env, scWidth, scHeight))
            using (Graphics gra = Graphics.FromHdc((IntPtr)Display.hDC))
            {
                gra.DrawImageUnscaled(img, rect.top, rect.left);
            }

        }//end com
    }//end method

    private Bitmap getImg(IEnvelope env, int width, int height)
    {
        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        using (ComReleaser com = new ComReleaser())
        using (Graphics g = Graphics.FromImage(bmp))
        //using (MemoryStream mem = new MemoryStream())
        {
            IMap map = this.m_Map;

            IActiveView actView = (IActiveView)map;
            tagRECT tag = new tagRECT();
            tag.left = tag.top = 0;
            tag.right = width;
            tag.bottom = height;

            actView.Output(g.GetHdc().ToInt32(), 96, ref tag, env, null);

            ////描画確定と破棄
            //g.Dispose();
            ////確任用
            //bmp.Save(@"c:\test.png", ImageFormat.Png);
        }//end graphics

        return bmp;
    }

    public bool Visible
    {
        get
        {
            return this.m_IsVisble;
        }
        set
        {
            this.m_IsVisble = value;
        }
    }

    public IEnvelope AreaOfInterest
    {
        get { return ((IActiveView)this.m_Map).FullExtent; }
    }
    public ISpatialReference SpatialReference
    {
        set { this.m_Map.SpatialReference = value; }
    }

    public string Name
    {
        get
        {
            return this.m_Map.Name;
        }
        set
        {
            this.m_Map.Name = value;
        }
    }

    public int SupportedDrawPhases
    {
        get { return (int)ESRI.ArcGIS.esriSystem.esriDrawPhase.esriDPGeography; }
    }

    public bool Cached
    {
        get
        {
            return false;
        }
        set
        {
        }
    }

    public double MaximumScale
    {
        get
        {
            return 0;
        }
        set
        {
        }
    }

    public double MinimumScale
    {
        get
        {
            return 0;
        }
        set
        {
        }
    }

    public bool ShowTips
    {
        get
        {
            return false;
        }
        set
        {
        }
    }

    public bool Valid
    {
        get { return true; }
    }

    public string get_TipText(double x, double y, double Tolerance)
    {
        return null;
    }

    //---------------------------------------------------------------
    #endregion ILayer実c1-30523?
    //---------------------------------------------------------------

}
カテゴリー: 開発 タグ: パーマリンク