#2681 JavaFX Map Redesign

#2681 Map tab as a JavaFX component

Replaces the Swing JXMapViewer map with a pure JavaFX canvas tile map in gui/map:
OpenStreetMap tiles with a memory and disk cache, track, Radio Reference site and
user location layers, the site hover card and context menu, and the map controls.
Removes the bundled SwingX map viewer copy and moves GeoPosition to the map package.

Co-authored-by: denny <dennis.sheirer@gmail.com>
This commit is contained in:
Denny Sheirer
2026-11-18 02:29:58 -05:00
committed by GitHub
parent bc7dd201ab
commit f47479b8d4
92 changed files with 4248 additions and 10417 deletions

View File

@@ -0,0 +1,113 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.gui.map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import io.github.dsheirer.map.GeoPosition;
import org.junit.jupiter.api.Test;
/**
* Tests the map projection and viewport conversions
*/
public class MapViewportTest
{
@Test
void projectionRoundTrips()
{
assertEquals(0.5, Mercator.x(0), 1e-12);
assertEquals(0.5, Mercator.y(0), 1e-12);
assertEquals(0.0, Mercator.y(Mercator.MAX_LATITUDE), 1e-9, "top of the map");
assertEquals(43.048, Mercator.latitude(Mercator.y(43.048)), 1e-9);
assertEquals(-76.147, Mercator.longitude(Mercator.x(-76.147)), 1e-9);
assertEquals(-170.0, Mercator.normalizeLongitude(190.0), 1e-9);
}
@Test
void openStreetMapTileOfAPosition()
{
//Syracuse, NY is in tile 12/1181/1504 of the OpenStreetMap tiles
int zoom = 12;
assertEquals(1181, (int)Math.floor(Mercator.x(-76.147) * Mercator.tileCount(zoom)));
assertEquals(1504, (int)Math.floor(Mercator.y(43.048) * Mercator.tileCount(zoom)));
}
@Test
void centerIsInTheMiddleOfTheView()
{
MapViewport viewport = new MapViewport();
viewport.setSize(800, 600);
viewport.setZoom(12);
viewport.setCenter(new GeoPosition(43.048, -76.147));
assertEquals(400, viewport.getScreenX(-76.147), 0.5);
assertEquals(300, viewport.getScreenY(43.048), 0.5);
GeoPosition center = viewport.getPosition(400, 300);
assertEquals(43.048, center.getLatitude(), 1e-3);
assertEquals(-76.147, center.getLongitude(), 1e-3);
}
@Test
void zoomKeepsThePointUnderTheMouse()
{
MapViewport viewport = new MapViewport();
viewport.setSize(800, 600);
viewport.setZoom(10);
viewport.setCenter(new GeoPosition(43.048, -76.147));
GeoPosition before = viewport.getPosition(650, 120);
viewport.zoomAt(13, 650, 120);
GeoPosition after = viewport.getPosition(650, 120);
assertEquals(13, viewport.getZoom());
//Within a pixel at zoom 13, since the view edges are whole pixels
assertEquals(before.getLatitude(), after.getLatitude(), 2e-4);
assertEquals(before.getLongitude(), after.getLongitude(), 2e-4);
}
@Test
void panWrapsAroundTheWorldAndStopsAtThePoles()
{
MapViewport viewport = new MapViewport();
viewport.setSize(800, 600);
viewport.setZoom(3);
viewport.setCenter(new GeoPosition(0, 179));
viewport.pan(Mercator.worldSize(3) * 0.01, 0);
assertEquals(-177.4, viewport.getCenter().getLongitude(), 0.01, "wrapped past 180 degrees");
viewport.pan(0, -1_000_000);
assertEquals(0, viewport.getTop(), "stops at the top of the map");
viewport.pan(0, 1_000_000);
assertEquals(Mercator.worldSize(3) - 600, viewport.getTop(), "stops at the bottom of the map");
}
@Test
void positionsAcrossTheDateLineAreDrawnNearTheView()
{
MapViewport viewport = new MapViewport();
viewport.setSize(800, 600);
viewport.setZoom(4);
viewport.setCenter(new GeoPosition(0, 179.5));
//Just east of the date line is on the right of the view, not a world away
double x = viewport.getScreenX(-179.5);
assertEquals(400 + Mercator.worldSize(4) / 360.0, x, 1.0);
}
}

View File

