Skip to content

Commit deacd6d

Browse files
committed
Bring Layer and Workspace up to the new style
1 parent 0354ee4 commit deacd6d

File tree

2 files changed

+80
-89
lines changed

2 files changed

+80
-89
lines changed

geoscript/src/main/scala/layer/Layer.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ package org.geoscript
22

33
import java.io.File
44

5-
// import org.opengis.feature.simple.{ SimpleFeature, SimpleFeatureType }
6-
// import org.opengis.feature.`type`.{ AttributeDescriptor, GeometryDescriptor }
7-
import org.{ geotools => gt }
85
import com.vividsolutions.jts.{ geom => jts }
96

107
import org.geoscript.feature._
118
import org.geoscript.filter._
129
import org.geoscript.geometry._
1310
import org.geoscript.projection._
14-
import org.geoscript.workspace.{Directory,Workspace}
11+
import org.geoscript.workspace._
1512

1613
package object layer {
1714
type Layer = org.geotools.data.FeatureSource[Schema, Feature]
@@ -36,23 +33,33 @@ package object layer {
3633
* operations.
3734
*/
3835
def features: FeatureCollection =
39-
source.getFeatures(new gt.data.Query)
36+
source.getFeatures(new org.geotools.data.Query)
4037

4138
/**
4239
* Get a filtered feature collection.
4340
*/
4441
def filter(pred: Filter): FeatureCollection =
45-
source.getFeatures(new gt.data.Query(name, pred))
42+
source.getFeatures(new org.geotools.data.Query(name, pred))
4643

4744
/**
4845
* Get the number of features currently in the layer.
4946
*/
50-
def count: Int = source.getCount(new gt.data.Query())
47+
def count: Int = source.getCount(new org.geotools.data.Query())
5148

5249
/**
5350
* Get the bounding box of this Layer, in the format:
5451
*/
5552
def envelope: Envelope = source.getBounds() // in schema.geometry.projection
53+
54+
/**
55+
* Test whether the data source supports modifications and return a
56+
* WritableLayer if so. Otherwise, a None is returned.
57+
*/
58+
def writable: Option[WritableLayer] =
59+
source match {
60+
case (writable: WritableLayer) => Some(writable)
61+
case _ => None
62+
}
5663
}
5764

5865
implicit class RichWritableLayer(val store: WritableLayer) extends AnyVal {
@@ -69,7 +76,7 @@ package object layer {
6976
* repeated use of += when adding multiple features.
7077
*/
7178
def ++= (features: Traversable[Feature]) {
72-
val tx = new gt.data.DefaultTransaction
79+
val tx = new org.geotools.data.DefaultTransaction
7380
val writer = dstore.getFeatureWriterAppend(store.name, tx)
7481

7582
try {
@@ -107,7 +114,7 @@ package object layer {
107114
}
108115

109116
def update(filter: Filter)(replace: Feature => Unit) {
110-
val tx = new gt.data.DefaultTransaction
117+
val tx = new org.geotools.data.DefaultTransaction
111118
val writer = filter match {
112119
case Include => dstore.getFeatureWriter(store.name, tx)
113120
case filter => dstore.getFeatureWriter(store.name, filter, tx)
Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,88 @@
1-
package org.geoscript.workspace
1+
package org.geoscript
22

33
import java.io.{File, Serializable}
44
import org.geoscript.feature._
55
import org.geoscript.layer._
66
import org.{geotools => gt}
77
import scala.collection.JavaConversions._
88

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
1611

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)
1816

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)
2831
}
29-
underlying.createSchema(builder.buildFeatureType())
30-
layer(name)
32+
33+
def create(schema: Schema): Layer = create(schema.name, schema.fields: _*)
3134
}
32-
33-
def create(schema: Schema): Layer = create(schema.name, schema.fields: _*)
34-
override def toString = "<Workspace: %s>".format(params)
3535
}
3636

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()
4540
}
46-
}
4741

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
5546

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+
}
7561

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
8066

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+
}
9076

91-
object Directory {
92-
private val factory = new gt.data.shapefile.ShapefileDataStoreFactory
77+
object Directory {
78+
private val factory = new gt.data.shapefile.ShapefileDataStoreFactory
9379

94-
def apply(path: String): Workspace = apply(new File(path))
80+
def apply(path: String): Workspace = apply(new File(path))
9581

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[_, _])
10286
}
10387
}
10488
}

0 commit comments

Comments
 (0)