DotSpatialを使ってみる。
1年以上前から、Dotspatialを使いたいと考えていたのですが、version 1.4でAttibuteがやっと文字化けしなくなっていた。version 1.3までは、どうやってもできなかった。
ちなみに、Dotspatialとは、.Net 4.0環境で動くGISアプリを作成するためのLibrary群です。
詳しくは、DotSpatialからダウンロードできます。
なんでもできそうですし、できるようです。
DotSpatial 1.4 Minimal.zipをダウンロードして、解凍するとDemoApp.exeがあるので試してみることができます。
GISアプリを作成
せっかくなので、DotspatialのClass libraryをVB.NET2010で登録して、超~簡単なGISアプリを作成してみました。
tutorialも豊富なので、あえてここでは書きませんが、tutorialの1から9を参考すれば、結構なアプリが作れそうです。
ますは、上記HPのDotSpatial_Tutorial_1.docxを参考にして作成します。
dllの参照設定については、事前にDotSpatial_Tutorial_1.docxですませておいてくださいね。
Form Design
Formのdesignは、MapWinGISのtutorialで使ったいるものを流用して、
な感じにします。
legendも標準で、Toolboxなども標準でありますし、LayoutMapもありますし、一応何でもそろっているので、簡単にGISアプリの作成ができます。ただし、英語です。
Sourceからcompileすれば、日本語化もOKでしょうけど、ちょっと大変そうですね。
操作をする上で、それほど難解な英語ではないので、大丈夫ですね。
Coding
たとえば、shape fileを読み込んでmapとlegendに表示させるには、たった1行です。
Map1.AddLayer()
拡大モードにするには、たった1行です。
Map1.FunctionMode = FunctionMode.ZoomIn
なんだか拍子抜けするくらい簡単です。
サンプルとしてCodeを載せます。
Imports DotSpatial.Data Imports DotSpatial.Controls Imports DotSpatial.Symbology Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) End Sub Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click 'AddLayer() method is used to add a shapefile in to mapcontrol Map1.AddLayer() End Sub Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click 'Clear() method is used to clear the shapelayers from mapcontrol Map1.Layers.Clear() End Sub Private Sub ToolNoneBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolNoneBtn.Click 'None function is used to change the mouse cursor to default Map1.FunctionMode = FunctionMode.None End Sub Private Sub ToolZoominBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolZoominBtn.Click 'ZoomIn function is used to ZoomIn the map Map1.FunctionMode = FunctionMode.ZoomIn End Sub Private Sub ToolZoomoutBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolZoomoutBtn.Click 'ZoomIn function is used to ZoomOut the map Map1.FunctionMode = FunctionMode.ZoomOut End Sub Private Sub ToolPanBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolPanBtn.Click 'Pan function is used to pan the map Map1.FunctionMode = FunctionMode.Pan End Sub Private Sub ToolFullExtentBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolFullExtentBtn.Click 'ZoomToMaxExtent method is used to Extent the shape file Map1.ZoomToMaxExtent() End Sub Private Sub ToolIdentityBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolIdentityBtn.Click 'Info function is used to get the information of the selected shape Map1.FunctionMode = FunctionMode.Info End Sub Private Sub ToolUnselectBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolUnselectBtn.Click 'Select function is used to select a shape on the shape file Map1.FunctionMode = FunctionMode.None End Sub Private Sub ToolSelectBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolSelectBtn.Click 'Select function is used to select a shape on the shape file Map1.FunctionMode = FunctionMode.Select End Sub Private Sub ToolAttibuteBtn_Click(sender As System.Object, e As System.EventArgs) Handles ToolAttibuteBtn.Click Dim frmAttribute As New Form2 frmAttribute.t_map = Map1 frmAttribute.ShowDialog(Me) End Sub End Class
がMain formのCodeです。
frmAttributeでAttributeをDataGridViewに表示しています。
このDataGridViewが、version1.3までは、文字化けして、field値がずれていたのですが、1.4では大丈夫のようです。
programの実行
.Net 4の環境でなければ動きませんので、注意してください。
作成したDotspatialCheck.exeを動かすとメイン画面が表示されます。
メイン画面の表示
メイン画面でAdd LayersをクリックするとOpenDialogが表示されます。
今回は、Shape fileのみに対応しているので、拡張子がshpだけを読み込みます。
OpenDialogの表示
Shape fileを選択すると、Mapとlegendに表示されます。
神奈川県のサンプルを読み込んで表示
c14_regionDDの下のpolygon iconをdouble clickするとLayerのplygon symbolizer Formが表示されます。これは、あらかじめprogramされています。
plygon symbolizer Form
c14_regionDDのlayernameををdouble clickするとLayer Property Formが表示されます。ここで、Category分類で色を設定などが行えます。
Layer Property Form
Layer Property Formでname fieldでuniqueに色づけした例です。
神奈川県のサンプルをLayer Property後の表示
Legend Control上でmouse right clickでpopup menuが表示されます。これも、あらかじめprogramされているものです。
popup menuの表示
個別属性値の表示もあらかじめprogramされているものです。mouse coursurを変更すれば属性値の表示ができます。
個別属性値の表示例
Attibute Table Viewをclickして表示したものです。この別formの表示ついては、programmingを行いました。
form2では、たかだか20行程度です。ただし、polygon shape fileのみの対応です。
Attibute Table View Form
今回のまとめ
では、今回のまとめを
-
Dotspatial 1.4のClass libraryを使って、超~簡単GISアプリを作成しました。
-
ほとんど、programmingすることなく実装できることがわかりました。
vb.net2010だけでなく、C#でも動作することができます。ただし、.NET 4.0の環境だけです。
簡単に自前のソフトウェアにちょっとしたGIS機能を付け加えるには良いと思います。
今後は、MapWinGISのtutorialが終わった後に、Dotspatialのtutorialをやってみたいと考えています。
実行形式のみですが、サンプルプログラムをこちらにアップしておきます。
サンプルプログラムおよびサンプルデータを使って、お使いのPCの不具合が生じても一切責任は持てませんので、あくまでも自己責任にて使用してください。
Exercise
実行形式のみをアップしているので、今回のProgramをsourceから作成してみてください。
Comments are closed.