|
1 |
| -package org.geoscript.workspace |
| 1 | +package org.geoscript |
2 | 2 |
|
3 | 3 | import java.io.{File, Serializable}
|
4 | 4 | import org.geoscript.feature._
|
5 | 5 | import org.geoscript.layer._
|
6 | 6 | import org.{geotools => gt}
|
7 | 7 | import scala.collection.JavaConversions._
|
8 | 8 |
|
9 |
| -class Workspace( |
10 |
| - val underlying: gt.data.DataStore, |
11 |
| - val params: java.util.HashMap[String, java.io.Serializable] |
12 |
| -) { |
13 |
| - def count = underlying.getTypeNames.length |
14 |
| - def names: Seq[String] = underlying.getTypeNames |
15 |
| - def layer(theName: String): Layer = underlying.getFeatureSource(theName) |
| 9 | +package object workspace { |
| 10 | + type Workspace = org.geotools.data.DataStore |
16 | 11 |
|
17 |
| - def create(name: String, fields: Field*): Layer = create(name, fields) |
| 12 | + implicit class RichWorkspace(val workspace: Workspace) { |
| 13 | + def count = workspace.getTypeNames.length |
| 14 | + def names: Seq[String] = workspace.getTypeNames |
| 15 | + def layer(theName: String): Layer = workspace.getFeatureSource(theName) |
18 | 16 |
|
19 |
| - def create(name: String, fields: Traversable[Field]): Layer = { |
20 |
| - val builder = new gt.feature.simple.SimpleFeatureTypeBuilder |
21 |
| - builder.setName(name) |
22 |
| - fields foreach { |
23 |
| - case field: GeoField => |
24 |
| - builder.crs(field.projection) |
25 |
| - builder.add(field.name, field.binding) |
26 |
| - case field => |
27 |
| - builder.add(field.name, field.binding) |
| 17 | + def create(name: String, fields: Field*): Layer = create(name, fields) |
| 18 | + |
| 19 | + def create(name: String, fields: Traversable[Field]): Layer = { |
| 20 | + val builder = new gt.feature.simple.SimpleFeatureTypeBuilder |
| 21 | + builder.setName(name) |
| 22 | + fields foreach { |
| 23 | + case field: GeoField => |
| 24 | + builder.crs(field.projection) |
| 25 | + builder.add(field.name, field.binding) |
| 26 | + case field => |
| 27 | + builder.add(field.name, field.binding) |
| 28 | + } |
| 29 | + workspace.createSchema(builder.buildFeatureType()) |
| 30 | + layer(name) |
28 | 31 | }
|
29 |
| - underlying.createSchema(builder.buildFeatureType()) |
30 |
| - layer(name) |
| 32 | + |
| 33 | + def create(schema: Schema): Layer = create(schema.name, schema.fields: _*) |
31 | 34 | }
|
32 |
| - |
33 |
| - def create(schema: Schema): Layer = create(schema.name, schema.fields: _*) |
34 |
| - override def toString = "<Workspace: %s>".format(params) |
35 | 35 | }
|
36 | 36 |
|
37 |
| -object Workspace { |
38 |
| - def apply(params: Pair[String, java.io.Serializable]*): Workspace = { |
39 |
| - val jparams = new java.util.HashMap[String, java.io.Serializable]() |
40 |
| - jparams.putAll(params.toMap[String, java.io.Serializable]) |
41 |
| - new Workspace( |
42 |
| - org.geotools.data.DataStoreFinder.getDataStore(jparams), |
43 |
| - jparams |
44 |
| - ) |
| 37 | +package workspace { |
| 38 | + object Memory { |
| 39 | + def apply() = new org.geotools.data.memory.MemoryDataStore() |
45 | 40 | }
|
46 |
| -} |
47 | 41 |
|
48 |
| -object Memory { |
49 |
| - def apply() = |
50 |
| - new Workspace( |
51 |
| - new gt.data.memory.MemoryDataStore(), |
52 |
| - new java.util.HashMap |
53 |
| - ) |
54 |
| -} |
| 42 | + object Postgis { |
| 43 | + val factory = new gt.data.postgis.PostgisNGDataStoreFactory |
| 44 | + val create: (java.util.HashMap[_,_]) => gt.data.DataStore = |
| 45 | + factory.createDataStore |
55 | 46 |
|
56 |
| -object Postgis { |
57 |
| - val factory = new gt.data.postgis.PostgisNGDataStoreFactory |
58 |
| - val create: (java.util.HashMap[_,_]) => gt.data.DataStore = |
59 |
| - factory.createDataStore |
60 |
| - |
61 |
| - def apply(params: (String,java.io.Serializable)*) = { |
62 |
| - val connection = new java.util.HashMap[String,java.io.Serializable] |
63 |
| - connection.put("port", "5432") |
64 |
| - connection.put("host", "localhost") |
65 |
| - connection.put("user", "postgres") |
66 |
| - connection.put("passwd","") |
67 |
| - connection.put("charset","utf-8") |
68 |
| - connection.put("dbtype", "postgis") |
69 |
| - for ((key,value) <- params) { |
70 |
| - connection.put(key,value) |
71 |
| - } |
72 |
| - new Workspace(create(connection), connection) |
73 |
| - } |
74 |
| -} |
| 47 | + def apply(params: (String,java.io.Serializable)*) = { |
| 48 | + val connection = new java.util.HashMap[String,java.io.Serializable] |
| 49 | + connection.put("port", "5432") |
| 50 | + connection.put("host", "localhost") |
| 51 | + connection.put("user", "postgres") |
| 52 | + connection.put("passwd","") |
| 53 | + connection.put("charset","utf-8") |
| 54 | + connection.put("dbtype", "postgis") |
| 55 | + for ((key,value) <- params) { |
| 56 | + connection.put(key,value) |
| 57 | + } |
| 58 | + create(connection) |
| 59 | + } |
| 60 | + } |
75 | 61 |
|
76 |
| -object SpatiaLite { |
77 |
| - val factory = new gt.data.spatialite.SpatiaLiteDataStoreFactory |
78 |
| - private val create: (java.util.HashMap[_,_]) => gt.data.DataStore = |
79 |
| - factory.createDataStore |
| 62 | + object SpatiaLite { |
| 63 | + val factory = new gt.data.spatialite.SpatiaLiteDataStoreFactory |
| 64 | + private val create: (java.util.HashMap[_,_]) => gt.data.DataStore = |
| 65 | + factory.createDataStore |
80 | 66 |
|
81 |
| - def apply(params: (String,java.io.Serializable)*) = { |
82 |
| - val connection = new java.util.HashMap[String,java.io.Serializable] |
83 |
| - connection.put("dbtype","spatialite") |
84 |
| - for ((key,value) <- params) { |
85 |
| - connection.put(key,value) |
86 |
| - } |
87 |
| - new Workspace(create(connection), connection) |
88 |
| - } |
89 |
| -} |
| 67 | + def apply(params: (String,java.io.Serializable)*) = { |
| 68 | + val connection = new java.util.HashMap[String,java.io.Serializable] |
| 69 | + connection.put("dbtype","spatialite") |
| 70 | + for ((key,value) <- params) { |
| 71 | + connection.put(key,value) |
| 72 | + } |
| 73 | + create(connection: java.util.HashMap[_,_]) |
| 74 | + } |
| 75 | + } |
90 | 76 |
|
91 |
| -object Directory { |
92 |
| - private val factory = new gt.data.shapefile.ShapefileDataStoreFactory |
| 77 | + object Directory { |
| 78 | + private val factory = new gt.data.shapefile.ShapefileDataStoreFactory |
93 | 79 |
|
94 |
| - def apply(path: String): Workspace = apply(new File(path)) |
| 80 | + def apply(path: String): Workspace = apply(new File(path)) |
95 | 81 |
|
96 |
| - def apply(path: File): Workspace = { |
97 |
| - val params = new java.util.HashMap[String, java.io.Serializable] |
98 |
| - params.put("url", path.toURI.toURL) |
99 |
| - val store = factory.createDataStore(params: java.util.Map[_, _]) |
100 |
| - new Workspace(store, params) { |
101 |
| - override def toString = "<Directory: [%s]>".format(params.get("url")) |
| 82 | + def apply(path: File): Workspace = { |
| 83 | + val params = new java.util.HashMap[String, java.io.Serializable] |
| 84 | + params.put("url", path.toURI.toURL) |
| 85 | + factory.createDataStore(params: java.util.Map[_, _]) |
102 | 86 | }
|
103 | 87 | }
|
104 | 88 | }
|
0 commit comments