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. Must not be changed without a migration. */
public static final String NAME = "latLonText";
/* Localization key prefix: 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");
}
/* Labels 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 decimal places (JSON key "precision"), range 0..12. */
private final IntegerProperty mPrecision = new IntegerProperty() {
{
/* The range limits values set through the UI or set(); it is not checked when loading from JSON. */
setRange(0, 12);
}
@Override
public String getName() {
return "precision";
}
/* Default value 7. In the saved JSON, before the first change the value key may be absent. */
@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, bottom/center alignment, 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);
}
/* Property order = order in the settings panel and in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mGeo,
mFont,
mTextAlign,
mDisplayType,
mPrecision
};
}
/* Alignment, format, and 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: geo sensor atom, coordinate formatting, text output. */
@Override
public void render(IndicatorContext indicatorContext) {
LatLonTextContext ctx = (LatLonTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The atom for the frame time. No data: the frame is not drawn. */
GeoSensorAtom atom = ctx.geo.queryAtom();
if (atom == null) {
return;
}
/* Latitude and 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);
};
}
/* 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);
}
/* Form "55.7558000°N, 37.6173000°E": the absolute value and the letter of the compass side. */
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 decimals in seconds is fixed (2), precision is ignored. */
private static String formatDMS(double lat, double lon) {
return toDMS(lat, "N", "S") + ", " + toDMS(lon, "E", "W");
}
/* Form 55°45'20.88"N: seconds with two decimals 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: 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;
/* 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 letters, and degrees-minutes-seconds. For the decimal formats, the number of decimal places is configurable.
Data. GeoSensorProperty (JSON key Geo) provides ctx.geo. render calls ctx.geo.queryAtom(); if there is no atom, the layer is not drawn. Latitude and longitude are computed as atom.calcLat(ctx.geo.rawTime) and atom.calcLon(ctx.geo.rawTime): linear interpolation inside the atom window, with values in decimal degrees.
Settings. Five properties in the order Geo, font, textAlign, displayType, precision.
-
fontis aFontColorProperty, size 48, white color. -
textAlignis aTextAlignProperty,CENTERby default. -
displayTypeis anEnumArrayProperty<DisplayType>("Format"):DECIMAL(0),DECIMAL_NSEW(1),DMS(2);DECIMALby default. -
precisionis anIntegerProperty("Decimal places") with a range of 0-12 and a default value of 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, format, and number of decimal places are moved to the "Appearance" group.
Formatting. The text is produced 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 always have two decimals |
In the DECIMAL_NSEW and DMS formats, the sign of the coordinate is replaced by the letters N/S (latitude) and E/W (longitude).
Drawing. Anti-aliasing is turned on, the font is taken with ctx.scale applied, the color is set, and the text is output through ctx.textAlign.drawText(...).
- If there is no data on the frame, the layer is empty.
- The indicator has no text template or units setting: the order "latitude, longitude" and the
,separator are fixed in the code. - In the
DMSformat, theprecisionsetting is ignored. - The
precisionrange (0-12) is checked when a value is set in the UI; a value read from JSON is not brought into the range. In the format,max(0, precision)is handled. - The property names (
Geo,font,textAlign,displayType,precision) are part of the saved format: renaming breaks loading of projects.