public class SpriteGraphicFactory extends Object implements ExternalGraphicFactory, GraphicCache
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
Modifier and Type | Field and Description |
---|---|
static String |
FORMAT
ExternalGraphic instances with this format will be handled by the SpriteGraphicFactory . |
protected static Map<URL,BufferedImage> |
imageCache |
protected static Map<URL,SpriteIndex> |
indexCache |
Constructor and Description |
---|
SpriteGraphicFactory() |
Modifier and Type | Method and 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.
|
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 provided
URL . |
public static final String FORMAT
ExternalGraphic
instances with this format will be handled by the SpriteGraphicFactory
.protected static final Map<URL,BufferedImage> imageCache
protected static final Map<URL,SpriteIndex> indexCache
public Icon getIcon(Feature feature, Expression url, String format, int size) throws Exception
ExternalGraphicFactory
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.getIcon
in interface ExternalGraphicFactory
Exception
protected static Map<String,String> parseFragmentParams(URL url)
MBFormatException
if missing. The "size" parameter is optional and defaults to "1".protected static String parseIconName(URL url)
url
- The url from which to parse the icon name.IllegalArgumentException
- If the icon name could not be parsed.protected static URL parseBaseUrl(URL loc) throws MalformedURLException
/path/to/sprite#iconName
will return path/to/sprite
loc
- The URL.MalformedURLException
protected SpriteIndex getSpriteIndex(URL baseUrl) throws IOException
baseUrl
- The base URL of the Mapbox sprite source (no extension).IOException
protected HTTPClient getHttpClient()
public void clearCache()
clearCache
in interface GraphicCache
Copyright © 1996–2023 Geotools. All rights reserved.