Monthly Archives: 11月 2012 - Page 20

MapWinGISを使った簡単なexample [Chapter 1]

MapWinGISを使った簡単なexample

[Chapter 1]では[Chapter 0]でインストールしたMapWindowのdllとocxを使って、超簡単にshape fileを表示して、拡大、縮小、移動などを処理するプログラムを作成します。

Program開発環境は、Visual Basic.NET 2010です。
Visual Sutdioの使い方は、一通り問題なく操作でき、一度はGUIのprogramを作成したことがある中級者を対象にしています。 したがって、vb.netの操作に関しては、端折ります。

Map controlの登録

適当な名前で新規project作成します。
新規作成は、Windows フォームアプリケーションです。

新規project作成後、MapWinGIS.ocxの登録を行います。

ツールボックス→アイテム選択→comコンポーネント
map compormentをチェックして登録

ツールボックスにMap Controlが登録されます。

これで、MapWinGIS.ocxを使えるようになります。

ここで、一度projectを保存しておきましょう。
続いてprogram codingをします。

Program Coding

まずは、From designをして、Codingします。

Form Design

Form designは簡単exampleなので、必要最低限にしておきます。
こんな感じ、
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex01_image01.jpg
Form Design

formに配置するものは
コントロール名:AxMap1
Dock: fillに設定

ToolStrip1にbutton1を追加
ToolStrip2にcommnad button類を追加

Trrview1(Layernameを表示する)

StatusStrip1(とりあえず)

SplitContainer(form1を拡大した際にcontrol類もあわせてresizeさせるために使用)

Coding

Add Layerでshape fileを読み込んで表示させる部分を作成します。

Map Controlをフォーム上に配置すると自動的にActivexを.NETで利用できる
AxMapWinGIS、MapWinGISが参照設定に追加されます。

そして、importsしておきます。

Imports AxMapWinGIS
Imports MapWinGIS
Layerの追加

subroutine AddLayerを作成します。
今後の拡張を考えて、OpenFileDialogでshape fileを選択できるようにしておきます。

 Private Sub AddLayer()
        Dim shpfile As MapWinGIS.Shapefile
        Dim openDlg As OpenFileDialog = New OpenFileDialog()
        Dim handle As Integer
        Dim ext As String = ""
 
        'initialize dialog
        openDlg.Filter = "Supported Formats|*.shp|Shapefile (*.shp)|*.shp"
        openDlg.CheckFileExists = True
 
        If (openDlg.ShowDialog(Me) = DialogResult.OK) Then
 
            'get the extension of the file
            ext = System.IO.Path.GetExtension(openDlg.FileName)
 
            If ext = ".shp" Then
 
                shpfile = New MapWinGIS.ShapefileClass()
 
                'open the shapefile
                Dim blmCheck As Boolean = shpfile.Open(openDlg.FileName)
 
                'add the shapefile to the map
                handle = AxMap1.AddLayer(shpfile, True)
 
                AxMap1.Redraw()
 
                TreeView1.Nodes.Add(System.IO.Path.GetFileNameWithoutExtension(shpfile.Filename))
 
            End If
 
        End If
    End Sub

Dim shpfile As MapWinGIS.ShapefileでShapefileをshpfileと定義して、以降このshpfileを使います。
Dim handle As Integer は読み込まれたfileのユニークな番号と考えよいと思います。
このhandleを使って、複数呼び出したshape fileを識別します。

おおまかなprogramの流れは、

  1. shape file を開く
  2. shpfileをMapWinGIS.ShapefileClass()で新規作成
  3. shpfile.Openでファイルを読み込む
  4. AxMap1.AddLayerでAxMap1に描画
  5. TreeView1.Nodes.AddでLayernameを追加

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex01_image02.jpg
ToolStrip1にbutton1をAddLayerに変更

Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        AddLayer()
    End Sub

でAddLayer susbroutineを呼び出します。

この段階で、保存後、program実行するとshape fileを読み込んで表示することができます。
これに拡大縮小、移動や全体表示のコマンドを付け加えてみます。

Map Control Commandの追加

拡大縮小と移動に関しては、CursorModeの変更だけです。
ますは、
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex01_image03.jpg
ToolStrip2にbutton類を変更

たった1行でできますね、説明の不要でしょうか。

    Private Sub ToolStripButtonZoomIn_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButtonZoomIn.Click
        AxMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomIn
    End Sub
 
 
    Private Sub ToolStripButtonZoomOut_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButtonZoomOut.Click
        AxMap1.CursorMode = MapWinGIS.tkCursorMode.cmZoomOut
    End Sub
 
 
    Private Sub ToolStripButtonPan_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButtonPan.Click
        AxMap1.CursorMode = MapWinGIS.tkCursorMode.cmPan
    End Sub
 
    Private Sub ToolStripButtonNone_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButtonNone.Click
        AxMap1.CursorMode = MapWinGIS.tkCursorMode.cmNone
    End Sub

Full extents(全体表示)に関しては、ZoomToMaxExtentsのeventで記述します。

    Private Sub ToolStripButtonFullExtents_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButtonFullExtents.Click
        AxMap1.ZoomToMaxExtents()
    End Sub

Programの実行

projectを保存後、実行してみましょう。

Add Layerをクリックするとファイル選択ダイアログが表示されます。
拡張子がshpだけを選択でいるようになっています。

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex01_image04.jpg
ファイル選択ダイアログ

サンプルの神奈川県のshape fileを読み込んでみます。

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex01_image05.jpg
神奈川県のshape fileを読み込んだ例

どうでしょうか? 表示されましたか?
ほかのshape fileを読み込んでも表示されるはずですが、注意点があります

  1. 測地系・座標系は考慮していない、したがって同じ測地系・座標系しかoverlayできない。*.prjファイルがないとおかしな表示になると思います。
  2. 先に読み込んだshape fileから順番にlayerを重ねていくので、下のlayerが隠れてしまう。

などの注意点があります。
これは今後layerの移動については、是非とも必要な機能ですね。

今回のまとめ

MapWinGIS.ocx version4.8.6を登録して簡単なshape file viewerを作成しました。
このMap controlを利用すれば、codeingが簡単にできました。

vb.netd2010だけでなく、C#でも、visual studio2008でも動作することができます。
簡単に自前のソフトウェアにちょっとしたGIS機能を付け加えるには良いと思います。
source code sampleです。活用してください。
ただし、あくまでの自分用のサンプルですので、バグなどがある可能性が高いので、原文のままの使用は避けてください。

サンプルコードを使って、お使いのPCの不具合が生じても一切責任は持てませんので、あくまでも自己責任にて使用してください。

Exercise

今回のProgramを少し発展させて以下の項目を付け加えてみてください。

  1. Remove Layerで表示しているlayerを削除するsubroutineを付け加える。
  2. Treeviewの並び替えを行い、AxMap1のLayer表示順を変更する。

参考となる資料は、MapWinGIS documentationを活用すると良いでしょう。

Social Widgets powered by AB-WebLog.com.