Class SpriteGraphicFactory

  • All Implemented Interfaces:
    ExternalGraphicFactory, GraphicCache

    public class SpriteGraphicFactory
    extends Object
    implements ExternalGraphicFactory, GraphicCache
    Implementation of an ExternalGraphicFactory that takes the address of a Mapbox-style sprite sheet resource and an icon name, and retrieves the icon from the sprite sheet.

    Note that this factory expects the MBStyleTransformer to produce ExternalGraphic instances with slightly modified URLs using one of the following forms: {baseUrl}#{iconName} {baseUrl}#icon={iconName}&size={sizeMultiplier}

    Only the baseUrl is used to retrieve the sprite sheet (at {baseUrl}.png) and sprite index (at {baseUrl}.json). The iconName (required) is then used by this factory to select the correct icon from the spritesheet, and the size (optional) is used to scale the icon. For example, for the following style:

     {
      "version": 8,
      "name": "A Style",
      "sprite": "file:/GeoServerDataDirs/release/styles/testSpritesheet",
      "glyphs": "...",
      "sources": {...},
      "layers": [...]
     }
     

    If a layer in this style references an icon in the spritesheet, e.g. iconName, then the constructed URL for the external graphic should be file:/GeoServerDataDirs/release/styles/testSpritesheet#iconName

    See Also:
    https://www.mapbox.com/mapbox-gl-js/style-spec/#sprite
    • Constructor Detail

      • SpriteGraphicFactory

        public SpriteGraphicFactory()
    • Method Detail

      • getIcon

        public Icon getIcon​(Feature feature,
                            Expression url,
                            String format,
                            int size)
                     throws Exception
        Description copied from interface: ExternalGraphicFactory
        Turns the specified URL into an Icon, eventually using the Feature attributes to evaluate CQL expressions embedded in the url.
        The size parameter defines the size of the image (so that vector based symbols can be drawn at the specified size directly), or may be zero or negative if the size was not specified (in that case the "natural" size of the image will be used, which is the size in pixels for raster images, and 16 for any format that does not have a specific size, according to the SLD spec).
        null will be returned if this factory cannot handle the provided url.
        Specified by:
        getIcon in interface ExternalGraphicFactory
        Throws:
        Exception
      • parseFragmentParams

        protected static Map<String,​String> parseFragmentParams​(URL url)
        Parse the parameters from the URL fragment in the provided URL (interpreting the fragment like a query string). The "name" parameter is required and will cause an MBFormatException if missing. The "size" parameter is optional and defaults to "1".
        Returns:
        Sprite parameters map providing name, icons, size values.
      • parseIconName

        protected static String parseIconName​(URL url)
        Parse the icon name from the provided URL. E.g., /path/to/sprite#iconName will return iconName.
        Parameters:
        url - The url from which to parse the icon name.
        Returns:
        The icon name.
        Throws:
        IllegalArgumentException - If the icon name could not be parsed.
      • parseBaseUrl

        protected static URL parseBaseUrl​(URL loc)
                                   throws MalformedURLException
        Return the base URL (without an appended icon name) from the provided URL. /path/to/sprite#iconName will return path/to/sprite
        Parameters:
        loc - The URL.
        Returns:
        The URL, without an appended icon name.
        Throws:
        MalformedURLException
      • getSpriteIndex

        protected SpriteIndex getSpriteIndex​(URL baseUrl)
                                      throws IOException
        Retrieve the sprite sheet index for the provided sprite base url. The base url should have no extension.
        Parameters:
        baseUrl - The base URL of the Mapbox sprite source (no extension).
        Returns:
        The sprite sheet index
        Throws:
        IOException
      • getHttpClient

        protected HTTPClient getHttpClient()
      • clearCache

        public void clearCache()
        Images are cached by this factory. This method can be used to drop the cache.
        Specified by:
        clearCache in interface GraphicCache