Skip to content

Commit 65da420

Browse files
authored
Merge pull request #51 from geoscript/GSG-ISSUE50
SpatiaLite based on OGR
2 parents 68c4d5a + 3f4a6ef commit 65da420

File tree

7 files changed

+107
-87
lines changed

7 files changed

+107
-87
lines changed

examples/layer_spatialite.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import geoscript.layer.Layer
2+
import geoscript.workspace.SpatiaLite
3+
4+
SpatiaLite spatiaLite = new SpatiaLite("naturalearth.sqlite")
5+
6+
println "Layers:"
7+
spatiaLite.layers.each { Layer layer ->
8+
println " ${layer.name}"
9+
println " # Features = ${layer.count}"
10+
println " Schema = ${layer.schema}"
11+
}

examples/naturalearth.sqlite

5.81 MB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,6 @@
100100
<artifactId>gt-jdbc-mysql</artifactId>
101101
<version>${gt.version}</version>
102102
</dependency>
103-
<dependency>
104-
<groupId>org.geotools.jdbc</groupId>
105-
<artifactId>gt-jdbc-spatialite</artifactId>
106-
<version>20.0</version>
107-
</dependency>
108103
<dependency>
109104
<groupId>org.geotools.xsd</groupId>
110105
<artifactId>gt-xsd-wfs</artifactId>
@@ -267,25 +262,10 @@
267262
<artifactId>gt-geobuf</artifactId>
268263
<version>${gt.version}</version>
269264
</dependency>
270-
<dependency>
271-
<groupId>org.gdal</groupId>
272-
<artifactId>gdal</artifactId>
273-
<version>1.11.2</version>
274-
</dependency>
275265
<dependency>
276266
<groupId>org.geotools</groupId>
277267
<artifactId>gt-ogr-jni</artifactId>
278268
<version>${gt.version}</version>
279-
<exclusions>
280-
<exclusion>
281-
<groupId>org.geotools</groupId>
282-
<artifactId>gt-data</artifactId>
283-
</exclusion>
284-
<exclusion>
285-
<groupId>org.gdal</groupId>
286-
<artifactId>gdal</artifactId>
287-
</exclusion>
288-
</exclusions>
289269
</dependency>
290270
<dependency>
291271
<groupId>org.codehaus.groovy</groupId>
@@ -399,15 +379,10 @@
399379
<version>2.22.0</version>
400380
<configuration>
401381
<forkMode>once</forkMode>
402-
<argLine>-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=1024m -Djava.library.path="${env.GEOSCRIPT_GDAL_HOME}"</argLine>
382+
<argLine>-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=1024m</argLine>
403383
<systemPropertyVariables>
404384
<org.geotools.referencing.forceXY>true</org.geotools.referencing.forceXY>
405385
</systemPropertyVariables>
406-
<environmentVariables>
407-
<PATH>${env.GEOSCRIPT_GDAL_HOME}${path.sepatator}${env.PATH}</PATH>
408-
<DYLD_LIBRARY_PATH>${env.GEOSCRIPT_GDAL_HOME}${path.sepatator}${env.DYLD_LIBRARY_PATH}</DYLD_LIBRARY_PATH>
409-
<LD_LIBRARY_PATH>${env.GEOSCRIPT_GDAL_HOME}${path.sepatator}${env.LD_LIBRARY_PATH}</LD_LIBRARY_PATH>
410-
</environmentVariables>
411386
</configuration>
412387
</plugin>
413388
<plugin>

