Search in sources :

Example 86 with StyledLayerDescriptor

use of org.geotools.styling.StyledLayerDescriptor in project sldeditor by robward-scisys.

the class RasterReader method createRasterSLDData.

/**
 * Creates the raster sld.
 *
 * @param rasterFile the raster file
 * @return the styled layer descriptor
 */
@Override
public SLDDataInterface createRasterSLDData(File rasterFile) {
    if (rasterFile == null) {
        return null;
    }
    StyledLayerDescriptor sld = null;
    AbstractGridFormat format = GridFormatFinder.findFormat(rasterFile);
    AbstractGridCoverage2DReader reader = null;
    try {
        reader = format.getReader(rasterFile);
    } catch (UnsupportedOperationException e) {
        ConsoleManager.getInstance().error(this, Localisation.getField(RasterTool.class, "RasterReader.unknownFormat") + rasterFile.getAbsolutePath());
        return null;
    }
    BufferedImage img = null;
    try {
        img = ImageIO.read(rasterFile);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
        return null;
    }
    WritableRaster raster = img.getRaster();
    Style style = createRGBStyle(reader, raster);
    sld = sf.createStyledLayerDescriptor();
    NamedLayer namedLayer = sf.createNamedLayer();
    namedLayer.addStyle(style);
    sld.addStyledLayer(namedLayer);
    File sldFilename = ExternalFilenames.createSLDFilename(rasterFile);
    StyleWrapper styleWrapper = new StyleWrapper(sldFilename.getName());
    String sldContents = sldWriter.encodeSLD(null, sld);
    SLDData sldData = new SLDData(styleWrapper, sldContents);
    sldData.setSLDFile(sldFilename);
    sldData.setReadOnly(false);
    return sldData;
}
Also used : SLDData(com.sldeditor.common.data.SLDData) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) StyleWrapper(com.sldeditor.common.data.StyleWrapper) WritableRaster(java.awt.image.WritableRaster) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) Style(org.geotools.styling.Style) NamedLayer(org.geotools.styling.NamedLayer) File(java.io.File)

Example 87 with StyledLayerDescriptor

use of org.geotools.styling.StyledLayerDescriptor in project sldeditor by robward-scisys.

the class YSLDTool method exportToYSLD.

/**
 * Export to YSLD.
 */
private void exportToYSLD() {
    SLDWriterInterface ysldWriter = SLDWriterFactory.createWriter(SLDOutputFormatEnum.YSLD);
    for (SLDDataInterface sldData : sldDataList) {
        StyledLayerDescriptor sld = SLDUtils.createSLDFromString(sldData);
        String layerName = sldData.getLayerNameWithOutSuffix();
        if (sld != null) {
            String sldString = ysldWriter.encodeSLD(sldData.getResourceLocator(), sld);
            String destinationFolder = sldData.getSLDFile().getParent();
            File fileToSave = GenerateFilename.findUniqueName(destinationFolder, layerName, YSLDTool.YSLD_FILE_EXTENSION);
            String ysldFilename = fileToSave.getName();
            if (fileToSave.exists()) {
                ConsoleManager.getInstance().error(this, Localisation.getField(YSLDTool.class, "YSLDTool.destinationAlreadyExists") + " " + ysldFilename);
            } else {
                ConsoleManager.getInstance().information(this, Localisation.getField(YSLDTool.class, "YSLDTool.exportToYSLDMsg") + " " + ysldFilename);
                try (BufferedWriter out = new BufferedWriter(new FileWriter(fileToSave))) {
                    out.write(sldString);
                } catch (IOException e) {
                    ConsoleManager.getInstance().exception(this, e);
                }
            }
        }
    }
}
Also used : StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) SLDDataInterface(com.sldeditor.common.SLDDataInterface) FileWriter(java.io.FileWriter) SLDWriterInterface(com.sldeditor.common.output.SLDWriterInterface) IOException(java.io.IOException) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 88 with StyledLayerDescriptor

use of org.geotools.styling.StyledLayerDescriptor in project sldeditor by robward-scisys.

the class SLDEditorCommon method populate.

/**
 * Populate.
 *
 * @param sldData the sld data
 */
