Categories
VB.NET

Отваряне на ESRI Shape (SHP) файлове

Проект на VB.NET за отваряне на ESRI Shape (*.shp) файлове.

Използва е библиотеката SharpMap.

Функционалност:

  • Отваряне на файл;
  • Показване/скриване на слой;
  • Приближаване в пълен обхват;
  • Местене.

Този код може да се използва и модифицира без никакви ограничения.

Автор: GNNMobile.eu

Imports SharpMap
Imports System.IO
Imports System.Drawing
Public Class Main
Private isNewLayer As Boolean = False
Private Sub formLoad() Handles MyBase.Load
Try
btnZoomToExtents.Enabled = False
btnPan.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub zoomToExtents() Handles btnZoomToExtents.Click
Try
With MC
.Map.ZoomToExtents()
.Refresh()
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub pan() Handles btnPan.Click
Try
MC.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub open() Handles btnOpen.Click
Try
With OpenFD
.InitialDirectory = Directory.GetCurrentDirectory
.Title = "Отваряне на Shp файл:"
.Filter = "Shp файлове (*.shp)|*.shp"
End With
If OpenFD.ShowDialog = DialogResult.OK Then
addLayer(OpenFD.SafeFileName, OpenFD.FileName)
isNewLayer = True
Layers.Items.Add(OpenFD.SafeFileName, True)
btnZoomToExtents.Enabled = True
btnPan.Enabled = True
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub changeLayerVisibility() Handles Layers.ItemCheck
Try
If isNewLayer = False Then
Dim layer As SharpMap.Layers.ILayer = MC.Map.Layers(Layers.SelectedItem)
layer.Enabled = If(Layers.GetItemCheckState(Layers.SelectedIndex) = 0, True, False)
MC.Refresh()
Else
isNewLayer = False
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub addLayer(ByVal layerName As String, ByVal file As String)
Dim vectorLayer = New SharpMap.Layers.VectorLayer(layerName)
vectorLayer.DataSource = New SharpMap.Data.Providers.ShapeFile(file, True)
With MC
.Map.Layers.Add(vectorLayer)
.Map.ZoomToExtents()
.Refresh()
End With
End Sub
End Class
view raw Main.vb hosted with ❤ by GitHub

Визуализация:

SharpMap Shapefile Demo