Repository navigation
GeoFigureContext
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 GeoFigureContext.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official.geo;
import com.velitask.plugin.official.GeoMapIndicator;
import com.velitask.sdk.figures.FigureContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import org.abricos.geo.MapZoom;
/* Base context of all geo figures: a copy of the map geometry from the indicator context. */
public class GeoFigureContext extends FigureContext<GeoMapIndicator.GeoMapContext> {
/* Zoom of the frame; null if no geo sensor is selected. */
public MapZoom zoom;
/* Map center in global tile pixels at this zoom. */
public double centerPx;
public double centerPy;
/* Indicator map matrix: shift, scale, tilt, rotation. */
public AffineTransform mapTransform;
/* The values are copied from the indicator context once per frame. */
public GeoFigureContext(GeoMapIndicator.GeoMapContext indCtx) {
super(indCtx);
zoom = indCtx.zoom;
centerPx = indCtx.centerPx;
centerPy = indCtx.centerPy;
mapTransform = indCtx.mapTransform;
}
/* Latitude/longitude -> map pixels -> screen point via mapTransform (taking rotation and tilt into account). */
public Point2D.Double toScreen(double lat, double lon) {
Point2D.Double p = new Point2D.Double(zoom.pixelX(lon), zoom.pixelY(lat));
mapTransform.transform(p, p);
return p;
}
}GeoFigureContext is the common base context for all figures of the GeoMapIndicator map. It takes the ready geometry (zoom, map center, matrix) from the indicator context and provides a single method, toScreen(lat, lon), which converts a geographic point into layer coordinates. The figures GeoTrackFigure, GeoPositionFigure, GeoPlaceBoundaryFigure, and GeoPlaceLabelsFigure extend it with their own nested contexts holding their own properties.
Data. The context extends FigureContext<GeoMapIndicator.GeoMapContext>, so the whole indicator context is available through indicator, while graphics, width, height, and scale come from the base FigureContext. The four own fields, zoom, centerPx, centerPy, and mapTransform, are copied from GeoMapContext in the constructor.
Settings. The class has no properties. The figures' properties read the nested contexts through mX.get(indCtx) or mX.createContext(indCtx), where indCtx is the indicator context.
Drawing. toScreen(lat, lon) computes the map pixel at the current zoom (zoom.pixelX(lon), zoom.pixelY(lat)) and passes it through mapTransform. The same method accounts for the shift to the frame center, the fractional scale of the "Fit track" mode, the 3D tilt compression, and rotation. The result is a screen point, Point2D.Double, in the layer coordinate system, without rounding.
- Each
toScreen(...)call creates a newPoint2D.Double; in loops over thousands of track points this is the only allocation per point. - If the zoom is
null(no geo sensor),toScreen(...)must not be called: the figures checkzoomfirst. - The fields are public and mutable, but the figures should only read them; the single source of the values is
GeoMapContext. - The method does not check whether the point is inside the visible area: the figure itself does the clipping.