Repository navigation
LatLonTextIndicator
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not kept in the working file LatLonTextIndicator.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official;
import com.velitask.sdk.FrameContext;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.data.GeoSensorAtom;
import com.velitask.sdk.properties.ArrayProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.GeoSensorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.TextAlignProperty;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.Locale;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;
/* Text with the latitude and longitude of the current track point in one of three formats. */
public class LatLonTextIndicator extends Indicator {
/* Stable indicator name in the layer JSON. It must not be changed without a migration. */
public static final String NAME = "latLonText";
/* Prefix of the localization keys: Plugin.Indicator.latLonText. */
private static final String KEY = _KEY + "." + NAME;
/* Output formats. JSON stores the index (ordinal): 0, 1, 2. */
public enum DisplayType {
DECIMAL,
DECIMAL_NSEW,
DMS
}
/* Geo sensor (JSON key "Geo"). */
private final GeoSensorProperty mGeo = new GeoSensorProperty();
/* Font and color (JSON key "font"), size 48, white color. */
private final FontColorProperty mFont = new FontColorProperty();
/* Text alignment; CENTER by default (index 4). */
private final TextAlignProperty mTextAlign = new TextAlignProperty();
/* Format choice (JSON key "displayType"); DECIMAL by default (index 0). */
private final EnumArrayProperty<DisplayType> mDisplayType = new EnumArrayProperty<>(DisplayType.class) {
@Override
public String getName() {
return "displayType";
}
@Override
public String getTitle() {
return localized(KEY + ".displayType.title");
}
/* Captions of the choice items, the order matches DisplayType. */
@Override
protected String[] defineTitles() {
return new String[]{
localized(KEY + ".displayType.decimal"),
localized(KEY + ".displayType.decimalNsew"),
localized(KEY + ".displayType.dms")
};
}
};
/* Number of digits after the decimal point (JSON key "precision"), range 0..12. */
private final IntegerProperty mPrecision = new IntegerProperty() {
{
/* The range limits values set through the interface or set(); it is not checked when loading from JSON. */
setRange(0, 12);
}
@Override
public String getName() {
return "precision";
}
/* The default value is 7. In the saved JSON, before the first change the key value may be missing. */
@Override
public Integer getDefault() {
return 7;
}
@Override
public String getTitle() {
return localized(KEY + ".precision.title");
}
};
@Override
public String getName() {
return NAME;
}
@Override
public String getTitle() {
return localized(KEY + ".title");
}
@Override
public String getDescription() {
return localized(KEY + ".description");
}
@Override
public String getTags() {
return localized(KEY + ".tags");
}
/* Default maket: layer 800 x 80, aligned bottom/center, margins (left, top, right, bottom) = null, null, 50, null. */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.BOTTOM);
maket.setHorizontal(HorizontalAlign.CENTER);
maket.setLayerMargin(null, null, 50d, null);
maket.setLayerSize(800d, 80d);
}
/* Order of properties = order in the settings panel and in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mGeo,
mFont,
mTextAlign,
mDisplayType,
mPrecision
};
}
/* The alignment, the format and the precision are in the "Appearance" group. */
@Override
public void configureDisplay(DisplayConfig config) {
config.set(mTextAlign, PropertyGroup.APPEARANCE);
config.set(mDisplayType, PropertyGroup.APPEARANCE);
config.set(mPrecision, PropertyGroup.APPEARANCE);
}
/* Drawing: the geo sensor atom, formatting the coordinates, text output. */
@Override
public void render(IndicatorContext indicatorContext) {
LatLonTextContext ctx = (LatLonTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The atom for the frame time; in a recording pause, the last atom before the pause. No data: the frame is not drawn. */
GeoSensorAtom atom = ctx.geo.queryAtomOrBefore();
if (atom == null) {
return;
}
/* The latitude and the longitude linearly interpolated to the frame time inside the atom window. */
double lat = atom.calcLat(ctx.geo.rawTime);
double lon = atom.calcLon(ctx.geo.rawTime);
/* The string is built without a text template and without a units setting. */
String text = format(lat, lon, ctx.displayType.value, ctx.precision.value);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = ctx.font.toFont(ctx.scale);
g.setFont(font);
g.setColor(ctx.font.color.value);
ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
}
/* Format choice; an unknown or empty type is treated as DECIMAL. */
private static String format(double lat, double lon, DisplayType type, int precision) {
if (type == null) {
type = DisplayType.DECIMAL;
}
return switch (type) {
case DECIMAL_NSEW ->
formatDecimalNSEW(lat, lon, precision);
case DMS ->
formatDMS(lat, lon);
default ->
formatDecimal(lat, lon, precision);
};
}
/* The form "55.7558000, 37.6173000": a sign for negative values, a decimal point (Locale.US). */
private static String formatDecimal(double lat, double lon, int precision) {
int p = Math.max(0, precision);
String fmt = "%." + p + "f, %." + p + "f";
return String.format(Locale.US, fmt, lat, lon);
}
/* The form "55.7558000°N, 37.6173000°E": the absolute value and a compass-direction letter. */
private static String formatDecimalNSEW(double lat, double lon, int precision) {
int p = Math.max(0, precision);
String fmt = "%." + p + "f°%s, %." + p + "f°%s";
return String.format(Locale.US, fmt,
Math.abs(lat), lat >= 0 ? "N" : "S",
Math.abs(lon), lon >= 0 ? "E" : "W");
}
/* Degrees-minutes-seconds; the number of digits of the seconds is fixed (2), precision is not taken into account. */
private static String formatDMS(double lat, double lon) {
return toDMS(lat, "N", "S") + ", " + toDMS(lon, "E", "W");
}
/* The form 55°45'20.88"N: seconds with two decimal places and leading zeros. */
private static String toDMS(double v, String pos, String neg) {
double abs = Math.abs(v);
int deg = (int) abs;
double minF = (abs - deg) * 60.0;
int min = (int) minF;
double sec = (minF - min) * 60.0;
return String.format(Locale.US, "%d°%02d'%05.2f\"%s",
deg, min, sec, v >= 0 ? pos : neg);
}
/* Frame context: the property values at the moment of the frame being drawn. */
public class LatLonTextContext extends IndicatorContext {
public final GeoSensorProperty.GeoSensorContext geo;
public final FontColorProperty.FontColorContext font;
public final TextAlignProperty.TextAlignContext textAlign;
public final ArrayProperty<DisplayType>.ArrayContext displayType;
public final IntegerProperty.IntegerContext precision;
/* The property contexts are created per frame (createContext(frame)). */
public LatLonTextContext(FrameContext frame) {
super(frame);
geo = mGeo.createContext(frame);
font = mFont.createContext(frame);
textAlign = mTextAlign.createContext(frame);
displayType = mDisplayType.createContext(frame);
precision = mPrecision.createContext(frame);
}
}
/* Context factory, called for every frame. */
@Override
public IndicatorContext createContext(FrameContext frame) {
return new LatLonTextContext(frame);
}
}LatLonTextIndicator outputs the geographic coordinates of the current track point as text. Three formats are supported: signed decimal, decimal with compass-direction letters, and degrees-minutes-seconds. For the decimal formats the number of digits after the decimal point is configurable.
Data. GeoSensorProperty (JSON key Geo) provides ctx.geo. render calls ctx.geo.queryAtomOrBefore(): the atom that covers the frame time, and in a recording pause the last atom before the pause (the coordinates stay at the last point before the pause). The atom is taken exact, not a simplified copy of the track from a small scale. If there is no atom (outside the recording), the layer is not drawn. The latitude and the longitude are calculated as atom.calcLat(ctx.geo.rawTime) and atom.calcLon(ctx.geo.rawTime): a linear interpolation inside the atom window, the values in decimal degrees.
Settings. Five properties in the order Geo, font, textAlign, displayType, precision.
-
font:FontColorProperty, size 48, white color. -
textAlign:TextAlignProperty,CENTERby default. -
displayType:EnumArrayProperty<DisplayType>("Format"):DECIMAL(0),DECIMAL_NSEW(1),DMS(2);DECIMALby default. -
precision:IntegerProperty("Decimal places") with the range 0-12 and the default value 7. The live state of the property is created empty, and the value 7 is returned bygetDefault()until the user selects anything.
In configureDisplay() the alignment, the format and the number of digits are moved into the "Appearance" group.
Formatting. The text is built by the methods formatDecimal, formatDecimalNSEW and formatDMS with Locale.US, so the separator is always a dot.
| Format | Example |
precision taken into account |
|---|---|---|
DECIMAL |
55.7558000, 37.6173000 |
yes |
DECIMAL_NSEW |
55.7558000°N, 37.6173000°E |
yes |
DMS |
55°45'20.88"N, 37°37'02.28"E |
no, seconds are always with two decimal places |
The sign of the coordinate in the DECIMAL_NSEW and DMS formats is replaced with the letters N/S (latitude) and E/W (longitude).
Drawing. Antialiasing is turned on, the font is taken with ctx.scale applied, the color is set and the text is output through ctx.textAlign.drawText(...).
- Outside the recording the layer is empty; in a recording pause the coordinates of the last point before the pause are shown.
- The indicator has no text template and no units setting: the order "latitude, longitude" and the separator
,are fixed in the code. - In the
DMSformat theprecisionsetting is ignored. - The range of
precision(0-12) is checked when the value is set in the interface; a value read from JSON is not brought into the range. The format handlesmax(0, precision). - The property names (
Geo,font,textAlign,displayType,precision) are part of the saved format: renaming breaks the loading of projects.