Skip to content

GeoPlaceBoundaryFigure

roosit edited this page Oct 16, 2026 · 2 revisions

GeoPlaceBoundaryFigure

Up | Previous | Next | Русский

Full source with short comments

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 GeoPlaceBoundaryFigure.java: 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.IndicatorContext;
import com.velitask.sdk.figures.Figure;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DisplayHint;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.LineProperty;
import com.velitask.sdk.properties.MapPlacesProperty;
import com.velitask.sdk.properties.MapVectorProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SizeProperty;
import com.velitask.sdk.properties.TransparencyProperty;
import com.velitask.sdk.properties.VisibleProperty;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.abricos.geo.GeoUtils;
import org.abricos.geo.vector.Feature;
import org.abricos.geo.vector.GeometryType;
import org.abricos.geo.vector.VectorDataTypes;
import org.abricos.geo.vector.VectorTile;

/* Figure "Place boundaries": the outline of a city, town, village, or hamlet. */
public class GeoPlaceBoundaryFigure extends Figure<GeoFigureContext> {

    /* Figure name (the type field in the figure's JSON). */
    public static final String NAME = "placeBoundary";

    /* Prefix of the localization keys. */
    private static final String KEY = GeoMapIndicator.KEY + "." + NAME;

    /* Vector data exists up to zoom 12; at higher zooms the zoom-12 tile is used. */
    private static final int MAX_DATA_ZOOM = 12;

    /* "Nearest" mode: searches in expanding rings of tiles, no more than 6 rings. */
    private static final int NEAREST_MAX_RADIUS = 6;
    private static final int NEAREST_SAFETY_RINGS = 1;

    /* All types of places. */
    private static final Set<String> ALL_PLACE_TYPES = Set.of(
            VectorDataTypes.CITY, VectorDataTypes.TOWN, VectorDataTypes.VILLAGE, VectorDataTypes.HAMLET);

    /* Number of segments of the circle that replaces the boundary when a place has only a point. */
    private static final int CIRCLE_POINTS = 24;

    /* Mode: active only (where the position is now), nearest, all polygons. The index is stored in JSON. */
    public enum DisplayMode {
        ACTIVE, NEAREST, ALL
    }

    /* Display mode property; "Active only" by default. */
    private final EnumArrayProperty<DisplayMode> mMode
            = new EnumArrayProperty<>(DisplayMode.class) {
        @Override
        public String getName() {
            return "mode";
        }

        @Override
        public String getTitle() {
            return localized(KEY + ".mode.title");
        }

        @Override
        protected String[] defineTitles() {
            return new String[]{
                localized(KEY + ".mode.active"),
                localized(KEY + ".mode.nearest"),
                localized(KEY + ".mode.all")
            };
        }
    };

    /* Vector data sources: boundaries and points of places; not saved to JSON. */
    private final MapVectorProperty mVector = new MapVectorProperty();
    private final MapPlacesProperty mPlaces = new MapPlacesProperty();

    /* Outline line: default color (180, 80, 0), thickness 1..20, 2 by default. */
    private final LineProperty mLine = new LineProperty();

    {
        mLine.getColor().setDefault(new Color(180, 80, 0));
        mLine.getColor().set(new Color(180, 80, 0));
        mLine.getThickness().setRange(1, 20);
        mLine.getThickness().setDefault(2);
        mLine.getThickness().set(2);
    }

    /* Name of the fill color property. */
    private static final String FILL_COLOR = "fillColor";
    /* Outline fill (255, 160, 60, 60); drawn when "Show fill" is on. */
    private final ColorProperty mFillColor = new ColorProperty(
            new Color(255, 160, 60, 60), FILL_COLOR, localized(KEY + "." + FILL_COLOR + ".title"));

    /* Show the fill of ordinary outlines; off by default. */
    private final VisibleProperty mShowFill = toggle("showFill", false);

    /* Highlight the current (or nearest) outline; on by default. */
    private final VisibleProperty mShowHighlight = toggle("showHighlight", true);

