1+ using alatas . GeoJSON4EntityFramework ;
2+ using ExampleCSharp ;
3+ using System ;
4+ using System . Data ;
5+ using System . Data . Entity . Spatial ;
6+ using System . Data . SqlClient ;
7+ using System . Diagnostics ;
8+ using System . IO ;
9+ using System . Linq ;
10+
11+
12+ static class Module1
13+ {
14+
15+ private static MenuItem [ ] Menu = {
16+ new MenuItem ( "WKT -> FeatureCollection" , FeatureCollectionFromWKT ) ,
17+ new MenuItem ( "WKT -> Feature" , FeatureFromWKT ) ,
18+ new MenuItem ( "WKT -> Geometry" , GeometryFromWKT ) ,
19+ new MenuItem ( "Database -> FeatureCollection" , FeatureCollectionFromDB ) ,
20+ new MenuItem ( "Database -> Feature" , FeatureFromDB ) ,
21+ new MenuItem ( "Database -> Geometry" , GeometryFromDB )
22+
23+ } ;
24+
25+ public static void Main ( )
26+ {
27+ do
28+ {
29+ Console . Clear ( ) ;
30+ Console . WriteLine ( "GeoJson For EntityFramework Example" ) ;
31+ Console . WriteLine ( new String ( '-' , 24 ) ) ;
32+ Console . WriteLine ( "Examples:" ) ;
33+
34+ for ( byte i = 1 ; i <= Menu . Length ; i ++ )
35+ {
36+ Console . WriteLine ( i + ". " + Menu [ i - 1 ] . Title ) ;
37+ }
38+
39+ Console . Write ( "Enter the number (Q for Quit): " ) ;
40+
41+ string selection = Console . ReadLine ( ) ;
42+
43+ if ( selection . ToUpper ( ) == "Q" )
44+ break ;
45+
46+ int intSelection ;
47+ if ( Int32 . TryParse ( selection , out intSelection ) )
48+ {
49+
50+ if ( intSelection >= 1 & intSelection <= Menu . Length )
51+ {
52+
53+ Console . WriteLine ( new String ( '-' , 24 ) ) ;
54+ dynamic outjson = Menu [ intSelection - 1 ] . Method . Invoke ( ) ;
55+
56+ if ( outjson != null )
57+ {
58+ string fileName = Path . Combine ( StartupPath . FullName , "out" + DateTime . Now . ToString ( "yyyyMMddHHmmss" ) + ".json" ) ;
59+
60+ File . WriteAllText ( fileName , outjson , System . Text . Encoding . UTF8 ) ;
61+ Console . WriteLine ( "GeoJSON saved : " + fileName ) ;
62+ }
63+
64+ Console . Read ( ) ;
65+ }
66+ }
67+
68+ } while ( true ) ;
69+
70+ }
71+
72+ public static string FeatureCollectionFromWKT ( )
73+ {
74+ string [ ] WKTs = {
75+ "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))" ,
76+ "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))" ,
77+ "LINESTRING (1 1, 2 2)"
78+ } ;
79+
80+ FeatureCollection features = new FeatureCollection ( WKTs ) ;
81+ return features . Serialize ( prettyPrint : true ) ;
82+ }
83+
84+ public static string FeatureFromWKT ( )
85+ {
86+ string WKT = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))" ;
87+
88+ Feature feature = new Feature ( WKT ) ;
89+ return feature . Serialize ( prettyPrint : true ) ;
90+ }
91+
92+ public static string GeometryFromWKT ( )
93+ {
94+ string WKT = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))" ;
95+
96+ GeoJsonGeometry geometry = GeoJsonGeometry . FromWKTGeometry ( WKT ) ;
97+ return geometry . Serialize ( prettyPrint : true ) ;
98+ }
99+
100+ public static string FeatureCollectionFromDB ( )
101+ {
102+ if ( ! TestDBConnection ( ) )
103+ return null ;
104+
105+ using ( Entities db = new Entities ( ) )
106+ {
107+
108+ DbGeometry [ ] data = ( from row in db . SampleTables select row . SpatialData ) . ToArray ( ) ;
109+
110+ FeatureCollection features = new FeatureCollection ( data ) ;
111+ return features . Serialize ( prettyPrint : true ) ;
112+ }
113+ }
114+
115+ public static string FeatureFromDB ( )
116+ {
117+ if ( ! TestDBConnection ( ) )
118+ return null ;
119+
120+ using ( Entities db = new Entities ( ) )
121+ {
122+ DbGeometry data = ( from row in db . SampleTables select row . SpatialData ) . FirstOrDefault ( ) ;
123+
124+ Feature feature = new Feature ( data ) ;
125+ return feature . Serialize ( prettyPrint : true ) ;
126+ }
127+ }
128+
129+ public static string GeometryFromDB ( )
130+ {
131+ if ( ! TestDBConnection ( ) )
132+ return null ;
133+
134+
135+ using ( Entities db = new Entities ( ) )
136+ {
137+ DbGeometry data = ( from row in db . SampleTables select row . SpatialData ) . FirstOrDefault ( ) ;
138+
139+ GeoJsonGeometry geometry = GeoJsonGeometry . FromDbGeometry ( data ) ;
140+ return geometry . Serialize ( prettyPrint : true ) ;
141+ }
142+ }
143+
144+ #region "Util"
145+ public static DirectoryInfo StartupPath
146+ {
147+ get
148+ {
149+ return new DirectoryInfo ( Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ) ) ;
150+ }
151+ }
152+
153+ public static bool TestDBConnection ( )
154+ {
155+ Console . WriteLine ( "Checking LocalDB Installation" ) ;
156+ string localDB = GetLocalDB ( ) ;
157+
158+ if ( localDB == null )
159+ {
160+ Console . WriteLine ( "LocalDB isn't installed, please download and install SQL Server LocalDB 2016+ from https://go.microsoft.com/fwlink/?LinkID=799012" ) ;
161+ Process . Start ( "https://go.microsoft.com/fwlink/?LinkID=799012" ) ;
162+ return false ;
163+
164+ }
165+ else
166+ {
167+ Console . WriteLine ( "Locating sampla database file" ) ;
168+ string mdfPath = StartupPath . Parent . Parent . Parent . FullName + "\\ TestDB\\ SpatialExample.mdf" ;
169+
170+ if ( ! File . Exists ( mdfPath ) )
171+ {
172+ Console . WriteLine ( "Sample database file not found: " + mdfPath ) ;
173+ return false ;
174+ }
175+ else
176+ {
177+ AppDomain . CurrentDomain . SetData ( "DataDirectory" , StartupPath . Parent . Parent . Parent . FullName + "\\ TestDB\\ " ) ;
178+
179+ Console . WriteLine ( "Connecting to MSSQLLocalDB instance" ) ;
180+
181+ SqlConnection c = new SqlConnection ( "data source=(LocalDB)\\ MSSQLLocalDB;integrated security=True;attachdbfilename=" + mdfPath + ";" ) ;
182+
183+ try
184+ {
185+ c . Open ( ) ;
186+ c . Close ( ) ;
187+ }
188+ catch ( Exception ex )
189+ {
190+ Console . WriteLine ( "Error when connecting LocalDB instance: " + ex . Message ) ;
191+ return false ;
192+ }
193+
194+ return true ;
195+ }
196+ }
197+ }
198+
199+ private static string GetLocalDB ( )
200+ {
201+ string exeFileName = "SqlLocalDB.exe" ;
202+
203+ if ( File . Exists ( exeFileName ) )
204+ {
205+ return Path . GetFullPath ( exeFileName ) ;
206+ }
207+
208+ foreach ( string p in Environment . GetEnvironmentVariable ( "PATH" ) . Split ( ';' ) )
209+ {
210+ dynamic fullPath = Path . Combine ( p , exeFileName ) ;
211+ if ( File . Exists ( fullPath ) )
212+ {
213+ return fullPath ;
214+ }
215+ }
216+ return null ;
217+ }
218+ private struct MenuItem
219+ {
220+ public MenuItem ( string Title , Func < string > Method )
221+ {
222+ this . Title = Title ;
223+ this . Method = Method ;
224+ }
225+ public string Title { get ; set ; }
226+ public Func < string > Method { get ; set ; }
227+ }
228+
229+ #endregion
230+ }
0 commit comments