Package org.geotools.mbstyle.sprite
Class SpriteGraphicFactory
- Object
-
- SpriteGraphicFactory
-
- All Implemented Interfaces:
ExternalGraphicFactory
,GraphicCache
public class SpriteGraphicFactory extends Object implements ExternalGraphicFactory, GraphicCache
Implementation of anExternalGraphicFactory
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 produceExternalGraphic
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
-
-
Field Summary
Fields Modifier and Type Field Description static String
FORMAT
ExternalGraphic
instances with this format will be handled by theSpriteGraphicFactory
.protected static Map<URL,BufferedImage>
imageCache
protected static Map<URL,SpriteIndex>
indexCache
-
Constructor Summary
Constructors Constructor Description SpriteGraphicFactory()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearCache()
Images are cached by this factory.protected HTTPClient
getHttpClient()
Icon
getIcon(Feature feature, Expression url, String format, int size)
Turns the specified URL into an Icon, eventually using the Feature attributes to evaluate CQL expressions embedded in the url.
Thesize
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.protected SpriteIndex
getSpriteIndex(URL baseUrl)
Retrieve the sprite sheet index for the provided sprite base url.protected static URL
parseBaseUrl(URL loc)
Return the base URL (without an appended icon name) from the provided URL.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).protected static String
parseIconName(URL url)
Parse the icon name from the providedURL
.
-
-
-
Field Detail
-
FORMAT
public static final String FORMAT
ExternalGraphic
instances with this format will be handled by theSpriteGraphicFactory
.- See Also:
- Constant Field Values
-
imageCache
protected static final Map<URL,BufferedImage> imageCache
-
indexCache
protected static final Map<URL,SpriteIndex> indexCache
-
-
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.
Thesize
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 interfaceExternalGraphicFactory
- 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 anMBFormatException
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)
- 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 returnpath/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 interfaceGraphicCache
-
-