Skip to content

Commit 320a318

Browse files
committed
Merge pull request #42 from jericks/gpkg_workspace
Add GeoPackage Workspace
2 parents fde5efb + 905d85e commit 320a318

File tree

8 files changed

+226
-0
lines changed

8 files changed

+226
-0
lines changed

doc/api/workspace/geopackage.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
:class:`workspace.GeoPackage`
2+
=====================
3+
4+
.. class:: workspace.GeoPackage(config)
5+
6+
:arg config: ``Object`` Configuration object.
7+
8+
Create a workspace from an GeoPackage database.
9+
10+
11+
Config Properties
12+
-----------------
13+
14+
.. describe:: database
15+
16+
``String``
17+
Path to the database (required).
18+
19+
Properties
20+
----------
21+
22+
.. attribute:: GeoPackage.layers
23+
24+
``Array``
25+
The available layers in the workspace.
26+
27+
.. attribute:: GeoPackage.names
28+
29+
``Array``
30+
The available layer names in the workspace.
31+
32+
33+
Methods
34+
-------
35+
36+
37+
.. function:: GeoPackage.add
38+
39+
:arg layer: :class:`layer.Layer` The layer to be added.
40+
:arg options: ``Object`` Options for adding the layer.
41+
42+
Options:
43+
* `name`: ``String`` Name for the new layer.
44+
* `filter`: :class:`filter.Filter` Filter to apply to features before adding.
45+
* `projection`: :class:`proj.Projection` Destination projection for the layer.
46+
47+
:returns: :class:`layer.Layer`
48+
49+
Create a new layer in this workspace with the features from an existing
50+
layer. If a layer with the same name already exists in this workspace,
51+
you must provide a new name for the layer.
52+
53+
.. function:: GeoPackage.close
54+
55+
Close the workspace. This discards any existing connection to the
56+
underlying data store and discards the reference to the store.
57+
58+
.. function:: GeoPackage.get
59+
60+
:arg name: ``String`` Layer name.
61+
:returns: :class:`layer.Layer`
62+
63+
Get a layer by name. Returns ``undefined`` if name doesn't correspond
64+
to a layer source in the workspace.
65+
66+
67+
68+
69+
70+
71+

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
<artifactId>gt-swing</artifactId>
135135
<version>${gt.version}</version>
136136
</dependency>
137+
<dependency>
138+
<groupId>org.geotools</groupId>
139+
<artifactId>gt-geopkg</artifactId>
140+
<version>${gt.version}</version>
141+
</dependency>
137142
</dependencies>
138143
<build>
139144
<plugins>

src/main/resources/org/geoscript/js/lib/geoscript/workspace.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ exports.H2 = require("./workspace/h2").H2;
3939
/** api: classes[] = mysql */
4040
exports.MySQL = require("./workspace/mysql").MySQL;
4141

42+
/** api: classes[] = geopackage */
43+
exports.GeoPackage = require("./workspace/geopackage").GeoPackage;
44+
4245
/** private: classes[] = spatialite */
4346
exports.SpatiaLite = require("./workspace/spatialite").SpatiaLite;
4447

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
var register = require("./util").register;
2+
var Factory = require("../factory").Factory;
3+
var Workspace = require("./workspace").Workspace;
4+
var UTIL = require("../util");
5+
6+
var GeoPkgDataStoreFactory = Packages.org.geotools.geopkg.GeoPkgDataStoreFactory;
7+
8+
/** private: (define)
9+
* module = workspace
10+
* class = GeoPackage
11+
*/
12+
13+
var prepConfig = function(config) {
14+
if (config) {
15+
if (typeof config === "string") {
16+
config = {database: config};
17+
}
18+
if (!(typeof config.database === "string" || config.database instanceof file.Path)) {
19+
throw "GeoPackage config must include database path.";
20+
}
21+
config = {
22+
database: String(config.database)
23+
};
24+
}
25+
return config;
26+
};
27+
28+
/** private: (extends)
29+
* workspace/workspace.js
30+
*/
31+
var GeoPackage = UTIL.extend(Workspace, {
32+
33+
/** private: config[database]
34+
* ``String``
35+
* Path to the database (required).
36+
*/
37+
38+
/** private: constructor
39+
* .. class:: GeoPackage
40+
*
41+
* :arg config: ``Object`` Configuration object.
42+
*
43+
* Create a workspace from a GeoPackage enabled database.
44+
*/
45+
constructor: function GeoPackage(config) {
46+
Workspace.prototype.constructor.apply(this, [prepConfig(config)]);
47+
},
48+
49+
/** private: method[_create]
50+
* :arg config: ``Object``
51+
* :returns: ``org.geotools.jdbc.JDBCDataStore``
52+
*
53+
* Create the underlying store for the workspace.
54+
*/
55+
_create: function(config) {
56+
config.dbtype = "geopkg";
57+
var factory = new GeoPkgDataStoreFactory();
58+
return factory.createDataStore(config);
59+
},
60+
61+
/** private: property[config]
62+
*/
63+
get config() {
64+
return {
65+
type: this.constructor.name,
66+
database: this.database
67+
};
68+
}
69+
70+
});
71+
72+
exports.GeoPackage = GeoPackage;
73+
74+
// register a geopackage factory for the module
75+
register(new Factory(GeoPackage, {
76+
handles: function(config) {
77+
var capable = false;
78+
if (typeof config.type === "string" && config.type.toLowerCase() === "geopackage") {
79+
try {
80+
config = prepConfig(config);
81+
capable = true;
82+
} catch (err) {
83+
// pass
84+
}
85+
}
86+
return capable;
87+
}
88+
}));

