forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoPackage.cs
More file actions
166 lines (148 loc) · 5.97 KB
/
GeoPackage.cs
File metadata and controls
166 lines (148 loc) · 5.97 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SQLite;
using SharpMap.Data.Providers.Tiles;
using SharpMap.Layers;
using SharpMap.Styles;
namespace SharpMap.Data.Providers
{
/// <summary>
/// A class wrapping a GeoPackage
/// </summary>
/// <seealso href="http://www.geopackage.org/"/>
public class GeoPackage
{
/// <summary>
/// Method to open a geopackage file
/// </summary>
/// <param name="filename">The filename of the GeoPackage</param>
/// <param name="password">The password to access the GeoPackage</param>
/// <returns></returns>
public static GeoPackage Open(string filename, string password = null)
{
try
{
GpkgUtility.CheckRequirements(filename, password);
return new GeoPackage(GpkgUtility.CreateConnectionString(filename, password));
}
catch (Exception)
{
throw new GeoPackageException(string.Format("Failed to open GeoPackage '{0}'", filename));
}
}
private readonly string _connectionString;
/// <summary>
/// Creates an instance of this class
/// </summary>
/// <param name="connectionString"></param>
private GeoPackage(string connectionString)
{
_connectionString = connectionString;
}
/// <summary>
/// Gets the <see cref="GpkgContent">Features</see> stored in the GeoPackage
/// </summary>
public ReadOnlyCollection<GpkgContent> Features
{
get
{
return new ReadOnlyCollection<GpkgContent>(ReadContents("features"));
}
}
/// <summary>
/// Gets the <see cref="GpkgContent">Tiles</see> stored in the GeoPackage
/// </summary>
public ReadOnlyCollection<GpkgContent> Tiles
{
get
{
return new ReadOnlyCollection<GpkgContent>(ReadContents("tiles"));
}
}
/// <summary>
/// Method to read the content from the 'gpkg_contents' table, filtered by <paramref name="kind"/>
/// </summary>
/// <param name="kind">The kind of content to filter for</param>
/// <returns>A list of content</returns>
private IList<GpkgContent> ReadContents(string kind)
{
var res = new List<GpkgContent>();
using (var cn = new SQLiteConnection(_connectionString))
{
cn.Open();
var cmd = new SQLiteCommand("SELECT * FROM \"gpkg_contents\" WHERE \"data_type\"='" + kind + "';", cn);
var rdr = cmd.ExecuteReader();
while (rdr.Read())
res.Add(new GpkgContent(rdr, _connectionString));
}
return res;
}
/// <summary>
/// Method to create an async tile layer
/// </summary>
/// <param name="name">The name of the layer</param>
/// <returns>A tile layer</returns>
public ILayer GetTileLayer(string name)
{
var content = FindContent(Tiles, name);
if (content == null)
throw new ArgumentException(string.Format("No tile layer named '{0}'", name));
var schema = new GpkgTileSchema(content);
return new TileLayer(new GpkgTileSource(content, schema), content.TableName);
}
/// <summary>
/// Method to create an async tile layer
/// </summary>
/// <param name="name">The name of the layer</param>
/// <returns>A tile layer</returns>
public ITileAsyncLayer GetTileAsyncLayer(string name)
{
var content = FindContent(Tiles, name);
if (content == null)
throw new ArgumentException(string.Format("No tile layer named '{0}'", name));
var schema = new GpkgTileSchema(content);
return new TileAsyncLayer(new GpkgTileSource(content, schema), content.TableName);
}
/// <summary>
/// Creates a <see cref="IProvider"/> for the <paramref name="content"/>.
/// </summary>
/// <param name="content">A content of the package</param>
/// <returns>A provider.</returns>
public IProvider GetFeatureProvider(GpkgContent content)
{
if (content == null)
throw new ArgumentNullException("content");
var p = new GpkgProvider(content);
return p;
}
/// <summary>
/// Method to get a feature layer for the given content
/// </summary>
/// <param name="content">The content</param>
/// <param name="createLayer">A delegate function to create the layer</param>
/// <returns>A layer</returns>
public ILayer GetFeatureLayer(GpkgContent content, Func<GpkgContent, IProvider, ILayer> createLayer = null)
{
if (content == null)
throw new ArgumentNullException("content");
createLayer = createLayer ?? CreateVectorLayer;
return createLayer(content, GetFeatureProvider(content));
}
#region private helper methods
private static ILayer CreateVectorLayer(GpkgContent content, IProvider provider)
{
return new VectorLayer(content.TableName, provider) { Style = VectorStyle.CreateRandomStyle() };
}
private static GpkgContent FindContent(IEnumerable<GpkgContent> contents, string contentName)
{
foreach (var content in contents)
{
if (string.Equals(content.TableName, contentName, StringComparison.CurrentCultureIgnoreCase))
return content;
}
return null;
}
#endregion
}
}