Skip to content

Commit cb03fa8

Browse files
committed
Fix Cursor paging bug with Layers that don't natively support paging
1 parent 3468317 commit cb03fa8

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/main/groovy/geoscript/layer/Cursor.groovy

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Cursor implements Iterator {
4646

4747
/**
4848
* Create a new Cursor with a FeatureCollection
49-
* @param options A Map of options (sort, start, max)
49+
* @param options A Map of options (sort - a List of Strings, start - is 0 based, max - the max number of features)
5050
* @param col The GeoTools FeatureCollection
5151
*/
5252
Cursor(Map options = [:], FeatureCollection<SimpleFeatureType, SimpleFeature> col) {
@@ -57,7 +57,7 @@ class Cursor implements Iterator {
5757

5858
/**
5959
* Create a new Cursor with a FeatureCollection and a Layer
60-
* @param options A Map of options (sort, start, max)
60+
* @param options A Map of options (sort - a List of Strings, start - is 0 based, max - the max number of features)
6161
* @param col The GeoTools FeatureCollection
6262
* @param layer The Geoscript Layer
6363
*/
@@ -78,8 +78,8 @@ class Cursor implements Iterator {
7878
}
7979
if (options.containsKey("start") && options.containsKey("max")) {
8080
long start = options.start as long
81-
long end = start + options.max as long
82-
this.iter = new MaxFeaturesIterator<SimpleFeature>(this.iter, start, end)
81+
long max = options.max as long
82+
this.iter = new MaxFeaturesIterator<SimpleFeature>(this.iter, start, max)
8383
}
8484
}
8585

src/main/groovy/geoscript/layer/Layer.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ class Layer {
461461
* @param sort A List of Lists [[Field or Field name, "ASC" or "DESC"],...] or a List of Strings ["name DESC", "price ASC"]
462462
* that define the sort order. Not all Layers support sorting!
463463
* @param max The maximum number of Features to include in the Cursor
464-
* @param start The index of the record to start the cursor at. Together with maxFeatures this simulates paging.
464+
* @param start The zero based index of the record to start the cursor at. Together with maxFeatures this simulates paging.
465465
* Not all Layers support the start index and paging!
466466
* @param fields A List of Fields or Field names to include. Used to select only a subset of Fields.
467467
* @return A Cursor

src/test/groovy/geoscript/layer/CursorTestCase.groovy

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,33 @@ class CursorTestCase {
5959
assertEquals 49, names.size()
6060
}
6161

62+
@Test void sortStartAndMax() {
63+
// Property files don't natively support paging
64+
File file = new File(getClass().getClassLoader().getResource("points.properties").toURI())
65+
assertNotNull(file)
66+
Layer layer = new Property(file)
67+
assertNotNull(layer)
68+
// 3 to 4
69+
Cursor c = layer.getCursor(sort: ["name DESC"], start: 2, max: 2)
70+
assertEquals "point 2", c.next()['name']
71+
assertEquals "point 1", c.next()['name']
72+
assertFalse c.hasNext()
73+
c.close()
74+
// 2 to 4
75+
c = layer.getCursor(sort: ["name DESC"], start: 1, max: 3)
76+
assertEquals "point 3", c.next()['name']
77+
assertEquals "point 2", c.next()['name']
78+
assertEquals "point 1", c.next()['name']
79+
assertFalse c.hasNext()
80+
c.close()
81+
// 1 to 3
82+
c = layer.getCursor(sort: ["name DESC"], start: 0, max: 3)
83+
assertEquals "point 4", c.next()['name']
84+
assertEquals "point 3", c.next()['name']
85+
assertEquals "point 2", c.next()['name']
86+
assertFalse c.hasNext()
87+
c.close()
88+
}
89+
6290
}
6391

0 commit comments

Comments
 (0)