org.geotools.styling.ColorMapEntry Java Examples

The following examples show how to use org.geotools.styling.ColorMapEntry. 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: MultipleColourMapEntry.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses the list.
 *
 * @param entries the entries
 */
public void parseList(List<ColorMapEntry> entries) {
    firstEntry = entries.get(0);
    String labelValue = firstEntry.getLabel();
    String opacityValue = firstEntry.getOpacity().toString();
    String quantityValue = firstEntry.getQuantity().toString();
    String colourValue = firstEntry.getColor().toString();

    for (ColorMapEntry entry : entries) {
        if ((labelValue != null) && labelValue.compareTo(entry.getLabel()) != 0) {
            labelMultipleValue = false;
        }
        if ((opacityValue != null)
                && opacityValue.compareTo(entry.getOpacity().toString()) != 0) {
            opacityMultipleValue = false;
        }
        if ((quantityValue != null)
                && quantityValue.compareTo(entry.getQuantity().toString()) != 0) {
            quantityMultipleValue = false;
        }
        if ((colourValue != null) && colourValue.compareTo(entry.getColor().toString()) != 0) {
            colourMultipleValue = false;
        }
    }
}
 
Example #3
Source File: EncodeColourMap.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Encode colour map entries into a string.
 *
 * @param colourMap the colour map
 * @return the string
 */
public static String encode(ColorMap colourMap) {

    StringBuilder sb = new StringBuilder();

    for (ColorMapEntry entry : colourMap.getColorMapEntries()) {
        sb.append((entry.getLabel() == null) ? "" : entry.getLabel());
        sb.append(SEPARATOR);
        sb.append(entry.getColor());
        sb.append(SEPARATOR);
        sb.append(Double.valueOf(entry.getOpacity().toString()));
        sb.append(SEPARATOR);
        sb.append(entry.getQuantity());
        sb.append(ENTRY_SEPARATOR);
    }

    return sb.toString();
}
 
Example #4
Source File: ColourMapModel.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Populate.
 *
 * @param value the value
 */
public void populate(ColorMap value) {
    colourMapList.clear();

    for (ColorMapEntry colourMapEntry : value.getColorMapEntries()) {
        Expression colourExpression = colourMapEntry.getColor();
        Expression opacityExpression = colourMapEntry.getOpacity();
        Expression quantityExpression = colourMapEntry.getQuantity();
        String label = colourMapEntry.getLabel();

        ColourMapData data = new ColourMapData();

        data.setColour(colourExpression);
        data.setOpacity(opacityExpression);
        data.setQuantity(quantityExpression);
        data.setLabel(label);
        colourMapList.add(data);
    }
    this.fireTableDataChanged();
}
 
Example #5
Source File: ColourRampPanel.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Populate value.
 *
 * @param spinner the spinner
 * @param colorMapEntry the color map entry
 */
private void populateValue(FieldConfigInteger spinner, ColorMapEntry colorMapEntry) {
    if (spinner == null) {
        return;
    }

    if (colorMapEntry == null) {
        return;
    }

    int quantity = 1;

    Expression quantityExpression = colorMapEntry.getQuantity();

    if (quantityExpression != null) {
        Object quantityValue = ((LiteralExpressionImpl) quantityExpression).getValue();
        if (quantityValue instanceof Integer) {
            quantity = ((Integer) quantityValue).intValue();
        } else if (quantityValue instanceof Double) {
            quantity = ((Double) quantityValue).intValue();
        } else if (quantityValue instanceof String) {
            quantity = Double.valueOf((String) quantityValue).intValue();
        }
    }

    spinner.populateField(quantity);
}
 
Example #6
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 #7
Source File: FieldConfigColourMap.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/** Items selected. */
protected void itemsSelected() {
    removeButton.setEnabled(true);

    List<ColorMapEntry> entries;
    if (table.getSelectedRowCount() == 1) {
        ColorMapEntry entry = model.getColourMapEntry(table.getSelectedRow());
        entries = new ArrayList<>();
        entries.add(entry);
    } else {
        entries = model.getColourMapEntries(table.getSelectedRows());
    }
    colourMapEntryPanel.setSelectedEntry(entries);
}
 
Example #8
Source File: ColourMapModel.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the colour map entries.
 *
 * @param selectedRows the selected rows
 * @return the colour map entries
 */
public List<ColorMapEntry> getColourMapEntries(int[] selectedRows) {
    if (selectedRows == null) {
        return null;
    }
    List<ColorMapEntry> entries = new ArrayList<>();

    for (int selectedRow : selectedRows) {
        ColourMapData colourMapData = colourMapList.get(selectedRow);

        entries.add(createColourMapEntry(colourMapData));
    }

    return entries;
}
 
Example #9
Source File: ColourMapModel.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the colour map entry.
 *
 * @param selectedRow the selected row
 * @return the colour map entry
 */
public ColorMapEntry getColourMapEntry(int selectedRow) {
    if ((selectedRow < 0) || (selectedRow >= colourMapList.size())) {
        return null;
    }
    ColourMapData colourMapData = colourMapList.get(selectedRow);

    return createColourMapEntry(colourMapData);
}
 
Example #10
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 #11
Source File: ColourMapModel.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the colour map.
 *
 * @return the colour map
 */
public ColorMap getColourMap() {
    ColorMap colourMap = new ColorMapImpl();

    for (ColourMapData data : colourMapList) {
        ColorMapEntry entry = createColourMapEntry(data);

        colourMap.addColorMapEntry(entry);
    }
    return colourMap;
}
 
Example #12
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 #13
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 #14
Source File: ColourRampPanel.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void populate(ColorMap value) {
    if (value != null) {
        ColorMapEntry[] entries = value.getColorMapEntries();
        if ((entries != null) && (entries.length > 0)) {
            populateValue(minValueSpinner, entries[0]);
            populateValue(maxValueSpinner, entries[entries.length - 1]);
        }
    }
}
 
Example #15
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 #16
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 #17
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);
    }
}