Military icons provide default styling and decorations out-of-the-box. They come with labels containing all modifier information, movement direction arrows indicating the speed of a vehicle, and a selection rectangle to indicate which icon is currently active.
MilSymStyle already allows for some customization:
you can influence the style of the icon itself,
you can choose not to draw the selection rectangle,
and you get some options to affect the length of the movement direction arrow.
However, if those options aren’t enough to help you accomplish the effect that you need,
you can always decorate your icons through a FeaturePainter that wraps the MilitarySymbologyPainter.
This article demonstrates how to customize icon decorations by working out three use cases:
Start from a custom decorator painter
For each of the icon decorations discussed in this article,
you must start from the creation of a decorator painter.
For this purpose, you can use a FeaturePainter that delegates to the MilitarySymbologyPainter for regular painting.
Chances are that you already have such a painter, for clustering purposes for example.
If that’s the case, you can just re-use that painter and add functionality on top of it.
class MilitarySymbologyDecoratorPainter extends BasicFeaturePainter {
private readonly _symbologyPainter: MilitarySymbologyPainter;
constructor(symbologyPainter: MilitarySymbologyPainter) {
super();
this._symbologyPainter = symbologyPainter;
//add these so that when the symbology painter is invalidated internally
// (for example because a new icon is fetched from the symbology service),
// this ripples through correctly.
this._symbologyPainter.on("InvalidateById", (id: string) => this.invalidateById(id));
this._symbologyPainter.on("InvalidateAll", () => this.invalidateAll());
this._symbologyPainter.on("Invalidate", (feature: Feature) => this.invalidate(feature));
}
Get the style of the military icon
To decorate a military icon correctly, you must know which style was used to draw the icon.
You can find that out by calling getIconStyle. Once the icon has resolved, this call returns its style.
From that style, you can learn the offsets and the size.
public getIconInfo(feature: Feature, shape: Shape, paintState: PaintState, map: RIAMap): {
offsetX: number,
offsetY: number,
size: number
} | null {
const iconStyle = this._symbologyPainter.getIconStyle(feature, shape, paintState, map);
if (iconStyle) {
return {
offsetX: Number.parseInt(`${iconStyle.offsetX}`),
offsetY: Number.parseInt(`${iconStyle.offsetY}`),
size: Number.parseInt(`${iconStyle.width}`),
}
}
return null;
}
Decorate a symbol with a country flag
It’s possible to replace the default country label with a country flag image.
| LuciadRIA doesn’t include the country flag image used in this section. To decorate with country flags, you must provide your own set of flag images. |
To replace the country label with a flag, you must adapt both the paintLabel and the paintBody method of your custom decorator painter.
Inside the paintLabel method, you must disable the country modifier.
Inside the paintBody method, you must add the functionality to draw the country flag.
The next sections explain each of these steps in more detail.
Disable the country label
Inside paintLabel, disable the country modifier right before calling the delegate painter
and re-enable the country modifier right after that call.
paintLabel(labelCanvas: LabelCanvas, feature: Feature, shape: Shape, layer: Layer, map: RIAMap,
paintState: PaintState): void {
if (feature.properties.code) {
// the country label is replaced by a country flag (in paintBody),
// to remove it from the label, set the country modifier to "undefined" while painting the label
const countryBefore = feature.properties.modifiers["country"];
feature.properties.modifiers["country"] = undefined;
this._symbologyPainter.paintLabel(labelCanvas, feature, shape, layer, map, paintState);
feature.properties.modifiers["country"] = countryBefore;
}
}
Enable the country flag
Inside paintBody, you can add functionality to draw a country flag after painting the regular icon body.
Make sure to use the icon size and offsets retrieved from getIconInfo. See Get the style of the military icon for more information.
|
paintBody(geoCanvas: GeoCanvas, feature: Feature, shape: Shape, layer: FeatureLayer, map: RIAMap,
paintState: PaintState): void {
// if the feature is a military feature
if (feature.properties.code) {
// first delegate to draw the military feature
this._symbologyPainter.paintBody(geoCanvas, feature, shape, layer, map, paintState);
// if the feature represents an icon
if (shape.type === ShapeType.POINT) {
const iconInfo = this.getIconInfo(feature, shape, paintState, map);
if (iconInfo) {
const countryFlagInfo = retrieveCountryFlagInfo(feature, 0.33 * iconInfo.size);
if (countryFlagInfo) {
geoCanvas.drawIcon(shape, {
url: countryFlagInfo.url,
anchorX: "0%",
anchorY: "100%",
offsetX: `${iconInfo.offsetX + (iconInfo.size / 2)}px`,
offsetY: `${iconInfo.offsetY - (iconInfo.size / 2)}px`,
width: countryFlagInfo.sizePercentage,
height: countryFlagInfo.sizePercentage,
})
}
}
}
}
}
}
This is how you can fetch the correct URL and size parameters:
// A cache to keep the actual sizes (widths) of the country flag image files!
const FLAG_WIDTH_CACHE: Map<string, number> = new Map();
// The information needed to draw the country flag icon
interface CountryFlagInfo {
// the url of the image
url: string,
// the percentage (e.g. "30%") to apply to the image to get the desired with.
sizePercentage: string,
}
// Retrieve the url for this country + the sizePercentage (to keep the height-width-ratio correct) to apply to the current size of the flag
function retrieveCountryFlagInfo(feature: Feature, desiredWidth: number): CountryFlagInfo | null {
const country = feature.properties.modifiers["country"];
if (!country) {
return null;
}
const countryFlagUrl = `some_path_to_images/countryFlags/${country}.png`;
let countryFlagWidth = FLAG_WIDTH_CACHE.get(countryFlagUrl);
if (!countryFlagWidth) {
countryFlagWidth = 20;
loadImageFromString(countryFlagUrl).then((image: HTMLImageElement) => {
countryFlagWidth = image.width;
FLAG_WIDTH_CACHE.set(countryFlagUrl, countryFlagWidth);
});
}
return {
url: countryFlagUrl,
sizePercentage: `${100 * (desiredWidth / countryFlagWidth)}%`,
}
}
// A function to load the image, so that after loading we can get its actual width!
function loadImageFromString(url: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const image = new Image();
image.addEventListener('load', () => resolve(image));
image.addEventListener("error", () => reject(new Error("Cannot load image")));
image.crossOrigin = "Anonymous";
image.src = url;
})
}
Show a selection circle instead of a rectangle
By default, LuciadRIA shows a rectangle around a selected symbol. This section discusses how to replace the default rectangle with a selection circle.
First, make sure to create a MilitarySymbologyPainter that
doesn’t paint the selection rectangle.
MilitarySymbologyPainter without selection rectangle.
const symbologyPainterWithoutSelectionRectangle = new MilitarySymbologyPainter(MIL_STD_2525b, {
codeFunction: function(feature: Feature) {
//We return the SIDC of the military symbol
return feature.properties.code;
},
style: function(feature: Feature): MilSymStyle {
return {
// don't draw the selection rectangle
rectangle: false
}
},
});
Next, add code inside paintBody to draw the selection rectangle.
Make sure to use the icon size and offsets retrieved from getIconInfo. See Get the style of the military icon for more information.
|
paintBody to draw the selection circle.
paintBody(geoCanvas: GeoCanvas, feature: Feature, shape: Shape, layer: FeatureLayer, map: RIAMap,
paintState: PaintState): void {
// if the feature is a military feature
if (feature.properties.code) {
// first delegate to draw the military feature
this._symbologyPainter.paintBody(geoCanvas, feature, shape, layer, map, paintState);
// if the feature represents an icon and it is currently selected
if (shape.type === ShapeType.POINT && paintState.selected) {
const iconInfo = this.getIconInfo(feature, shape, paintState, map);
if (iconInfo) {
// paint the orange selection circle
geoCanvas.drawIcon(shape, {
image: createCircle({
width: iconInfo.size,
height: iconInfo.size,
stroke: "#FF9900",
strokeWidth: 2,
}),
offsetX: iconInfo.offsetX,
offsetY: iconInfo.offsetY,
})
}
}
}
}
Replace movement direction arrows with distance lines
You can replace the default speed arrow on a symbol with a custom line that shows distance over time.
By default, LuciadRIA draws a movement direction arrow or speed leader, a line without an arrow,
if the speedLabel and movementDirection modifiers are present.
In the default implementation, LuciadRIA tries to convert the speed into a certain length.
The higher the speed, the longer the arrow.
To replace the arrow with a line of which the endpoint indicates
where the military unit will be in 30 seconds,
temporarily disable the speedLabel and movementDirection modifiers before
calling the delegate MilitarySymbologyPainter paintBody method.
After that, you can draw your own movement line.
Disable the movement modifiers
Inside paintBody, disable the modifiers right before calling the delegate painter,
and re-enable them right after that call.
paintBody(geoCanvas: GeoCanvas, feature: Feature, shape: Shape, layer: FeatureLayer, map: RIAMap,
paintState: PaintState): void {
// if the feature is a military feature
if (feature.properties.code) {
// disable the modifiers that trigger the default arrow
const speedBefore = feature.properties.modifiers["speedLabel"];
const orientationBefore = feature.properties.modifiers["movementDirection"];
feature.properties.modifiers["speedLabel"] = undefined;
feature.properties.modifiers["movementDirection"] = undefined;
// draw the feature
this._symbologyPainter.paintBody(geoCanvas, feature, shape, layer, map, paintState);
// re-enable these modifiers, so that the label is correct
feature.properties.modifiers["speedLabel"] = speedBefore;
feature.properties.modifiers["movementDirection"] = orientationBefore;
// Paint the movement direction line
if (shape.type === ShapeType.POINT) {
this.paintMovementDirectionAsDistanceAfterXs(geoCanvas, feature, shape as Point, 50);
}
}
}
Paint the distance line
After painting the regular icon body, you can add functionality to draw a movement distance line inside paintBody.
private paintMovementDirectionAsDistanceAfterXs(geoCanvas: GeoCanvas, feature: Feature, locationNow: Point,
deltaTimeInS: number): void {
// get the speed (for now, expected in m/s) and direction (= azimuth)
const speedLabel = feature.properties.modifiers["speedLabel"];
const direction = feature.properties.modifiers["movementDirection"];
if (speedLabel && direction) {
const speed = parseInt(speedLabel, 10);
const distanceAfterDeltaTime = speed * deltaTimeInS;
const geodesy = createGeodesy(locationNow.reference!);
const locationEnd = geodesy.interpolate(locationNow, distanceAfterDeltaTime, direction);
const movementLine = createPolyline(locationNow.reference, [locationNow, locationEnd]);
geoCanvas.drawShape(movementLine, {
stroke: {
width: 3,
color: "#e600ff"
},
});
}
}