Java Code Examples for org.geotools.styling.ColorMapEntry#setQuantity()

The following examples show how to use org.geotools.styling.ColorMapEntry#setQuantity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageDataLoader.java    From gama with GNU General Public License v3.0 7 votes vote down vote up
private static Style createStyle(int band, double min, double max) {

		FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
		StyleFactory sf = CommonFactoryFinder.getStyleFactory();

		RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
		ColorMap cMap = sf.createColorMap();
		ColorMapEntry start = sf.createColorMapEntry();
		start.setColor(ff.literal("#ff0000"));
		start.setQuantity(ff.literal(min));
		ColorMapEntry end = sf.createColorMapEntry();
		end.setColor(ff.literal("#0000ff"));
		end.setQuantity(ff.literal(max));

		cMap.addColorMapEntry(start);
		cMap.addColorMapEntry(end);
		sym.setColorMap(cMap);
		Style style = SLD.wrapSymbolizers(sym);

		return style;
	}
 
Example 2
Source File: RasterReader.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates the rgb image symbol.
 *
 * @param sym the sym
 * @param cov the cov
 * @param raster the raster
 */
private void createRGBImageSymbol(
        RasterSymbolizer sym, GridCoverage2D cov, WritableRaster raster) {
    double dest;
    List<Double> valueList = new ArrayList<>();

    GridEnvelope2D gridRange2D = cov.getGridGeometry().getGridRange2D();
    for (int x = 0; x < gridRange2D.getWidth(); x++) {
        for (int y = 0; y < gridRange2D.getHeight(); y++) {
            try {
                dest = raster.getSampleDouble(x, y, 0);

                if (!valueList.contains(dest)) {
                    valueList.add(dest);
                }
            } catch (Exception e) {
                ConsoleManager.getInstance().exception(this, e);
            }
        }
    }

    ColorMapImpl colourMap = new ColorMapImpl();

    // Sort the unique sample values in ascending order
    Collections.sort(valueList);

    // Create colour amp entries in the colour map for all the sample values
    for (Double value : valueList) {
        ColorMapEntry entry = new ColorMapEntryImpl();
        Literal colourExpression =
                ff.literal(ColourUtils.fromColour(ColourUtils.createRandomColour()));
        entry.setColor(colourExpression);
        entry.setQuantity(ff.literal(value.doubleValue()));

        colourMap.addColorMapEntry(entry);
    }

    colourMap.setType(ColorMap.TYPE_VALUES);
    sym.setColorMap(colourMap);
}
 
Example 3
Source File: MultipleColourMapEntry.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the colour map entry.
 *
 * @return the colour map entry
 */
public ColorMapEntry getColourMapEntry() {
    ColorMapEntry entry = new ColorMapEntryImpl();

    if (firstEntry != null) {
        entry.setLabel(labelMultipleValue ? firstEntry.getLabel() : null);
        entry.setOpacity(opacityMultipleValue ? firstEntry.getOpacity() : null);
        entry.setQuantity(quantityMultipleValue ? firstEntry.getQuantity() : null);
        entry.setColor(colourMultipleValue ? firstEntry.getColor() : null);
    }

    return entry;
}
 
Example 4
Source File: ColourMapModel.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates the colour map entry.
 *
 * @param data the data
 * @return the color map entry
 */
private ColorMapEntry createColourMapEntry(ColourMapData data) {
    ColorMapEntry entry = new ColorMapEntryImpl();
    entry.setColor(data.getColourExpression());
    entry.setOpacity(data.getOpacity());
    entry.setQuantity(data.getQuantity());
    entry.setLabel(data.getLabel());
    return entry;
}
 