src/main/groovy/geoscript/workspace/OGR.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class OGR extends Workspace {
2828
/**
2929
* The driver string (http://www.gdal.org/ogr/ogr_formats.html)
3030
*/
31-
private String driver
31+
protected String driver
3232

3333
/**
3434
* The dataset string (see individual formats)
3535
*/
36-
private String dataset
36+
protected String dataset
3737

3838
/**
3939
* Create a new OGR Workspace
@@ -322,4 +322,4 @@ class OGR extends Workspace {
322322
ogr
323323
}
324324
}
325-
}
325+
}

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

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
11
package geoscript.workspace
22

3+
import geoscript.feature.Schema
4+
import geoscript.layer.Cursor
5+
import geoscript.layer.Layer
36
import 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

src/test/groovy/geoscript/workspace/OGRTestCase.groovy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class OGRTestCase {
140140
assertTrue ogr.toString().startsWith("OGR (Driver = GeoJSON, Dataset = ")
141141
assertTrue ogr.toString().endsWith("states.geojson)")
142142
assertEquals 1, ogr.names.size()
143-
assertTrue ogr.names.contains("OGRGeoJSON")
143+
assertTrue ogr.names.contains("states")
144144
assertEquals 1, ogr.layers.size()
145145
int i = 0
146146
layer.eachFeature{f ->
@@ -165,7 +165,7 @@ class OGRTestCase {
165165
assertTrue ogr.toString().startsWith("OGR (Driver = GeoJSON, Dataset = ")
166166
assertTrue ogr.toString().endsWith("states.geojson)")
167167
assertEquals 1, ogr.names.size()
168-
assertTrue ogr.names.contains("OGRGeoJSON")
168+
assertTrue ogr.names.contains("states")
169169
assertEquals 1, ogr.layers.size()
170170
int i = 0
171171
layer.eachFeature{f ->
@@ -281,4 +281,4 @@ class OGRTestCase {
281281
assertEquals 49, i
282282
}
283283
}
284-
}
284+
}
Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
package geoscript.workspace
22

3+
import geoscript.layer.Shapefile
4+
import org.junit.Rule
35
import org.junit.Test
6+
import org.junit.rules.TemporaryFolder
7+
48
import static org.junit.Assert.*
59
import geoscript.layer.Layer
6-
import geoscript.feature.Field
7-
import geoscript.feature.Schema
8-
910

1011
/**
1112
* The SpatiaLite Unit Test
1213
*/
1314
class SpatiaLiteTestCase {
1415

15-
@Test void constructrors() {
16-
17-
/*File file = new File(getClass().getClassLoader().getResource("states.shp").toURI()).parentFile
18-
assertNotNull(file)
19-
20-
SpatiaLite spatialite = new SpatiaLite("db.sqlite", file)
21-
assertNotNull(spatialite)
22-
23-
assertEquals 1, spatialite.layers.size()
24-
assertEquals "[countries]", spatialite.layers.toString()*/
25-
assertTrue(true)
16+
@Rule
17+
public TemporaryFolder folder = new TemporaryFolder()
18+
19+
private boolean shouldRunTest() {
20+
boolean isAvailable = OGR.isAvailable()
21+
if (!isAvailable) {
22+
println "OGR is not available!"
23+
} else {
24+
OGR.setErrorHandler("quiet")
25+
}
26+
isAvailable
2627
}
2728

29+
@Test void writeReadSpatialite() {
30+
if (shouldRunTest()) {
31+
File file = new File(folder.newFolder("states_sqlite"), "states.sqlite")
32+
SpatiaLite spatialite = new SpatiaLite(file)
33+
File shpFile = new File(getClass().getClassLoader().getResource("states.shp").toURI())
34+
Layer shpLayer = new Shapefile(shpFile)
35+
Layer layer = spatialite.create(shpLayer.cursor)
36+
assertNotNull layer
37+
assertEquals "SpatiaLite", spatialite.format
38+
assertTrue spatialite.toString().startsWith("SpatiaLite(")
39+
assertTrue spatialite.toString().endsWith("states.sqlite)")
40+
assertEquals 1, spatialite.names.size()
41+
assertTrue spatialite.names.contains("states")
42+
assertEquals 1, spatialite.layers.size()
43+
int i = 0
44+
layer.eachFeature{f ->
45+
assertNotNull f.geom
46+
assertNotNull f['state_name']
47+
i++
48+
}
49+
assertEquals 49, i
50+
}
51+
}
2852
}
2953

0 commit comments

Comments
 (0)