@@ -16,28 +16,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.map;
package io.github.dsheirer.gui.map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import io.github.dsheirer.map.GeoPosition;
import io.github.dsheirer.service.radioreference.SiteRecord;
import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.DefaultTileFactory;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.TileFactoryInfo;
import org.junit.jupiter.api.Test;
/**
* Tests the radio reference site markers and card on the map, without loading map tiles
*/
public class RadioReferenceSiteMapTest
public class SiteLayerTest
{
private static SiteRecord site(String system, String description, double latitude, double longitude, int channels)
{
@@ -56,45 +50,45 @@ public class RadioReferenceSiteMapTest
@Test
void hitTestFindsTowersUnderTheMouse()
{
//Offline tile source with the OSM geometry - nothing is loaded without painting
JXMapViewer map = new JXMapViewer();
map.setTileFactory(new DefaultTileFactory(new TileFactoryInfo(1, 17, 19, 256, true, true,
"file:///nonexistent", "x", "y", "z")));
map.setSize(800, 600);
map.setZoom(6);
map.setCenterPosition(new GeoPosition(43.05, -76.15));
MapViewport viewport = new MapViewport();
viewport.setSize(800, 600);
viewport.setZoom(13);
viewport.setCenter(new GeoPosition(43.05, -76.15));
SiteRecord syracuse = site("System A", "Syracuse", 43.0481, -76.1474, 4);
SiteRecord sameTower = site("System B", "Syracuse", 43.0481, -76.1474, 4);
SiteRecord fulton = site("System C", "Fulton", 43.3226, -76.4171, 4);
RadioReferenceSitePainter painter = new RadioReferenceSitePainter(() -> List.of(syracuse, sameTower, fulton));
SiteLayer layer = new SiteLayer(() -> List.of(syracuse, sameTower, fulton));
Point2D pixel = map.getTileFactory().geoToPixel(new GeoPosition(43.0481, -76.1474), map.getZoom());
Point tower = new Point((int)Math.round(pixel.getX() - map.getViewportBounds().getX()),
(int)Math.round(pixel.getY() - map.getViewportBounds().getY()) - 8);
//Middle of the tower, above its base at the site location
double x = viewport.getScreenX(-76.1474);
double y = viewport.getScreenY(43.0481) - 8;
assertEquals(List.of("System A", "System B"),
painter.getSitesAt(map, tower).stream().map(SiteRecord::systemName).toList(), "both towers at the spot");
assertTrue(painter.getSitesAt(map, new Point(tower.x + 60, tower.y)).isEmpty());
layer.getSitesAt(viewport, x, y).stream().map(SiteRecord::systemName).toList(), "both towers at the spot");
assertTrue(layer.getSitesAt(viewport, x + 60, y).isEmpty());
painter.setShowSites(false);
assertTrue(painter.getSitesAt(map, tower).isEmpty(), "hidden sites aren't found");
layer.setShowSites(false);
assertTrue(layer.getSitesAt(viewport, x, y).isEmpty(), "hidden sites aren't found");
}
@Test
void cardShowsSystemSiteAndChannels()
{
String html = RadioReferenceSiteCard.html(List.of(site("Fire & Rescue <Interop>", "Syracuse", 43.0481,
SiteCard.Content content = SiteCard.content(List.of(site("Fire & Rescue <Interop>", "Syracuse", 43.0481,
-76.1474, 30)), null);
assertTrue(html.contains("<b>Fire &amp; Rescue &lt;Interop&gt;</b>"), "escaped system name");
assertTrue(html.contains("Project 25 Phase II"));
assertTrue(html.contains("Site 3 &middot; Syracuse &middot; Onondaga County"));
assertTrue(html.contains("NAC 1A3 &middot; RFSS 1 &middot; Zone 2"));
assertTrue(html.contains("851.01250 <b>C</b>"), "control channel marked");
assertTrue(html.contains("851.26250 <b>A</b>"), "alternate control channel marked");
assertTrue(html.contains("+ 6 more channels"), "24 channels shown");
assertFalse(html.contains(" mi") || html.contains(" km"), "no distance without a location");
assertEquals(1, content.entries().size());
SiteCard.Entry entry = content.entries().getFirst();
assertEquals("Fire & Rescue <Interop>", entry.title());
assertEquals(List.of("Project 25 Phase II · APCO-25 Common Air Interface Exclusive",
"Site 3 · Syracuse · Onondaga County", "NAC 1A3 · RFSS 1 · Zone 2"), entry.lines(),
"no distance without a location");
assertEquals(new SiteCard.Channel("851.01250", "C"), entry.channels().get(0), "control channel marked");
assertEquals(new SiteCard.Channel("851.26250", "A"), entry.channels().get(1), "alternate control marked");
assertEquals(new SiteCard.Channel("851.51250", null), entry.channels().get(2));
assertEquals(24, entry.channels().size(), "24 channels shown");
assertEquals(List.of("+ 6 more channels · C control, A alternate control"), entry.notes());
assertEquals(0, content.moreSites());
}
@Test
@@ -107,9 +101,10 @@ public class RadioReferenceSiteMapTest
sites.add(site("System " + index, "Syracuse", 43.0481, -76.1474, 2));
}
String html = RadioReferenceSiteCard.html(sites, null);
assertTrue(html.contains("System 2"));
assertFalse(html.contains("System 3"));
assertTrue(html.contains("+ 2 more sites here"));
SiteCard.Content content = SiteCard.content(sites, null);
assertEquals(List.of("System 0", "System 1", "System 2"),
content.entries().stream().map(SiteCard.Entry::title).toList());
assertEquals(2, content.moreSites());
assertFalse(content.entries().getFirst().notes().getFirst().contains("more channels"));
}
}

View File

@@ -0,0 +1,137 @@
/*
* *****************************************************************************
* Copyright (C) 2014-2026 Dennis Sheirer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* ****************************************************************************
*/
package io.github.dsheirer.gui.map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
/**
* Tests choosing tiles for the theme and the user's key, and recoloring light tiles for dark themes
*/
public class TileServerTest
{
@Test
void serverWithoutDarkStyleIsRecoloredInDarkThemes()
{
TileSource light = TileServer.OPEN_STREET_MAP.getSource(false, null);
TileSource dark = TileServer.OPEN_STREET_MAP.getSource(true, null);
assertFalse(light.darken());
assertFalse(light.dark());
assertTrue(dark.darken());
assertTrue(dark.dark());
assertEquals("osm/light", dark.cacheName(), "recolored from the cached light tiles");
assertEquals("https://tile.openstreetmap.org/12/1181/1504.png", dark.getUrl(new TileKey(12, 1181, 1504)));
}
@Test
void serverWithDarkStyleFollowsTheTheme()
{
TileSource light = TileServer.ESRI_GRAY.getSource(false, null);
TileSource dark = TileServer.ESRI_GRAY.getSource(true, null);
assertFalse(dark.darken(), "dark style isn't recolored");
assertTrue(dark.dark());
assertEquals("esri.gray/dark", dark.cacheName());
assertNotEquals(light.cacheName(), dark.cacheName());
assertEquals("https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Dark_Gray_Base/MapServer/tile/" +
"12/1504/1181", dark.getUrl(new TileKey(12, 1181, 1504)), "row before column");
}
@Test
void keyedServerNeedsAKey()
{
TileSource missing = TileServer.CARTO.getSource(true, " ");
assertFalse(missing.isAvailable());
assertTrue(missing.unavailableReason().contains("API key"));
TileSource keyed = TileServer.CARTO.getSource(true, " abc+def ");
assertTrue(keyed.isAvailable());
assertNull(keyed.unavailableReason());
assertEquals("https://basemaps.cartocdn.com/rastertiles/dark_all/5/9/12.png?key=abc%2Bdef",
keyed.getUrl(new TileKey(5, 9, 12)), "key trimmed and encoded");
assertEquals("carto/dark", keyed.cacheName(), "cache doesn't depend on the key");
}
@Test
void subdomainsSpreadTheTiles()
{
TileSource source = TileServer.OPEN_TOPO_MAP.getSource(false, null);
assertEquals("https://a.tile.opentopomap.org/1/0/0.png", source.getUrl(new TileKey(1, 0, 0)));
assertEquals("https://b.tile.opentopomap.org/1/1/0.png", source.getUrl(new TileKey(1, 1, 0)));
assertEquals("https://c.tile.opentopomap.org/1/1/1.png", source.getUrl(new TileKey(1, 1, 1)));
}
@Test
void unknownServerIsOpenStreetMap()
{
assertEquals(TileServer.OPEN_STREET_MAP, TileServer.get(null));
assertEquals(TileServer.OPEN_STREET_MAP, TileServer.get("gone"));
assertEquals(TileServer.CARTO, TileServer.get("carto"));
}
@Test
void choiceFollowsTheThemeOrKeepsItsStyle()
{
TileChoice theme = TileChoice.get("carto", null);
assertEquals(TileChoice.Style.THEME, theme.style(), "stored server without a style follows the theme");
assertEquals("carto/dark", theme.getSource(true, "k").cacheName());
assertEquals("carto/light", theme.getSource(false, "k").cacheName());
TileChoice light = TileChoice.get("carto", "LIGHT");
assertEquals("carto/light", light.getSource(true, "k").cacheName(), "light in a dark theme");
assertFalse(light.getSource(true, "k").dark());
TileSource dark = TileChoice.get("osm", "DARK").getSource(false, null);
assertTrue(dark.dark(), "dark in a light theme");
assertTrue(dark.darken(), "recolored, no dark style");
assertEquals(TileChoice.Style.THEME, TileChoice.get("osm", "gone").style());
assertEquals(TileServer.SERVERS.size() * 3, TileChoice.CHOICES.size());
assertEquals("CARTO - Auto", theme.toString());
assertEquals("CARTO - Dark", TileChoice.get("carto", "DARK").toString());
}
@Test
void darkenerInvertsLightnessAndKeepsHue()
{
assertEquals(gray(TileDarkener.DARKEST), TileDarkener.darken(0xFFFFFFFF) & 0xFFFFFF, "white turns dark");
assertEquals(gray(TileDarkener.LIGHTEST), TileDarkener.darken(0xFF000000) & 0xFFFFFF, "black turns light");
assertEquals(0x80000000, TileDarkener.darken(0x80FFFFFF) & 0xFF000000, "alpha kept");
//Light blue water turns dark blue: blue stays the strongest channel and it gets darker
int water = TileDarkener.darken(0xFFAAD3DF);
int r = (water >> 16) & 0xFF;
int g = (water >> 8) & 0xFF;
int b = water & 0xFF;
assertTrue(b > g && g > r, "hue kept");
assertTrue(b < 0x80, "darker");
}
private static int gray(double lightness)
{
int value = (int)Math.round(lightness * 255.0);
return (value << 16) | (value << 8) | value;
}
}