Skip to content

Commit 7e609dd

Browse files
committed
Add Raster crop by geometry method
1 parent f023149 commit 7e609dd

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

doc/api/raster.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ Methods
125125

126126
Crop the current Raster to only include data in the given Bounds.
127127

128+
.. function:: Raster.crop(geometry)
129+
130+
:arg geometry: :class:`geom.Geometry` The Geometry to use when cropping the Raster
131+
132+
Crop the current Raster to only include data in the given Geometry.
133+
134+
128135
:class:`raster.Band`
129136
====================
130137

src/main/java/org/geoscript/js/raster/Raster.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.geoscript.js.GeoObject;
44
import org.geoscript.js.geom.Bounds;
5+
import org.geoscript.js.geom.Geometry;
56
import org.geoscript.js.geom.Point;
67
import org.geoscript.js.proj.Projection;
78
import org.geotools.coverage.grid.GridCoordinates2D;
@@ -140,11 +141,17 @@ public Object getValue(Object pointOrPixel) {
140141
}
141142

142143
@JSFunction
143-
public Raster crop(Bounds bounds) {
144+
public Raster crop(Object boundsOrGeometry) {
144145
CoverageProcessor processor = new CoverageProcessor();
145146
ParameterValueGroup params = processor.getOperation("CoverageCrop").getParameters();
146147
params.parameter("Source").setValue(coverage);
147-
params.parameter("Envelope").setValue(new org.geotools.geometry.GeneralEnvelope(bounds.unwrap()));
148+
if (boundsOrGeometry instanceof Bounds) {
149+
Bounds bounds = (Bounds) boundsOrGeometry;
150+
params.parameter("Envelope").setValue(new org.geotools.geometry.GeneralEnvelope(bounds.unwrap()));
151+
} else {
152+
Geometry geometry = (Geometry) boundsOrGeometry;
153+
params.parameter("ROI").setValue(geometry.unwrap());
154+
}
148155
GridCoverage2D newCoverage = (GridCoverage2D) processor.doOperation(params);
149156
return new Raster(this.getParentScope(), newCoverage);
150157
}

src/test/resources/org/geoscript/js/tests/geoscript/raster/test_raster.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ exports["test: get raster pixel from point"] = function() {
8787
assert.strictEqual(20, pixel.y, "Value should be 20");
8888
};
8989

90-
exports["test: crop a raster"] = function() {
90+
exports["test: crop a raster with a bounds"] = function() {
9191
var format = new raster.Format({source: admin.raster.source});
9292
var tif = format.read({});
9393
var smallTif = tif.crop(new geom.Bounds([-180,-90, 0, 0]))
@@ -96,4 +96,15 @@ exports["test: crop a raster"] = function() {
9696
assert.strictEqual(-90, Math.round(bounds.minY), "Min Y should be -90");
9797
assert.strictEqual(0, Math.round(bounds.maxX), "Max X should be 0");
9898
assert.strictEqual(0, Math.round(bounds.maxY), "Max Y should be 0");
99+
};
100+
101+
exports["test: crop a raster with a geometry"] = function() {
102+
var format = new raster.Format({source: admin.raster.source});
103+
var tif = format.read({});
104+
var smallTif = tif.crop(new geom.Point([0, 0]).buffer(4))
105+
var bounds = smallTif.bounds;
106+
assert.strictEqual(-4, Math.round(bounds.minX), "Min X should be -4");
107+
assert.strictEqual(-4, Math.round(bounds.minY), "Min Y should be -4");
108+
assert.strictEqual(4, Math.round(bounds.maxX), "Max X should be 4");
109+
assert.strictEqual(4, Math.round(bounds.maxY), "Max Y should be 4");
99110
};

0 commit comments

Comments
 (0)