Skip to content

Commit 265e768

Browse files
author
Travis Yeik
committed
Separated out sqlite methods. Added NUTest
1 parent 83b2b25 commit 265e768

23 files changed

+9693
-137
lines changed

ProjNet4GeoAPI.sln

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29020.237
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.4.33103.184
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjNET", "src\ProjNet\ProjNET.csproj", "{E028BDD2-55E1-4E5F-BE31-35FAEC8D6428}"
77
EndProject
@@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjNET.Tests", "test\ProjN
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjNet.Benchmark", "src\ProjNet.Benchmark\ProjNet.Benchmark.csproj", "{1E5D1CC5-8CFE-4C9D-9553-900469C57D6C}"
1717
EndProject
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProjNet.Sqlite", "src\ProjNet.Sqlite\ProjNet.Sqlite.csproj", "{3D364E7E-7D2D-42FB-A62B-137BBA2F8F1E}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{1E5D1CC5-8CFE-4C9D-9553-900469C57D6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{1E5D1CC5-8CFE-4C9D-9553-900469C57D6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{1E5D1CC5-8CFE-4C9D-9553-900469C57D6C}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{3D364E7E-7D2D-42FB-A62B-137BBA2F8F1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{3D364E7E-7D2D-42FB-A62B-137BBA2F8F1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{3D364E7E-7D2D-42FB-A62B-137BBA2F8F1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{3D364E7E-7D2D-42FB-A62B-137BBA2F8F1E}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

src/ProjNet/IO/CoordinateSystemInfo.cs renamed to src/ProjNet.Sqlite/CoordinateSystemInfo.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,53 @@
55

66
namespace ProjNet.IO
77
{
8+
/// <summary>
9+
/// Model for coordinate system in the database
10+
/// </summary>
811
[Table("srs_data")]
912
public class CoordinateSystemInfo
1013
{
14+
/// <summary>
15+
///
16+
/// </summary>
1117
public string Authority { get; set; }
18+
/// <summary>
19+
///
20+
/// </summary>
1221
[PrimaryKey]
1322
public int Code { get; set; }
23+
/// <summary>
24+
///
25+
/// </summary>
1426
public string Name { get; set; }
27+
/// <summary>
28+
///
29+
/// </summary>
1530
public string Alias { get; set; }
31+
/// <summary>
32+
///
33+
/// </summary>
1634
public bool IsDeprecated { get; set; }
35+
/// <summary>
36+
///
37+
/// </summary>
1738
[Column("type")]
1839
public string SystemType { get; set; }
40+
/// <summary>
41+
///
42+
/// </summary>
1943
public string WKT { get; set; }
2044

21-
public CoordinateSystemInfo()
22-
{ }
45+
/// <summary>
46+
/// Parameterless construction for initialization
47+
/// </summary>
48+
public CoordinateSystemInfo()
49+
{
50+
}
2351

52+
/// <summary>
53+
/// Holds info about the coordinate system
54+
/// </summary>
2455
public CoordinateSystemInfo(string name, string alias, string authority, int code, string systemType, bool isDeprecated, string wkt)
2556
{
2657
Authority = authority;
@@ -29,7 +60,7 @@ public CoordinateSystemInfo(string name, string alias, string authority, int cod
2960
Alias = alias;
3061
SystemType = systemType;
3162
IsDeprecated = isDeprecated;
32-
WKT = wkt;
63+
WKT = wkt;
3364
}
3465
}
3566
}

src/ProjNet/Services/DatabaseCoordinateService.cs renamed to src/ProjNet.Sqlite/DatabaseCoordinateService.cs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,31 @@ namespace ProjNet.Services
1212
/// <summary>
1313
/// Coordinate System Service tied to a database
1414
/// </summary>
15-
internal class DatabaseCoordinateService : ICoordinateSystemService
15+
public class DatabaseCoordinateService : CoordinateSystemService, ICoordinateSystemService
1616
{
1717
private readonly DatabaseProvider _dbProvider;
18-
private readonly CoordinateSystemServices _css;
1918

2019
/// <summary>
2120
/// Initializes a DatabaseCoordinateService to retreive
22-
/// Coordinate Systems from an internal databse
21+
/// Coordinate Systems from an internal database
2322
/// </summary>
24-
public DatabaseCoordinateService(CoordinateSystemServices css)
23+
public DatabaseCoordinateService(CoordinateSystemFactory csFactory)
24+
: base(csFactory)
2525
{
26-
_css = css;
2726
_dbProvider = new DatabaseProvider();
2827
}
2928

29+
/// <summary>
30+
/// Initializes a DatabaseCoordinateService to retreive
31+
/// Coordinate Systems from an internal database
32+
/// </summary>
33+
public DatabaseCoordinateService()
34+
: this(new CoordinateSystemFactory())
35+
{
36+
_dbProvider = new DatabaseProvider();
37+
}
38+
39+
#region Interface Methods
3040
/// <summary>
3141
/// Returns the coordinate system by <paramref name="srid" /> identifier
3242
/// </summary>
@@ -35,7 +45,10 @@ public DatabaseCoordinateService(CoordinateSystemServices css)
3545
public CoordinateSystem GetCoordinateSystem(int srid)
3646
{
3747
var sysInfo = _dbProvider.GetCoordinateSystemInfo(srid).Result;
38-
return _css.CreateFromWkt(sysInfo.WKT);
48+
if (sysInfo != null)
49+
return CsFactory.CreateFromWkt(sysInfo.WKT);
50+
51+
return null;
3952
}
4053

4154
/// <summary>
@@ -64,18 +77,48 @@ public CoordinateSystem GetCoordinateSystem(string authority, long code)
6477
return (int)authorityCode;
6578
}
6679

80+
/// <summary>
81+
/// Returns number of coordinate systems registered by the service
82+
/// </summary>
6783
public int Count => _dbProvider.GetCount().Result;
6884

85+
/// <summary>
86+
/// Adds a coordinate system to the dictionary based on srid
87+
/// </summary>
88+
/// <param name="srid"></param>
89+
/// <param name="coordinateSystem"></param>
6990
public void AddCoordinateSystem(int srid, CoordinateSystem coordinateSystem)
7091
{
71-
// At the moment, the database does not support srids
72-
// (i.e., it is keyed by the code and there cannot be the same code from multiple authorities)
92+
if (coordinateSystem.Name == null)
93+
throw new ArgumentNullException("Name is null.");
94+
if (coordinateSystem.AuthorityCode <= 0)
95+
coordinateSystem.AuthorityCode = srid;
96+
if (string.IsNullOrEmpty(coordinateSystem.Authority))
97+
coordinateSystem.Authority = "EPSG";
98+
if (string.IsNullOrEmpty(coordinateSystem.Alias))
99+
coordinateSystem.Alias = coordinateSystem.Name;
100+
73101
AddCoordinateSystem(coordinateSystem);
74102
}
75103

104+
/// <summary>
105+
/// Adds a coordinate system to the dictionary.
106+
/// The authority code is defaulted as the srid.
107+
/// </summary>
108+
/// <param name="coordinateSystem"></param>
109+
/// <returns></returns>
76110
public int AddCoordinateSystem(CoordinateSystem coordinateSystem)
77111
{
78112
return _dbProvider.AddCoordinateSystem(coordinateSystem).Result;
79113
}
114+
115+
/// <summary>
116+
/// Removes a coordinate system from the database
117+
/// </summary>
118+
public bool RemoveCoordinateSystem(int srid)
119+
{
120+
return _dbProvider.RemoveCoordinateSystem(srid).Result;
121+
}
122+
#endregion
80123
}
81124
}
Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace ProjNet.IO
99
{
10+
/// <summary>
11+
/// Handles calls to the database
12+
/// </summary>
1013
public class DatabaseProvider
1114
{
1215
private string _connectionString;
@@ -17,10 +20,18 @@ public class DatabaseProvider
1720
/// </summary>
1821
public DatabaseProvider()
1922
{
20-
var currentDir = AppContext.BaseDirectory;
23+
string currentDir = AppContext.BaseDirectory;
2124
_connectionString = Path.Combine(currentDir, "proj.db");
2225
}
2326

27+
/// <summary>
28+
/// Creates an instance of the DatabaseProvider
29+
/// </summary>
30+
public DatabaseProvider(string database)
31+
{
32+
_connectionString = database;
33+
}
34+
2435
/// <summary>
2536
/// Initializes the Database
2637
/// </summary>
@@ -58,9 +69,9 @@ internal async Task<int> GetCount()
5869
/// </summary>
5970
internal async Task<int> AddCoordinateSystem(CoordinateSystem coordinateSystem)
6071
{
61-
CoordinateSystemType coordType = GetSystemType(coordinateSystem.WKT);
72+
var coordType = GetSystemType(coordinateSystem.WKT);
6273
var coordInfo = new CoordinateSystemInfo(coordinateSystem.Name, coordinateSystem.Alias,
63-
coordinateSystem.Authority, (int)coordinateSystem.AuthorityCode,
74+
coordinateSystem.Authority, (int)coordinateSystem.AuthorityCode,
6475
coordType.ToString(), false, coordinateSystem.WKT);
6576

6677
return await AddCoordinateSystem(coordInfo);
@@ -72,6 +83,20 @@ internal async Task<int> AddCoordinateSystem(CoordinateSystemInfo coordinateInfo
7283
return await Database.InsertAsync(coordinateInfo);
7384
}
7485

86+
/// <summary>
87+
/// Removes a coordninate system from the database
88+
/// </summary>
89+
90+
internal async Task<bool> RemoveCoordinateSystem(int srid)
91+
{
92+
await Init();
93+
var csInfo = await GetCoordinateSystemInfo(srid);
94+
if (csInfo != null)
95+
return await Database.DeleteAsync(csInfo) > 0;
96+
97+
return false;
98+
}
99+
75100
/// <summary>
76101
/// Parses the wkt to get the appropriate CoordinateSystemType
77102
/// </summary>
@@ -96,5 +121,7 @@ private CoordinateSystemType GetSystemType(string wkt)
96121

97122
return CoordinateSystemType.unknown;
98123
}
124+
125+
99126
}
100127
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
5+
<SignAssembly>true</SignAssembly>
6+
<EnableApiCompat>true</EnableApiCompat>
7+
<LangVersion>latest</LangVersion>
8+
</PropertyGroup>
9+
10+
<PropertyGroup Label="NuGet Package Info">
11+
<PackageId>ProjNet.Sqlite</PackageId>
12+
<Authors>Travis Yeik, NetTopologySuite-Team</Authors>
13+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
14+
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
15+
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
16+
<Authors>Travis Yeik</Authors>
17+
<PackageDescription>Provides an extension to ProjNet's ICoordinateSystemService by providing a database for coordinate systems</PackageDescription>
18+
<PackageTags>OGC;SFS;Projection</PackageTags>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<None Remove="proj.db" />
27+
</ItemGroup>
28+
29+
<ItemGroup>
30+
<Content Include="proj.db">
31+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
</Content>
33+
</ItemGroup>
34+
35+
<ItemGroup>
36+
<ProjectReference Include="..\ProjNet\ProjNET.csproj" />
37+
</ItemGroup>
38+
39+
</Project>

0 commit comments

Comments
 (0)