src/test/resources/org/geoscript/js/tests/admin.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ var meta = {
5454
}
5555
}
5656
},
57+
geopkg: {
58+
source: path("data/geopkg.zip"),
59+
dest: path("tmp"),
60+
setUp: function() {
61+
meta.geopkg.tearDown();
62+
unzip(meta.geopkg.source, meta.geopkg.dest);
63+
},
64+
tearDown: function() {
65+
if (FS.exists(meta.geopkg.dest)) {
66+
FS.removeTree(meta.geopkg.dest);
67+
}
68+
}
69+
},
5770
pg: {
5871
driver: new Packages.org.postgresql.Driver,
5972
setUp: function() {
115 KB
Binary file not shown.

src/test/resources/org/geoscript/js/tests/geoscript/test_workspace.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ exports["test: Workspace.from_"] = function() {
3434

3535
exports["test: Directory"] = require("./workspace/test_directory");
3636
exports["test: H2"] = require("./workspace/test_h2");
37+
exports["test: GeoPackage"] = require("./workspace/test_geopackage");
3738
exports["test: Memory"] = require("./workspace/test_memory");
3839
// exports["test: PostGIS"] = require("./workspace/test_postgis");
3940

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
var ASSERT = require("assert");
2+
var WORKSPACE = require("geoscript/workspace");
3+
var LAYER = require("geoscript/layer");
4+
var FS = require("fs");
5+
var admin = require("../../admin");
6+
7+
var database = FS.join(admin.geopkg.dest, "geoscript.gpkg");
8+
exports.setUp = admin.geopkg.setUp;
9+
exports.tearDown = admin.geopkg.tearDown;
10+
11+
exports["test: constructor"] = function() {
12+
13+
var geopkg = new WORKSPACE.GeoPackage();
14+
15+
ASSERT.ok(geopkg instanceof WORKSPACE.Workspace, "instanceof Workspace");
16+
ASSERT.ok(geopkg instanceof WORKSPACE.GeoPackage, "instanceof GeoPackage");
17+
18+
geopkg.close();
19+
20+
};
21+
22+
exports["test: names"] = function() {
23+
24+
var geopkg = new WORKSPACE.GeoPackage({database: database});
25+
26+
ASSERT.ok(geopkg.names.indexOf("states") > -1, "geopkg.names includes 'states'");
27+
28+
geopkg.close();
29+
30+
};
31+
32+
exports["test: get"] = function() {
33+
34+
var geopkg = new WORKSPACE.GeoPackage({database: database});
35+
36+
var states = geopkg.get("states");
37+
ASSERT.ok(states instanceof LAYER.Layer, "get returns a layer instance");
38+
39+
geopkg.close();
40+
41+
};
42+
43+
if (require.main == module.id) {
44+
system.exit(require("test").run(exports));
45+
}

0 commit comments

Comments
 (0)