Category Archives: MapWinGIS - Page 9

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

簡単なGeoProcessingのexample

今回は、VectorデータのPolygonを用いたDissolve解析を作成してみます。
Dissolveは、対象とするPolygonの任意の属性値をもとに集約する機能です。空間解析について、ArcGIS Tipsにまとめられていますので、一読してください。

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

Program Coding

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

呼び出すDissolve作成の設定formを次に作成します。

Dissolve作成の設定Form

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

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image02.jpg
Dissolve作成の設定Form

Coding

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

  1. 元になるShape file
  2. 属性値のField名
  3. 出力Shape file

が必要になります。

frmDissolve load時

まずは、参照の追加で、

  1. MapWinUtility.dll

を追加しておきます。

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

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

Form load時のcodeingは、

    Private Sub frmDissolve_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) & _
                    "_Dissolve.shp"
            End If
        End If
 
        PopulateFieldList(cmbField)
    End Sub

Dissolve解析に使用するShape fileはt_sfでMain formから受け渡されます。
また、出力Shapefile名もdefaultで”_Dissolve.shp”をつけるようにしておきます。
PopulateFieldList(cmbField)で、選択したShape fileのField名をComboBoxに入れます。

Dissolveの作成

Dissolve作成は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("Layerが選択されていません。")
        ElseIf cmbField.Text = String.Empty Then
            Logger.Msg("Fieldが選択されていません。")
        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
                'Dissolve
                t_sf.GeometryEngine = IIf(chkUseClipper.Checked,
                                          MapWinGIS.tkGeometryEngine.engineClipper,
                                          MapWinGIS.tkGeometryEngine.engineGeos)
 
                Dim result As MapWinGIS.Shapefile
                result = t_sf.Dissolve(cmbField.SelectedIndex, 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 Dissolve 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
                    Me.Close()
                Else
                    MapWinUtility.Logger.Msg("ディソルブ作成に失敗しました。" & _
                                             vbNewLine & t_sf.ErrorMsg(t_sf.LastErrorCode))
                End If
            End If
        End If
    End Sub

Dissolveの作成の大まかな流れは、

  1. 変数類のチェック
  2. 基準となるFieldIndex
  3. Dissolve作成
  4. Dissolve Shape fileの保存と追加

となります。

Dissolveの作成のStatementは、
Dissolve(cmbField.SelectedIndex, False)です。

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

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

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

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

Main Formのcodeing

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image01.jpg
Dissolve作成の設定のbutton

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

    Private Sub DissolveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DissolveToolStripMenuItem.Click
        'set Dissolve
        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 frmDissolve As New frmDissolve()
 
            frmDissolve.Text = "ディソルブの作成:選択Layer名=[" & Legend1.Map.LayerName(layerHandle) & "]"
            frmDissolve.t_sf = AxMap1.get_GetObject(layerHandle)
            frmDissolve.t_legend = Legend1
 
            frmDissolve.ShowDialog(Me)
        End If
    End Sub

一応、Shape fileのtypeでチェックして、t_sfとt_legendのobjectを引き渡します。

Programの実行

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

今回は、神奈川県のサンプルc14_region.shpを読み込んで表示させます。c14_region.shpは神奈川県市区町村データでPolygonデータです。
ツール→Dissolve表示させます。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image03.jpg
Dissolve表示画面

Dissolve作成formの表示して、Dissolve作成に使う属性値Fieldを選択します。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image04.jpg
Dissolve作成formの表示

Dissolve作成後の、Map上に追加されて、三浦市を拡大した状態です。選択すると三浦市の島部分がマルチパートのpolygonとなっています。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image06.jpg
Dissolve作成表示例

比較のため、Dissolve作成前の、三浦市を拡大した状態です。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex092_image05.jpg
Dissolve作成前の表示例

今回のまとめ

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

では、今回のまとめを

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

[Chapter 9]では後2回に分けて、他のよく使いそうなGeoProcessingの作成してみたいと考えています。空間検索と、Intersect、Clip、Unionなどを考えています。

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

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

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

Exercise

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

  1. 選択したShape featureだけにDissolveできるようにcodeを追加する。
  2. 他のGeoProcessingに挑戦してみる。MapWinGIS Activex GeoProcessingを参考にしてください。
  3. Dissolve作成の元ファイルを、自由に選択できるようにComboBoxを配置して処理できるようにCodeを追加する。

Social Widgets powered by AB-WebLog.com.