Search in sources :

Example 1 with SLDParser

use of org.geotools.styling.SLDParser in project hale by halestudio.

the class XMLStylePage3 method getStyle.

/**
 * @see FeatureStylePage#getStyle(boolean)
 */
@Override
public Style getStyle(boolean force) throws Exception {
    if (viewer == null || (!force && !changed)) {
        return null;
    }
    IDocument doc = viewer.getDocument();
    SLDParser parser = new SLDParser(styleFactory, new StringReader(doc.get()));
    Style[] styles = parser.readXML();
    return styles[0];
}
Also used : StringReader(java.io.StringReader) Style(org.geotools.styling.Style) SLDParser(org.geotools.styling.SLDParser) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with SLDParser

use of org.geotools.styling.SLDParser in project hale by halestudio.

the class StyleServiceImpl method addStyles.

/**
 * @see StyleService#addStyles(URL)
 */
@Override
public boolean addStyles(URL url) {
    SLDParser stylereader;
    try {
        stylereader = new SLDParser(styleFactory, url);
        Style[] styles = stylereader.readXML();
        addStyles(styles);
        return true;
    } catch (Exception e) {
        // $NON-NLS-1$
        _log.error("Error reading styled layer descriptor", e);
        return false;
    }
}
Also used : Style(org.geotools.styling.Style) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) SLDParser(org.geotools.styling.SLDParser)

Example 3 with SLDParser

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

the class SLDUtils method createSLDFromString.

/**
 * Creates a StyledLayerDescriptor object containing a SLD from a string.
 *
 * @param sldData the sld data
 * @return the styled layer descriptor
 */
public static StyledLayerDescriptor createSLDFromString(SLDDataInterface sldData) {
    if ((sldData == null) || (sldData.getSld() == null)) {
        return null;
    }
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
    InputStream stream = new ByteArrayInputStream(sldData.getSld().getBytes());
    SLDParser styleReader = new SLDParser(styleFactory, stream);
    URL resourceLocator = getResourceLocator(sldData);
    sldData.setResourceLocator(resourceLocator);
    setResourcelocator(styleReader, resourceLocator);
    StyledLayerDescriptor sld = null;
    try {
        sld = styleReader.parseSLD();
    } catch (RuntimeException e) {
        String errorMessage = String.format("SLD Parser error : %s", sldData.getStyle().toString());
        ConsoleManager.getInstance().error(SLDUtils.class, errorMessage);
        ConsoleManager.getInstance().error(SLDUtils.class, e.getMessage());
    }
    return sld;
}
Also used : StyleFactory(org.geotools.styling.StyleFactory) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SLDParser(org.geotools.styling.SLDParser) URL(java.net.URL)

Example 4 with SLDParser

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

the class SLDUtils method readSLDFile.

/**
 * Creates a StyledLayerDescriptor object containing a SLD by reading the contents of a file.
 *
 * @param file the file
 * @return the styled layer descriptor
 */
public static StyledLayerDescriptor readSLDFile(File file) {
    StyledLayerDescriptor sld = null;
    if (file != null) {
        StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory();
        try {
            // By using URL here allows external graphics to loaded properly
            URL url = file.toURI().toURL();
            SLDParser styleReader = new SLDParser(styleFactory, url);
            setResourcelocator(styleReader, file.toURI().toURL());
            sld = styleReader.parseSLD();
        } catch (MalformedURLException e) {
            ConsoleManager.getInstance().exception(SLDUtils.class, e);
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(SLDUtils.class, e);
        }
    }
    return sld;
}
Also used : StyleFactory(org.geotools.styling.StyleFactory) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) MalformedURLException(java.net.MalformedURLException) SLDParser(org.geotools.styling.SLDParser) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with SLDParser

use of org.geotools.styling.SLDParser in project hale by halestudio.

the class SLDStyleReader method execute.

/**
 * @see AbstractIOProvider#execute(ProgressIndicator, IOReporter)
 */
@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    progress.begin("Load styles from SLD", ProgressIndicator.UNKNOWN);
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
    InputStream in = getSource().getInput();
    try {
        SLDParser stylereader = new SLDParser(styleFactory, in);
        styles = stylereader.readXML();
        reporter.setSuccess(true);
    } catch (Exception e) {
        reporter.error(new IOMessageImpl("Loading styles from SLD failed.", e));
        reporter.setSuccess(false);
    } finally {
        in.close();
        progress.end();
    }
    return reporter;
}
Also used : StyleFactory(org.geotools.styling.StyleFactory) InputStream(java.io.InputStream) IOMessageImpl(eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl) SLDParser(org.geotools.styling.SLDParser) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) IOException(java.io.IOException)

Aggregations

SLDParser (org.geotools.styling.SLDParser)5 StyleFactory (org.geotools.styling.StyleFactory)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 URL (java.net.URL)2 Style (org.geotools.styling.Style)2 StyledLayerDescriptor (org.geotools.styling.StyledLayerDescriptor)2 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 IOMessageImpl (eu.esdihumboldt.hale.common.core.io.report.impl.IOMessageImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 IDocument (org.eclipse.jface.text.IDocument)1 FeatureTypeStyle (org.geotools.styling.FeatureTypeStyle)1