    /* Name of the highlight color property. */
    private static final String HIGHLIGHT_COLOR = "highlightColor";
    /* Highlight color (255, 210, 70). */
    private final ColorProperty mHighlightColor = new ColorProperty(
            new Color(255, 210, 70), HIGHLIGHT_COLOR, localized(KEY + "." + HIGHLIGHT_COLOR + ".title"));

    /* Highlight transparency 0..100, 55 by default. */
    private final TransparencyProperty mHighlightTransparency = new TransparencyProperty() {
        @Override
        public Integer getDefault() {
            return 55;
        }
    };

    /* For each type of place - a switch and a minimum zoom: cities from 8, towns from 10, villages from 12, hamlets from 14. */
    private final VisibleProperty mCityShow = toggle("cityShow", true);
    private final SizeProperty mCityZoom = zoomSpinner("cityZoom", 8);

    private final VisibleProperty mTownShow = toggle("townShow", true);
    private final SizeProperty mTownZoom = zoomSpinner("townZoom", 10);

    private final VisibleProperty mVillageShow = toggle("villageShow", true);
    private final SizeProperty mVillageZoom = zoomSpinner("villageZoom", 12);

    private final VisibleProperty mHamletShow = toggle("hamletShow", true);
    private final SizeProperty mHamletZoom = zoomSpinner("hamletZoom", 14);

    /* Switch factory: name, default value. */
    private VisibleProperty toggle(String name, boolean def) {
        return new VisibleProperty() {
            {
                setDefault(def);
                set(def);
            }

            @Override
            public String getName() {
                return name;
            }

            @Override
            public String getTitle() {
                return localized(KEY + "." + name + ".title");
            }
        };
    }

    /* Minimum-zoom spinner factory: range 1..19. */
    private SizeProperty zoomSpinner(String name, int def) {
        return new SizeProperty() {
            {
                setRange(1, 19);
            }

            @Override
            public Integer getDefault() {
                return def;
            }

            @Override
            public String getName() {
                return name;
            }

            @Override
            public String getTitle() {
                return localized(KEY + "." + name + ".title");
            }
        };
    }

    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public String getTitle() {
        return localized(KEY + ".title");
    }

