Skip to content

Commit d6dbe2a

Browse files
committed
Work on getting examples module building again
1 parent e7ed998 commit d6dbe2a

File tree

12 files changed

+421
-139
lines changed

12 files changed

+421
-139
lines changed

examples/src/main/scala/example/ColorRamp.scala

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@ package org.geoscript.example
33
import org.geoscript.feature._
44
import org.geoscript.layer._
55
import org.geoscript.style._
6-
import org.geoscript.style.builder._
76

87
object ColorRamp extends org.geoscript.feature.GeoCrunch {
9-
def main(args: Array[String]) = {
10-
val Array(shapefile, property, sldfile) = args take 3
8+
// def main(args: Array[String]) = {
9+
// val Array(shapefile, property, sldfile) = args take 3
1110

12-
val shp = Shapefile(shapefile)
13-
val style = colorRamp(shp, property)
11+
// val shp = Shapefile(shapefile)
12+
// val style = colorRamp(shp, property)
1413

15-
val xformer = new org.geotools.styling.SLDTransformer
16-
val sldStream = new java.io.FileOutputStream(sldfile)
17-
xformer.setIndentation(2)
18-
xformer.transform(style, sldStream)
19-
sldStream.flush()
20-
sldStream.close()
21-
}
14+
// val xformer = new org.geotools.styling.SLDTransformer
15+
// val sldStream = new java.io.FileOutputStream(sldfile)
16+
// xformer.setIndentation(2)
17+
// xformer.transform(style, sldStream)
18+
// sldStream.flush()
19+
// sldStream.close()
20+
// }
2221

23-
def hex(c: java.awt.Color): Paint =
24-
Color(literal(
25-
"#%02x02x02x".format(c.getRed, c.getGreen, c.getBlue)))
22+
// def hex(c: java.awt.Color): Paint =
23+
// Color(literal(
24+
// "#%02x02x02x".format(c.getRed, c.getGreen, c.getBlue)))
2625

27-
def colorRamp(data: Layer, propertyName: String): Style = {
28-
val propertyView = data.features.view.map(f => f.get[Double](propertyName))
29-
val min = propertyView.min
30-
val max = propertyView.max
26+
// def colorRamp(data: Layer, propertyName: String): Style = {
27+
// val propertyView = data.features.view.map(f => f.get[Double](propertyName))
28+
// val min = propertyView.min
29+
// val max = propertyView.max
3130

32-
val k = 10
33-
val breaks = for (i <- (0 to k)) yield (i * max + (k - i) * min) / k
34-
val ranges = (breaks sliding 2).toSeq
35-
val colors = (Seq.iterate(java.awt.Color.RED, k){ _.darker }).reverse
36-
val rules =
37-
for {
38-
(Seq(min, max), color) <- ranges zip colors
39-
filter = "%s BETWEEN %f AND %f".format(propertyName, min, max)
40-
} yield
41-
Fill(hex(color)) where cql(filter)
42-
rules.reduce(_ and _).build
43-
}
31+
// val k = 10
32+
// val breaks = for (i <- (0 to k)) yield (i * max + (k - i) * min) / k
33+
// val ranges = (breaks sliding 2).toSeq
34+
// val colors = (Seq.iterate(java.awt.Color.RED, k){ _.darker }).reverse
35+
// val rules =
36+
// for {
37+
// (Seq(min, max), color) <- ranges zip colors
38+
// filter = "%s BETWEEN %f AND %f".format(propertyName, min, max)
39+
// } yield
40+
// Fill(hex(color)) where cql(filter)
41+
// rules.reduce(_ and _).build
42+
// }
43+
???
4444
}

examples/src/main/scala/example/FirstProject.scala

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
11
package org.geoscript.example
22

3-
import org.geoscript._
3+
import scala.collection.JavaConverters._
4+
import org.geoscript.feature._
5+
import org.geoscript.layer._
6+
import org.geoscript.workspace._
47

