Search code examples
geotools

Can't load a service for category "ExternalGraphicFactory"


I'm using geotools-18.5, with JavaFx in Inteliji IDE. When I want to create PointSymbolizer from a svg or png image.

StyleBuilder builder = new StyleBuilder();
ExternalGraphic extGraphic = builder.createExternalGraphic("file:/home/cuongnv/test.svg", "svg");

I build code OK, but when run, I received warning:

WARNING: Can't load a service for category "ExternalGraphicFactory". Provider org.geotools.renderer.style.ImageGraphicFactory could not be instantiated.

Can someone help me ?

Here is full code:

private Style createStyleBuilder(){
        StyleBuilder builder = new StyleBuilder();
        FilterFactory2 ff = builder.getFilterFactory();

        // RULE 1
        // first rule to draw cities

        // define a point symbolizer representing a city
        Graphic city = builder.createGraphic();
        city.setSize(ff.literal(50));
        ExternalGraphic extGraphic =        builder.createExternalGraphic("file:/home/cuongnv/Javafx/GeoTool/geotools_fx_tutorial-master/geotools-fx/src/main/resources/images/console.svg", "svg"); // svg
        city.addExternalGraphic(extGraphic);
        PointSymbolizer pointSymbolizer = builder.createPointSymbolizer(city);
        Rule rule1 = builder.createRule(pointSymbolizer);
        rule1.setName("rule1");
        rule1.getDescription().setTitle("City");
        rule1.getDescription().setAbstract("Rule for drawing cities");
        Rule rules[] = new Rule[] {rule1};
        FeatureTypeStyle featureTypeStyle = builder.createFeatureTypeStyle("Feature", rules);
        Style style = builder.createStyle();
        style.setName("style");
        style.getDescription().setTitle("User Style");
        style.getDescription().setAbstract("Definition of Style");
        style.featureTypeStyles().add(featureTypeStyle);
        return style;

    }

TYPE = DataUtilities.createType(
                    "Dataset",
                    "geometry:Geometry:srid=4326"
                            + ","
                            + "name:String,"
                            + "id:String"
            );
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
            GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
            com.vividsolutions.jts.geom.Point point = geometryFactory.createPoint(new Coordinate(x,y));

            featureBuilder.add(point);
            SimpleFeature feature = featureBuilder.buildFeature(null);


            DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
            featureCollection.add(feature);

            Style style = createStyleBuilder();
            Layer layer = new FeatureLayer(featureCollection, style);
            layer.setTitle("New Point");
            mapContent.layers().add(layer);

Solution

  • You have set the image mime type incorrectly, it should be:

    ExternalGraphic extGraphic = builder.createExternalGraphic("file:/stuff/ian/geoserver/data/styles/burg02.svg", "image/svg"); // svg
    

    and all will work.

    EDIT

    If you are still having issues then try adding this code to the end of the createStyle module and look at the generated SVG, possibly in GeoServer to test it out.

    SLDTransformer styleTransform = new SLDTransformer();
    StyledLayerDescriptor sld = sf.createStyledLayerDescriptor();
    UserLayer layer = sf.createUserLayer();
    layer.setLayerFeatureConstraints(new FeatureTypeConstraint[] {null});
    sld.addStyledLayer(layer);
    layer.addUserStyle(style);
    
    try {
      String xml = styleTransform.transform(sld);
      System.out.println(xml);
    } catch (TransformerException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }