Category Archives: Windows - Page 100

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

簡単なGeoProcessingのexample

今回で簡単なGeoProcessing[Chapter 9]は終了します。最後にClipを作成してみます。Clipは2つのShape Fileをoverlay部分くり抜く機能です。イメージとしては、Clipな感じです。
GeoProcessing(空間解析)について、ArcGIS Tipsにまとめられていますので、一読してください。

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

Program Coding

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

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

Clip作成の設定Form

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

blog.godo-tys.jp_wp-content_gallery_mapwingis_ex096_image01.jpg
Clip作成の設定Form

Coding

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

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

が必要になります。

frmClip 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 frmClip_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) & _
                    "_Clip.shp"
            End If
        End If
 
        PopulateLayerList(cmbxClipWith)
    End Sub

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

Clipの作成

Clip作成は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("Clip Layerが選択されていません。")
        ElseIf cmbxClipWith.Text = String.Empty Then
            Logger.Msg("Clip 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.Clip(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 Clip 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

Clipの大まかな流れは、

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

となります。

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

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

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

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

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

Main Formのcodeing

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

Clip作成の設定の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の実行

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

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

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

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

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

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

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

今回のまとめ

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

では、今回のまとめを

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

ここで、気づいたと思うのですが、基本的にIntersect,Union,ClipはほとんどCodeingが同じで、それぞれの作成時statementが違うだけです。
これは、一つにまとめてClassとして作成する方が後のメンテナンスには良いですね。

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

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

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

Exercise

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

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

Social Widgets powered by AB-WebLog.com.