public static void rasterizePolygonGeometry ( Window active , Geometry polygon , RasterData raster , RasterData rasterToMap , double value , IHMProgressMonitor monitor ) { GeometryFactory gFactory = new GeometryFactory ( ) ; int rows = active . getRows ( ) ; int cols = active . getCols ( ) ; double delta = active . getWEResolution ( ) / 4.0 ; monitor . beginTask ( "rasterizing..." , rows ) ; //$NON-NLS-1$ for ( int i = 0 ; i < rows ; i ++ ) { monitor . worked ( 1 ) ; // do scan line to fill the polygon Coordinate west = rowColToCenterCoordinates ( active , i , 0 ) ; Coordinate east = rowColToCenterCoordinates ( active , i , cols - 1 ) ; LineString line = gFactory . createLineString ( new Coordinate [ ] { west , east } ) ; if ( polygon . intersects ( line ) ) { Geometry internalLines = polygon . intersection ( line ) ; Coordinate [ ] coords = internalLines . getCoordinates ( ) ; for ( int j = 0 ; j < coords . length ; j = j + 2 ) { Coordinate startC = new Coordinate ( coords [ j ] . x + delta , coords [ j ] . y ) ; Coordinate endC = new Coordinate ( coords [ j + 1 ] . x - delta , coords [ j + 1 ] . y ) ; int [ ] startcol = coordinateToNearestRowCol ( active , startC ) ; int [ ] endcol = coordinateToNearestRowCol ( active , endC ) ; if ( startcol == null || endcol == null ) { // vertex is outside of the region, ignore it continue ; } /* * the part in between has to be filled */ for ( int k = startcol [ 1 ] ; k <= endcol [ 1 ] ; k ++ ) { if ( rasterToMap != null ) { raster . setValueAt ( i , k , rasterToMap . getValueAt ( i , k ) ) ; } else { raster . setValueAt ( i , k , value ) ; } } } } } }
Fill polygon areas mapping on a raster