|
12 | 12 | import org.geotools.coverage.processing.CoverageProcessor; |
13 | 13 | import org.geotools.geometry.DirectPosition2D; |
14 | 14 | import org.geotools.geometry.jts.ReferencedEnvelope; |
| 15 | +import org.geotools.process.raster.RangeLookupProcess; |
| 16 | +import org.jaitools.numeric.Range; |
15 | 17 | import org.mozilla.javascript.*; |
16 | 18 | import org.mozilla.javascript.annotations.JSConstructor; |
17 | 19 | import org.mozilla.javascript.annotations.JSFunction; |
18 | 20 | import org.mozilla.javascript.annotations.JSGetter; |
19 | | -import org.opengis.coverage.Coverage; |
20 | 21 | import org.opengis.coverage.SampleDimension; |
21 | 22 | import org.opengis.geometry.DirectPosition; |
22 | 23 | import org.opengis.geometry.Envelope; |
@@ -166,6 +167,65 @@ public Raster reproject(Projection projection) { |
166 | 167 | return new Raster(this.getParentScope(), newCoverage); |
167 | 168 | } |
168 | 169 |
|
| 170 | + @JSFunction |
| 171 | + public Raster reclassify(NativeArray ranges, NativeObject options) { |
| 172 | + int band = (int) options.getOrDefault("band", 0); |
| 173 | + double noData = (double) options.getOrDefault("noData",0); |
| 174 | + List<Range> rangeList = new ArrayList<>(); |
| 175 | + int[] pixelValues = new int[ranges.size()]; |
| 176 | + for(int i = 0; i<ranges.size(); i++) { |
| 177 | + NativeObject rangeObj = (NativeObject) ranges.get(i); |
| 178 | + pixelValues[i] = getInt(rangeObj.get("value")); |
| 179 | + rangeList.add(Range.create( |
| 180 | + Double.parseDouble(rangeObj.get("min").toString()), |
| 181 | + (boolean) rangeObj.getOrDefault("minIncluded", true), |
| 182 | + Double.parseDouble(rangeObj.get("max").toString()), |
| 183 | + (boolean) rangeObj.getOrDefault("maxIncluded", true) |
| 184 | + )); |
| 185 | + } |
| 186 | + RangeLookupProcess process = new RangeLookupProcess(); |
| 187 | + GridCoverage2D newCoverage = process.execute(this.coverage, band, rangeList, pixelValues, noData, null); |
| 188 | + return new Raster(this.getParentScope(), newCoverage); |
| 189 | + } |
| 190 | + |
| 191 | + @JSGetter |
| 192 | + public NativeObject getExtrema() { |
| 193 | + CoverageProcessor processor = new CoverageProcessor(); |
| 194 | + ParameterValueGroup params = processor.getOperation("Extrema").getParameters(); |
| 195 | + params.parameter("Source").setValue(coverage); |
| 196 | + GridCoverage2D coverage = (GridCoverage2D) processor.doOperation(params); |
| 197 | + Map<String, Object> values = new HashMap<>(); |
| 198 | + values.put("min", coverage.getProperty("minimum")); |
| 199 | + values.put("max",coverage.getProperty("maximum")); |
| 200 | + return (NativeObject) javaToJS(values, this.getParentScope()); |
| 201 | + } |
| 202 | + |
| 203 | + @JSFunction |
| 204 | + public Object getMinValue(int band) { |
| 205 | + double minValue = this.coverage.getSampleDimension(band).getMinimumValue(); |
| 206 | + if (Double.isInfinite(minValue)) { |
| 207 | + minValue = ((double[])this.getExtrema().get("min"))[band]; |
| 208 | + } |
| 209 | + return minValue; |
| 210 | + } |
| 211 | + |
| 212 | + @JSFunction |
| 213 | + public Object getMaxValue(int band) { |
| 214 | + double maxValue = this.coverage.getSampleDimension(band).getMaximumValue(); |
| 215 | + if (Double.isInfinite(maxValue)) { |
| 216 | + maxValue = ((double[])this.getExtrema().get("max"))[band]; |
| 217 | + } |
| 218 | + return maxValue; |
| 219 | + } |
| 220 | + |
| 221 | + private int getInt(Object obj) { |
| 222 | + if (obj instanceof Number) { |
| 223 | + return ((Number)obj).intValue(); |
| 224 | + } else { |
| 225 | + return getInt(Double.parseDouble(obj.toString())); |
| 226 | + } |
| 227 | + } |
| 228 | + |
169 | 229 | @Override |
170 | 230 | public String toString() { |
171 | 231 | return this.getName(); |
|
0 commit comments