public void populate(SLDDataInterface sldData) {
    String layerName = sldData.getLayerName();
    File sldEditorFile = sldData.getSldEditorFile();
    if (sldEditorFile != null) {
        ConsoleManager.getInstance().information(this, String.format("%s : %s", Localisation.getString(SLDEditor.class, "SLDEditor.loadedSLDEditorFile"), sldEditorFile.getAbsolutePath()));
    }
    ConsoleManager.getInstance().information(this, String.format("%s : %s", Localisation.getString(SLDEditor.class, "SLDEditor.loadedSLDFile"), layerName));
    StyledLayerDescriptor sld = SLDUtils.createSLDFromString(sldData);
    SelectedSymbol selectedSymbolInstance = SelectedSymbol.getInstance();
    selectedSymbolInstance.setSld(sld);
    selectedSymbolInstance.setFilename(layerName);
    selectedSymbolInstance.setName(layerName);
    SLDEditorFile.getInstance().setSLDData(sldData);
    // Reload data source if sticky flag is set
    boolean isDataSourceSticky = SLDEditorFile.getInstance().isStickyDataSource();
    DataSourceInterface dataSource = DataSourceFactory.createDataSource(null);
    DataSourcePropertiesInterface previousDataSource = dataSource.getDataConnectorProperties();
    dataSource.reset();
    if (isDataSourceSticky) {
        SLDEditorFile.getInstance().setDataSource(previousDataSource);
    }
    dataSource.connect(ExternalFilenames.removeSuffix(layerName), SLDEditorFile.getInstance(), CheckAttributeFactory.getCheckList());
    VendorOptionManager.getInstance().loadSLDFile(SLDEditorUIPanels.getInstance(), sld, sldData);
    LegendManager.getInstance().sldLoaded(sldData.getLegendOptions());
    SLDEditorFile.getInstance().fileOpenedSaved();
}
Also used : SelectedSymbol(com.sldeditor.common.data.SelectedSymbol) DataSourceInterface(com.sldeditor.datasource.DataSourceInterface) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) DataSourcePropertiesInterface(com.sldeditor.common.DataSourcePropertiesInterface) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) File(java.io.File)

Example 89 with StyledLayerDescriptor

use of org.geotools.styling.StyledLayerDescriptor in project sldeditor by robward-scisys.

the class VendorOptionUI method populate.

/**
 * Populate.
 *
 * @param selectedSymbol the selected symbol
 */
public void populate(SelectedSymbol selectedSymbol) {
    if (selectedSymbol != null) {
        StyledLayerDescriptor sld = selectedSymbol.getSld();
        updatePanel(sld);
    }
}
Also used : StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor)

Example 90 with StyledLayerDescriptor

use of org.geotools.styling.StyledLayerDescriptor in project sldeditor by robward-scisys.

the class SLDWriterImpl method encodeSLD.

/**
 * Encode sld to a string.
 *
 * @param resourceLocator the resource locator
 * @param sld the sld
 * @return the string
 */
@Override
public String encodeSLD(URL resourceLocator, StyledLayerDescriptor sld) {
    String xml = "";
    if (sld != null) {
        InlineDatastoreVisitor duplicator = new InlineDatastoreVisitor();
        sld.accept(duplicator);
        StyledLayerDescriptor sldCopy = (StyledLayerDescriptor) duplicator.getCopy();
        if (resourceLocator != null) {
            SLDExternalImages.updateOnlineResources(resourceLocator, sldCopy);
        }
        SLDTransformer transformer = new SLDTransformer();
        transformer.setIndentation(2);
        try {
            xml = transformer.transform(sldCopy);
        } catch (TransformerException e) {
            ConsoleManager.getInstance().exception(this, e);
        }
    }
    return xml;
}
Also used : StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) SLDTransformer(org.geotools.styling.SLDTransformer) TransformerException(javax.xml.transform.TransformerException)

Aggregations

StyledLayerDescriptor (org.geotools.styling.StyledLayerDescriptor)134 Test (org.junit.jupiter.api.Test)80 Rule (org.geotools.styling.Rule)54 Style (org.geotools.styling.Style)52 FeatureTypeStyle (org.geotools.styling.FeatureTypeStyle)50 NamedLayer (org.geotools.styling.NamedLayer)48 SLDData (com.sldeditor.common.data.SLDData)25 StyleFactoryImpl (org.geotools.styling.StyleFactoryImpl)24 File (java.io.File)22 StyledLayer (org.geotools.styling.StyledLayer)21 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)19 SLDDataInterface (com.sldeditor.common.SLDDataInterface)17 GraphicPanelFieldManager (com.sldeditor.ui.detail.GraphicPanelFieldManager)17 SLDTreeTools (com.sldeditor.ui.tree.SLDTreeTools)17 StyleWrapper (com.sldeditor.common.data.StyleWrapper)16 IOException (java.io.IOException)16 PointSymbolizer (org.geotools.styling.PointSymbolizer)16 FieldConfigString (com.sldeditor.ui.detail.config.FieldConfigString)15 PolygonSymbolizer (org.geotools.styling.PolygonSymbolizer)13 DataSourceAttributeData (com.sldeditor.datasource.attribute.DataSourceAttributeData)12