Skip to content

Commit 10562b1

Browse files
committed
Add Workspace.wrap static method that create the correct GeoScript Workspace for the GeoTools DataStore. Useful for getting the specific GeoScript Workspace from a connection map or connection string.
1 parent 5dd0a9d commit 10562b1

File tree

11 files changed

+213
-0
lines changed

11 files changed

+213
-0
lines changed

src/main/groovy/geoscript/workspace/Directory.groovy

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package geoscript.workspace
22

33
import geoscript.layer.Layer
44
import org.geotools.data.directory.DirectoryDataStore
5+
import org.geotools.data.shapefile.ShapefileDataStore
56
import org.geotools.data.shapefile.ShapefileDataStoreFactory
67

78
/**
@@ -30,6 +31,22 @@ class Directory extends Workspace {
3031
this(new File(dir))
3132
}
3233

34+
/**
35+
* Create a Directory Workspace from a GeoTools DirectoryDataStore
36+
* @param ds The GeoTools DirectoryDataStore
37+
*/
38+
Directory(DirectoryDataStore ds) {
39+
super(ds)
40+
}
41+
42+
/**
43+
* Create a Directory Workspace from a GeoTools ShapefileDataStore
44+
* @param ds The GeoTools ShapefileDataStore
45+
*/
46+
Directory(ShapefileDataStore ds) {
47+
super(ds)
48+
}
49+
3350
/**
3451
* Get the format
3552
* @return The workspace format name

src/main/groovy/geoscript/workspace/GeoPackage.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package geoscript.workspace
22

33
import org.geotools.data.DataStore
44
import org.geotools.geopkg.GeoPkgDataStoreFactory
5+
import org.geotools.jdbc.JDBCDataStore
56

67
/**
78
* A GeoPackage Workspace.
@@ -29,6 +30,14 @@ class GeoPackage extends Database {
2930
this(new File(fileName), userName, password)
3031
}
3132

33+
/**
34+
* Create a new GeoPackage Workspace from a GeoTools JDBCDataStore
35+
* @param ds The GeoTools JDBCDataStore
36+
*/
37+
GeoPackage(JDBCDataStore ds) {
38+
super(ds)
39+
}
40+
3241
/**
3342
* Get the format
3443
* @return The workspace format name

src/main/groovy/geoscript/workspace/H2.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package geoscript.workspace
33
import org.geotools.data.DataStore
44
import org.geotools.data.h2.H2DataStoreFactory
55
import org.geotools.data.h2.H2JNDIDataStoreFactory
6+
import org.geotools.jdbc.JDBCDataStore
67

78
/**
89
* A H2 Workspace connects to a spatially enabled H2 database.
@@ -73,6 +74,14 @@ class H2 extends Database {
7374
options.get("user", "sa"), options.get("password", ""))
7475
}
7576

77+
/**
78+
* Create a new H2 Workspace from a GeoTools JDBCDataStore
79+
* @param ds The GeoTools JDBCDataStore
80+
*/
81+
H2(JDBCDataStore ds) {
82+
super(ds)
83+
}
84+
7685
/**
7786
* Get the format
7887
* @return The workspace format name

src/main/groovy/geoscript/workspace/Memory.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ class Memory extends Workspace {
2222
super(new MemoryDataStore())
2323
}
2424

25+
/**
26+
* Create a new Memory Workspace from a GeoTools MemoryDataStore
27+
* @param ds The MemoryDataStore
28+
*/
29+
Memory(MemoryDataStore ds) {
30+
super(ds)
31+
}
32+
2533
/**
2634
* Get the format
2735
* @return The workspace format name

src/main/groovy/geoscript/workspace/MySQL.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package geoscript.workspace
33
import org.geotools.data.DataStore
44
import org.geotools.data.mysql.MySQLDataStoreFactory
55
import org.geotools.data.mysql.MySQLJNDIDataStoreFactory
6+
import org.geotools.jdbc.JDBCDataStore
67

78
/**
89
* A MySQL Workspace connects to a MySQL database.
@@ -41,6 +42,14 @@ class MySQL extends Database {
4142
options.get("user", System.getProperty("user.name")), options.get("password")))
4243
}
4344

45+
/**
46+
* Create a new MySQL Workspace from a GeoTools JDBCDataStore
47+
* @param ds The GeoTools JDBCDataStore
48+
*/
49+
MySQL(JDBCDataStore ds) {
50+
super(ds)
51+
}
52+
4453
/**
4554
* Get the format
4655
* @return The workspace format name

src/main/groovy/geoscript/workspace/PostGIS.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package geoscript.workspace
33
import org.geotools.data.DataStore
44
import org.geotools.data.postgis.PostgisNGDataStoreFactory
55
import org.geotools.data.postgis.PostgisNGJNDIDataStoreFactory
6+
import org.geotools.jdbc.JDBCDataStore
67

78
/**
89
* A PostGIS Workspace connects to a PostGIS database.
@@ -50,6 +51,14 @@ class PostGIS extends Database {
5051
)
5152
}
5253

54+
/**
55+
* Create a new PostGIS Workspace from a GeoTools JDBCDataStore
56+
* @param ds The GeoTools JDBCDataStore
57+
*/
58+
PostGIS(JDBCDataStore ds) {
59+
super(ds)
60+
}
61+
5362
/**
5463
* Get the format
5564
* @return The workspace format name

src/main/groovy/geoscript/workspace/Property.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package geoscript.workspace
22

33
import geoscript.layer.Layer
44
import org.geotools.data.DataStore
5+
import org.geotools.data.property.PropertyDataStore
56
import org.geotools.data.property.PropertyDataStoreFactory
67

78
/**
@@ -30,6 +31,14 @@ class Property extends Workspace {
3031
this(new File(directory).absoluteFile)
3132
}
3233

34+
/**
35+
* Create a new Property Workspace from a GeoTools PropertyDataStore
36+
* @param ds The GeoTools PropertyDataStore
37+
*/
38+
Property(PropertyDataStore ds) {
39+
super(ds)
40+
}
41+
3342
/**
3443
* Create a new Property Workspace with a directory
3544
* @param directory The directory

src/main/groovy/geoscript/workspace/SpatiaLite.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package geoscript.workspace
22

33
import org.geotools.data.DataStore
44
import org.geotools.data.spatialite.SpatiaLiteDataStoreFactory
5+
import org.geotools.jdbc.JDBCDataStore
56

67
/**
78
* A SpatiaLite Workspace connects to a SpatiaLite database.
@@ -30,6 +31,14 @@ class SpatiaLite extends Database {
3031
this(name, new File(dir).absoluteFile)
3132
}
3233

34+
/**
35+
* Create a new SpatiaLite Workspace from a GeoTools JDBCDataStore
36+
* @param ds The GeoTools JDBCDataStore
37+
*/
38+
SpatiaLite(JDBCDataStore ds) {
39+
super(ds)
40+
}
41+
3342
/**
3443
* Get the format
3544
* @return The workspace format name

src/main/groovy/geoscript/workspace/WFS.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class WFS extends Workspace {
3232
super(new WFSDataStoreFactory().createDataStore(createParams(url, options)))
3333
}
3434

35+
/**
36+
* Create a new WFS Workspace from a GeoTools WFSDataStore
37+
* @param ds The GeoTools WFSDataStore
38+
*/
39+
WFS(WFSDataStore ds) {
40+
super(ds)
41+
}
42+
3543
/**
3644
* Get the format
3745
* @return The workspace format name

src/main/groovy/geoscript/workspace/Workspace.groovy

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,75 @@ class Workspace {
307307
[key: param.name, type: param.type.name, required: param.required]
308308
}
309309
}
310+
311+
/**
312+
* Get a Workspace from a parameter string
313+
* @param paramString The parameter string
314+
* @return A Workspace or null
315+
*/
316+
static Workspace getWorkspace(String paramString) {
317+
getWorkspace(getParametersFromString(paramString))
318+
}
319+
320+
/**
321+
* Get a Workspace from a connection paramater Map
322+
* @param params The Map of connection parameters
323+
* @return A Workspace or null
324+
*/
325+
static Workspace getWorkspace(Map params) {
326+
wrap(DataStoreFinder.getDataStore(params))
327+
}
328+
329+
/**
330+
* Wrap a GeoTools DataStore in the appropriate GeoScript Workspace
331+
* @param ds The GeoTools DataStore
332+
* @return A GeoScript Workspace or null
333+
*/
334+
static Workspace wrap(DataStore ds) {
335+
if (ds == null) {
336+
null
337+
}
338+
else if (ds instanceof org.geotools.data.directory.DirectoryDataStore ||
339+
ds instanceof org.geotools.data.shapefile.ShapefileDataStore) {
340+
new Directory(ds)
341+
}
342+
else if (ds instanceof org.geotools.data.memory.MemoryDataStore) {
343+
new Memory(ds)
344+
}
345+
else if (ds instanceof org.geotools.data.property.PropertyDataStore) {
346+
new Property(ds)
347+
}
348+
else if (ds instanceof org.geotools.data.wfs.WFSDataStore) {
349+
new WFS(ds)
350+
}
351+
else if (ds instanceof org.geotools.jdbc.JDBCDataStore) {
352+
def jdbcds = ds as org.geotools.jdbc.JDBCDataStore
353+
if (jdbcds.dataStoreFactory instanceof org.geotools.geopkg.GeoPkgDataStoreFactory) {
354+
new GeoPackage(ds)
355+
}
356+
else if (jdbcds.dataStoreFactory instanceof org.geotools.data.h2.H2DataStoreFactory ||
357+
jdbcds.dataStoreFactory instanceof org.geotools.data.h2.H2JNDIDataStoreFactory) {
358+
new H2(ds)
359+
}
360+
else if (jdbcds.dataStoreFactory instanceof org.geotools.data.mysql.MySQLDataStoreFactory ||
361+
jdbcds.dataStoreFactory instanceof org.geotools.data.mysql.MySQLJNDIDataStoreFactory) {
362+
new MySQL(ds)
363+
}
364+
else if (jdbcds.dataStoreFactory instanceof org.geotools.data.postgis.PostgisNGDataStoreFactory ||
365+
jdbcds.dataStoreFactory instanceof org.geotools.data.postgis.PostgisNGJNDIDataStoreFactory) {
366+
new PostGIS(ds)
367+
}
368+
else if (jdbcds.dataStoreFactory instanceof org.geotools.data.spatialite.SpatiaLiteDataStoreFactory ||
369+
jdbcds.dataStoreFactory instanceof org.geotools.data.spatialite.SpatiaLiteJNDIDataStoreFactory) {
370+
new SpatiaLite(ds)
371+
}
372+
else (ds instanceof org.geotools.jdbc.JDBCDataStore) {
373+
new Database(ds)
374+
}
375+
}
376+
else {
377+
new Workspace(ds)
378+
}
379+
}
380+
310381
}

0 commit comments

Comments
 (0)