    @Override
    /* Order and set of the saved properties of the figure. */
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mMode,
            mLine, mFillColor, mShowFill,
            mShowHighlight, mHighlightColor, mHighlightTransparency,
            mCityShow, mCityZoom,
            mTownShow, mTownZoom,
            mVillageShow, mVillageZoom,
            mHamletShow, mHamletZoom
        };
    }

    @Override
    /* All settings are in the appearance group; zooms are spinners. */
    public void configureDisplay(DisplayConfig config) {
        config.set(mMode, PropertyGroup.APPEARANCE);
        config.set(mLine, PropertyGroup.APPEARANCE);
        config.set(mFillColor, PropertyGroup.APPEARANCE);
        config.set(mShowFill, PropertyGroup.APPEARANCE);
        config.set(mShowHighlight, PropertyGroup.APPEARANCE);
        config.set(mHighlightColor, PropertyGroup.APPEARANCE);
        config.set(mHighlightTransparency, PropertyGroup.APPEARANCE);
        for (SizeProperty s : new SizeProperty[]{mCityZoom, mTownZoom, mVillageZoom, mHamletZoom}) {
            config.set(s, PropertyGroup.APPEARANCE, DisplayHint.SPINNER);
        }
        for (VisibleProperty v : new VisibleProperty[]{mCityShow, mTownShow, mVillageShow, mHamletShow}) {
            config.set(v, PropertyGroup.APPEARANCE);
        }
    }

    @Override
    /* Choosing the data by mode, then drawing the outlines. */
    public void render(GeoFigureContext baseCtx) {
        BoundaryContext ctx = (BoundaryContext) baseCtx;
        if (ctx.zoom == null) {
            return;
        }
        int z = ctx.zoom.getLevel();

        /* Types of places allowed by the switches and the current zoom. */
        Set<String> required = new LinkedHashSet<>();
        if (ctx.cityShow && z >= ctx.cityZoom) {
            required.add(VectorDataTypes.CITY);
        }
        if (ctx.townShow && z >= ctx.townZoom) {
            required.add(VectorDataTypes.TOWN);
        }
        if (ctx.villageShow && z >= ctx.villageZoom) {
            required.add(VectorDataTypes.VILLAGE);
        }
        if (ctx.hamletShow && z >= ctx.hamletZoom) {
            required.add(VectorDataTypes.HAMLET);
        }

        /* A position is needed for "Active only" and "Nearest"; without it nothing is drawn. */
        boolean active = ctx.displayMode == DisplayMode.ACTIVE;
        boolean nearest = ctx.displayMode == DisplayMode.NEAREST;
        boolean hasPos = !Double.isNaN(ctx.indicator.posLat) && !Double.isNaN(ctx.indicator.posLon);
        boolean needCurrent = hasPos && (active || ctx.showHighlight);
        if (active || nearest) {
            if (!hasPos) {
                return;
            }
        } else if (required.isEmpty() && !ctx.showHighlight) {
            return;
        }

        /* The data tile number is computed with a shift if the map zoom is above 12. */
        int dataZoom = Math.min(z, MAX_DATA_ZOOM);
        int shift = z - dataZoom;
        int maxData = (1 << dataZoom) - 1;
        boolean preview = ctx.indicator.player.isPreview;

        Set<String> seenPoly = new HashSet<>();
        Set<String> seenNode = new HashSet<>();
        List<Feature> polygons = new ArrayList<>();
        List<Feature> nodes = new ArrayList<>();
        Set<String> nodeSubtypes = active
                ? ALL_PLACE_TYPES
                : (nearest ? (required.isEmpty() ? ALL_PLACE_TYPES : required) : null);

        /* Active only: one tile under the position. */
        if (active) {
            int px = clamp(((int) (ctx.zoom.pixelX(ctx.indicator.posLon) / GeoUtils.TILE_SIZE)) >> shift, maxData);
            int py = clamp(((int) (ctx.zoom.pixelY(ctx.indicator.posLat) / GeoUtils.TILE_SIZE)) >> shift, maxData);
            collectTile(px, py, dataZoom, preview, seenPoly, polygons, nodeSubtypes, seenNode, nodes);
        /* Nearest: rings of tiles around the position until data is found (plus one spare ring). */
        } else if (nearest) {
            int ptx = ((int) (ctx.zoom.pixelX(ctx.indicator.posLon) / GeoUtils.TILE_SIZE)) >> shift;
            int pty = ((int) (ctx.zoom.pixelY(ctx.indicator.posLat) / GeoUtils.TILE_SIZE)) >> shift;
            Set<Long> requested = new HashSet<>();
            int found = -1;
            for (int rad = 0; rad <= NEAREST_MAX_RADIUS; rad++) {
                boolean pending = collectRing(ptx, pty, rad, dataZoom, maxData, preview,
                        requested, seenPoly, polygons, nodeSubtypes, seenNode, nodes);
                boolean any = !polygons.isEmpty() || !nodes.isEmpty();
                if (any && found < 0) {
                    found = rad;
                }
                if (found >= 0 && rad >= found + NEAREST_SAFETY_RINGS) {
                    break;
                }
                if (found < 0 && preview && pending) {
                    break;
                }
            }
        /* All polygons: tiles within the radius of the visible area around the map center. */
        } else {
            double cx = ctx.centerPx;
            double cy = ctx.centerPy;
            double extra = Math.max(0.05, ctx.indicator.extraScale);
            double radiusPx = Math.hypot(ctx.width, ctx.height) / extra + GeoUtils.TILE_SIZE;
            int maxIndex = (1 << z) - 1;
            int xMin = Math.max((int) Math.floor((cx - radiusPx) / GeoUtils.TILE_SIZE) - 1, 0);
            int yMin = Math.max((int) Math.floor((cy - radiusPx) / GeoUtils.TILE_SIZE) - 1, 0);
            int xMax = Math.min((int) Math.floor((cx + radiusPx) / GeoUtils.TILE_SIZE) + 1, maxIndex);
            int yMax = Math.min((int) Math.floor((cy + radiusPx) / GeoUtils.TILE_SIZE) + 1, maxIndex);
            Set<Long> requested = new HashSet<>();
            for (int tx = xMin; tx <= xMax; tx++) {
                for (int ty = yMin; ty <= yMax; ty++) {
                    int px = tx >> shift;
                    int py = ty >> shift;
                    long pkey = (((long) px) << 32) | (py & 0xffffffffL);
                    if (!requested.add(pkey)) {
                        continue;
                    }
                    collectTile(px, py, dataZoom, preview, seenPoly, polygons, null, seenNode, nodes);
                }
            }
        }

        /* For places without a polygon, the outline is replaced with a circle (city 2000 m, town 1000 m, village 500 m, hamlet 250 m). */
        if (active || nearest) {
            int areaCount = polygons.size();
            for (Feature node : nodes) {
                boolean inArea = false;
                for (int i = 0; i < areaCount; i++) {
                    if (contains(polygons.get(i).rings(), node.lon(), node.lat())) {
                        inArea = true;
                        break;
                    }
                }
                if (!inArea) {
                    polygons.add(circleFeature(node));
                }
            }
        }
        if (polygons.isEmpty()) {
            return;
        }

        /* Outline thickness and highlight color: alpha = 255 × (100 − transparency) / 100. */
        Graphics2D g = ctx.graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        float lw = (float) Math.max(1.0, ctx.line.thickness * ctx.scale);
        BasicStroke baseStroke = new BasicStroke(lw, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        BasicStroke highlightStroke = new BasicStroke(lw * 1.6f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

        Color baseFill = ctx.fillColor;
        int hlAlpha = (int) Math.round(255.0 * (100 - ctx.highlightTransparency) / 100.0);
        Color highlightFill = new Color(ctx.highlightColor.getRed(), ctx.highlightColor.getGreen(),
                ctx.highlightColor.getBlue(), Math.max(0, Math.min(255, hlAlpha)));

        /* Parsing the polygons: the special one (current or nearest) and the ordinary ones; unnamed ones are skipped. */
        List<Path2D.Double> normal = new ArrayList<>();
        Feature special = null;
        double bestDist = Double.MAX_VALUE;
        double cosLat = Math.cos(Math.toRadians(ctx.indicator.posLat));
        for (Feature poly : polygons) {
            double[][][] rings = poly.rings();
            if (rings == null || rings.length == 0) {
                continue;
            }
            if (poly.getName() == null || poly.getName().isBlank()) {
                continue;
            }
            if (nearest) {
                String type = poly.getSubtype();
                if (!required.isEmpty() && (type == null || !required.contains(type))) {
                    continue;
                }
                double d = polyDistance(rings, ctx.indicator.posLon, ctx.indicator.posLat, cosLat);
                if (d < bestDist) {
                    bestDist = d;
                    special = poly;
                }
                continue;
            }
            if (special == null && needCurrent
                    && contains(rings, ctx.indicator.posLon, ctx.indicator.posLat)) {
                special = poly;
                continue;
            }
            if (active) {
                continue;
            }
            String type = poly.getSubtype();
            if (type == null || !required.contains(type)) {
                continue;
            }
            Path2D.Double path = buildPath(ctx, rings);
            if (path != null) {
                normal.add(path);
            }
        }
        /* Drawing the ordinary outlines, then the special one: highlight, or fill if there is no highlight. */
        Path2D.Double current = special != null ? buildPath(ctx, special.rings()) : null;

        for (Path2D.Double path : normal) {
            if (ctx.showFill) {
                g.setColor(baseFill);
                g.fill(path);
            }
            g.setColor(ctx.line.color);
            g.setStroke(baseStroke);
            g.draw(path);
        }
        if (current != null) {
            if (ctx.showHighlight) {
                g.setColor(highlightFill);
                g.fill(current);
            } else if (ctx.showFill) {
                g.setColor(baseFill);
                g.fill(current);
            }
            g.setColor(ctx.line.color);
            g.setStroke(ctx.showHighlight ? highlightStroke : baseStroke);
            g.draw(current);
        }
    }

    /* Collects the tiles of one ring around the position; returns true if data is still loading. */
    private boolean collectRing(int ptx, int pty, int rad, int dataZoom, int maxData, boolean preview,
            Set<Long> requested, Set<String> seenPoly, List<Feature> polygons,
            Set<String> nodeSubtypes, Set<String> seenNode, List<Feature> nodes) {
        boolean pending = false;
        for (int dx = -rad; dx <= rad; dx++) {
            for (int dy = -rad; dy <= rad; dy++) {
                if (Math.max(Math.abs(dx), Math.abs(dy)) != rad) {
                    continue;
                }
                int px = clamp(ptx + dx, maxData);
                int py = clamp(pty + dy, maxData);
                long pkey = (((long) px) << 32) | (py & 0xffffffffL);
                if (!requested.add(pkey)) {
                    continue;
                }
                if (collectTile(px, py, dataZoom, preview, seenPoly, polygons,
                        nodeSubtypes, seenNode, nodes)) {
                    pending = true;
                }
            }
        }
        return pending;
    }

    /* Tile loading: in preview - from the cache, on export - waiting up to 5 s; duplicates by id are discarded. */
    private boolean collectTile(int px, int py, int dataZoom, boolean preview,
            Set<String> seenPoly, List<Feature> polygons,
            Set<String> nodeSubtypes, Set<String> seenNode, List<Feature> nodes) {
        VectorTile bt = preview
                ? mVector.getFeatures(VectorDataTypes.PLACE_BOUNDARY, dataZoom, px, py)
                : mVector.getFeaturesBlocking(VectorDataTypes.PLACE_BOUNDARY, dataZoom, px, py, 5_000);
        boolean pending = !bt.isLoaded();
        if (bt.getFeatures() != null) {
            for (Feature f : bt.getFeatures()) {
                if (f.getId() != null && !seenPoly.add(f.getId())) {
                    continue;
                }
                polygons.add(f);
            }
        }
        if (nodeSubtypes != null && !nodeSubtypes.isEmpty()) {
            VectorTile pt = preview
                    ? mPlaces.getPlaces(dataZoom, px, py, nodeSubtypes)
                    : mPlaces.getPlacesBlocking(dataZoom, px, py, nodeSubtypes, 5_000);
            if (pt.getFeatures() != null) {
                for (Feature f : pt.getFeatures()) {
                    if (f.getId() != null && !seenNode.add(f.getId())) {
                        continue;
                    }
                    nodes.add(f);
                }
            }
        }
        return pending;
    }

    /* A circular polygon around the point of a place. */
    private static Feature circleFeature(Feature node) {
        double lon = node.lon();
        double lat = node.lat();
        double rMeters = radiusMeters(node.getSubtype());
        double latR = rMeters / 111_320.0;
        double lonR = rMeters / (111_320.0 * Math.cos(Math.toRadians(lat)));
        double[][] ring = new double[CIRCLE_POINTS + 1][];
        for (int i = 0; i <= CIRCLE_POINTS; i++) {
            double a = 2.0 * Math.PI * i / CIRCLE_POINTS;
            ring[i] = new double[]{lon + lonR * Math.cos(a), lat + latR * Math.sin(a)};
        }
        return new Feature(node.getId(), node.getName(), node.getSubtype(),
                GeometryType.POLYGON, new double[][][]{ring});
    }

    /* Circle radius by the type of place. */
    private static double radiusMeters(String subtype) {
        if (subtype == null) {
            return 300.0;
        }
        switch (subtype) {
            case "city":
                return 2000.0;
            case "town":
                return 1000.0;
            case "village":
                return 500.0;
            case "hamlet":
                return 250.0;
            default:
                return 300.0;
        }
    }

    /* Clamping of the tile index. */
    private static int clamp(int v, int max) {
        return Math.max(0, Math.min(v, max));
    }

    /* Distance from the position to the polygon (0 if the position is inside). */
    private static double polyDistance(double[][][] rings, double lon, double lat, double cosLat) {
        if (contains(rings, lon, lat)) {
            return 0.0;
        }
        double min = Double.MAX_VALUE;
        for (double[][] ring : rings) {
            if (ring == null || ring.length < 2) {
                continue;
            }
            for (int i = 0, j = ring.length - 1; i < ring.length; j = i++) {
                double d = segDistance(lon, lat, ring[j][0], ring[j][1], ring[i][0], ring[i][1], cosLat);
                if (d < min) {
                    min = d;
                }
            }
        }
        return min;
    }

    /* Distance from a point to a segment, with the longitude corrected by the cosine of the latitude. */
    private static double segDistance(double plon, double plat,
            double alon, double alat, double blon, double blat, double cosLat) {
        double px = plon * cosLat;
        double ax = alon * cosLat;
        double bx = blon * cosLat;
        double dx = bx - ax;
        double dy = blat - alat;
        double len2 = dx * dx + dy * dy;
        double t = len2 > 0 ? ((px - ax) * dx + (plat - alat) * dy) / len2 : 0.0;
        t = Math.max(0.0, Math.min(1.0, t));
        double cxp = ax + t * dx;
        double cyp = alat + t * dy;
        double ex = px - cxp;
        double ey = plat - cyp;
        return Math.sqrt(ex * ex + ey * ey);
    }

    /* Point-in-polygon test (even-odd rule). */
    private static boolean contains(double[][][] rings, double lon, double lat) {
        boolean inside = false;
        for (double[][] ring : rings) {
            if (ring == null || ring.length < 3) {
                continue;
            }
            int n = ring.length;
            for (int i = 0, j = n - 1; i < n; j = i++) {
                double xi = ring[i][0];
                double yi = ring[i][1];
                double xj = ring[j][0];
                double yj = ring[j][1];
                boolean intersect = ((yi > lat) != (yj > lat))
                        && (lon < (xj - xi) * (lat - yi) / (yj - yi) + xi);
                if (intersect) {
                    inside = !inside;
                }
            }
        }
        return inside;
    }

    /* The outline in screen coordinates via toScreen. */
    private static Path2D.Double buildPath(BoundaryContext ctx, double[][][] rings) {
        Path2D.Double path = new Path2D.Double();
        for (double[][] ring : rings) {
            if (ring == null || ring.length < 2) {
                continue;
            }
            for (int i = 0; i < ring.length; i++) {
                Point2D.Double p = ctx.toScreen(ring[i][1], ring[i][0]);
                if (i == 0) {
                    path.moveTo(p.x, p.y);
                } else {
                    path.lineTo(p.x, p.y);
                }
            }
            path.closePath();
        }
        return path;
    }

    @Override
    /* Creation of the context from the indicator context. */
    public GeoFigureContext createContext(IndicatorContext indCtx) {
        return new BoundaryContext((GeoMapIndicator.GeoMapContext) indCtx);
    }

    /* Figure context: the property values for a frame. */
    public class BoundaryContext extends GeoFigureContext {

        public final DisplayMode displayMode;
        public final LineProperty.LineContext line;
        public final Color fillColor;
        public final boolean showFill;
        public final boolean showHighlight;
        public final Color highlightColor;
        public final int highlightTransparency;

        public final boolean cityShow;
        public final int cityZoom;
        public final boolean townShow;
        public final int townZoom;
        public final boolean villageShow;
        public final int villageZoom;
        public final boolean hamletShow;
        public final int hamletZoom;

        public BoundaryContext(GeoMapIndicator.GeoMapContext indCtx) {
            super(indCtx);
            displayMode = mMode.get(indCtx);
            line = mLine.createContext(indCtx);
            fillColor = mFillColor.get(indCtx);
            showFill = mShowFill.get(indCtx);
            showHighlight = mShowHighlight.get(indCtx);
            highlightColor = mHighlightColor.get(indCtx);
            highlightTransparency = mHighlightTransparency.get(indCtx);

            cityShow = mCityShow.get(indCtx);
            cityZoom = mCityZoom.get(indCtx);
            townShow = mTownShow.get(indCtx);
            townZoom = mTownZoom.get(indCtx);
            villageShow = mVillageShow.get(indCtx);
            villageZoom = mVillageZoom.get(indCtx);
            hamletShow = mHamletShow.get(indCtx);
            hamletZoom = mHamletZoom.get(indCtx);
        }
    }
}

Purpose

GeoPlaceBoundaryFigure shows on the map the boundaries of places - the administrative polygons of cities, towns, villages, and hamlets. The figure can highlight the place where the position currently is, the place nearest to it, or draw all places in the visible area. It is supported only in GeoMapIndicator and is not added by default.

How it works

Data. Vector data comes through MapVectorProperty (of the place-boundary type, VectorDataTypes.PLACE_BOUNDARY) and MapPlacesProperty (the points of places). The data tiles are VectorTile objects with a set of Feature items: each has an id, a name, a subtype (city, town, village, hamlet), and a geometry (rings()). Data exists only up to zoom 12, so at higher zooms the tile index is shifted (z - 12). The position is taken from posLat / posLon of the indicator context (GeoMapContext); if there is no data, they are NaN.

Settings. 15 properties are saved in the figure JSON: mode, line, fillColor, showFill, showHighlight, highlightColor, transparency, and a ...Show / ...Zoom pair for each of city, town, village, hamlet. The mode is the index of DisplayMode: 0 active, 1 nearest, 2 all. A place is drawn if its switch is on and the map zoom is not below the threshold ("from zoom"). MapVectorProperty and MapPlacesProperty are not saved.

Rendering. A list of polygons is collected by mode:

  • Active only - one tile under the position; the outline that contains the position is drawn. The type switches and zoom thresholds do not affect this mode: the place is determined only by the position.
  • Nearest - tiles around the position in rings (up to six); after the first place is found, one more ring is checked; the polygon with the minimum distance to the position among the allowed types is chosen.
  • All polygons - all tiles within the radius of the visible area; places of the allowed types are drawn.

If a place has only a point in the data, a circle with a radius of 2000, 1000, 500, or 250 m (city, town, village, hamlet) is drawn instead of the outline. Unnamed polygons are skipped. Ordinary outlines are drawn with the line line and, with showFill, the fillColor fill. The special (current or nearest) outline is drawn on top: with showHighlight - filled with the highlightColor color at the transparency transparency and with a line 1.6 times thicker; without highlight - like an ordinary one, but with a fill when showFill is on.

Features and limitations

  • While the position is unknown (NaN), the "Active only" and "Nearest" modes draw nothing; the "All polygons" mode works without a position.
  • In the "All polygons" mode, with the highlight off and an empty set of allowed types, there is nothing to draw, and the figure finishes immediately.
  • In preview, data is taken from the cache without waiting: outlines may appear with a delay; on export, the tiles are requested with a wait of up to 5 seconds.
  • The highlight transparency applies only to the highlight fill; the outline line is drawn opaque in the line color.
  • The figure depends on external vector data; without it, it draws nothing and does not report an error.
  • In the "Nearest" mode, with all types turned off (or the zoom below all thresholds), the set of allowed types is empty, and the figure considers places of any type.

Clone this wiki locally