Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
230 lines (183 loc) · 6.62 KB
/
Copy pathProgram.cs
File metadata and controls
230 lines (183 loc) · 6.62 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using alatas.GeoJSON4EntityFramework;
using ExampleCSharp;
using System;
using System.Data;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
static class Module1
{
private static MenuItem[] Menu = {
new MenuItem("WKT -> FeatureCollection", FeatureCollectionFromWKT),
new MenuItem("WKT -> Feature", FeatureFromWKT),
new MenuItem("WKT -> Geometry", GeometryFromWKT),
new MenuItem("Database -> FeatureCollection", FeatureCollectionFromDB),
new MenuItem("Database -> Feature", FeatureFromDB),
new MenuItem("Database -> Geometry", GeometryFromDB)
};
public static void Main()
{
do
{
Console.Clear();
Console.WriteLine("GeoJson For EntityFramework Example");
Console.WriteLine(new String('-', 24));
Console.WriteLine("Examples:");
for (byte i = 1; i <= Menu.Length; i++)
{
Console.WriteLine(i + ". " + Menu[i - 1].Title);
}
Console.Write("Enter the number (Q for Quit): ");
string selection = Console.ReadLine();
if (selection.ToUpper() == "Q")
break;
int intSelection;
if (Int32.TryParse(selection, out intSelection))
{
if (intSelection >= 1 & intSelection <= Menu.Length)
{
Console.WriteLine(new String('-', 24));
dynamic outjson = Menu[intSelection - 1].Method.Invoke();
if (outjson != null)
{
string fileName = Path.Combine(StartupPath.FullName, "out" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".json");
File.WriteAllText(fileName, outjson, System.Text.Encoding.UTF8);
Console.WriteLine("GeoJSON saved : " + fileName);
}
Console.Read();
}
}
} while (true);
}
public static string FeatureCollectionFromWKT()
{
string[] 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)"
};
FeatureCollection features = new FeatureCollection(WKTs);
return features.Serialize(prettyPrint: true);
}
public static string FeatureFromWKT()
{
string WKT = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))";
Feature feature = new Feature(WKT);
return feature.Serialize(prettyPrint: true);
}
public static string GeometryFromWKT()
{
string WKT = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))";
GeoJsonGeometry geometry = GeoJsonGeometry.FromWKTGeometry(WKT);
return geometry.Serialize(prettyPrint: true);
}
public static string FeatureCollectionFromDB()
{
if (!TestDBConnection())
return null;
using (Entities db = new Entities())
{
DbGeometry[] data = (from row in db.SampleTables select row.SpatialData).ToArray();
FeatureCollection features = new FeatureCollection(data);
return features.Serialize(prettyPrint: true);
}
}
public static string FeatureFromDB()
{
if (!TestDBConnection())
return null;
using (Entities db = new Entities())
{
DbGeometry data = (from row in db.SampleTables select row.SpatialData).FirstOrDefault();
Feature feature = new Feature(data);
return feature.Serialize(prettyPrint: true);
}
}
public static string GeometryFromDB()
{
if (!TestDBConnection())
return null;
using (Entities db = new Entities())
{
DbGeometry data = (from row in db.SampleTables select row.SpatialData).FirstOrDefault();
GeoJsonGeometry geometry = GeoJsonGeometry.FromDbGeometry(data);
return geometry.Serialize(prettyPrint: true);
}
}
#region "Util"
public static DirectoryInfo StartupPath
{
get
{
return new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
}
}
public static bool TestDBConnection()
{
Console.WriteLine("Checking LocalDB Installation");
string localDB = GetLocalDB();
if (localDB == null)
{
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");
string mdfPath = StartupPath.Parent.Parent.Parent.FullName + "\\TestDB\\SpatialExample.mdf";
if (!File.Exists(mdfPath))
{
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");
SqlConnection c = new SqlConnection("data source=(LocalDB)\\MSSQLLocalDB;integrated security=True;attachdbfilename=" + mdfPath + ";");
try
{
c.Open();
c.Close();
}
catch (Exception ex)
{
Console.WriteLine("Error when connecting LocalDB instance: " + ex.Message);
return false;
}
return true;
}
}
}
private static string GetLocalDB()
{
string exeFileName = "SqlLocalDB.exe";
if (File.Exists(exeFileName))
{
return Path.GetFullPath(exeFileName);
}
foreach (string p in Environment.GetEnvironmentVariable("PATH").Split(';'))
{
dynamic fullPath = Path.Combine(p, exeFileName);
if (File.Exists(fullPath))
{
return fullPath;
}
}
return null;
}
private struct MenuItem
{
public MenuItem(string Title, Func<string> Method)
{
this.Title = Title;
this.Method = Method;
}
public string Title { get; set; }
public Func<string> Method { get; set; }
}
#endregion
}