-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathDatabaseCoordinateService.cs
More file actions
148 lines (131 loc) · 5.35 KB
/
Copy pathDatabaseCoordinateService.cs
File metadata and controls
148 lines (131 loc) · 5.35 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
using ProjNet.CoordinateSystems;
using ProjNet.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace ProjNet.Services
{
/// <summary>
/// Coordinate System Service tied to a database
/// </summary>
public class DatabaseCoordinateService : CoordinateSystemService, ICoordinateSystemService
{
private readonly DatabaseProvider _dbProvider;
/// <summary>
/// Initializes a DatabaseCoordinateService to retreive
/// Coordinate Systems from an internal database
/// </summary>
public DatabaseCoordinateService(CoordinateSystemFactory csFactory)
: base(csFactory)
{
_dbProvider = new DatabaseProvider();
}
/// <summary>
/// Initializes a DatabaseCoordinateService to retreive
/// Coordinate Systems from an internal database
/// </summary>
public DatabaseCoordinateService()
: this(new CoordinateSystemFactory())
{
_dbProvider = new DatabaseProvider();
}
#region Interface Methods
/// <summary>
/// Returns the coordinate system by <paramref name="srid" /> identifier
/// </summary>
/// <param name="srid">The initialization for the coordinate system</param>
/// <returns>The coordinate system.</returns>
public CoordinateSystem GetCoordinateSystem(int srid)
{
var sysInfo = _dbProvider.GetCoordinateSystemInfo(srid).Result;
if (sysInfo != null)
return CsFactory.CreateFromWkt(sysInfo.WKT);
return null;
}
/// <summary>
/// Returns the coordinate system by <paramref name="srid" /> identifier
/// </summary>
/// <param name="srid">The initialization for the coordinate system</param>
/// <returns>The coordinate system.</returns>
public async Task<CoordinateSystem> GetCoordinateSystemAsync(int srid)
{
var sysInfo = await _dbProvider.GetCoordinateSystemInfo(srid);
if (sysInfo != null)
return CsFactory.CreateFromWkt(sysInfo.WKT);
return null;
}
/// <summary>
/// Returns the coordinate system by <paramref name="authority" /> and <paramref name="code" />.
/// </summary>
/// <param name="authority">The authority for the coordinate system</param>
/// <param name="code">The code assigned to the coordinate system by <paramref name="authority" />.</param>
/// <returns>The coordinate system.</returns>
public CoordinateSystem GetCoordinateSystem(string authority, long code)
{
int? srid = GetSRID(authority, code);
if (srid.HasValue)
return GetCoordinateSystem(srid.Value);
return null;
}
/// <summary>
/// Searches the table names based on an expression
/// </summary>
/// <param name="name"></param>
public Task<IEnumerable<CoordinateSystemInfo>> SearchCoordinateSystemAsync(string name)
{
return _dbProvider.SearchCoordinateSystemAsync(name);
}
/// <summary>
/// Method to get the identifier, by which this coordinate system can be accessed.
/// </summary>
/// <param name="authority">The authority name</param>
/// <param name="authorityCode">The code assigned by <paramref name="authority" /></param>
/// <returns>The identifier or <value>null</value></returns>
public int? GetSRID(string authority, long authorityCode)
{
return (int)authorityCode;
}
/// <summary>
/// Returns number of coordinate systems registered by the service
/// </summary>
public int Count => _dbProvider.GetCount().Result;
/// <summary>
/// Adds a coordinate system to the dictionary based on srid
/// </summary>
/// <param name="srid"></param>
/// <param name="coordinateSystem"></param>
public void AddCoordinateSystem(int srid, CoordinateSystem coordinateSystem)
{
if (coordinateSystem.Name == null)
throw new ArgumentNullException("Name is null.");
if (coordinateSystem.AuthorityCode <= 0)
coordinateSystem.AuthorityCode = srid;
if (string.IsNullOrEmpty(coordinateSystem.Authority))
coordinateSystem.Authority = "EPSG";
if (string.IsNullOrEmpty(coordinateSystem.Alias))
coordinateSystem.Alias = coordinateSystem.Name;
AddCoordinateSystem(coordinateSystem);
}
/// <summary>
/// Adds a coordinate system to the dictionary.
/// The authority code is defaulted as the srid.
/// </summary>
/// <param name="coordinateSystem"></param>
/// <returns></returns>
public int AddCoordinateSystem(CoordinateSystem coordinateSystem)
{
return _dbProvider.AddCoordinateSystem(coordinateSystem).Result;
}
/// <summary>
/// Removes a coordinate system from the database
/// </summary>
public bool RemoveCoordinateSystem(int srid)
{
return _dbProvider.RemoveCoordinateSystem(srid).Result;
}
#endregion
}
}