VectorToRasterProcess implements VectorProcess { public static GridCoverage2D process( SimpleFeatureCollection features, Object attribute, Dimension gridDim, Envelope bounds, String covName, ProgressListener monitor) throws VectorToRasterException { VectorToRasterProcess process = new VectorToRasterProcess(); return process.convert(features, attribute, gridDim, bounds, covName, monitor); }  static GridCoverage2D process(
            SimpleFeatureCollection features,
            Object attribute,
            Dimension gridDim,
            Envelope bounds,
            String covName,
            ProgressListener monitor); @DescribeResult(name = "result", description = "Rasterized grid") GridCoverage2D execute(
        @DescribeParameter(name = "features", description = "Features to process", min = 1, max = 1) SimpleFeatureCollection features,
        @DescribeParameter(name = "rasterWidth", description = "Width of the output grid in pixels", min = 1, max = 1, minValue = 1) Integer rasterWidth,
        @DescribeParameter(name = "rasterHeight", description = "Height of the output grid in pixels", min = 1, max = 1, minValue = 1) Integer rasterHeight,
        @DescribeParameter(name = "title", description = "Title to use for the output grid", min = 0, max = 1, defaultValue = "raster" ) String title,
        @DescribeParameter(name = "attribute", description = "Attribute name to use for the raster cell values", min = 1, max = 1) String attribute,
        @DescribeParameter(name = "bounds", description = "Bounding box of the area to rasterize", min = 0, max = 1) Envelope bounds,
        ProgressListener progressListener);  }
@Test public void rasterizePolygons() throws Exception { SimpleFeatureCollection features = createPolys(); ReferencedEnvelope bounds = features.getBounds(); Dimension gridDim = new Dimension( (int)bounds.getWidth(), (int)bounds.getHeight()); String covName = "Test"; ProgressListener monitor = null; GridCoverage2D cov = VectorToRasterProcess.process( features, "value", gridDim, bounds, covName, monitor); Map<Integer, Envelope> rects = new HashMap<Integer, Envelope>(); SimpleFeatureIterator iter = features.features(); while (iter.hasNext()) { SimpleFeature sf = iter.next(); rects.put((Integer)sf.getAttribute("value"), ((Geometry) sf.getDefaultGeometry()).getEnvelopeInternal()); } try { MathTransform2D mt = cov.getGridGeometry().getGridToCRS2D(PixelOrientation.CENTER); Point gridP = new Point(); Point2D.Double geoP = new Point2D.Double(); int[] covValue = new int[1]; for (gridP.y = 0; gridP.y < gridDim.height; gridP.y++) { for (gridP.x = 0; gridP.x < gridDim.width; gridP.x++) { mt.transform(gridP, geoP); } cov.evaluate(geoP, covValue); if (covValue[0] != 0) { assertTrue(rects.get(covValue[0]).contains(geoP.x, geoP.y)); } } } catch (TransformException tex) { throw new RuntimeException(tex); } }
@Test public void rasterizePoints() throws Exception { ReferencedEnvelope bounds = new ReferencedEnvelope(-10, 10, -20, 20, DefaultEngineeringCRS.GENERIC_2D); Dimension gridDim = new Dimension(100, 100); SimpleFeatureCollection features = createPoints(bounds, gridDim); String covName = "Test"; ProgressListener monitor = null; GridCoverage2D cov = VectorToRasterProcess.process( features, "value", gridDim, bounds, covName, monitor); SimpleFeatureIterator iter = features.features(); int[] covValues = new int[1]; try { while (iter.hasNext()) { SimpleFeature feature = iter.next(); Coordinate coord = ((Geometry)feature.getDefaultGeometry()).getCoordinate(); Point2D worldPos = new Point2D.Double(coord.x, coord.y); int value = (Integer) feature.getAttribute("value"); cov.evaluate(worldPos, covValues); assertEquals(value, covValues[0]); } } finally { iter.close(); } }