5-
object Identify extends App {
6-
val shp = layer.Shapefile(args(0))
7-
println("Schema for %s".format(shp.schema.name))
8-
for (field <- shp.schema.fields) println(field)
8+
/**
9+
* An example app for printing out some info about the schema of a shapefile.
10+
*/
11+
object Identify extends App {
12+
// by extending the scala.App trait from the Scala standard library, we avoid
13+
// writing the def main(args: Array[String]) that is otherwise required for
14+
// an object to be executable.
15+
16+
if (args.isEmpty) {
17+
// The variable `args` provided by scala.App is a Seq[String] containing the
18+
// command line arguments. We need at least one so we can interpret it as a
19+
// path!
20+
println(
21+
""" |Usage: Identify <path>
22+
|An example script demonstrating the use of GeoScript to display a
23+
|Shapefile's schema.
24+
|""".stripMargin)
25+
System.exit(0)
26+
}
27+
28+
// we take the "head" (first element) of the command line parameters to use as
29+
// the path to the Shapefile we will be inspecting.
30+
val path: String = args.head
31+
32+
// Since the ShapefileDatastore constructor requires a URL, we need to perform
33+
// some gymnastics. By constructing a java.io.File, we can handle relative
34+
// paths. Converting to a URI before converting to URL is recomended because
35+
// of some problems with file path handling in the Java standard library.
36+
// GeoScript doesn't attempt to address this issue, but the Scala-IO and
37+
// Rapture.IO projects are two options that you could investigate for more
38+
// convenient path handling.
39+
val url: java.net.URL = new java.io.File(path).toURI.toURL
40+
41+
// Now we construct a Workspace for the Shapefile. Consult the GeoTools
42+
// documentation for other types of DataStore that may be used.
43+
val workspace: Workspace = new org.geotools.data.shapefile.ShapefileDataStore(url)
44+
45+
// Because we know that a Shapefile contains exactly one layer, we can simply
46+
// use the first and only layer from the workspace.
47+
val layer: Layer = workspace.layers.head
48+
49+
// Now we build up a list of strings (we will concatenate them and print
50+
// them all at once.)
51+
val descriptionLines: Seq[String] =
52+
// Prefixing a string literal with 's' allows us to use ${} syntax for
53+
// embedding Scala expressions.
54+
Seq(s"Schema for ${layer.schema.name}") ++
55+
// Using an 'f' prefix works similarly, but also allows us to use
56+
// printf-style formatting.
57+
layer.schema.fields.map(fld => f"${fld.name}%10s: ${fld.binding.getSimpleName}")
58+
59+
println(descriptionLines.mkString("\n"))
960
}
Lines changed: 133 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,138 @@
11
package org.geoscript.example
22

3-
import org.geoscript._
4-
5-
object Intersections {
6-
def process(src: layer.Layer, dest: layer.Layer, joinField: String) {
7-
println("Processing %s".format(src.schema.name))
8-
9-
for (feat <- src.features) {
10-
val intersections =
11-
src.filter(filter.Filter.intersects(feat.geometry))
12-
dest ++=
13-
intersections.filter(_.id > feat.id).map { corner =>
14-
feature.Feature(
15-
"geom" -> (feat.geometry intersection corner.geometry),
16-
(joinField + "Left") -> feat.get[Any](joinField),
17-
(joinField + "Right") -> corner.get[Any](joinField)
18-
)
19-
}
20-
}
21-
22-
println("Found %d intersections".format(dest.count))
23-
}
3+
import org.geoscript.feature._
4+
import org.geoscript.filter._
5+
import org.geoscript.filter.builder._
6+
import org.geoscript.geometry._
7+
import org.geoscript.layer._
8+
import org.geoscript.workspace._
9+
10+
/**
11+
* An example app for creating a shapefile containing all intersections between
12+
* two input Shapefiles
13+
*/
14+
object Intersections extends App {
15+
// by extending the scala.App trait from the Scala standard library, we avoid
16+
// writing the def main(args: Array[String]) that is otherwise required for
17+
// an object to be executable.
2418

25-
def rewrite(schema: feature.Schema, fieldName: String): feature.Schema =
26-
feature.Schema(
27-
schema.name + "_intersections",
28-
feature.Field(
29-
"geom",
30-
classOf[com.vividsolutions.jts.geom.Geometry],
31-
schema.geometry.projection
32-
),
33-
feature.Field(fieldName + "Left", classOf[String]),
34-
feature.Field(fieldName + "Right", classOf[String])
35-
)
36-
37-
def main(args: Array[String]) = {
38-
if (args.length == 0) {
39-
println("You need to provide the path to a shapefile as an argument to this example.")
40-
} else {
41-
val src = layer.Shapefile(args(0))
42-
val joinField =
43-
src.schema.fields.find { _.gtBinding == classOf[String] } match {
44-
case Some(f) => f.name
45-
case None => "id"
46-
}
47-
val dest = src.workspace.create(rewrite(src.schema, joinField))
48-
process(src, dest, joinField)
49-
}
19+
if (args.size < 3) {
20+
// We need three arguments: first shapefile to scan, second shapefile to
21+
// scan, shapefile to store results.
22+
println(
23+
""" |Usage: Intersections <first shapefile> <second shapefile> <output shapefile>
24+
|Computes all intersections between the two input shapefiles and stores the
25+
|results in the output shapefile. The output will also have two fields
26+
|named left_id and right_id containing the ids of the features that
27+
|intersected. (This is just an example - NOTE that shapefile features do not
28+
|have stable identifiers.)""".stripMargin)
29+
System.exit(0)
5030
}
31+
32+
// for convenience, we create a little function for connecting to shapefiles.
33+
val connect = (path: String) =>
34+
new org.geotools.data.shapefile.ShapefileDataStore(
35+
new java.io.File(path).toURI.toURL): Workspace
36+
37+
// Here we use a pattern match to concisely extract the arguments into
38+
// individual variables.
39+
val Array(leftPath, rightPath, outputPath) = (args take 3)
40+
val leftLayer = connect(leftPath).layers.head
41+
val rightLayer = connect(rightPath).layers.head
42+
43+
// There are a few different ways to compute the intersections.
44+
// The simplest is to use a Scala for-comprehension.
45+
// val intersections =
46+
// for {
47+
// l <- leftLayer.features
48+
// r <- rightLayer.features
49+
// if l.geometry intersects r.geometry
50+
// } yield (l.geometry intersection r.geometry, l.id, r.id)
51+
52+
// This produces correct results, but there are some performance problems.
53+
// * It fetches all features from the 'right' layer on each step of iterating
54+
// through the 'left' layer. This might mean a lot of disk access!
55+
// * The results are stored in memory. Since we're just going to write the
56+
// features to a new shapefile it would be nice to avoid that. It would
57+
// save some memory, and also might complete faster if we can start writing
58+
// the results before we finish finding all the intersections.
59+
//
60+
// We can avoid repetitive requests to the underlying store by copying all the
61+
// features into an in-memory collection before scanning.
62+
63+
// val leftFeatures = leftLayer.features.to[Vector]
64+
// val rightFeatures = rightLayer.features.to[Vector]
65+
66+
// val intersections2 =
67+
// for {
68+
// l <- leftFeatures
69+
// r <- rightFeatures
70+
// if l.geometry intersects r.geometry
71+
// } yield (l.geometry intersection r.geometry, l.id, r.id)
72+
73+
// This trades off memory in order to speed up the processing, so it's
74+
// still only going to work for small datasets. Instead of performing the
75+
// filtering in Scala code, we can use the GeoTools Query system to have it
76+
// performed by the datastore itself. Depending on the datastore filters will
77+
// be more or less completely executed by the underlying engine. For example,
78+
// filters executed against a Postgis database can be largely converted to
79+
// SQL. For Shapefiles most filter operations are executed in-process, but
80+
// they are at least able to take advantage of a spatial index.
81+
82+
val intersections3 =
83+
for {
84+
l <- leftLayer.features
85+
r <- rightLayer.filter(
86+
Literal(l.geometry) intersects Property(rightLayer.schema.geometryField.name))
87+
} yield (l.geometry intersection r.geometry, l.id, r.id)
88+
89+
intersections3.foreach(println)
90+
91+
// require(intersections.toSeq == intersections.toSeq.distinct)
92+
93+
// def process(src: layer.Layer, dest: layer.Layer, joinField: String) {
94+
// println("Processing %s".format(src.schema.name))
95+
96+
// for (feat <- src.features) {
97+
// val intersections =
98+
// src.filter(filter.Filter.intersects(feat.geometry))
99+
// dest ++=
100+
// intersections.filter(_.id > feat.id).map { corner =>
101+
// feature.Feature(
102+
// "geom" -> (feat.geometry intersection corner.geometry),
103+
// (joinField + "Left") -> feat.get[Any](joinField),
104+
// (joinField + "Right") -> corner.get[Any](joinField)
105+
// )
106+
// }
107+
// }
108+
109+
// println("Found %d intersections".format(dest.count))
110+
// }
111+
112+
// def rewrite(schema: feature.Schema, fieldName: String): feature.Schema =
113+
// feature.Schema(
114+
// schema.name + "_intersections",
115+
// feature.Field(
116+
// "geom",
117+
// classOf[com.vividsolutions.jts.geom.Geometry],
118+
// schema.geometry.projection
119+
// ),
120+
// feature.Field(fieldName + "Left", classOf[String]),
121+
// feature.Field(fieldName + "Right", classOf[String])
122+
// )
123+
124+
// def main(args: Array[String]) = {
125+
// if (args.length == 0) {
126+
// println("You need to provide the path to a shapefile as an argument to this example.")
127+
// } else {
128+
// val src = layer.Shapefile(args(0))
129+
// val joinField =
130+
// src.schema.fields.find { _.gtBinding == classOf[String] } match {
131+
// case Some(f) => f.name
132+
// case None => "id"
133+
// }
134+
// val dest = src.workspace.create(rewrite(src.schema, joinField))
135+
// process(src, dest, joinField)
136+
// }
137+
// }
51138
}

examples/src/main/scala/example/Postgis.scala

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import org.geoscript.projection._
77
import org.geoscript.workspace._
88

99
object PostgisTest extends App {
10-
val conflict = Postgis("database" -> "conflict")
11-
val fields = conflict.layer("conflictsite").schema.fields
12-
13-
for (field <- fields) println(field.name)
14-
val workSpaceTest = Postgis()
15-
16-
val test = workSpaceTest.create("test",
17-
Field("name", classOf[String]),
18-
Field("geom", classOf[Geometry], lookupEPSG("EPSG:4326").get)
19-
)
10+
// val conflict = Postgis("database" -> "conflict")
11+
// val fields = conflict.layer("conflictsite").schema.fields
12+
//
13+
// for (field <- fields) println(field.name)
14+
// val workSpaceTest = Postgis()
15+
//
16+
// val test = workSpaceTest.create("test",
17+
// Field("name", classOf[String]),
18+
// Field("geom", classOf[Geometry], lookupEPSG("EPSG:4326").get)
19+
// )
2020

21-
test += Feature("name" -> "test", "geom" -> Point(43,74))
21+
// test += Feature("name" -> "test", "geom" -> Point(43,74))
22+
???
2223
}

examples/src/main/scala/example/Render.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ import org.geoscript.layer.Shapefile,
77
org.geoscript.io.Sink
88

99
object Render {
10-
def reference(e: org.geoscript.geometry.Envelope, p: Projection) =
11-
new org.geotools.geometry.jts.ReferencedEnvelope(e, p)
10+
// def reference(e: org.geoscript.geometry.Envelope, p: Projection) =
11+
// new org.geotools.geometry.jts.ReferencedEnvelope(e, p)
1212

13-
def main(args: Array[String]) {
14-
val states = Shapefile("geoscript/src/test/resources/data/states.shp")
15-
val theme = CSS.fromFile("geocss/src/test/resources/states.css")
16-
val frame = (1024, 1024)
17-
val viewport = Viewport.pad(reference(states.envelope, LatLon), frame)
18-
render(
19-
viewport,
20-
Seq(MapLayer(states, theme))
21-
) on PNG(Sink.file("states.png"), frame)
22-
}
13+
// def main(args: Array[String]) {
14+
// val states = Shapefile("geoscript/src/test/resources/data/states.shp")
15+
// val theme = CSS.fromFile("geocss/src/test/resources/states.css")
16+
// val frame = (1024, 1024)
17+
// val viewport = Viewport.pad(reference(states.envelope, LatLon), frame)
18+
// render(
19+
// viewport,
20+
// Seq(MapLayer(states, theme))
21+
// ) on PNG(Sink.file("states.png"), frame)
22+
// }
23+
???
2324
}

0 commit comments

Comments
 (0)