88import org .geotools .coverage .grid .GridCoverage2D ;
99import org .geotools .coverage .grid .GridEnvelope2D ;
1010import org .geotools .coverage .grid .GridGeometry2D ;
11+ import org .geotools .coverage .processing .CoverageProcessor ;
1112import org .geotools .geometry .DirectPosition2D ;
1213import org .geotools .geometry .jts .ReferencedEnvelope ;
1314import org .mozilla .javascript .*;
1415import org .mozilla .javascript .annotations .JSConstructor ;
1516import org .mozilla .javascript .annotations .JSFunction ;
1617import org .mozilla .javascript .annotations .JSGetter ;
18+ import org .opengis .coverage .Coverage ;
1719import org .opengis .coverage .SampleDimension ;
1820import org .opengis .geometry .DirectPosition ;
1921import org .opengis .geometry .Envelope ;
22+ import org .opengis .parameter .ParameterValueGroup ;
2023import org .opengis .referencing .crs .CoordinateReferenceSystem ;
2124import 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 ();
0 commit comments