gridデータの読み込みexample
[Chapter 3-2]では[Chapter 3-1]で作成したプログラムに、grid format dataを追加していきます。
raster imageやdemファイルなどをgridデータとして読み込む場合に使用します。
ここで、MapWInGISで取り扱うことができるdata fromatをおさらいしておきます。
詳しくは、MapWinGIS Overviewを見てください。
data format
です。
多くのgrid data formatに対応しています。
今回は、gridデータとして、rasterと同じ arc、img、tifの3種類を読み込んで描画してみます。
Program開発環境は、Visual Basic.NET 2010です。
Visual Sutdioの使い方は、一通り問題なく操作でき、一度はGUIのprogramを作成したことがある中級者を対象にしています。 したがって、vb.netの操作に関しては、端折ります。
Map controlの登録
Map Controlの登録については、[Chapter 1]を見てください。
Program Coding
まずは、From designをして、Codingします。
Form Design
Form designはiconを使ってtoolbarにsettingします。
こんな感じ、
Form Design
AxMap1のプロパティの変更
CursorMode → cmNone
MapUnits → umDecimalDegrees (サンプルの神奈川県のshape fileがJGD2000世界測地系)
SendMouseDown → True
SendMouseMove → True
SendMouseUp → false
ここで、mouse eventを利用するには、必ずSendMouse~~をtrueにしてください。falseではeventを捕まえることができません。
参照の追加
gridファイルとしてrasterを取り扱う場合、dllを参照追加します。
追加するdllは、MapWinUtility.dllです。
参照先は、MapWindowをインストールした場所にあります。defaultでは、C:¥Program Files¥MapWindowのなかにあります。
Coding
ToolStrip1
Grid layersを以下のようにします。
Private Sub ToolStripButton3_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton3.Click 'add grid layer Dim imgfile As MapWinGIS.Image Dim openDlg As OpenFileDialog = New OpenFileDialog() Dim handle As Integer Dim ext As String = "" 'Create an instance for the grid raster Dim grid As New MapWinGIS.Grid() 'Create a new grid header object Dim gridHeader As New MapWinGIS.GridHeader() 'initialize dialog openDlg.Filter = "Supported Raster Formats|*.asc;*.img;*.tif|ASCII Grid Images (*.asc)|*.asc|Erdas Images (*.img)|*.img|GeoTIF (*.tif)|*.tif" openDlg.CheckFileExists = True If (openDlg.ShowDialog(Me) = DialogResult.OK) Then 'get the extension of the file ext = System.IO.Path.GetExtension(openDlg.FileName) If (ext = ".tif" Or ext = ".asc" Or ext = ".img") Then 'Open the grid If (ext = ".tif") Then grid.Open(openDlg.FileName, MapWinGIS.GridDataType.DoubleDataType, False, MapWinGIS.GridFileType.GeoTiff, Nothing) ElseIf (ext = ".asc") Then grid.Open(openDlg.FileName, MapWinGIS.GridDataType.DoubleDataType, False, MapWinGIS.GridFileType.Ascii, Nothing) ElseIf (ext = ".img") Then grid.Open(openDlg.FileName, MapWinGIS.GridDataType.DoubleDataType, False, MapWinGIS.GridFileType.Esri, Nothing) End If 'Get the header of the raster gridHeader = grid.Header 'Create a raster grid color scheme Dim gridScheme As New MapWinGIS.GridColorScheme() 'Assign the no data color to lack gridScheme.NoDataColor = System.Convert.ToUInt32( System.Drawing.ColorTranslator.ToOle(Color.Transparent)) 'Set the attributes of the color scheme gridScheme.UsePredefined(CDbl(grid.Minimum), CDbl(grid.Maximum), MapWinGIS.PredefinedColorScheme.SummerMountains) 'Convert the grid to an image 'Create a Utili instance Dim mapUtil As New MapWinGIS.Utils() imgfile = New MapWinGIS.ImageClass() 'and use it to convert the grid to the image imgfile = mapUtil.GridToImage(grid, gridScheme, Nothing) 'Finally add the layer to the map handle = AxMap1.AddLayer(imgfile, True) rootNode.Nodes.Add(System.IO.Path.GetFileNameWithoutExtension(grid.Filename)) End If End If End Sub
shape fileの読み込みと同じような流れになっています。
-
MapWinGIS.grid objectを作成します。
-
gridデータとして arc、img、tifの3種類のみ選択できるようにopenDialogを設定します。
-
grid.Openでrasterをgridデータとして読み込みます。
-
colorschemeを設定します。
-
Dim mapUtil As New MapWinGIS.Utils()でmapUtilをinstanceします。
-
MapWinGIS.ImageClassをnewでimgfileを作成する。
-
myUtil.GridToImageでgridをimagefileに変換します。
-
AxMap1.AddLayer(imgfile, True)で描画する。
これだけでrasterをgridとして読み込んで表示されます。
Programの実行
ex03のprojectを保存後、実行してみましょう。
Grid Layerをクリックするとファイル選択ダイアログが表示されます。
拡張子がasc,img,tifだけを選択でいるようになっています。他のgrid formatを読み込む場合は、追加してください。
サンプルの神奈川県の標高rasterの(tif)を読み込んでみます。
このサンプルtifはGeotiffですので、緯度経度情報を持っているため、mousemoveで緯度経度が変わります。
また、shape fileのoverlayすることができます。
神奈川県の標高rasterの(tif)を読み込んだ例
サンプルの神奈川県の標高値のASCII Grid Images (asc)を読み込んでみます。
ASCII Grid Imagesはgridデータとしても読み込むことができますが、ASCIIデータですので、editorで中身を見ることができます。 simulationの解析結果などで私がよく使うformatです。
これも、座標情報を持っているため、shape fileのoverlayすることができます。
神奈川県の標高値のASCII Grid Images (asc)を読み込んだ例
今回追加したprogramで、gridScheme.NoDataColorでNoDataの場合は透過するようにしていますが、うまく機能してません。
これは、ちょっと調べてみる必要がありますね。
今回のまとめ
-
rasterをgridデータして読み込んで描画する。
-
gridcolorschemeでcolorの設定をする。
-
その他機能の追加
vb.netd2010だけでなく、C#でも、visual studio2008でも動作することができます。
簡単に自前のソフトウェアにちょっとしたGIS機能を付け加えるには良いと思います。
今回は 追加部分が少ないのでsource code sampleはありません。raster dataと同じサンプルデータを使用しています。
サンプルコードあるいはサンプルデータを使って、お使いのPCの不具合が生じても一切責任は持てませんので、あくまでも自己責任にて使用してください。
Exercise
今回のProgramを少し発展させて以下の項目を付け加えてみてください。
-
rasterの個別属性値を取得してみる。
-
他のraster formatにも対応してみる。
-
gridColorschemeを自前で用意してみる。
参考となる資料は、MapWinGIS documentationを活用すると良いでしょう。
最近のコメント