Example 5
Source File: FieldConfigPopulationTest.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Test method for {@link
 * com.sldeditor.ui.detail.config.FieldConfigPopulation#populateColourMapField(com.sldeditor.common.xml.ui.FieldIdEnum,
 * org.geotools.styling.ColorMap)}. Test method for {@link
 * com.sldeditor.ui.detail.config.FieldConfigPopulation#getColourMap(com.sldeditor.common.xml.ui.FieldIdEnum)}.
 * Test method for {@link
 * com.sldeditor.ui.detail.config.FieldConfigPopulation#getColourMap(com.sldeditor.ui.detail.config.FieldId)}.
 */
@Test
public void testColourMap() {

    FieldIdEnum fieldId = FieldIdEnum.DESCRIPTION;

    GraphicPanelFieldManager fieldConfigManager = new GraphicPanelFieldManager(String.class);

    FieldConfigColourMap colourMapField =
            new FieldConfigColourMap(
                    new FieldConfigCommonData(Geometry.class, fieldId, "label", true, false));
    colourMapField.createUI();
    fieldConfigManager.add(fieldId, colourMapField);

    ColorMap expectedValue = new ColorMapImpl();
    ColorMapEntry entry = new ColorMapEntryImpl();
    StyleBuilder styleBuilder = new StyleBuilder();

    entry.setColor(styleBuilder.colorExpression(Color.PINK));
    entry.setQuantity(styleBuilder.literalExpression(2.3));
    expectedValue.addColorMapEntry(entry);
    FieldConfigPopulation obj = new FieldConfigPopulation(fieldConfigManager);
    obj.populateColourMapField(fieldId, expectedValue);
    assertEquals(
            expectedValue.getColorMapEntries().length,
            obj.getColourMap(fieldId).getColorMapEntries().length);

    // This shouldn't work as it does not know about the field
    FieldIdEnum wrongFieldEnum = FieldIdEnum.ELSE_FILTER;
    obj.populateColourMapField(wrongFieldEnum, expectedValue);
    assertNull(obj.getColourMap(wrongFieldEnum));
}
 
Example 6
Source File: MultipleColourMapEntryTest.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Test method for {@link
 * com.sldeditor.ui.detail.config.colourmap.MultipleColourMapEntry#parseList(java.util.List)}.
 * Test method for {@link
 * com.sldeditor.ui.detail.config.colourmap.MultipleColourMapEntry#getColourMapEntry()}.
 */
@Test
public void testParseList() {
    FilterFactory ff = CommonFactoryFinder.getFilterFactory();

    MultipleColourMapEntry testObj = new MultipleColourMapEntry();

    assertNotNull(testObj.getColourMapEntry());

    ColorMapEntry c1 = new ColorMapEntryImpl();
    String expectedLabel = "abc";
    String expectedColour = "#123456";
    double expectedOpacity = 0.5;
    int expectedQuantity = 42;

    c1.setLabel(expectedLabel);
    c1.setColor(ff.literal(expectedColour));
    c1.setOpacity(ff.literal(expectedOpacity));
    c1.setQuantity(ff.literal(expectedQuantity));
    List<ColorMapEntry> expectedList = new ArrayList<ColorMapEntry>();
    expectedList.add(c1);

    ColorMapEntry c2 = new ColorMapEntryImpl();
    c2.setLabel(expectedLabel);
    c2.setColor(ff.literal(expectedColour));
    c2.setOpacity(ff.literal(expectedOpacity));
    c2.setQuantity(ff.literal(expectedQuantity));
    expectedList.add(c2);

    ColorMapEntry c3 = new ColorMapEntryImpl();
    c3.setLabel(expectedLabel);
    c3.setColor(ff.literal(expectedColour));
    c3.setOpacity(ff.literal(expectedOpacity));
    c3.setQuantity(ff.literal(expectedQuantity));
    expectedList.add(c3);

    testObj.parseList(expectedList);

    ColorMapEntry actual = testObj.getColourMapEntry();

    // All the same
    assertNotNull(testObj.getColourMapEntry());
    assertEquals(actual.getLabel(), expectedLabel);
    assertEquals(actual.getColor().toString(), expectedColour);
    assertEquals(actual.getOpacity().toString(), String.valueOf(expectedOpacity));
    assertEquals(actual.getQuantity().toString(), String.valueOf(expectedQuantity));

    // Change label
    c2.setLabel("different");
    testObj.parseList(expectedList);

    actual = testObj.getColourMapEntry();

    assertNotNull(testObj.getColourMapEntry());
    assertNull(actual.getLabel());
    assertEquals(actual.getColor().toString(), expectedColour);
    assertEquals(actual.getOpacity().toString(), String.valueOf(expectedOpacity));
    assertEquals(actual.getQuantity().toString(), String.valueOf(expectedQuantity));

    // Change colour
    c1.setColor(ff.literal("#987654"));
    testObj.parseList(expectedList);

    actual = testObj.getColourMapEntry();

    assertNotNull(testObj.getColourMapEntry());
    assertNull(actual.getLabel());
    assertNull(actual.getColor());
    assertEquals(actual.getOpacity().toString(), String.valueOf(expectedOpacity));
    assertEquals(actual.getQuantity().toString(), String.valueOf(expectedQuantity));

    // Change opacity
    c3.setOpacity(ff.literal(1.0));
    testObj.parseList(expectedList);

    actual = testObj.getColourMapEntry();

    assertNotNull(testObj.getColourMapEntry());
    assertNull(actual.getLabel());
    assertNull(actual.getColor());
    assertNull(actual.getOpacity());
    assertEquals(actual.getQuantity().toString(), String.valueOf(expectedQuantity));

    // Change quantity
    c2.setQuantity(ff.literal(39.0));
    testObj.parseList(expectedList);

    actual = testObj.getColourMapEntry();

    assertNotNull(testObj.getColourMapEntry());
    assertNull(actual.getLabel());
    assertNull(actual.getColor());
    assertNull(actual.getOpacity());
    assertNull(actual.getQuantity());
}
 
Example 7
Source File: OmsCoverageViewer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
@Execute
public void viewCoverage() throws Exception {
    StyleFactory sf = CommonFactoryFinder.getStyleFactory(null);
    // RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
    // Style rasterStyle = SLD.wrapSymbolizers(sym);

    StyleBuilder sB = new StyleBuilder(sf);
    RasterSymbolizer rasterSym = sf.createRasterSymbolizer();

    ColorMap colorMap = sf.createColorMap();

    RenderedImage renderedImage = raster.getRenderedImage();
    double max = Double.NEGATIVE_INFINITY;
    double min = Double.POSITIVE_INFINITY;
    RectIter iter = RectIterFactory.create(renderedImage, null);
    do {
        do {
            double value = iter.getSampleDouble();
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        } while( !iter.nextPixelDone() );
        iter.startPixels();
    } while( !iter.nextLineDone() );

    // red to blue
    Color fromColor = Color.blue;
    Color toColor = Color.red;
    Expression fromColorExpr = sB
            .colorExpression(new java.awt.Color(fromColor.getRed(), fromColor.getGreen(), fromColor.getBlue(), 255));
    Expression toColorExpr = sB
            .colorExpression(new java.awt.Color(toColor.getRed(), toColor.getGreen(), toColor.getBlue(), 255));
    Expression fromExpr = sB.literalExpression(min);
    Expression toExpr = sB.literalExpression(max);

    ColorMapEntry entry = sf.createColorMapEntry();
    entry.setQuantity(fromExpr);
    entry.setColor(fromColorExpr);
    colorMap.addColorMapEntry(entry);

    entry = sf.createColorMapEntry();
    entry.setQuantity(toExpr);
    entry.setColor(toColorExpr);
    colorMap.addColorMapEntry(entry);

    rasterSym.setColorMap(colorMap);

    Style rasterStyle = SLD.wrapSymbolizers(rasterSym);

    // Set up a MapContext with the two layers
    final MapContent map = new MapContent();
    map.setTitle("Coverage Viewer");
    map.addLayer(new GridCoverageLayer(raster, rasterStyle));

    // Create a JMapFrame with a menu to choose the display style for the
    final JMapFrame frame = new JMapFrame(map);
    frame.setSize(800, 600);
    frame.enableStatusBar(true);
    frame.enableTool(JMapFrame.Tool.ZOOM, JMapFrame.Tool.PAN, JMapFrame.Tool.RESET);
    frame.enableToolBar(true);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter(){
        public void windowClosing( WindowEvent e ) {
            frame.setVisible(false);
        }
    });

    while( frame.isVisible() ) {
        Thread.sleep(300);
    }
}
 
Example 8
Source File: OmsMapsViewer.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
private void addCoverages( final MapContent map ) throws Exception {
    if (inRasters == null) {
        return;
    }
    RasterSymbolizer rasterSym = sf.createRasterSymbolizer();
    ColorMap colorMap = sf.createColorMap();

    for( String rasterPath : inRasters ) {
        GridCoverage2D readRaster = OmsRasterReader.readRaster(rasterPath);
        RenderedImage renderedImage = readRaster.getRenderedImage();
        double max = Double.NEGATIVE_INFINITY;
        double min = Double.POSITIVE_INFINITY;
        RectIter iter = RectIterFactory.create(renderedImage, null);
        do {
            do {
                double value = iter.getSampleDouble();
                if (value > max) {
                    max = value;
                }
                if (value < min) {
                    min = value;
                }
            } while( !iter.nextPixelDone() );
            iter.startPixels();
        } while( !iter.nextLineDone() );

        // red to blue
        Color fromColor = Color.blue;
        Color midColor = Color.green;
        Color toColor = Color.red;
        Expression fromColorExpr = sb
                .colorExpression(new java.awt.Color(fromColor.getRed(), fromColor.getGreen(), fromColor.getBlue(), 255));
        Expression midColorExpr = sb
                .colorExpression(new java.awt.Color(midColor.getRed(), midColor.getGreen(), midColor.getBlue(), 255));
        Expression toColorExpr = sb
                .colorExpression(new java.awt.Color(toColor.getRed(), toColor.getGreen(), toColor.getBlue(), 255));
        Expression fromExpr = sb.literalExpression(min);
        Expression midExpr = sb.literalExpression(min + (max - min) / 2);
        Expression toExpr = sb.literalExpression(max);

        ColorMapEntry entry = sf.createColorMapEntry();
        entry.setQuantity(fromExpr);
        entry.setColor(fromColorExpr);
        colorMap.addColorMapEntry(entry);

        entry = sf.createColorMapEntry();
        entry.setQuantity(midExpr);
        entry.setColor(midColorExpr);
        colorMap.addColorMapEntry(entry);

        entry = sf.createColorMapEntry();
        entry.setQuantity(toExpr);
        entry.setColor(toColorExpr);
        colorMap.addColorMapEntry(entry);

        rasterSym.setColorMap(colorMap);

        Style rasterStyle = SLD.wrapSymbolizers(rasterSym);

        GridCoverageLayer layer = new GridCoverageLayer(readRaster, rasterStyle);

        map.addLayer(layer);
    }
}