11package geoscript.workspace
22
3+ import geoscript.feature.Schema
4+ import geoscript.layer.Cursor
5+ import geoscript.layer.Layer
36import org.geotools.data.DataStore
4- import org.geotools.data.spatialite.SpatiaLiteDataStoreFactory
5- import org.geotools.jdbc.JDBCDataStore
7+ import org.geotools.data.ogr.OGRDataStore
68
79/**
810 * A SpatiaLite Workspace connects to a SpatiaLite database.
911 * <p ><blockquote ><pre >
10- * SpatiaLite spatialite = new SpatiaLite("db.sqlite", "databases" )
12+ * SpatiaLite spatialite = new SpatiaLite("db.sqlite")
1113 * </pre></blockquote></p>
1214 * @author Jared Erickson
1315 */
14- class SpatiaLite extends Database {
16+ class SpatiaLite extends OGR {
1517
16- /**
17- * Create a new SpatiaLite Workspace from a name and directory
18- * @param name The name of the database
19- * @param dir The File directory containing the database
20- */
21- SpatiaLite (String name , File dir ) {
22- super (createDataStore(name, dir))
18+ SpatiaLite (File file ) {
19+ this (file. absolutePath)
2320 }
2421
25- /**
26- * Create a new SpatiaLite Workspace from a name and directory
27- * @param name The name of the database
28- * @param dir The directory name containing the database
29- */
30- SpatiaLite (String name , String dir ) {
31- this (name, new File (dir). absoluteFile)
22+ SpatiaLite (String fileName ) {
23+ super (" SQLite" , fileName)
3224 }
3325
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)
26+ protected SpatiaLite (OGRDataStore dataStore ) {
27+ super (dataStore)
28+ }
29+
30+ @Override
31+ Layer create (Map options , Schema schema , boolean write ) {
32+ (options. get(" options" ,[]) as List ). add(" SPATIALITE=YES" )
33+ super . create(options, schema, write)
34+ }
35+
36+ @Override
37+ Layer create (Map options = [:], Cursor c ) {
38+ (options. get(" options" ,[]) as List ). add(" SPATIALITE=YES" )
39+ super . create(options, c)
40+ }
41+
42+ @Override
43+ Layer add (Map options , Layer layer ) {
44+ (options. get(" options" ,[]) as List ). add(" SPATIALITE=YES" )
45+ super . add(options, layer)
46+ }
47+
48+ @Override
49+ Layer add (Map options , Layer layer , String name , int chunk = 1000 ) {
50+ (options. get(" options" ,[]) as List ). add(" SPATIALITE=YES" )
51+ super . add(options, layer, name, chunk)
4052 }
4153
4254 /**
@@ -48,14 +60,12 @@ class SpatiaLite extends Database {
4860 }
4961
5062 /**
51- * Create a new SpatiaLite DataStore from a name and directory
63+ * The String representation
64+ * @return A String representation
5265 */
53- private static DataStore createDataStore (String name , File dir ) {
54- Map params = [:]
55- params. put(" database" , new File (dir,name). absolutePath)
56- params. put(" dbtype" , " spatialite" )
57- SpatiaLiteDataStoreFactory f = new SpatiaLiteDataStoreFactory ()
58- f. createDataStore(params)
66+ @Override
67+ String toString () {
68+ " SpatiaLite(${ dataset} )"
5969 }
6070
6171 /**
@@ -67,8 +77,8 @@ class SpatiaLite extends Database {
6777 Map getParametersFromString (String str ) {
6878 Map params = [:]
6979 if (! str. contains(" =" ) && (str. endsWith(" .sqlite" ) || str. endsWith(" .spatialite" ))) {
70- params. put(" dbtype " , " spatialite " )
71- params. put(" database " , new File (str). absolutePath)
80+ params. put(" DriverName " , " SQLite " )
81+ params. put(" DatasourceName " , new File (str). absolutePath)
7282 } else {
7383 params = super . getParametersFromString(str)
7484 }
@@ -78,12 +88,13 @@ class SpatiaLite extends Database {
7888 @Override
7989 SpatiaLite create (String type , Map params ) {
8090 if (type. equalsIgnoreCase(' spatialite' )) {
81- params[ ' dbtype ' ] = ' spatialite '
91+ params. put( " DriverName " , " SQLite " )
8292 if (params. containsKey(' file' )) {
83- params[' database' ] = params[' file' ]
93+ Object file = params[" file" ]
94+ params. put(" DatasourceName" , file instanceof File ? file. absolutePath: new File (file. toString()). absolutePath)
8495 }
8596 if (params[' database' ] instanceof File ) {
86- params[' database ' ] = (params[' database' ] as File ). absolutePath
97+ params[' DatasourceName ' ] = (params[' database' ] as File ). absolutePath
8798 }
8899 super . create(params)
89100 } else {
@@ -94,11 +105,10 @@ class SpatiaLite extends Database {
94105 @Override
95106 SpatiaLite create (DataStore dataStore ) {
96107 SpatiaLite spatialite = null
97- if (dataStore instanceof org.geotools.jdbc.JDBCDataStore ) {
98- def jdbcds = dataStore as org.geotools.jdbc.JDBCDataStore
99- if (jdbcds. dataStoreFactory instanceof org.geotools.data.spatialite.SpatiaLiteDataStoreFactory ||
100- jdbcds. dataStoreFactory instanceof org.geotools.data.spatialite.SpatiaLiteJNDIDataStoreFactory ) {
101- spatialite = new SpatiaLite (dataStore)
108+ if (dataStore instanceof org.geotools.data.ogr.OGRDataStore ) {
109+ def ogrDataSource = dataStore as org.geotools.data.ogr.OGRDataStore
110+ if (ogrDataSource. @ogrDriver. equalsIgnoreCase(" SQLite" )) {
111+ spatialite = new SpatiaLite (ogrDataSource)
102112 }
103113 }
104114 spatialite
0 commit comments