Category Archives: vb.net - Page 8

簡単なGeoProcessingのexample [Chapter 9-5]

簡単なGeoProcessingのexample

今回は、Unionを作成してみます。Unionは2つのShape Fileの結合してoverlay部分を分割抽出する機能です。イメージとしては、Unionな感じです。
GeoProcessing(空間解析)について、ArcGIS Tipsにまとめられていますので、一読してください。

元になるprogramは、簡単なGeoProcessingのexample [Chapter 9-1]のex09に付け加えていきます。

Program Coding

今回は、簡単なGeoProcessingのexample [Chapter 9-1]に若干designを付け加えます。
併せてUnion作成の設定formを作成します。
Union作成の設定formの作成は、カテゴリー分類example [Chapter 6-2]のカテゴリー分類の設定formの作成と同様な方法で作成します。

Form design

Main Form

Main Form designはiconを使ってtoolbarにsettingします。ex09そのまんまですね。
こんな感じ、

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image01.jpg
ToolStripContainerを使って、ToolStripbarを追加しています。

次に、Union作成の設定formを作成します。

Union作成の設定Form

Main Formから呼び出すUnion作成の設定Form designはこんな感じ、

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image01.jpg
Union作成の設定Form

Coding

それでは、Union作成の設定Form(frmUnion)からcodingを始めていきます。
Unionの設定で必要なデータは、

  1. 対象のShape file
  2. Unionに使うPolygon Shape file
  3. 出力ファイル名

が必要になります。

frmUnion load時

まずは、参照の追加で、

  1. MapWinUtility.dll
  2. MapWindow.Interfaces.dll

を追加しておきます。

Objectをimportsして、変数などの定義をします。

Imports System.IO
 
Imports MapWindow.Interfaces
Imports System.Windows.Forms
Imports MapWinUtility
 
Public Class frmUnion
 
    Public t_sf As New MapWinGIS.Shapefile()
    Public t_map As New AxMapWinGIS.AxMap
    Public t_legend As New LegendControl.Legend
 
End Class

Form load時のcodeingは、

    Private Sub frmUnion_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
        If Not t_sf Is Nothing Then
            txtLayerName.Text = t_sf.Filename
 
            If txtOutFileName.Text = "" Then
                txtOutFileName.Text = Path.GetDirectoryName(t_sf.Filename) & "" & _
                    Path.GetFileNameWithoutExtension(t_sf.Filename) & _
                    "_Union.shp"
            End If
        End If
 
        PopulateLayerList(cmbxClipWith)
    End Sub

Union解析に使用するShape fileはt_sfでMain formから受け渡されます。
また、出力Shapefile名もdefaultで”_Union.shp”をつけるようにしておきます。
PopulateLayerList(cmbxClipWith)で、Polygon Shape fileのLayernameをComboBoxに入れます。

Unionの作成

Union作成はOK buttonのclick eventに実装します。Codeは、

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        If txtLayerName.Text = String.Empty Then
            Logger.Msg("Union Layerが選択されていません。")
        ElseIf cmbxClipWith.Text = String.Empty Then
            Logger.Msg("Union Layerが選択されていません。")
        ElseIf txtOutFileName.Text = String.Empty Then
            Logger.Msg("出力ファイル名が指定されていません。")
        ElseIf File.Exists(txtOutFileName.Text) Then
            Logger.Msg("選択したファイル名は既に存在します。" & vbCrLf & _
                       "他のファイル名に変更してください。")
        Else
            If Not t_sf Is Nothing Then
                Cursor.Current = Cursors.WaitCursor
                'Clip
                Dim sfOverlay As New MapWinGIS.Shapefile
                sfOverlay = t_map.get_GetObject(cmbxClipWith.SelectedIndex)
                t_sf.GeometryEngine = IIf(chkUseClipper.Checked,
                                          MapWinGIS.tkGeometryEngine.engineClipper,
                                          MapWinGIS.tkGeometryEngine.engineGeos)
 
                Dim result As MapWinGIS.Shapefile
                result = t_sf.Union(False, sfOverlay, False)
                If Not result Is Nothing Then
                    If Not result.SaveAs(txtOutFileName.Text, Nothing) Then
                        MapWinUtility.Logger.Msg(result.ErrorMsg(result.LastErrorCode))
                    End If
                    result.Close()
                    'Add Union Shape file
                    If chkbxAddResults.Checked And
                        System.IO.File.Exists(txtOutFileName.Text) Then
 
                        Dim shpfile As New MapWinGIS.Shapefile
 
                        shpfile.Open(txtOutFileName.Text)
 
                        'add the shapefile to the map and legend
                        Dim handle As Integer = t_legend.Layers.Add(shpfile, True)
 
                        'set the layer name
                        t_legend.Map.LayerName(handle) =
                            System.IO.Path.GetFileNameWithoutExtension(shpfile.Filename)
 
                    End If
                    Cursor.Current = Cursors.Default
                    sfOverlay = Nothing
                    Me.Close()
                Else
                    MapWinUtility.Logger.Msg("ユニオン作成に失敗しました。" & _
                                             vbNewLine & t_sf.ErrorMsg(t_sf.LastErrorCode))
                End If
            End If
        End If
    End Sub

