-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathModule1.vb
More file actions
169 lines (132 loc) · 6.05 KB
/
Module1.vb
File metadata and controls
169 lines (132 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Imports alatas.GeoJSON4EntityFramework
Module Module1
Private Menu As MenuItem() = {
New MenuItem("WKT -> FeatureCollection", AddressOf FeatureCollectionFromWKT),
New MenuItem("WKT -> Feature", AddressOf FeatureFromWKT),
New MenuItem("WKT -> Geometry", AddressOf GeometryFromWKT),
New MenuItem("Database -> FeatureCollection", AddressOf FeatureCollectionFromDB),
New MenuItem("Database -> Feature", AddressOf FeatureFromDB),
New MenuItem("Database -> Geometry", AddressOf GeometryFromDB)
}
Sub Main()
Do
Console.Clear()
Console.WriteLine("GeoJson For EntityFramework Example")
Console.WriteLine(StrDup(24, "-"))
Console.WriteLine("Examples:")
For i As Byte = 1 To Menu.Length
Console.WriteLine(i & ". " & Menu(i - 1).Title)
Next
Console.Write("Enter the number (Q for Quit): ")
Dim selection As String = Console.ReadLine
If selection.ToUpper = "Q" Then Exit Do
If IsNumeric(selection) AndAlso (selection >= 1 And selection <= Menu.Length) Then
Console.WriteLine(StrDup(24, "-"))
Dim outjson = Menu(selection - 1).Method.Invoke
If outjson IsNot Nothing Then
Dim fileName As String = IO.Path.Combine(StartupPath.FullName, "out" & Now.ToString("yyyyMMddHHmmss") & ".json")
IO.File.WriteAllText(fileName, outjson, Text.Encoding.UTF8)
Console.WriteLine("GeoJSON saved : " & fileName)
End If
Console.Read()
End If
Loop
End Sub
Function FeatureCollectionFromWKT() As String
Dim WKTs = {"POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))",
"MULTIPOINT ((10 40), (40 30), (20 20), (30 10))",
"LINESTRING (1 1, 2 2)"}
Dim features = New FeatureCollection(WKTs)
Return features.Serialize(prettyPrint:=True)
End Function
Function FeatureFromWKT() As String
Dim WKT = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"
Dim feature = New Feature(WKT)
Return feature.Serialize(prettyPrint:=True)
End Function
Function GeometryFromWKT() As String
Dim WKT = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"
Dim geometry = GeoJsonGeometry.FromWKTGeometry(WKT)
Return geometry.Serialize(prettyPrint:=True)
End Function
Function FeatureCollectionFromDB() As String
If Not TestDBConnection() Then Return Nothing
Using db As New SpatialExampleEntities
Dim data = From row In db.SampleTables Select row.SpatialData
Dim features = New FeatureCollection(data.ToArray)
Return features.Serialize(prettyPrint:=True)
End Using
End Function
Function FeatureFromDB() As String
If Not TestDBConnection() Then Return Nothing
Using db As New SpatialExampleEntities
Dim data = From row In db.SampleTables Take 1 Select row.SpatialData
Dim feature = New Feature(data.FirstOrDefault)
Return feature.Serialize(prettyPrint:=True)
End Using
End Function
Function GeometryFromDB() As String
If Not TestDBConnection() Then Return Nothing
Using db As New SpatialExampleEntities
Dim data = From row In db.SampleTables Take 1 Select row.SpatialData
Dim geometry = GeoJsonGeometry.FromDbGeometry(data.FirstOrDefault)
Return geometry.Serialize(prettyPrint:=True)
End Using
End Function
#Region "Util"
ReadOnly Property StartupPath As IO.DirectoryInfo
Get
Return New IO.DirectoryInfo(IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location))
End Get
End Property
Function TestDBConnection() As Boolean
Console.WriteLine("Checking LocalDB Installation")
Dim localDB As String = GetLocalDB()
If localDB Is Nothing Then
Console.WriteLine("LocalDB isn't installed, please download and install SQL Server LocalDB 2016+ from https://go.microsoft.com/fwlink/?LinkID=799012")
Process.Start("https://go.microsoft.com/fwlink/?LinkID=799012")
Return False
Else
Console.WriteLine("Locating sampla database file")
Dim mdfPath As String = StartupPath.Parent.Parent.Parent.FullName & "\TestDB\SpatialExample.mdf"
If Not IO.File.Exists(mdfPath) Then
Console.WriteLine("Sample database file not found: " & mdfPath)
Return False
Else
AppDomain.CurrentDomain.SetData("DataDirectory", StartupPath.Parent.Parent.Parent.FullName & "\TestDB\")
Console.WriteLine("Connecting to MSSQLLocalDB instance")
Dim c As New SqlClient.SqlConnection("data source=(LocalDB)\MSSQLLocalDB;integrated security=True;attachdbfilename=" & mdfPath & ";")
Try
c.Open()
c.Close()
Catch ex As Exception
Console.WriteLine("Error when connecting LocalDB instance: " & ex.Message)
Return False
End Try
Return True
End If
End If
End Function
Private Function GetLocalDB() As String
Dim exeFileName As String = "SqlLocalDB.exe"
If IO.File.Exists(exeFileName) Then
Return IO.Path.GetFullPath(exeFileName)
End If
For Each p As String In Environment.GetEnvironmentVariable("PATH").Split(";")
Dim fullPath = IO.Path.Combine(p, exeFileName)
If IO.File.Exists(fullPath) Then
Return fullPath
End If
Next
Return Nothing
End Function
Private Structure MenuItem
Sub New(Title As String, Method As Func(Of String))
Me.Title = Title
Me.Method = Method
End Sub
Property Title As String
Property Method As Func(Of String)
End Structure
#End Region
End Module