/
GeoScriptTestCase.groovy
256 lines (230 loc) · 9.49 KB
/
GeoScriptTestCase.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package geoscript
import geoscript.feature.Feature
import geoscript.feature.Schema
import geoscript.filter.Expression
import geoscript.geom.Point
import geoscript.layer.Cursor
import geoscript.proj.Geodetic
import geoscript.layer.Format
import geoscript.layer.GeoTIFF
import geoscript.layer.Raster
import geoscript.workspace.Memory
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import static org.junit.Assert.*
import geoscript.geom.*
import geoscript.layer.Layer
import geoscript.filter.Color
import geoscript.proj.Projection
import geoscript.workspace.Workspace
/**
* The unit test for the GeoScript class.
* @author Jared Erickson
*/
class GeoScriptTestCase {
@Rule
public TemporaryFolder folder = new TemporaryFolder()
@Test void wrap() {
assertTrue GeoScript.wrap(new Feature([the_geom: "POINT (1 1)"],"pt").f) instanceof Feature
assertTrue GeoScript.wrap(new Schema("widgets", "geom:Point:srid=4326").featureType) instanceof Schema
assertTrue GeoScript.wrap(new Point(111,-47).g) instanceof Point
assertTrue GeoScript.wrap(new Bounds(0,0,10,10).env) instanceof Bounds
assertTrue GeoScript.wrap(new Expression(12).expr) instanceof Expression
assertTrue GeoScript.wrap(new Layer().getCursor().col) instanceof Cursor
assertTrue GeoScript.wrap(new Layer().fs) instanceof Layer
assertTrue GeoScript.wrap(new Projection("EPSG:4326").crs) instanceof Projection
assertTrue GeoScript.wrap(new Geodetic("clrk66").ellipsoid) instanceof Geodetic
assertTrue GeoScript.wrap(new Memory().ds) instanceof Workspace
List data = [
[0,0,0,0,0,0,0],
[0,1,1,1,1,1,0],
[0,1,2,3,2,1,0],
[0,1,1,1,1,1,0],
[0,0,0,0,0,0,0]
]
Bounds bounds = new Bounds(0, 0, 7, 5, "EPSG:4326")
assertTrue GeoScript.wrap(new Raster(data, bounds).coverage) instanceof Raster
assertTrue GeoScript.wrap(new GeoTIFF(null).gridFormat) instanceof Format
assertTrue GeoScript.wrap(1) instanceof Integer
assertTrue GeoScript.wrap("ABC") instanceof String
}
@Test void unwrap() {
assertTrue GeoScript.unwrap(new Point(111,-47)) instanceof com.vividsolutions.jts.geom.Point
assertTrue GeoScript.unwrap(new Feature([the_geom: "POINT (1 1)"],"pt")) instanceof org.opengis.feature.simple.SimpleFeature
assertTrue GeoScript.unwrap(new Schema("widgets", "geom:Point:srid=4326")) instanceof org.opengis.feature.simple.SimpleFeatureType
assertTrue GeoScript.unwrap(new Bounds(0,0,10,10)) instanceof org.geotools.geometry.jts.ReferencedEnvelope
assertTrue GeoScript.unwrap(new Expression(12)) instanceof org.opengis.filter.expression.Expression
assertTrue GeoScript.unwrap(new Layer().getCursor()) instanceof org.geotools.feature.FeatureCollection
assertTrue GeoScript.unwrap(new Layer()) instanceof org.geotools.data.FeatureSource
assertTrue GeoScript.unwrap(new Projection("EPSG:4326")) instanceof org.opengis.referencing.crs.CoordinateReferenceSystem
assertTrue GeoScript.unwrap(new Geodetic("clrk66")) instanceof org.geotools.referencing.datum.DefaultEllipsoid
assertTrue GeoScript.unwrap(new Memory()) instanceof org.geotools.data.DataStore
List data = [
[0,0,0,0,0,0,0],
[0,1,1,1,1,1,0],
[0,1,2,3,2,1,0],
[0,1,1,1,1,1,0],
[0,0,0,0,0,0,0]
]
Bounds bounds = new Bounds(0, 0, 7, 5, "EPSG:4326")
assertTrue GeoScript.unwrap(new Raster(data, bounds)) instanceof org.geotools.coverage.grid.GridCoverage2D
assertTrue GeoScript.unwrap(new GeoTIFF()) instanceof org.geotools.coverage.grid.io.AbstractGridFormat
assertTrue GeoScript.unwrap(1) instanceof Integer
assertTrue GeoScript.unwrap("ABC") instanceof String
}
@Test void listAsPoint() {
use (GeoScript) {
Point pt = [1,2] as Point
assertEquals "POINT (1 2)", pt.wkt
}
}
@Test void listAsMultiPoint() {
use(GeoScript) {
MultiPoint p = [[1,1],[2,2]] as MultiPoint
assertEquals "MULTIPOINT ((1 1), (2 2))", p.wkt
}
}
@Test void listAsLineString() {
use(GeoScript) {
LineString line = [[1,2],[2,3],[3,4]] as LineString
assertEquals "LINESTRING (1 2, 2 3, 3 4)", line.wkt
}
}
@Test void listAsMultiLineString() {
use(GeoScript) {
MultiLineString line = [[[1,2],[3,4]], [[5,6],[7,8]]] as MultiLineString
assertEquals "MULTILINESTRING ((1 2, 3 4), (5 6, 7 8))", line.wkt
}
}
@Test void listAsBounds() {
use(GeoScript) {
Bounds b = [1,3,2,4] as Bounds
assertEquals "(1.0,3.0,2.0,4.0)", b.toString()
}
}
@Test void listAsPolygon() {
use(GeoScript) {
Polygon p = [[[1,2],[3,4],[5,6],[1,2]]] as Polygon
assertEquals "POLYGON ((1 2, 3 4, 5 6, 1 2))", p.wkt
}
}
@Test void listAsMultiPolygon() {
use(GeoScript) {
MultiPolygon p = [[[[1,2],[3,4],[5,6],[1,2]]], [[[7,8],[9,10],[11,12],[7,8]]]] as MultiPolygon
assertEquals "MULTIPOLYGON (((1 2, 3 4, 5 6, 1 2)), ((7 8, 9 10, 11 12, 7 8)))", p.wkt
}
}
@Test void fileAsShapefile() {
use(GeoScript) {
File file = new File(getClass().getClassLoader().getResource("states.shp").toURI())
Layer shp = file as Layer
assertEquals 49, shp.count
}
}
@Test void fileAsProperty() {
use(GeoScript) {
File file = new File(getClass().getClassLoader().getResource("points.properties").toURI())
Layer prop = file as Layer
assertEquals 4, prop.count
}
}
@Test void csvFileAsLayer() {
String csv = """"geom","name","price"
"POINT (111 -47)","House","12.5"
"POINT (121 -45)","School","22.7"
"""
File csvFile = folder.newFile("layer.csv")
csvFile.write(csv)
use(GeoScript) {
Layer layer = csvFile as Layer
assertEquals("csv geom: Point, name: String, price: String", layer.schema.toString())
assertEquals(2, layer.count)
layer.eachFeature { f ->
assertTrue(f.geom instanceof geoscript.geom.Point)
}
}
}
@Test void geoJsonFileAsLayer() {
String json = """{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[111,-47]},"properties":{"name":"House","price":12.5},"id":"fid-3eff7fce_131b538ad4c_-8000"},{"type":"Feature","geometry":{"type":"Point","coordinates":[121,-45]},"properties":{"name":"School","price":22.7},"id":"fid-3eff7fce_131b538ad4c_-7fff"}]}"""
File jsonFile = folder.newFile("layer.json")
jsonFile.write(json)
use(GeoScript) {
Layer layer = jsonFile as Layer
assertEquals("geojson", layer.name)
assertTrue(layer.schema.has("name"))
assertTrue(layer.schema.has("price"))
assertTrue(layer.schema.has("geometry"))
assertEquals(2, layer.count)
layer.eachFeature { f ->
assertTrue(f.geom instanceof geoscript.geom.Point)
}
}
}
@Test void stringAsColor() {
use(GeoScript) {
Color c = "255,255,255" as Color
assertEquals "#ffffff", c.hex
}
}
@Test void stringAsProjection() {
use(GeoScript) {
Projection p = "EPSG:2927" as Projection
assertEquals "EPSG:2927", p.id
}
}
@Test void stringAsGeometry() {
use(GeoScript) {
Geometry g = "POINT (1 1)" as Geometry
assertEquals "POINT (1 1)", g.wkt
}
}
@Test void stringAsExpression() {
use(GeoScript) {
Expression expr = "max(2,4)" as Expression
assertEquals "max([2], [4])", expr.toString()
}
}
@Test void stringAsGeodetic() {
use(GeoScript) {
Geodetic geod = "clrk66" as Geodetic
assertEquals "Geodetic [SPHEROID[\"Clarke 1866\", 6378206.4, 294.9786982138982]]", geod.toString()
}
}
@Test void stringAsWorkspace() {
use(GeoScript) {
URL url = getClass().getClassLoader().getResource("states.shp")
String str = "url='${url}' 'create spatial index'=true".toString()
Workspace w = str as Workspace
assertNotNull(w)
assertEquals("Directory", w.format)
}
}
@Test void mapAsWorkspace() {
use(GeoScript) {
URL url = getClass().getClassLoader().getResource("states.shp")
Workspace w = ["url": url] as Workspace
assertNotNull(w)
assertEquals("Directory", w.format)
}
}
@Test void zipUnzip() {
File dir = folder.newFolder("files")
List files = ["file1","file2","file3"].collect {
File file = new File(dir, "${it}.txt")
file.write("123")
file
}
File zipFile = folder.newFile("files.zip")
GeoScript.zip(files, zipFile)
assertTrue zipFile.exists()
assertTrue zipFile.length() > 0
File newDir = folder.newFolder("unzipped")
GeoScript.unzip(zipFile, newDir)
["file1","file2","file3"].each { String name ->
File f = new File(newDir, "${name}.txt")
assertTrue f.exists()
assertEquals "123", f.text
}
}
}