Unionの大まかな流れは、

  1. 変数類のチェック
  2. Unionのpolygonの選択
  3. Union作成
  4. UnionのShape fileの保存と追加

となります。

UnionのStatementは、
Union(False, sfOverlay, False)です。

詳細は、MapWinGIS Activex GeoProcessingを参考にしてください。

Union作成を抜粋すると、
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image08.jpg
となっています。

また、Geometry engineを選択することもできるようです。
GeometryEngineの設定は、
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image08.jpg
となっています。

このUnion作成の設定Formを次のMain Formから呼び出します。

Main Formのcodeing

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex094_image01.jpg
Unionの設定のbutton

Union作成の設定のbutton click event発生時のcodingは、

    Private Sub UnionToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UnionToolStripMenuItem.Click
        'set Union
        If AxMap1.NumLayers > 0 And
            (Legend1.Layers(layerHandle).Type = MapWindow.Interfaces.eLayerType.PointShapefile Or
             Legend1.Layers(layerHandle).Type = MapWindow.Interfaces.eLayerType.LineShapefile Or
             Legend1.Layers(layerHandle).Type = MapWindow.Interfaces.eLayerType.PolygonShapefile) Then
 
            Dim frmUnion As New frmUnion()
 
            frmUnion.Text = "ユニオンの作成:選択Layer名=[" & Legend1.Map.LayerName(layerHandle) & "]"
            frmUnion.t_sf = AxMap1.get_GetObject(layerHandle)
            frmUnion.t_map = AxMap1
            frmUnion.t_legend = Legend1
 
            frmUnion.ShowDialog(Me)
        End If
    End Sub

AxMap1のobjectを引き渡します。

Programの実行

では、ex095を保存して実行してみましょう。

今回は、神奈川県のサンプルc14_regionDD.shpとtestShape.shpを読み込んで表示させます。
ツール→Unionを表示させます。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image02.jpg
Intersect表示画面

Union作成formの表示して、Union作成に使うShape Layernameを選択します。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image03.jpg
Union作成formの表示

Unionのformの表示して、パラメータの設定が終了したところ。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image04.jpg
Unionパラメータの設定完了の表示

Union実行すると、Layerが追加された状態。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image05.jpg
Unionの結果

Union実行後、Unionの追加Layerのみを表示する。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image06.jpg
UnionのLayerのみ表示

Union実行後、Attribute Tableを表示して確認表示する。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex095_image09.jpg
UnionのLayerのAttribute表示

今回のまとめ

どうでしょうか? 簡単に作成できましたね。

では、今回のまとめを

  1. Union作成のcodeingの実装を行いました。
  2. 別Formを作成して、Unionの設定表示を行いました。

[Chapter 9]では、他のClipを追加してみます。

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

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

最後に、Layerを選択して次の操作を行うことを忘れないように。

Exercise

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

  1. 選択したShape featureだけにUnion作成できるようにcodeを追加する。
  2. GeoProcessingのform作成Classを作成してみる。

Social Widgets powered by AB-WebLog.com.