Skip to content

Commit f023149

Browse files
committed
Add Raster getPixel and crop methods
1 parent 1886a50 commit f023149

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

doc/api/raster.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Properties
9797
Methods
9898
-------
9999

100+
.. function:: Raster.getPixel(point)
101+
102+
:arg point: :class:`geom.Point` The geographic Point
103+
104+
Get a pixel ``Object`` with x and y properies.
105+
100106
.. function:: Raster.getPoint(x,y)
101107

102108
:arg x: ``Number`` The pixel's x position
@@ -113,6 +119,11 @@ Methods
113119

114120
Get a value for each band from the Raster.
115121

122+
.. function:: Raster.crop(bounds)
123+
124+
:arg bounds: :class:`geom.Bound` The Bounds of the new Raster
125+
126+
Crop the current Raster to only include data in the given Bounds.
116127

117128
:class:`raster.Band`
118129
====================

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
import org.geotools.coverage.grid.GridCoverage2D;
99
import org.geotools.coverage.grid.GridEnvelope2D;
1010
import org.geotools.coverage.grid.GridGeometry2D;
11+
import org.geotools.coverage.processing.CoverageProcessor;
1112
import org.geotools.geometry.DirectPosition2D;
1213
import org.geotools.geometry.jts.ReferencedEnvelope;
1314
import org.mozilla.javascript.*;
1415
import org.mozilla.javascript.annotations.JSConstructor;
1516
import org.mozilla.javascript.annotations.JSFunction;
1617
import org.mozilla.javascript.annotations.JSGetter;
18+
import org.opengis.coverage.Coverage;
1719
import org.opengis.coverage.SampleDimension;
1820
import org.opengis.geometry.DirectPosition;
1921
import org.opengis.geometry.Envelope;
22+
import org.opengis.parameter.ParameterValueGroup;
2023
import org.opengis.referencing.crs.CoordinateReferenceSystem;
2124
import org.opengis.referencing.operation.TransformException;
2225

@@ -106,6 +109,20 @@ public Point getPoint(int x, int y) {
106109
}
107110
}
108111

112+
@JSFunction
113+
public NativeObject getPixel(Point point) {
114+
GridGeometry2D gg = coverage.getGridGeometry();
115+
try {
116+
GridCoordinates2D gridCoordinates2D = gg.worldToGrid(new DirectPosition2D((double) point.getX(), (double) point.getY()));
117+
Map<String, Double> pixel = new HashMap<>();
118+
pixel.put("x", gridCoordinates2D.getX());
119+
pixel.put("y", gridCoordinates2D.getY());
120+
return (NativeObject) javaToJS(pixel, this.getParentScope());
121+
} catch (TransformException e) {
122+
throw ScriptRuntime.constructError("Error", "Error getting Pixel coordinate from Point for Raster.");
123+
}
124+
}
125+
109126
@JSFunction
110127
public Object getValue(Object pointOrPixel) {
111128
Point point;
@@ -122,6 +139,16 @@ public Object getValue(Object pointOrPixel) {
122139
return javaToJS(result, getParentScope());
123140
}
124141

142+
@JSFunction
143+
public Raster crop(Bounds bounds) {
144+
CoverageProcessor processor = new CoverageProcessor();
145+
ParameterValueGroup params = processor.getOperation("CoverageCrop").getParameters();
146+
params.parameter("Source").setValue(coverage);
147+
params.parameter("Envelope").setValue(new org.geotools.geometry.GeneralEnvelope(bounds.unwrap()));
148+
GridCoverage2D newCoverage = (GridCoverage2D) processor.doOperation(params);
149+
return new Raster(this.getParentScope(), newCoverage);
150+
}
151+
125152
@Override
126153
public String toString() {
127154
return this.getName();

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,25 @@ exports["test: get raster point from pixel"] = function() {
7575
exports["test: get raster point from pixel"] = function() {
7676
var format = new raster.Format({source: admin.raster.source});
7777
var tif = format.read({});
78-
print(tif.getValue(new geom.Point([-175.8, 81.8])));
79-
//assert.strictEqual(-69, tif.getValue(new geom.Point([-175.8, 81.8]), "double")[0], "Value should be -69");
80-
//assert.strictEqual(-69, tif.getValue({x: 10, y: 20}, "double")[0], "Value should be -69");
78+
assert.strictEqual(-69, tif.getValue(new geom.Point([-175.8, 81.8]), "double")[0], "Value should be -69");
79+
assert.strictEqual(-69, tif.getValue({x: 10, y: 20}, "double")[0], "Value should be -69");
80+
};
81+
82+
exports["test: get raster pixel from point"] = function() {
83+
var format = new raster.Format({source: admin.raster.source});
84+
var tif = format.read({});
85+
var pixel = tif.getPixel(new geom.Point([-175.8, 81.8]));
86+
assert.strictEqual(10, pixel.x, "Value should be 10");
87+
assert.strictEqual(20, pixel.y, "Value should be 20");
88+
};
89+
90+
exports["test: crop a raster"] = function() {
91+
var format = new raster.Format({source: admin.raster.source});
92+
var tif = format.read({});
93+
var smallTif = tif.crop(new geom.Bounds([-180,-90, 0, 0]))
94+
var bounds = smallTif.bounds;
95+
assert.strictEqual(-180, Math.round(bounds.minX), "Min X should be -180");
96+
assert.strictEqual(-90, Math.round(bounds.minY), "Min Y should be -90");
97+
assert.strictEqual(0, Math.round(bounds.maxX), "Max X should be 0");
98+
assert.strictEqual(0, Math.round(bounds.maxY), "Max Y should be 0");
8199
};

0 commit comments

Comments
 (0)