rasterデータの読み込みexample
[Chapter 3]では[Chapter 2]で作成したプログラムに、raster format dataの追加を2回に分けて追加していきます。
標高値や航空写真等のrasterを読み込めるで、shape fileとoverlayできれば、GIS Viewerとして十分に利用することができます。
ここで、MapWInGISで取り扱うことができるdata fromatをおさらいしておきます。
詳しくは、MapWinGIS Overviewを見てください。
data format
です。
多くのraster grid data formatに対応しています。
今回は、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を捕まえることができません。
Coding
ToolStrip1
Image layersを以下のようにします。
Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click 'add raster layer Dim imgfile As MapWinGIS.Image Dim openDlg As OpenFileDialog = New OpenFileDialog() Dim handle As Integer Dim ext As String = "" 'initialize dialog openDlg.Filter = "Supported raster Formats|*.asc;*.img;*.tif|ASCII Grid Images (*.asc)|*.asc|Erdas Images (*.img)|*.img|TIF (*.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 imgfile = New MapWinGIS.ImageClass() 'open the raster file imgfile.Open(openDlg.FileName) handle = AxMap1.AddLayer(imgfile, True) AxMap1.Redraw() rootNode.Nodes.Add(System.IO.Path.GetFileNameWithoutExtension(imgfile.Filename)) End If End If End Sub
shape fileの読み込みと同じような流れになっています。
-
MapWinGIS.Image objectを作成します。
-
raster arc、img、tifの3種類のみ選択できるようにopenDialogを設定します。
-
MapWinGIS.ImageClassをnewでimgfileを作成する。
-
imgfile.openで読み込む。
-
AxMap1.AddLayer(imgfile, True)で描画する。
これだけでrasterが表示されます。が、AxMap1上でmouseを動かすとエラーになります。
このエラーを回避するために、mousemove eventを修正します。
mousemove event 修正
AxMap1_MouseMoveEventの修正を行います。
vector ore rasterの判断をして、座標値の取得を行うように修正します。
Private Sub AxMap1_MouseMoveEvent(sender As System.Object, e As AxMapWinGIS._DMapEvents_MouseMoveEvent) Handles AxMap1.MouseMoveEvent Dim handle As Integer = 0 'first read layer Dim layer As Object = AxMap1.get_GetObject(handle) Dim type As String = String.Empty Dim sf As MapWinGIS.Shapefile Dim mf As MapWinGIS.Image Dim ex As New MapWinGIS.Extents Dim xProjected As Double, yProjected As Double If TryCast(layer, Shapefile) IsNot Nothing Then ' Shapefile If AxMap1.NumLayers > 0 Then 'get the shapefile object sf = AxMap1.get_GetObject(handle) End If Else ' Imagefile If AxMap1.NumLayers > 0 Then 'get the shapefile object mf = AxMap1.get_GetObject(handle) 'test to see if any shapes for this area lie withing the seleted bounds End If End If 'test to see if any shapes for this area lie withing the seleted bounds AxMap1.PixelToProj(e.x, e.y, xProjected, yProjected) ex.SetBounds(xProjected, yProjected, 0, xProjected, yProjected, 0) ToolStripStatusLabelx.Text = Format(xProjected, "###.######") ToolStripStatusLabely.Text = Format(yProjected, "###.######") End Sub
-
handle=0で一番最初に呼び出したファイルを基準とする。
-
TryCast(layer, Shapefile) IsNot Nothing Then以下でshape fileの場合の処理
-
else以下でraster fileの場合の処理
-
ToolStripStatusLabelx、ToolStripStatusLabelyに緯度経度表示
Programの実行
ex03のprojectを保存後、実行してみましょう。
Image Layersをクリックするとファイル選択ダイアログが表示されます。
拡張子がasc,img,tifだけを選択でいるようになっています。
サンプルの神奈川県の標高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)を読み込んだ例
今回のまとめ
-
rasterを読み込んで描画する。
-
AxMap1.get_GetObjectを使って、shape file or raster fileの評価をする。
-
その他機能の追加
vb.netd2010だけでなく、C#でも、visual studio2008でも動作することができます。
簡単に自前のソフトウェアにちょっとしたGIS機能を付け加えるには良いと思います。
今回は 追加部分が少ないのでsource code sampleはありません。raster dataのサンプルを用意しましたので、参考にしてください。
サンプルコードあるいはサンプルデータを使って、お使いのPCの不具合が生じても一切責任は持てませんので、あくまでも自己責任にて使用してください。
Exercise
今回のProgramを少し発展させて以下の項目を付け加えてみてください。
-
rasterの個別属性値を取得してみる。
-
他のraster formatにも対応してみる。
参考となる資料は、MapWinGIS documentationを活用すると良いでしょう。
Comments are closed.