Skip to content

Commit 1f54705

Browse files
authored
Merge pull request #12 from alatas/2.2.0
2.2.0
2 parents c6f7c2a + 6b64150 commit 1f54705

File tree

91 files changed

+5265
-756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+5265
-756
lines changed

ExampleCSharp/App.config

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
5+
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
6+
</configSections>
7+
<startup>
8+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
9+
</startup>
10+
<entityFramework>
11+
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
12+
<parameters>
13+
<parameter value="v13.0" />
14+
</parameters>
15+
</defaultConnectionFactory>
16+
<providers>
17+
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
18+
</providers>
19+
</entityFramework>
20+
<connectionStrings>
21+
<add name="Entities" connectionString="metadata=res://*/SampleModel.csdl|res://*/SampleModel.ssdl|res://*/SampleModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\SpatialExample.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
22+
</connectionStrings>
23+
</configuration>

ExampleCSharp/ExampleCSharp.csproj

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{30910CFD-585A-4CB1-ADA8-2291DBD2488E}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>ExampleCSharp</RootNamespace>
11+
<AssemblyName>ExampleCSharp</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
36+
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
37+
<Private>True</Private>
38+
</Reference>
39+
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
40+
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
41+
<Private>True</Private>
42+
</Reference>
43+
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
44+
<HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
45+
<Private>True</Private>
46+
</Reference>
47+
<Reference Include="System" />
48+
<Reference Include="System.ComponentModel.DataAnnotations" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Runtime.Serialization" />
51+
<Reference Include="System.Security" />
52+
<Reference Include="System.Xml.Linq" />
53+
<Reference Include="System.Data.DataSetExtensions" />
54+
<Reference Include="Microsoft.CSharp" />
55+
<Reference Include="System.Data" />
56+
<Reference Include="System.Net.Http" />
57+
<Reference Include="System.Xml" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<Compile Include="Program.cs" />
61+
<Compile Include="Properties\AssemblyInfo.cs" />
62+
<Compile Include="SampleModel.Context.cs">
63+
<AutoGen>True</AutoGen>
64+
<DesignTime>True</DesignTime>
65+
<DependentUpon>SampleModel.Context.tt</DependentUpon>
66+
</Compile>
67+
<Compile Include="SampleModel.cs">
68+
<AutoGen>True</AutoGen>
69+
<DesignTime>True</DesignTime>
70+
<DependentUpon>SampleModel.tt</DependentUpon>
71+
</Compile>
72+
<Compile Include="SampleModel.Designer.cs">
73+
<AutoGen>True</AutoGen>
74+
<DesignTime>True</DesignTime>
75+
<DependentUpon>SampleModel.edmx</DependentUpon>
76+
</Compile>
77+
<Compile Include="SampleTable.cs">
78+
<DependentUpon>SampleModel.tt</DependentUpon>
79+
</Compile>
80+
</ItemGroup>
81+
<ItemGroup>
82+
<None Include="App.config" />
83+
<None Include="packages.config" />
84+
<EntityDeploy Include="SampleModel.edmx">
85+
<Generator>EntityModelCodeGenerator</Generator>
86+
<LastGenOutput>SampleModel.Designer.cs</LastGenOutput>
87+
</EntityDeploy>
88+
<None Include="SampleModel.edmx.diagram">
89+
<DependentUpon>SampleModel.edmx</DependentUpon>
90+
</None>
91+
</ItemGroup>
92+
<ItemGroup>
93+
<ProjectReference Include="..\GeoJSON4EntityFramework\GeoJSON4EntityFramework.vbproj">
94+
<Project>{7b0f6694-a109-4f3f-84a0-9fa49193af31}</Project>
95+
<Name>GeoJSON4EntityFramework</Name>
96+
</ProjectReference>
97+
</ItemGroup>
98+
<ItemGroup>
99+
<Content Include="SampleModel.Context.tt">
100+
<Generator>TextTemplatingFileGenerator</Generator>
101+
<DependentUpon>SampleModel.edmx</DependentUpon>
102+
<LastGenOutput>SampleModel.Context.cs</LastGenOutput>
103+
</Content>
104+
<Content Include="SampleModel.tt">
105+
<Generator>TextTemplatingFileGenerator</Generator>
106+
<DependentUpon>SampleModel.edmx</DependentUpon>
107+
<LastGenOutput>SampleModel.cs</LastGenOutput>
108+
</Content>
109+
</ItemGroup>
110+
<ItemGroup>
111+
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
112+
</ItemGroup>
113+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
114+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
115+
Other similar extension points exist, see Microsoft.Common.targets.
116+
<Target Name="BeforeBuild">
117+
</Target>
118+
<Target Name="AfterBuild">
119+
</Target>
120+
-->
121+
</Project>

ExampleCSharp/Program.cs

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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

Comments
 (0)