Grid Layer Overlay Example

The following code shows how to create a GridLayer overlay and use it in a request for a named map.

Note the use of the CustomInflectionCollection whereby the colors are specified for each inflection.

Alternatively you can use a ComputedInflectionCollection with a start and end color and number of inflections. The color is spread evenly across the number of inflections.

For an explanation of the GridLayer elements, see GridLayer Elements.

public class RenderNamedMapWithGridLayerOverlay {

	public static void main(String[] args) {
        try {
            MappingServiceInterface mapping = Preference.getServiceinterface();
            Point mapCenter = BuildGeometry.buildPoint("mapinfo:coordsys 12, 62, 7, 0.0", 0, 0, null);
            MapView mapView = MappingView.getZoomAndCenterMapView(mapCenter,MappingUtility.buildDistance(19000, DistanceUnit.KILOMETER));

            NamedTable namedTable = new NamedTable();
            namedTable.setName("/Samples/NamedTables/WorldcapTable");
            
            Distance cellWidth = MappingUtility.buildDistance(50, DistanceUnit.MILE);
            
            IDWInterpolator interpolator = new IDWInterpolator();
            interpolator.setAggregationMethod(AggregationMethod.AVERAGE);
            interpolator.setExponent(2);
            interpolator.setMaxPoints(25);
            interpolator.setSearchRadius(100);
            
            CustomInflectionCollection inflectionCollection = new CustomInflectionCollection();
            inflectionCollection.setInflectionMethod(InflectionMethod.EQUAL_COUNT);
            inflectionCollection.setRoundBy(0.1);
            
            InflectionColorBinList inBinList = new InflectionColorBinList();
            
            InflectionColorBin red = new InflectionColorBin();            
            red.setColor("RED");
            inBinList.getInflectionColorBin().add(red);            
            
            InflectionColorBin yellow = new InflectionColorBin();            
            yellow.setColor("YELLOW");
            inBinList.getInflectionColorBin().add(yellow);
            
            InflectionColorBin green = new InflectionColorBin();            
            green.setColor("GREEN");
            inBinList.getInflectionColorBin().add(green);
            
            InflectionColorBin rgb = new InflectionColorBin();            
            rgb.setColor("rgb(0,255,255)");
            inBinList.getInflectionColorBin().add(rgb);
            
            InflectionColorBin blue = new InflectionColorBin();            
            blue.setColor("BLUE"); 
            inBinList.getInflectionColorBin().add(blue);
            
            inflectionCollection.setInflectionColorBinList(inBinList);

            //Creating GridEnvelope
            List<Pos> pointList = new ArrayList<Pos>(2); 
            pointList.add(BuildGeometry.buildPos(-1.6190368438028984E7, -5501153.098687401, null));
            pointList.add(BuildGeometry.buildPos(1.6606614137907444E7, 6743291.671893631, null));
            Envelope gridEnvelope = BuildGeometry.buildEnvelope("mapinfo:coordsys 12, 62, 7, 0.0", pointList);

            //Creating GridLayer
            Layer gridLayer	= MappingUtility.buildGridLayer(namedTable, null, cellWidth, "Cap_Pop", "Obj", interpolator, inflectionCollection, gridEnvelope);
           
            RenderNamedMapWithOverlayRequest  request = MappingServiceRequestBuilder.createRenderNamedMapOverlayRequest
            ("RenderNamedMapWithGridLayerOverlay","/Samples/NamedMaps/WorldMap", mapView, gridLayer);
            
            RenderNamedMapWithOverlayResponse response = mapping.renderNamedMapWithOverlay(request);
            
            PrintMappingResponse.printRenderNamedMapWithOverlayResponse(response);
        }
        catch (ServiceException se) {
        	PrintMappingResponse.printError(se);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

}