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

簡単なGeoProcessingのexample

GeoProcessingとは一般的には空間解析と呼ばれるもので、位置情報の空間データを分析することを「空間解析」と呼んでいます。
今回は、Vectorデータを用いたBuffer解析を作成してみます。
Bufferは空間解析の中でも多用されるツールです。例えば,駅勢圏を設定するための同心円状のポリゴンや,道路からの距離をポリゴンで表現するためなどに利用されます。空間解析について、ArcGIS Tipsにまとめられていますので、一読してください。

元になるprogramは、印刷と保存example [Chapter 8-2]のex08に付け加えていきます。

Program Coding

今回は、印刷と保存example [Chapter 8-2]に若干designを付け加えます。
併せてBuffer作成の設定formを作成します。
Buffer作成の設定formの作成は、カテゴリー分類example [Chapter 6-2]のカテゴリー分類の設定formの作成と同様な方法で作成します。

Form design

Main Form

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

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

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

Buffer作成の設定Form

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

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image03.jpg
Buffer作成の設定Form

Coding

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

  1. 元になるShape file
  2. Bufferの距離
  3. 出力Shape file

が必要になります。

frmBuffer load時

まずは、参照の追加で、

  1. MapWinGeoProc.dll
  2. MapWinUtility.dll

を追加しておきます。
プロジェクトの参照は、
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image09.jpg
な感じになります。

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

Imports System
Imports System.IO
 
Imports AxMapWinGIS
Imports MapWinGIS
Imports MapWinUtility
Imports MapWinGeoProc
Imports MapWindow.Interfaces
 
Public Class frmBuffer
 
    Public t_sf As New MapWinGIS.Shapefile()
    Public t_legend As New LegendControl.Legend
 
    Private m_units As MapWindow.Interfaces.UnitOfMeasure = UnitOfMeasure.Unknown
End Class

Form load時のcodeingは、

    Private Sub frmBuffer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
        If Not t_sf Is Nothing Then
            '元ファイル名
            txtLayerName.Text = t_sf.Filename
 
            '単位の設定
            Dim prj As String = t_sf.GeoProjection.ExportToProj4()
            If prj <> "" Then
                Dim units As MapWindow.Interfaces.UnitOfMeasure =
                    MapWinGeoProc.UnitConverter.GetShapefileUnits(prj)
                cmbUnits.DataSource =
                    [Enum].GetNames(GetType(MapWindow.Interfaces.UnitOfMeasure))
                If m_units = UnitOfMeasure.Unknown Then
                    cmbUnits.SelectedIndex = Convert.ToInt32(units)
                Else
                    cmbUnits.SelectedIndex = Convert.ToInt32(m_units)
                End If
            Else
                cmbUnits.Items.Add("Map units")
                cmbUnits.SelectedIndex = 0
            End If
 
            '出力ファイルの設定
            If txtOutFileName.Text = "" Then
                txtOutFileName.Text = Path.GetDirectoryName(t_sf.Filename) & "" & _
                    Path.GetFileNameWithoutExtension(t_sf.Filename) & _
                    "_Buffer.shp"
            End If
        End If
    End Sub

Buffer解析に使用するShape fileはt_sfでMain formから受け渡されます。
prjファイルから座標系を抽出し、緯度経度か平面座標かの取得をしてMapUnitをcmbUnitsに追加していきます。
また、出力Shapefile名もdefaultで”_Buffer.shp”をつけるようにしておきます。

Bufferの作成

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

    Private Sub btnOk_Click(sender As System.Object, e As System.EventArgs) Handles btnOk.Click
 
        Dim distance As Double
        If Not Double.TryParse(txtDistance.Text, distance) Then
            MapWinUtility.Logger.Msg("バッファ距離が未入力です。")
        ElseIf distance = 0.0 Then
            MapWinUtility.Logger.Msg("バッファ距離が指定されていません")
        ElseIf txtOutFileName.Text = String.Empty Then
            MapWinUtility.Logger.Msg("出力ファイル名が指定されていません。")
        ElseIf File.Exists(txtOutFileName.Text) Then
            MapWinUtility.Logger.Msg("選択したファイル名は既に存在します。" & vbCrLf & _
                                     "他のファイル名に変更してください。")
        Else
 
            If Not t_sf Is Nothing Then
                '単位系の変換
                If cmbUnits.Items.Count > 1 Then
                    Dim source As MapWindow.Interfaces.UnitOfMeasure =
                        CType(cmbUnits.SelectedIndex, MapWindow.Interfaces.UnitOfMeasure)
                    Dim target As MapWindow.Interfaces.UnitOfMeasure =
                        MapWinGeoProc.UnitConverter.
                        GetShapefileUnits(t_sf.GeoProjection.ExportToProj4())
                    If target = UnitOfMeasure.Unknown Then target = source
                    If source <> target Then
                        distance = (MapWinGeoProc.UnitConverter.
                                    ConvertLength(source, target, distance))
                    End If
                End If
 
                'Buffer作成
                '結果は、resultに作成
                Dim result As MapWinGIS.Shapefile
                result = t_sf.BufferByDistance(distance, CInt(udSegments.Value), False, True)
                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 Buffer Shape file
                    If chkbxAddResults.Checked And
                        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) =
                            Path.GetFileNameWithoutExtension(shpfile.Filename)
 
                    End If
                    Me.Close()
                Else
                    MapWinUtility.Logger.Msg("バッファ作成に失敗しました。" & _
                                             vbNewLine & t_sf.ErrorMsg(t_sf.LastErrorCode))
                End If
            End If
        End If
    End Sub

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

  1. 変数類のチェック
  2. BUffer距離の単位変換
  3. Buffer作成
  4. Buffer Shape fileの保存と追加

となります。

Bufferの作成のStatementは、
BufferByDistance(distance, CInt(udSegments.Value), False, True)です。
udSegments.ValueはBuffer作成時のsegmentの曲率?なのでしょうかね。 変更してもあまり変化がないです。

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

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

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

Main Formのcodeing

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image02.jpg
Buffer作成の設定のbutton

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

    Private Sub BufferToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BufferToolStripMenuItem.Click
        'set BufferNew
        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 frmBuffer As New frmBuffer()
 
            frmBuffer.Text = "バッファの作成:選択Layer名=[" & Legend1.Map.LayerName(layerHandle) & "]"
            frmBuffer.t_sf = AxMap1.get_GetObject(layerHandle)
            frmBuffer.t_legend = Legend1
 
            frmBuffer.ShowDialog(Me)
        End If
    End Sub

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

Programの実行

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

今回は、神奈川県のサンプルc14_railroad.shpを読み込んで表示させます。c14_railroad.shpは神奈川県近郊の鉄道データでPolyLineデータです。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image04.jpg
神奈川県のサンプルshape file表示

ツール→Buffer表示させます。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image05.jpg
Buffer表示画面

Buffer作成formの表示して、距離、segmentと出力ファイル名を設定します。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image06.jpg
Buffer作成formの表示

Buffer作成後の、Map上に追加されて、拡大した状態です。
blog.godo-tys.jp_wp-content_gallery_mapwingis_ex09_image07.jpg
Buffer作成表示例

今回のまとめ

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

では、今回のまとめを

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

後[Chapter 9]では数回に分けて、他のよく使いそうなGeoProcessingの作成してみたいと考えています。

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

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

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

Exercise

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

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

Comments are closed.

Social Widgets powered by AB-WebLog.com.