Skip to content

Commit a2ec197

Browse files
authored
Add Geobuf Workspace (#63)
Add Geobuf Workspace
1 parent 22e9d6b commit a2ec197

7 files changed

Lines changed: 215 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
.project
33
.settings/
44
/target/
5-
/data/
5+
/data/
6+
.idea
7+
*.iml

doc/api/workspace/geobuf.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
:class:`workspace.Geobuf`
2+
=====================
3+
4+
.. class:: workspace.Geobuf(config)
5+
6+
:arg config: ``Object`` Configuration object.
7+
8+
Create a workspace from an Geobuf directory.
9+
10+
11+
Config Properties
12+
-----------------
13+
14+
.. describe:: file
15+
16+
``String``
17+
Directory path to the Geobuf files (required).
18+
19+
Properties
20+
----------
21+
22+
.. attribute:: Geobuf.layers
23+
24+
``Array``
25+
The available layers in the workspace.
26+
27+
.. attribute:: Geobuf.names
28+
29+
``Array``
30+
The available layer names in the workspace.
31+
32+
33+
Methods
34+
-------
35+
36+
37+
.. function:: Geobuf.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:: Geobuf.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:: Geobuf.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
@@ -146,6 +146,11 @@
146146
<artifactId>gt-geopkg</artifactId>
147147
<version>${gt.version}</version>
148148
</dependency>
149+
<dependency>
150+
<groupId>org.geotools</groupId>
151+
<artifactId>gt-geobuf</artifactId>
152+
<version>${gt.version}</version>
153+
</dependency>
149154
</dependencies>
150155
<build>
151156
<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
@@ -42,6 +42,9 @@ exports.MySQL = require("./workspace/mysql").MySQL;
4242
/** api: classes[] = geopackage */
4343
exports.GeoPackage = require("./workspace/geopackage").GeoPackage;
4444

45+
/** api: classes[] = geobuf */
46+
exports.Geobuf = require("./workspace/geobuf").Geobuf;
47+
4548
/** private: classes[] = spatialite */
4649
exports.SpatiaLite = require("./workspace/spatialite").SpatiaLite;
4750

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

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
@@ -35,6 +35,7 @@ exports["test: Workspace.from_"] = function() {
3535
exports["test: Directory"] = require("./workspace/test_directory");
3636
exports["test: H2"] = require("./workspace/test_h2");
3737
exports["test: GeoPackage"] = require("./workspace/test_geopackage");
38+
exports["test: Geobu"] = require("./workspace/test_geobuf");
3839
exports["test: Memory"] = require("./workspace/test_memory");
3940
// exports["test: PostGIS"] = require("./workspace/test_postgis");
4041

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 GEOM = require("geoscript/geom");
5+
var FS = require("fs");
6+
7+
exports["test: constructor"] = function() {
8+
9+
var Files = Packages.java.nio.file.Files;
10+
var file = Files.createTempDirectory("geobuf").toFile().getAbsolutePath();
11+
var geobuf = new WORKSPACE.Geobuf({file: file});
12+
13+
ASSERT.ok(geobuf instanceof WORKSPACE.Workspace, "instanceof Workspace");
14+
ASSERT.ok(geobuf instanceof WORKSPACE.Geobuf, "instanceof Geobuf");
15+
16+
geobuf.close();
17+
18+
};
19+
20+
exports["test: create"] = function() {
21+
22+
var Files = Packages.java.nio.file.Files;
23+
var file = Files.createTempDirectory("geobuf").toFile().getAbsolutePath();
24+
var geobuf = new WORKSPACE.Geobuf({file: file});
25+
26+
var layer = new LAYER.Layer({
27+
name: "cities",
28+
fields: [{
29+
name: "name", type: "String"
30+
}, {
31+
name: "geom", type: "Point"
32+
}]
33+
});
34+
var geobufLayer = geobuf.add(layer);
35+
36+
geobufLayer.add({name: "San Francisco", geom: new GEOM.Point([-122.42, 37.78])});
37+
geobufLayer.add({name: "New York", geom: new GEOM.Point([-73.58, 40.47])});
38+
ASSERT.ok(geobufLayer.count == 2);
39+
40+
geobuf.close();
41+
};
42+
43+
if (require.main == module.id) {
44+
system.exit(require("test").run(exports));
45+
}

0 commit comments

Comments
 (0)