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

簡単なGeoProcessingのexample

今回は、Intersectを作成してみます。Intersectは2つのShape Fileの交差した部分を抽出する機能です。GeoProcessing(空間解析)について、ArcGIS Tipsにまとめられていますので、一読してください。

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

Program Coding

今回は、簡単なGeoProcessingのexample [Chapter 9-1]に若干designを付け加えます。
併せてInterSect作成の設定formを作成します。
InterSect作成の設定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を追加しています。

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

InterSect作成の設定Form

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

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex094_image02.jpg
InterSect作成の設定Form

Coding

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

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

が必要になります。

frmIntersect load時

まずは、参照の追加で、

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

を追加しておきます。

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

Imports System.IO
 
Imports MapWindow.Interfaces
Imports System.Windows.Forms
Imports MapWinUtility
 
Public Class frmIntersect
 
    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 frmIntersect_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) & _
                    "_Intersect.shp"
            End If
        End If
 
        PopulateLayerList(cmbxClipWith)
 
    End Sub

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

Intersetの作成

Intersect作成は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("Intersect Layerが選択されていません。")
        ElseIf cmbxClipWith.Text = String.Empty Then
            Logger.Msg("Intersect Layerが選択されていません。")
        ElseIf txtOutFileName.Text = String.Empty Then
            Logger.Msg("出力ファイル名が指定されていません。")
        ElseIf File.Exists(txtOutFileName.Text) Then
            Logger.Msg("選択したファイル名は既に存在します。" & vbCrLf & _
                       "他のファイル名に変更してください。")
            'ElseIf Not t_legend.Layers(cmbxClipWith.SelectedIndex).Type =
            'MapWindow.Interfaces.eLayerType.PolygonShapefile Then
            'Logger.Msg("Clip LayerがPolygonではありません。")
        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.GetIntersection(False, sfOverlay, False, MapWinGIS.ShpfileType.SHP_NULLSHAPE)
                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 Intersection	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

Intersectの大まかな流れは、

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

となります。

IntersectのStatementは、
GetIntersection(False, sfOverlay, False, MapWinGIS.ShpfileType.SHP_NULLSHAPE)です。

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

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

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

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

Main Formのcodeing

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

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

    Private Sub IntersectToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IntersectToolStripMenuItem.Click
        'set Intersect
        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 frmIntersect As New frmIntersect()
 
            frmIntersect.Text = "インターセクトの作成:選択Layer名=[" & Legend1.Map.LayerName(layerHandle) & "]"
            frmIntersect.t_sf = AxMap1.get_GetObject(layerHandle)
            frmIntersect.t_map = AxMap1
            frmIntersect.t_legend = Legend1
 
            frmIntersect.ShowDialog(Me)
        End If
    End Sub

AxMap1のobjectを引き渡します。

Programの実行

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

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

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

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

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

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

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

今回のまとめ

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

では、今回のまとめを

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

[Chapter 9]では、他のよく使いそうなGeoProcessingの作成してみたいと考えています。Intersect、Clip、Unionなどを考えています。

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

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

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

Exercise

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

  1. 選択したShape featureだけにIntersectできるようにcodeを追加する。
  2. 他のGeoProcessingに挑戦してみる。MapWinGIS Activex GeoProcessingを参考にしてください。

Comments are closed.

Social Widgets powered by AB-WebLog.com.