palm-client: Map screen with an OSM basemap (tools/make_map.py) and positioned nodes

This commit is contained in:
rich committed 2026-11-02 12:33:52 -05:00
1 parent f0ef46bef5
commit deec3112d4
12 files changed
+1330 -2

No files matched your search

+3
View File
@@ -19,3 +19,6 @@ firmware/meshcore/
# Palm SDK headers copied out of the Docker image for the editor (see Makefile: editor)
palm-client/.sdk/
palm-client/compile_flags.txt
# OpenStreetMap downloads for the basemap (palm-client/tools/make_map.py)
palm-client/tools/map-cache/
+2
View File
@@ -40,6 +40,7 @@
- [x] Install method confirmed: HotSync over the USB cradle from the dev Mac (`make install`)
- [x] Notification sounds per event kind, mute channels, larger text option (5x8 font)
- [x] Flood scopes (regions): manage the list of scopes, set the default scope, and set a scope per channel; discover from neighbouring repeaters. MeshCore v8+ has CMD_SET_FLOOD_SCOPE_KEY (54), CMD_SET_DEFAULT_FLOOD_SCOPE (63) / CMD_GET_DEFAULT_FLOOD_SCOPE (64)
- [ ] Map: clip polygons that leave the box (the lower Delaware shows only its banks); optional LZ tiles to halve the size; topo contours
- [ ] Region discovery beyond neighbours: keep each contact's out_path and send the reversed path as the reply path
## Open questions
@@ -87,3 +88,4 @@
- 2026-11-02: SB-LoRa receive is interrupt-driven: a slot IRQ* handler (hsCardAttrIntHandler) drains the SC16IS740 RX FIFO (IER RHR, trigger 8 or timeout) into a 1 KB ring passed as cardParam. The handler path (SbDrainRx and the bus core) is in segment 1 with no A5 references, checked in the disassembly, so it's safe in interrupt context and while the Visor sleeps. The app masks the slot interrupt around its own SPI transactions, and falls back to polling if the handler won't install. Host tests cover draining, ring wrap and overflow; untested on hardware.
- 2026-11-02: Mute Channel (Options menu; by channel key; mentions still sound; open-circle unread badge, left out of the title total) and Text size Small/Large on the new Preferences screen (5x8 misc-fixed via tools/pcf2bdf.py from XQuartz's PCF; gen_font.py builds both fonts). Verified on the Neo. Segments: 11.1 / 28.0 / 14.7 KB.
- 2026-11-02: Flood scopes: a list of scope names (Options > Flood Scopes), the radio's default (Set Default / No Default), a per-channel choice (Options > Channel Scope: radio default, none, or a named scope), and Discover, which asks neighbouring repeaters for their regions (SEND_ANON_REQ type 0x01). The session sets the scope override before each flood send and resets it on disconnect. Keys and frames are host-tested (tests/test_scopes.c; the key for "test" matches the radio's #test channel key). Verified on the Neo. Discover only reaches 0-hop repeaters for now.
- 2026-11-02: Map screen (Options > Map): an OpenStreetMap basemap of the Philadelphia metro in 4 greys with the contacts that advertise a position plotted on it (no position, not shown). `tools/make_map.py` downloads OSM roads, rail, water and parks (Overpass, cached in tools/map-cache), draws zooms 10-12 for the metro and 13 with residential streets for ~16 km around Center City, and writes build/VisorMeshMap.pdb (290 PackBits tiles, 1,045 KB); `make map`, `make install-map`. Zoom with -/+ or the hard buttons, drag to pan, tap a node for its info, tap again to open it. Fixed on the device: a long node name overflowed the info bar (reset), and the attribution overlapped the buttons. Verified on the Neo. Segment 3 is 24.9 KB.
+16 -2
View File
@@ -10,7 +10,7 @@ APP = visormesh
NAME = VisorMesh
CREATOR = VMsh
SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c
SRCS = src/main.c src/app.c src/home_form.c src/convo_form.c src/node_form.c src/channel_form.c src/convos.c src/protocol.c src/sha256.c src/transport_srm.c src/transport_sb.c src/sb_spi.c src/session.c src/settings.c src/presets.c src/contacts.c src/contacts_form.c src/contact_form.c src/heard.c src/remote_fmt.c src/remote.c src/remote_form.c src/messages.c src/channels.c src/preview_form.c src/ui/draw.c src/ui/text.c src/ui/rowlist.c src/ui/fontmetrics.c src/ports.c src/sounds.c src/mutes.c src/scopes.c src/scope_proto.c src/map_geo.c src/map_form.c
OBJS = $(SRCS:src/%.c=build/%.o)
CC = m68k-palmos-gcc
@@ -78,6 +78,8 @@ test: | build
build/test_sblora
gcc -std=c99 -Wall -Wextra -o build/test_scopes tests/test_scopes.c src/scope_proto.c src/protocol.c src/sha256.c
build/test_scopes
gcc -std=c99 -Wall -Wextra -D_DEFAULT_SOURCE -o build/test_map tests/test_map.c src/map_geo.c -lm
build/test_map
build:
mkdir -p build
@@ -91,6 +93,18 @@ install: all
@echo "Press the HotSync button on the Visor cradle..."
$(PILOT_XFER) -p $(PORT) -i build/$(APP).prc
# The basemap (tools/make_map.py; OSM data cached in tools/map-cache). `make map`
# downloads on the first run; MAP_ARGS passes options, e.g. MAP_ARGS="--max-zoom 12".
map: | build
python3 tools/make_map.py --cache tools/map-cache --fetch --out build/VisorMeshMap.pdb $(MAP_ARGS)
install-map:
@if pgrep -f tools/visor_bridge.py >/dev/null; then \
echo "Stopping visor_bridge.py (it would grab the Visor during HotSync)"; \
pkill -f tools/visor_bridge.py; sleep 1; fi
@echo "Press the HotSync button on the Visor cradle..."
$(PILOT_XFER) -p $(PORT) -i build/VisorMeshMap.pdb
clean:
rm -rf build
@@ -104,4 +118,4 @@ editor: image
-Wno-ignored-attributes -Wno-pragma-once-outside-header > compile_flags.txt
find .sdk/sdk-3.5 -type d | sed 's|^|-I|' >> compile_flags.txt
.PHONY: all test image install clean editor
.PHONY: all test image install clean editor map install-map
+22
View File
@@ -17,6 +17,7 @@ BEGIN
MENUITEM "Conversations" ID MainMenuHome "H"
MENUITEM "Contacts" ID MainMenuContacts "C"
MENUITEM "Node" ID MainMenuNode "N"
MENUITEM "Map" ID MainMenuMap "W"
MENUITEM "-" AUTOID
MENUITEM "Add Channel..." ID MainMenuAddChannel "A"
MENUITEM "Leave Channel" ID MainMenuLeaveChannel "L"
@@ -326,6 +327,27 @@ BEGIN
BUTTON "Cancel" ID ChanScopeCancelButton AT (PREVRIGHT+5 PREVTOP 36 12)
END
/* Map: the basemap with positioned nodes; drag to pan, tap a node, tap again to open */
FORM ID MapForm AT (0 0 160 160)
NOFRAME
USABLE
MENUID MainMenuBar
BEGIN
TITLE "Map"
GADGET ID MapGadget AT (0 15 160 130)
BUTTON "Done" ID MapDoneButton AT (1 147 28 12)
BUTTON "-" ID MapOutButton AT (PREVRIGHT+4 PREVTOP 14 12)
BUTTON "+" ID MapInButton AT (PREVRIGHT+2 PREVTOP 14 12)
END
ALERT ID MapAlert
INFORMATION
BEGIN
TITLE "Map"
MESSAGE "^1"
BUTTONS "OK"
END
ALERT ID MuteAlert
INFORMATION
BEGIN
+3
View File
@@ -15,6 +15,7 @@
#include "sounds.h"
#include "mutes.h"
#include "scopes.h"
#include "map_form.h"
#include "node_form.h"
#include "settings.h"
#include "ports.h"
@@ -46,6 +47,7 @@ static Boolean HandleMenu(UInt16 itemID)
case MainMenuHome: FrmGotoForm(HomeForm); return true;
case MainMenuContacts: FrmGotoForm(ContactsForm); return true;
case MainMenuNode: FrmGotoForm(MainForm); return true;
case MainMenuMap: FrmGotoForm(MapForm); return true;
case MainMenuPorts: FrmGotoForm(PortsForm); return true;
case MainMenuFontPreview: FrmGotoForm(FontPreviewForm); return true;
case MainMenuSounds: SoundsFormOpen(FrmGetActiveFormID()); return true;
@@ -84,6 +86,7 @@ static Boolean AppHandleEvent(EventType *event)
case RemoteForm: FrmSetEventHandler(form, RemoteFormHandleEvent); break;
case SoundsForm: FrmSetEventHandler(form, SoundsFormHandleEvent); break;
case ScopesForm: FrmSetEventHandler(form, ScopesFormHandleEvent); break;
case MapForm: FrmSetEventHandler(form, MapFormHandleEvent); break;
}
return true;
}
+506
View File
@@ -0,0 +1,506 @@
#include <PalmOS.h>
#include "resources.h"
#include "app.h"
#include "contacts.h"
#include "contact_form.h"
#include "session.h"
#include "map_geo.h"
#include "map_form.h"
#include "ui/draw.h"
#include "ui/text.h"
static Boolean OpenMap(void) VM_SECT3;
static void CloseMap(void) VM_SECT3;
static void LoadNodes(void) VM_SECT3;
static BitmapType *Tile(UInt16 rec) VM_SECT3;
static void ClampCenter(void) VM_SECT3;
static void Draw(void) VM_SECT3;
static void DrawNodes(const RectangleType *r, Int32 vx, Int32 vy) VM_SECT3;
static void DrawInfo(const RectangleType *r) VM_SECT3;
static void DrawFooter(void) VM_SECT3;
static void Halo(const Char *s, UInt16 len, Coord x, Coord y) VM_SECT3;
static Char TypeGlyph(UInt8 type) VM_SECT3;
static Int16 NodeAt(Coord x, Coord y) VM_SECT3;
static void Zoom(Int16 by) VM_SECT3;
static void SaveView(void) VM_SECT3;
static Int32 FloorDiv(Int32 a, Int32 b) VM_SECT3;
#define MAP_DB_NAME "VisorMeshMap"
#define MAP_PREFS_ID 5
#define MAP_PREFS_VER 1
#define MAX_NODES 150
#define CACHE_TILES 4
#define TAP_SLOP 4 /* pixels a pen can move and still be a tap */
#define HIT_RADIUS 8
#define NAME_LEN 16
typedef struct {
Int32 wx, wy; /* world pixels at the max zoom */
UInt16 index; /* contact record */
UInt8 type;
Boolean direct; /* a 0-hop neighbour */
UInt32 lastAdvert;
Char name[NAME_LEN + 1];
} MapNode;
typedef struct {
UInt16 version;
UInt8 zi;
UInt8 pad;
Int32 cx, cy;
} MapPrefs;
static DmOpenRef gDb = NULL;
static MemHandle gHdrH = NULL;
static VmMapHeader gH;
static UInt32 gOldDepth = 0;
static Boolean gDepthSet = false;
static struct { UInt16 rec; BitmapType *bmp; } gCache[CACHE_TILES];
static UInt16 gCacheNext;
static MapNode *gNodes = NULL;
static UInt16 gNodeCount;
static Int16 gSel = -1;
static UInt8 gZi;
static Int32 gCx, gCy; /* view centre, world pixels at the max zoom */
static Coord gDownX, gDownY;
static Boolean gPenDown;
static Int32 FloorDiv(Int32 a, Int32 b)
{
return a >= 0 ? a / b : -((-a + b - 1) / b);
}
static Char TypeGlyph(UInt8 type)
{
switch (type) {
case VM_ADV_TYPE_CHAT: return GLYPH_COMPANION;
case VM_ADV_TYPE_REPEATER: return GLYPH_REPEATER;
case VM_ADV_TYPE_ROOM: return GLYPH_ROOM;
case VM_ADV_TYPE_SENSOR: return GLYPH_SENSOR;
default: return GLYPH_UNKNOWN;
}
}
static Boolean OpenMap(void)
{
LocalID id = DmFindDatabase(0, MAP_DB_NAME);
MapPrefs prefs;
UInt16 size = sizeof(prefs), i;
Int32 x0, x1, y0, y1;
if (!id || (gDb = DmOpenDatabase(0, id, dmModeReadOnly)) == NULL)
return false;
gHdrH = DmQueryRecord(gDb, 0);
if (!gHdrH || !VmMapParseHeader((const UInt8 *)MemHandleLock(gHdrH), MemHandleSize(gHdrH), &gH)) {
if (gHdrH)
MemHandleUnlock(gHdrH);
gHdrH = NULL;
DmCloseDatabase(gDb);
gDb = NULL;
return false;
}
for (i = 0; i < CACHE_TILES; i++) {
gCache[i].rec = 0;
gCache[i].bmp = NULL;
}
gCacheNext = 0;
/* the last view, or the whole map at its widest zoom */
if (PrefGetAppPreferences(VM_CREATOR, MAP_PREFS_ID, &prefs, &size, true) != noPreferenceFound &&
size == sizeof(prefs) && prefs.version == MAP_PREFS_VER && prefs.zi < gH.zoomCount) {
gZi = prefs.zi;
gCx = prefs.cx;
gCy = prefs.cy;
} else {
VmMapWorld(&gH, gH.latS, gH.lonW, &x0, &y1);
VmMapWorld(&gH, gH.latN, gH.lonE, &x1, &y0);
gZi = 0;
gCx = (x0 + x1) / 2;
gCy = (y0 + y1) / 2;
}
ClampCenter();
return true;
}
static void CloseMap(void)
{
UInt16 i;
for (i = 0; i < CACHE_TILES; i++)
if (gCache[i].bmp)
BmpDelete(gCache[i].bmp);
if (gHdrH)
MemHandleUnlock(gHdrH);
gHdrH = NULL;
if (gDb)
DmCloseDatabase(gDb);
gDb = NULL;
if (gNodes)
MemPtrFree(gNodes);
gNodes = NULL;
}
static void SaveView(void)
{
MapPrefs prefs;
prefs.version = MAP_PREFS_VER;
prefs.zi = gZi;
prefs.pad = 0;
prefs.cx = gCx;
prefs.cy = gCy;
PrefSetAppPreferences(VM_CREATOR, MAP_PREFS_ID, MAP_PREFS_VER, &prefs, sizeof(prefs), true);
}
/* Contacts with a position inside the map; the rest aren't shown at all. */
static void LoadNodes(void)
{
UInt16 count, i;
VmContact c;
MapNode *n;
gNodeCount = 0;
gSel = -1;
gNodes = (MapNode *)MemPtrNew(MAX_NODES * sizeof(MapNode));
if (!gNodes)
return;
ContactsOpen();
count = ContactsCount();
for (i = 0; i < count && gNodeCount < MAX_NODES; i++) {
if (!ContactsGet(i, &c) || (c.latE6 == 0 && c.lonE6 == 0))
continue;
n = &gNodes[gNodeCount];
if (!VmMapWorld(&gH, c.latE6, c.lonE6, &n->wx, &n->wy))
continue;
n->index = i;
n->type = c.type;
n->direct = c.outPathLen != 0xFF && (c.outPathLen & 0x3F) == 0;
n->lastAdvert = c.lastAdvert;
StrNCopy(n->name, c.name, NAME_LEN);
n->name[NAME_LEN] = 0;
gNodeCount++;
}
}
/* A decoded tile, from a small cache; NULL for a blank one or on error. */
static BitmapType *Tile(UInt16 rec)
{
MemHandle h;
const UInt8 *src;
BitmapType *bmp;
UInt16 i, err;
UInt32 bytes = (UInt32)VM_MAP_TILE * VM_MAP_TILE * gH.bpp / 8;
Boolean ok;
for (i = 0; i < CACHE_TILES; i++)
if (gCache[i].rec == rec)
return gCache[i].bmp;
h = DmQueryRecord(gDb, rec);
if (!h || MemHandleSize(h) <= 1)
return NULL; /* an all-white tile */
i = gCacheNext;
gCacheNext = (gCacheNext + 1) % CACHE_TILES;
if (!gCache[i].bmp) {
gCache[i].bmp = BmpCreate(VM_MAP_TILE, VM_MAP_TILE, gH.bpp, NULL, &err);
if (!gCache[i].bmp)
return NULL;
}
bmp = gCache[i].bmp;
src = (const UInt8 *)MemHandleLock(h);
ok = VmUnpackBits(src, MemHandleSize(h), (UInt8 *)BmpGetBits(bmp), bytes);
MemHandleUnlock(h);
gCache[i].rec = ok ? rec : 0;
return ok ? bmp : NULL;
}
static void ClampCenter(void)
{
Int32 x0, x1, y0, y1;
VmMapWorld(&gH, gH.latS, gH.lonW, &x0, &y1);
VmMapWorld(&gH, gH.latN, gH.lonE, &x1, &y0);
if (gCx < x0) gCx = x0;
if (gCx > x1) gCx = x1;
if (gCy < y0) gCy = y0;
if (gCy > y1) gCy = y1;
}
/* Text with a one-pixel white outline, so it reads over roads. */
static void Halo(const Char *s, UInt16 len, Coord x, Coord y)
{
RGBColorType white = { 0, 255, 255, 255 }, black = { 0, 0, 0, 0 };
WinSetDrawMode(winOverlay);
WinSetTextColor(WinRGBToIndex(&white));
WinPaintChars(s, len, x - 1, y);
WinPaintChars(s, len, x + 1, y);
WinPaintChars(s, len, x, y - 1);
WinPaintChars(s, len, x, y + 1);
WinSetTextColor(WinRGBToIndex(&black));
WinPaintChars(s, len, x, y);
}
static void DrawNodes(const RectangleType *r, Int32 vx, Int32 vy)
{
RectangleType labels[MAX_NODES / 4];
UInt16 nLabels = 0, i, j, len;
Int32 x, y;
Coord sx, sy, w;
Char g;
Boolean clear, named = gZi + 2 >= gH.zoomCount; /* names at the two closest zooms */
MapNode *n;
FntSetFont(vmSmallFont);
for (i = 0; i < gNodeCount; i++) {
n = &gNodes[i];
VmMapToZoom(&gH, gZi, n->wx, n->wy, &x, &y);
x -= vx;
y -= vy;
if (x < -4 || y < -4 || x >= r->extent.x + 4 || y >= r->extent.y + 4)
continue;
sx = r->topLeft.x + (Coord)x;
sy = r->topLeft.y + (Coord)y;
g = TypeGlyph(n->type);
w = FntCharWidth(g);
Halo(&g, 1, sx - w / 2, sy - gUiFontHeight / 2);
if (n->direct) /* bold: heard directly */
WinPaintChars(&g, 1, sx - w / 2 + 1, sy - gUiFontHeight / 2);
if (!named && (Int16)i != gSel)
continue;
len = StrLen(n->name);
labels[nLabels].topLeft.x = sx + w / 2 + 2;
labels[nLabels].topLeft.y = sy - gUiFontHeight / 2;
labels[nLabels].extent.x = FntCharsWidth(n->name, len);
labels[nLabels].extent.y = gUiFontHeight;
clear = true;
for (j = 0; j < nLabels && clear; j++) {
RectangleType o;
RctGetIntersection(&labels[j], &labels[nLabels], &o);
clear = o.extent.x <= 0 || o.extent.y <= 0;
}
if (!clear && (Int16)i != gSel)
continue;
Halo(n->name, len, labels[nLabels].topLeft.x, labels[nLabels].topLeft.y);
if (nLabels < sizeof(labels) / sizeof(labels[0]) - 1)
nLabels++;
}
}
/* The selected node: a bar along the bottom of the map. */
static void DrawInfo(const RectangleType *r)
{
RectangleType bar;
MapNode *n;
/* glyph, name, age (up to 4) and the hint: 46 at most */
Char line[NAME_LEN + 48], age[12];
UInt32 now;
if (gSel < 0)
return;
n = &gNodes[gSel];
bar.topLeft.x = r->topLeft.x;
bar.topLeft.y = r->topLeft.y + r->extent.y - gUiFontHeight - 2;
bar.extent.x = r->extent.x;
bar.extent.y = gUiFontHeight + 2;
if (n->lastAdvert) {
now = SessionNow();
TxtFormatAge(age, now > n->lastAdvert ? now - n->lastAdvert : 0);
} else {
StrCopy(age, "-");
}
StrPrintF(line, "%c %s %s (tap again to open)", TypeGlyph(n->type), n->name, age);
WinEraseRectangle(&bar, 0);
FntSetFont(vmSmallFont);
WinSetDrawMode(winPaint);
DrawCut(line, bar.topLeft.x + 2, bar.topLeft.y + 1, bar.extent.x - 4);
WinInvertRectangle(&bar, 0);
}
/* The attribution, right of the buttons (which end at x 63): cut if it doesn't fit. */
#define FOOTER_X 66
static void DrawFooter(void)
{
FontID old = FntSetFont(vmSmallFont);
Coord room = 159 - FOOTER_X;
if (FntCharsWidth(gH.attribution, StrLen(gH.attribution)) > room)
DrawCut(gH.attribution, FOOTER_X, 150, room);
else
DrawRight(gH.attribution, 159, 150);
FntSetFont(old);
}
static void Draw(void)
{
FormType *form = FrmGetActiveForm();
RectangleType r;
Int32 cx, cy, vx, vy, col, row, col0, col1, row0, row1;
UInt16 rec;
BitmapType *bmp;
RectangleType t;
FrmGetObjectBounds(form, FrmGetObjectIndex(form, MapGadget), &r);
WinPushDrawState();
WinSetClip(&r);
VmMapToZoom(&gH, gZi, gCx, gCy, &cx, &cy);
vx = cx - r.extent.x / 2;
vy = cy - r.extent.y / 2;
col0 = FloorDiv(vx, VM_MAP_TILE);
col1 = FloorDiv(vx + r.extent.x - 1, VM_MAP_TILE);
row0 = FloorDiv(vy, VM_MAP_TILE);
row1 = FloorDiv(vy + r.extent.y - 1, VM_MAP_TILE);
for (row = row0; row <= row1; row++) {
for (col = col0; col <= col1; col++) {
t.topLeft.x = r.topLeft.x + (Coord)(col * VM_MAP_TILE - vx);
t.topLeft.y = r.topLeft.y + (Coord)(row * VM_MAP_TILE - vy);
t.extent.x = t.extent.y = VM_MAP_TILE;
rec = VmMapTileRecord(&gH, gZi, col, row);
bmp = rec ? Tile(rec) : NULL;
if (bmp)
WinDrawBitmap(bmp, t.topLeft.x, t.topLeft.y);
else
WinEraseRectangle(&t, 0);
}
}
DrawNodes(&r, vx, vy);
DrawInfo(&r);
WinResetClip();
WinPopDrawState();
}
static Int16 NodeAt(Coord px, Coord py)
{
FormType *form = FrmGetActiveForm();
RectangleType r;
Int32 cx, cy, x, y, dx, dy, best = (Int32)HIT_RADIUS * HIT_RADIUS + 1;
Int16 hit = -1;
UInt16 i;
FrmGetObjectBounds(form, FrmGetObjectIndex(form, MapGadget), &r);
VmMapToZoom(&gH, gZi, gCx, gCy, &cx, &cy);
for (i = 0; i < gNodeCount; i++) {
VmMapToZoom(&gH, gZi, gNodes[i].wx, gNodes[i].wy, &x, &y);
dx = r.topLeft.x + (x - (cx - r.extent.x / 2)) - px;
dy = r.topLeft.y + (y - (cy - r.extent.y / 2)) - py;
if (dx * dx + dy * dy < best) {
best = dx * dx + dy * dy;
hit = (Int16)i;
}
}
return hit;
}
static void Zoom(Int16 by)
{
Int16 zi = (Int16)gZi + by;
if (zi < 0 || zi >= gH.zoomCount)
return;
gZi = (UInt8)zi;
Draw();
}
Boolean MapFormHandleEvent(EventType *event)
{
FormType *form = FrmGetActiveForm();
RectangleType r;
UInt32 depths, want;
Coord dx, dy;
Int16 hit;
VmContact c;
switch (event->eType) {
case frmOpenEvent:
if (OpenMap()) {
/* the tiles' grey depth, if the screen can do it */
WinScreenMode(winScreenModeGet, NULL, NULL, &gOldDepth, NULL);
want = gH.bpp;
if (WinScreenMode(winScreenModeGetSupportedDepths, NULL, NULL, &depths, NULL) == errNone &&
(depths & (1UL << (want - 1))) && want != gOldDepth &&
WinScreenMode(winScreenModeSet, NULL, NULL, &want, NULL) == errNone)
gDepthSet = true;
LoadNodes();
FrmDrawForm(form);
DrawFooter();
Draw();
} else {
FrmDrawForm(form);
FrmCustomAlert(MapAlert, "No map installed. Build one with palm-client/tools/make_map.py "
"and HotSync build/VisorMeshMap.pdb.", "", "");
}
return true;
case frmCloseEvent:
if (gDb)
SaveView();
CloseMap();
if (gDepthSet)
WinScreenMode(winScreenModeSet, NULL, NULL, &gOldDepth, NULL);
gDepthSet = false;
break; /* let the form close normally */
case frmUpdateEvent:
FrmDrawForm(form);
if (gDb) {
DrawFooter();
Draw();
}
return true;
case penDownEvent:
if (!gDb)
break;
FrmGetObjectBounds(form, FrmGetObjectIndex(form, MapGadget), &r);
if (!RctPtInRectangle(event->screenX, event->screenY, &r))
break;
gPenDown = true;
gDownX = event->screenX;
gDownY = event->screenY;
return true;
case penUpEvent:
if (!gPenDown)
break;
gPenDown = false;
dx = event->screenX - gDownX;
dy = event->screenY - gDownY;
if (dx > TAP_SLOP || dx < -TAP_SLOP || dy > TAP_SLOP || dy < -TAP_SLOP) {
UInt8 shift = gH.zooms[gH.zoomCount - 1].z - gH.zooms[gZi].z;
gCx -= (Int32)dx << shift; /* drag the map along with the pen */
gCy -= (Int32)dy << shift;
ClampCenter();
} else {
hit = NodeAt(gDownX, gDownY);
if (hit >= 0 && hit == gSel && ContactsGet(gNodes[hit].index, &c)) {
ContactFormOpen(&c);
return true;
}
gSel = hit;
}
Draw();
return true;
case keyDownEvent:
if (!gDb)
break;
if (event->data.keyDown.chr == pageUpChr) {
Zoom(1);
return true;
}
if (event->data.keyDown.chr == pageDownChr) {
Zoom(-1);
return true;
}
break;
case ctlSelectEvent:
switch (event->data.ctlSelect.controlID) {
case MapInButton: if (gDb) Zoom(1); return true;
case MapOutButton: if (gDb) Zoom(-1); return true;
case MapDoneButton: FrmGotoForm(HomeForm); return true;
}
break;
default:
break;
}
return false;
}
+14
View File
@@ -0,0 +1,14 @@
/*
* Map screen: the basemap (the VisorMeshMap database from tools/make_map.py)
* with the contacts that advertise a position plotted on it. Nodes without a
* position aren't shown.
*/
#ifndef VM_MAP_FORM_H
#define VM_MAP_FORM_H
#include <PalmOS.h>
#include "sections.h"
Boolean MapFormHandleEvent(EventType *event) VM_SECT3;
#endif
+139
View File
@@ -0,0 +1,139 @@
#include "map_geo.h"
#define HDR_FIXED 66 /* '>4sHHBBiiiiIHH32s' */
#define HDR_ZOOM 16 /* '>BBHHHii' */
static UInt16 Be16(const UInt8 *p) VM_SECT3;
static Int32 Be32(const UInt8 *p) VM_SECT3;
static Int32 Lookup(const UInt8 *table, UInt16 count, Int32 v0, UInt32 step, Int32 v, Boolean *ok) VM_SECT3;
static UInt16 Be16(const UInt8 *p)
{
return (UInt16)((p[0] << 8) | p[1]);
}
static Int32 Be32(const UInt8 *p)
{
return (Int32)(((UInt32)p[0] << 24) | ((UInt32)p[1] << 16) | ((UInt32)p[2] << 8) | p[3]);
}
Boolean VmMapParseHeader(const UInt8 *p, UInt32 len, VmMapHeader *h)
{
UInt32 need;
UInt16 i;
const UInt8 *z;
if (len < HDR_FIXED || p[0] != 'V' || p[1] != 'M' || p[2] != 'a' || p[3] != 'p' || Be16(p + 4) != 1)
return 0;
if (Be16(p + 6) != VM_MAP_TILE)
return 0;
h->bpp = p[8];
h->zoomCount = p[9];
if (h->zoomCount == 0 || h->zoomCount > VM_MAP_MAX_ZOOMS || (h->bpp != 1 && h->bpp != 2 && h->bpp != 4))
return 0;
h->latS = Be32(p + 10);
h->lonW = Be32(p + 14);
h->latN = Be32(p + 18);
h->lonE = Be32(p + 22);
h->step = (UInt32)Be32(p + 26);
h->latCount = Be16(p + 30);
h->lonCount = Be16(p + 32);
for (i = 0; i < VM_MAP_ATTR_SIZE; i++)
h->attribution[i] = (Char)p[34 + i];
h->attribution[VM_MAP_ATTR_SIZE] = 0;
need = HDR_FIXED + (UInt32)h->zoomCount * HDR_ZOOM + 4UL * (h->latCount + h->lonCount);
if (len < need || h->step == 0 || h->latCount < 2 || h->lonCount < 2)
return 0;
for (i = 0; i < h->zoomCount; i++) {
z = p + HDR_FIXED + i * HDR_ZOOM;
h->zooms[i].z = z[0];
h->zooms[i].cols = Be16(z + 2);
h->zooms[i].rows = Be16(z + 4);
h->zooms[i].first = Be16(z + 6);
h->zooms[i].ox = Be32(z + 8);
h->zooms[i].oy = Be32(z + 12);
}
h->latTable = p + HDR_FIXED + h->zoomCount * HDR_ZOOM;
h->lonTable = h->latTable + 4 * h->latCount;
return 1;
}
/* Linear interpolation in a table of values at v0, v0 + step, ... */
static Int32 Lookup(const UInt8 *table, UInt16 count, Int32 v0, UInt32 step, Int32 v, Boolean *ok)
{
UInt32 d, i, frac;
Int32 a, b;
if (v < v0) {
*ok = 0;
return 0;
}
d = (UInt32)(v - v0);
i = d / step;
frac = d % step;
if (i >= (UInt32)count - 1) {
if (i == (UInt32)count - 1 && frac == 0)
return Be32(table + 4 * i);
*ok = 0;
return 0;
}
a = Be32(table + 4 * i);
b = Be32(table + 4 * (i + 1));
/* (b - a) is a few hundred pixels at most and frac < step = 10000: no overflow */
return a + (Int32)(((b - a) * (Int32)frac) / (Int32)step);
}
Boolean VmMapWorld(const VmMapHeader *h, Int32 latE6, Int32 lonE6, Int32 *wx, Int32 *wy)
{
Boolean ok = 1;
if (latE6 < h->latS || latE6 > h->latN || lonE6 < h->lonW || lonE6 > h->lonE)
return 0;
*wy = Lookup(h->latTable, h->latCount, h->latS, h->step, latE6, &ok);
*wx = Lookup(h->lonTable, h->lonCount, h->lonW, h->step, lonE6, &ok);
return ok;
}
void VmMapToZoom(const VmMapHeader *h, UInt8 zi, Int32 wx, Int32 wy, Int32 *x, Int32 *y)
{
UInt8 shift = h->zooms[h->zoomCount - 1].z - h->zooms[zi].z;
*x = (wx >> shift) - h->zooms[zi].ox;
*y = (wy >> shift) - h->zooms[zi].oy;
}
UInt16 VmMapTileRecord(const VmMapHeader *h, UInt8 zi, Int32 col, Int32 row)
{
const VmMapZoom *z = &h->zooms[zi];
if (col < 0 || row < 0 || col >= (Int32)z->cols || row >= (Int32)z->rows)
return 0;
return (UInt16)(z->first + row * z->cols + col);
}
Boolean VmUnpackBits(const UInt8 *src, UInt32 srcLen, UInt8 *dst, UInt32 dstLen)
{
UInt32 i = 0, o = 0, n;
UInt8 c, v;
while (o < dstLen) {
if (i >= srcLen)
return 0;
c = src[i++];
if (c < 128) {
n = (UInt32)c + 1;
if (i + n > srcLen || o + n > dstLen)
return 0;
while (n--)
dst[o++] = src[i++];
} else if (c > 128) {
n = 257 - (UInt32)c;
if (i >= srcLen || o + n > dstLen)
return 0;
v = src[i++];
while (n--)
dst[o++] = v;
}
}
return 1;
}
+52
View File
@@ -0,0 +1,52 @@
/*
* The basemap database (tools/make_map.py writes it; the layout is in its
* docstring): reading the header, placing a position on the map, and
* unpacking tiles. Positions are Web Mercator pixels; the tool stores world y
* and x at the max zoom every 0.01 degree across the map, and lower zooms are
* that shifted right, so no floating point is needed here.
*
* Plain C with no Palm OS calls, so it also builds on the host for tests.
*/
#ifndef VM_MAP_GEO_H
#define VM_MAP_GEO_H
#include "protocol.h"
#define VM_MAP_TILE 160
#define VM_MAP_MAX_ZOOMS 8
#define VM_MAP_ATTR_SIZE 32
typedef struct {
UInt8 z;
UInt16 cols, rows;
UInt16 first; /* record of tile (0, 0) */
Int32 ox, oy; /* world pixels at z of tile (0, 0)'s top-left */
} VmMapZoom;
typedef struct {
UInt8 bpp;
UInt8 zoomCount;
Int32 latS, lonW, latN, lonE; /* degrees * 1e6 */
UInt32 step; /* table step, degrees * 1e6 */
UInt16 latCount, lonCount;
Char attribution[VM_MAP_ATTR_SIZE + 1];
VmMapZoom zooms[VM_MAP_MAX_ZOOMS]; /* lowest zoom first */
const UInt8 *latTable, *lonTable; /* big-endian Int32s, in the header record */
} VmMapHeader;
/* The header record; the tables point into p, which must stay valid. */
Boolean VmMapParseHeader(const UInt8 *p, UInt32 len, VmMapHeader *h) VM_SECT3;
/* World pixels at the map's max zoom; false outside the map's area. */
Boolean VmMapWorld(const VmMapHeader *h, Int32 latE6, Int32 lonE6, Int32 *wx, Int32 *wy) VM_SECT3;
/* World pixels at the max zoom to pixels at zoom index zi, from the zoom's tile (0, 0). */
void VmMapToZoom(const VmMapHeader *h, UInt8 zi, Int32 wx, Int32 wy, Int32 *x, Int32 *y) VM_SECT3;
/* The record holding tile (col, row) at zoom index zi, or 0 if outside the grid. */
UInt16 VmMapTileRecord(const VmMapHeader *h, UInt8 zi, Int32 col, Int32 row) VM_SECT3;
/* PackBits: false if src runs out or overflows dst before dstLen bytes. */
Boolean VmUnpackBits(const UInt8 *src, UInt32 srcLen, UInt8 *dst, UInt32 dstLen) VM_SECT3;
#endif
+8
View File
@@ -26,6 +26,7 @@
#define MainMenuMuteChannel 1009
#define MainMenuChannelScope 1010
#define MainMenuScopes 1011
#define MainMenuMap 1012
#define MainLinkGroup 1
#define SettingsForm 1200
@@ -147,6 +148,13 @@
#define ChanScopeOKButton 2402
#define ChanScopeCancelButton 2403
#define MapForm 2500
#define MapGadget 2501
#define MapDoneButton 2502
#define MapOutButton 2503
#define MapInButton 2504
#define MapAlert 2505
#define SmallFontRes 1000
#define LargeFontRes 1001
#define FontPreviewForm 1400
+143
View File
@@ -0,0 +1,143 @@
/* Host-side tests for the basemap reader (src/map_geo.c). Run with `make test`. */
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "../src/map_geo.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); failures++; } \
} while (0)
/* Same projection as tools/make_map.py world_xy. */
static void world(double lat, double lon, int z, double *x, double *y)
{
double n = 256.0 * (1 << z), s = sin(lat * M_PI / 180.0);
*x = (lon + 180.0) / 360.0 * n;
*y = (0.5 - log((1 + s) / (1 - s)) / (4 * M_PI)) * n;
}
static UInt8 *put16(UInt8 *p, unsigned v) { p[0] = v >> 8; p[1] = v; return p + 2; }
static UInt8 *put32(UInt8 *p, long v) { p[0] = v >> 24; p[1] = v >> 16; p[2] = v >> 8; p[3] = v; return p + 4; }
/* The Philadelphia map's header, zooms 12 and 13, as the tool writes it. */
static UInt32 build_header(UInt8 *buf)
{
const double S = 39.70, W = -75.49, N = 40.20, E = -74.83;
const long step = 10000;
int latn = 51, lonn = 67, i, z;
UInt8 *p = buf;
double x, y;
memcpy(p, "VMap", 4); p += 4;
p = put16(p, 1); p = put16(p, VM_MAP_TILE);
*p++ = 2; *p++ = 2;
p = put32(p, 39700000); p = put32(p, -75490000); p = put32(p, 40200000); p = put32(p, -74830000);
p = put32(p, step); p = put16(p, latn); p = put16(p, lonn);
memset(p, 0, 32); memcpy(p, "(c) OpenStreetMap contributors", 30); p += 32;
for (z = 12; z <= 13; z++) {
long ox, oy;
world(N, W, z, &x, &y);
ox = (long)(x / 160) * 160;
oy = (long)(y / 160) * 160;
*p++ = z; *p++ = 0;
p = put16(p, 13); p = put16(p, 13); p = put16(p, z == 12 ? 1 : 170);
p = put32(p, ox); p = put32(p, oy);
}
for (i = 0; i < latn; i++) {
world(S + i * 0.01, W, 13, &x, &y);
p = put32(p, lround(y));
}
for (i = 0; i < lonn; i++) {
world(S, W + i * 0.01, 13, &x, &y);
p = put32(p, lround(x));
}
(void)E;
return (UInt32)(p - buf);
}
static void test_header(void)
{
static UInt8 buf[1024];
UInt32 len = build_header(buf);
VmMapHeader h;
CHECK(VmMapParseHeader(buf, len, &h));
CHECK(h.bpp == 2 && h.zoomCount == 2);
CHECK(h.latS == 39700000 && h.lonE == -74830000);
CHECK(strcmp(h.attribution, "(c) OpenStreetMap contributors") == 0);
CHECK(h.zooms[1].z == 13 && h.zooms[1].first == 170);
CHECK(!VmMapParseHeader(buf, len - 1, &h)); /* tables cut short */
buf[0] = 'X';
CHECK(!VmMapParseHeader(buf, len, &h));
}
static void test_projection(void)
{
static UInt8 buf[1024];
UInt32 len = build_header(buf);
VmMapHeader h;
/* City Hall, a point between table rows, and the map's corners */
static const double pts[][2] = {
{ 39.952583, -75.165222 }, { 40.0137, -75.1803 }, { 39.70, -75.49 }, { 40.20, -74.83 }, { 39.8765, -74.9012 }
};
unsigned i;
Int32 wx, wy, x, y;
double ex, ey;
VmMapParseHeader(buf, len, &h);
for (i = 0; i < sizeof(pts) / sizeof(pts[0]); i++) {
CHECK(VmMapWorld(&h, (Int32)lround(pts[i][0] * 1e6), (Int32)lround(pts[i][1] * 1e6), &wx, &wy));
world(pts[i][0], pts[i][1], 13, &ex, &ey);
CHECK(fabs(wx - ex) <= 1.0 && fabs(wy - ey) <= 1.0);
/* zoom 12 is half, from that zoom's tile origin */
VmMapToZoom(&h, 0, wx, wy, &x, &y);
world(pts[i][0], pts[i][1], 12, &ex, &ey);
CHECK(fabs((x + h.zooms[0].ox) - ex) <= 1.0 && fabs((y + h.zooms[0].oy) - ey) <= 1.0);
}
CHECK(!VmMapWorld(&h, 39690000, -75165222, &wx, &wy)); /* south of the map */
CHECK(!VmMapWorld(&h, 39952583, -74800000, &wx, &wy)); /* east of it */
CHECK(!VmMapWorld(&h, 0, 0, &wx, &wy)); /* "no position" */
}
static void test_tiles(void)
{
static UInt8 buf[1024];
VmMapHeader h;
VmMapParseHeader(buf, build_header(buf), &h);
CHECK(VmMapTileRecord(&h, 0, 0, 0) == 1);
CHECK(VmMapTileRecord(&h, 0, 12, 12) == 1 + 12 * 13 + 12);
CHECK(VmMapTileRecord(&h, 1, 2, 1) == 170 + 13 + 2);
CHECK(VmMapTileRecord(&h, 0, 13, 0) == 0);
CHECK(VmMapTileRecord(&h, 0, -1, 0) == 0);
}
static void test_unpack(void)
{
/* literal 3, run of 5 x 0xAA, literal 1, a no-op 0x80, run of 2 x 0x00 */
static const UInt8 src[] = { 2, 1, 2, 3, 0xFC, 0xAA, 0, 9, 0x80, 0xFF, 0 };
static const UInt8 want[] = { 1, 2, 3, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 9, 0, 0 };
UInt8 out[11];
CHECK(VmUnpackBits(src, sizeof(src), out, sizeof(out)));
CHECK(memcmp(out, want, sizeof(want)) == 0);
CHECK(!VmUnpackBits(src, sizeof(src), out, 7)); /* the run of 5 crosses the end */
CHECK(!VmUnpackBits(src, 5, out, sizeof(out))); /* input ends early */
}
int main(void)
{
test_header();
test_projection();
test_tiles();
test_unpack();
if (failures) {
printf("%d failure(s)\n", failures);
return 1;
}
printf("map: all tests passed\n");
return 0;
}
+422
View File
@@ -0,0 +1,422 @@
#!/usr/bin/env python3
"""Builds VisorMesh's basemap: a Palm database of 160x160 grey tiles drawn from
OpenStreetMap data, for the Map screen to plot nodes on.
tools/make_map.py --cache DIR [--fetch] [--bpp 2|4] [--min-zoom 10] [--max-zoom 13]
[--bbox S,W,N,E] [--detail-bbox S,W,N,E]
[--out build/VisorMeshMap.pdb] [--preview DIR]
--fetch download the OSM data for the bbox from the Overpass API into the
cache (one query per layer; skip it to reuse the cache)
--detail-bbox the area the max (street-level) zoom covers, with residential
streets; the other zooms cover --bbox without them. Residential
streets are most of the size, so this sets how big the file is.
--preview also write each zoom as a PNG, to look at the result
Data (c) OpenStreetMap contributors, ODbL. The tiles are a Produced Work: the
Map screen shows the attribution stored in the header.
Tiles use Web Mercator at the standard zooms (256-pixel world at zoom 0), cut
into 160-pixel squares. Each tile is PackBits-compressed rows of 1, 2 or 4 bit
pixels (MSB first; 0 = white); an all-white tile is a 1-byte record.
Database layout (big-endian, read by src/map_db.c):
record 0: header
'VMap', version u16, tile size u16, bpp u8, zoom count u8,
bbox lat S, lon W, lat N, lon E (i32, degrees * 1e6),
table step (u32, degrees * 1e6), lat count u16, lon count u16,
attribution (32 bytes, NUL-padded),
per zoom: zoom u8, pad u8, cols u16, rows u16, first record u16,
origin x i32, origin y i32 (world pixels at that zoom of tile 0,0)
lat table: world y at the max zoom for lat S + i * step (i32 each)
lon table: world x at the max zoom for lon W + i * step (i32 each)
records 1..: tiles, row by row, per zoom
"""
import argparse
import json
import math
import os
import struct
import sys
import time
import urllib.parse
import urllib.request
import zlib
TILE = 160
STEP_E6 = 10000 # projection table step: 0.01 degree
DEFAULT_BBOX = (39.70, -75.49, 40.20, -74.83) # Philadelphia metro
DEFAULT_DETAIL = (39.88, -75.26, 40.02, -75.06) # ~16 km around Center City
OVERPASS = 'https://overpass-api.de/api/interpreter'
USER_AGENT = 'VisorMesh-maptool/0.1 (personal use)'
ATTRIBUTION = '\u00a9 OpenStreetMap' # the short form OSM accepts on small screens; Latin-1 on the Visor
# Overpass queries per cache file; {b} is the bbox. Relations need "out geom"
# ("out tags" drops their members); ways only need tags and geometry.
LAYERS = {
'major.json': 'way["highway"~"^(motorway|trunk|primary|secondary|tertiary)(_link)?$"]{b};out tags geom qt;',
'minor.json': 'way["highway"~"^(residential|unclassified|living_street)$"]{b};out skel geom qt;',
'water_ways.json': '(way["natural"="water"]{b};way["waterway"="riverbank"]{b};'
'way["waterway"~"^(river|canal)$"]{b};);out tags geom qt;',
'water_rels.json': '(relation["natural"="water"]{b};relation["waterway"="riverbank"]{b};);out geom qt;',
'parks.json': '(way["leisure"="park"]{b};relation["leisure"="park"]{b};'
'way["landuse"~"^(forest|cemetery)$"]{b};way["railway"="rail"][!"service"]{b};);out geom qt;',
}
# Greys on a 0 (white) .. 15 (black) scale, mapped to the output depth (at 2 bpp:
# 0-4 white, 5-9 light, 10-14 dark, 15 black). Parks are a light dot texture so
# they stay distinct from water when there are only four greys.
PARK, WATER, RAIL, MINOR, MID, MAJOR = 6, 6, 11, 11, 14, 15
def fetch(cache, bbox):
b = '(%s)' % ','.join(str(v) for v in bbox)
for name, q in LAYERS.items():
path = os.path.join(cache, name)
if os.path.exists(path) and os.path.getsize(path) > 1000:
continue
query = '[out:json][timeout:300];' + q.format(b=b)
data = urllib.parse.urlencode({'data': query}).encode()
req = urllib.request.Request(OVERPASS, data=data, headers={'User-Agent': USER_AGENT})
print('fetching', name, '...', flush=True)
try:
with urllib.request.urlopen(req, timeout=400) as r, open(path, 'wb') as f:
f.write(r.read())
except Exception as e: # a busy server: carry on without the layer
print(' failed (%s); run --fetch again later' % e)
time.sleep(10) # be gentle with the public server
def load(cache, name):
path = os.path.join(cache, name)
if not os.path.exists(path) or os.path.getsize(path) < 1000:
print(' (no %s: layer left out)' % name)
return []
with open(path) as f:
return json.load(f).get('elements', [])
def world_xy(lat, lon, z):
"""Web Mercator world pixels at zoom z (256-pixel world at zoom 0)."""
n = 256 * (1 << z)
x = (lon + 180.0) / 360.0 * n
s = math.sin(math.radians(lat))
y = (0.5 - math.log((1 + s) / (1 - s)) / (4 * math.pi)) * n
return x, y
class Canvas:
def __init__(self, w, h):
self.w, self.h = w, h
self.px = bytearray(w * h)
def put(self, x, y, v, width):
r0 = -(width // 2)
for dy in range(r0, r0 + width):
yy = y + dy
if 0 <= yy < self.h:
row = yy * self.w
for dx in range(r0, r0 + width):
xx = x + dx
if 0 <= xx < self.w:
self.px[row + xx] = v
def line(self, pts, v, width=1, dash=0):
"""Polyline through integer points; dash > 0 draws dash pixels on, dash off."""
n = 0
for (x0, y0), (x1, y1) in zip(pts, pts[1:]):
dx, dy = abs(x1 - x0), -abs(y1 - y0)
sx, sy = (1 if x0 < x1 else -1), (1 if y0 < y1 else -1)
err = dx + dy
while True:
if not dash or (n // dash) % 2 == 0:
self.put(x0, y0, v, width)
n += 1
if x0 == x1 and y0 == y1:
break
e2 = 2 * err
if e2 >= dy:
err += dy
x0 += sx
if e2 <= dx:
err += dx
y0 += sy
def fill(self, rings, v, texture=False):
"""Even-odd fill of one or more closed rings (holes come out right);
texture: only every other pixel, in a checkerboard."""
edges = []
ymin, ymax = self.h, -1
for ring in rings:
for (x0, y0), (x1, y1) in zip(ring, ring[1:] + ring[:1]):
if y0 == y1:
continue
if y0 > y1:
x0, y0, x1, y1 = x1, y1, x0, y0
edges.append((y0, y1, x0, (x1 - x0) / (y1 - y0)))
ymin, ymax = min(ymin, y0), max(ymax, y1)
for y in range(max(0, int(ymin)), min(self.h, int(ymax) + 1)):
yc = y + 0.5
xs = sorted(x0 + (yc - e0) * k for e0, e1, x0, k in edges if e0 <= yc < e1)
row = y * self.w
for a, b in zip(xs[0::2], xs[1::2]):
for x in range(max(0, int(a + 0.5)), min(self.w, int(b + 0.5))):
if not texture or (x + y) % 2 == 0:
self.px[row + x] = v
def geom(el, project):
return [project(p['lat'], p['lon']) for p in el.get('geometry', []) if p]
def join_rings(lines):
"""Joins open way segments end to end into closed rings (OSM multipolygons
split a big outline, like a river, over many ways). Leftovers that never
close are dropped."""
rings, open_ = [], []
for pts in lines:
if len(pts) > 3 and pts[0] == pts[-1]:
rings.append(pts)
elif len(pts) > 1:
open_.append(list(pts))
while open_:
cur = open_.pop()
grown = True
while grown and cur[0] != cur[-1]:
grown = False
for i, seg in enumerate(open_):
if seg[0] == cur[-1]:
cur += seg[1:]
elif seg[-1] == cur[-1]:
cur += seg[-2::-1]
elif seg[-1] == cur[0]:
cur = seg[:-1] + cur
elif seg[0] == cur[0]:
cur = seg[:0:-1] + cur
else:
continue
open_.pop(i)
grown = True
break
if len(cur) > 3 and cur[0] == cur[-1]:
rings.append(cur)
return rings
def closed_rings(el, project):
"""A closed way's ring, or a relation's member ways joined into rings."""
if el['type'] == 'way':
g = geom(el, project)
return [g] if len(g) > 3 and el['geometry'][0] == el['geometry'][-1] else []
lines = [tuple((p['lat'], p['lon']) for p in m['geometry'])
for m in el.get('members', []) if m.get('type') == 'way' and m.get('geometry')]
return [[project(lat, lon) for lat, lon in ring] for ring in join_rings(lines)]
def render_zoom(z, layers, origin, cols, rows, detail):
ox, oy = origin
c = Canvas(cols * TILE, rows * TILE)
def project(lat, lon):
x, y = world_xy(lat, lon, z)
return int(round(x - ox)), int(round(y - oy))
for el in layers['parks']: # areas first, lines on top
t = el.get('tags', {})
if 'railway' not in t:
for ring in closed_rings(el, project):
c.fill([ring], PARK, texture=True)
for el in layers['water']:
t = el.get('tags', {})
if t.get('waterway') in ('river', 'canal'):
c.line(geom(el, project), WATER, 3 if z >= 12 else 2)
else:
rings = closed_rings(el, project)
if rings:
c.fill(rings, WATER)
if detail:
for el in layers['parks']:
if el.get('tags', {}).get('railway') == 'rail':
c.line(geom(el, project), RAIL, 1, dash=3)
for el in layers['minor']:
c.line(geom(el, project), MINOR, 1)
order = {'tertiary': 0, 'secondary': 1, 'primary': 2, 'trunk': 3, 'motorway': 3}
majors = sorted(layers['major'], key=lambda e: order.get(e.get('tags', {}).get('highway', '').replace('_link', ''), 0))
for el in majors:
kind = el.get('tags', {}).get('highway', '').replace('_link', '')
rank = order.get(kind, 0)
if z <= 10 and rank < 2:
continue # only primary and up at the widest view
if z == 11 and rank < 1:
continue
width = 1
if rank == 3:
width = 3 if z >= 13 else 2
elif rank == 2 and z >= 12:
width = 2
c.line(geom(el, project), MAJOR if rank >= 2 else MID, width)
return c
def to_depth(v, bpp):
return v * ((1 << bpp) - 1) // 15
def packbits(data):
out = bytearray()
i, n = 0, len(data)
while i < n:
j = i
while j + 1 < n and data[j + 1] == data[i] and j - i < 127:
j += 1
if j > i: # run of i..j
out += bytes([257 - (j - i + 1), data[i]])
i = j + 1
continue
j = i
while j + 1 < n and j - i < 127 and not (j + 2 < n and data[j + 1] == data[j + 2]):
j += 1
out.append(j - i)
out += data[i:j + 1]
i = j + 1
return bytes(out)
def unpackbits(data, size):
out = bytearray()
i = 0
while len(out) < size:
h = data[i]
i += 1
if h < 128:
out += data[i:i + h + 1]
i += h + 1
elif h > 128:
out += bytes([data[i]]) * (257 - h)
i += 1
return bytes(out[:size])
def tile_bytes(c, col, row, bpp):
per = 8 // bpp
rowbytes = TILE * bpp // 8
out = bytearray(rowbytes * TILE)
for y in range(TILE):
src = (row * TILE + y) * c.w + col * TILE
line = c.px[src:src + TILE]
base = y * rowbytes
for x in range(0, TILE, per):
b = 0
for k in range(per):
b = (b << bpp) | to_depth(line[x + k], bpp)
out[base + x // per] = b
return bytes(out)
def write_png(path, c, bpp):
"""The canvas as the Visor will show it: greys reduced to the output depth."""
top = (1 << bpp) - 1
shade = bytes(255 - to_depth(min(v, 15), bpp) * 255 // top for v in range(256))
raw = bytearray()
for y in range(c.h):
raw.append(0)
raw += c.px[y * c.w:(y + 1) * c.w].translate(shade)
def chunk(t, d):
return struct.pack('>I', len(d)) + t + d + struct.pack('>I', zlib.crc32(t + d) & 0xffffffff)
with open(path, 'wb') as f:
f.write(b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', c.w, c.h, 8, 0, 0, 0, 0))
+ chunk(b'IDAT', zlib.compress(bytes(raw), 9)) + chunk(b'IEND', b''))
def write_pdb(path, name, records):
now = int(time.time()) + 2082844800 # Palm epoch: 1904
n = len(records)
header = struct.pack('>32sHHIIIIII4s4sIIH', name.encode()[:31], 0x0008, 1, now, now, 0, 0, 0, 0,
b'Map ', b'VMsh', 0, 0, n)
offset = len(header) + 8 * n + 2
entries = b''
for i, r in enumerate(records):
entries += struct.pack('>IB3s', offset, 0, (i + 1).to_bytes(3, 'big'))
offset += len(r)
with open(path, 'wb') as f:
f.write(header + entries + b'\0\0' + b''.join(records))
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument('--cache', required=True)
ap.add_argument('--fetch', action='store_true')
ap.add_argument('--bbox', default=','.join(str(v) for v in DEFAULT_BBOX))
ap.add_argument('--detail-bbox', default=','.join(str(v) for v in DEFAULT_DETAIL))
ap.add_argument('--bpp', type=int, default=2, choices=(1, 2, 4))
ap.add_argument('--min-zoom', type=int, default=10)
ap.add_argument('--max-zoom', type=int, default=13)
ap.add_argument('--out', default=os.path.join(os.path.dirname(__file__), '..', 'build', 'VisorMeshMap.pdb'))
ap.add_argument('--preview')
a = ap.parse_args()
s, w, n, e = (float(v) for v in a.bbox.split(','))
os.makedirs(a.cache, exist_ok=True)
if a.fetch:
fetch(a.cache, (s, w, n, e))
print('loading OSM data ...', flush=True)
layers = {
'major': load(a.cache, 'major.json'),
'minor': load(a.cache, 'minor.json'),
'water': load(a.cache, 'water_ways.json') + load(a.cache, 'water_rels.json'),
'parks': load(a.cache, 'parks.json'),
}
zooms, tiles = [], []
first = 1
ds, dw, dn, de = (float(v) for v in a.detail_bbox.split(','))
for z in range(a.min_zoom, a.max_zoom + 1):
detail = z == a.max_zoom
x0, y0 = world_xy(dn if detail else n, dw if detail else w, z) # top-left
x1, y1 = world_xy(ds if detail else s, de if detail else e, z)
c0, r0 = int(x0 // TILE), int(y0 // TILE)
cols, rows = int(x1 // TILE) - c0 + 1, int(y1 // TILE) - r0 + 1
origin = (c0 * TILE, r0 * TILE)
t0 = time.time()
canvas = render_zoom(z, layers, origin, cols, rows, detail)
if a.preview:
os.makedirs(a.preview, exist_ok=True)
write_png(os.path.join(a.preview, 'z%d.png' % z), canvas, a.bpp)
size = 0
for r in range(rows):
for col in range(cols):
raw = tile_bytes(canvas, col, r, a.bpp)
if not any(raw):
rec = b'\0'
else:
rec = packbits(raw)
assert unpackbits(rec, len(raw)) == raw
tiles.append(rec)
size += len(rec)
zooms.append((z, cols, rows, first, origin))
first += cols * rows
print('zoom %d: %dx%d tiles, %d KB, %.0f s' % (z, cols, rows, size // 1024, time.time() - t0), flush=True)
# projection tables at the max zoom, covering the bbox
lat0, lon0 = int(round(s * 1e6)), int(round(w * 1e6))
lat_n = int(math.ceil((n - s) * 1e6 / STEP_E6)) + 1
lon_n = int(math.ceil((e - w) * 1e6 / STEP_E6)) + 1
zmax = a.max_zoom
lat_t = [int(round(world_xy((lat0 + i * STEP_E6) / 1e6, w, zmax)[1])) for i in range(lat_n)]
lon_t = [int(round(world_xy(s, (lon0 + i * STEP_E6) / 1e6, zmax)[0])) for i in range(lon_n)]
hdr = struct.pack('>4sHHBBiiiiIHH32s', b'VMap', 1, TILE, a.bpp, len(zooms),
lat0, lon0, int(round(n * 1e6)), int(round(e * 1e6)),
STEP_E6, lat_n, lon_n, ATTRIBUTION.encode('latin-1'))
for z, cols, rows, frec, (ox, oy) in zooms:
hdr += struct.pack('>BBHHHii', z, 0, cols, rows, frec, ox, oy)
hdr += struct.pack('>%di' % lat_n, *lat_t) + struct.pack('>%di' % lon_n, *lon_t)
os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True)
write_pdb(a.out, 'VisorMeshMap', [hdr] + tiles)
print('%d tiles -> %s (%d KB)' % (len(tiles), os.path.relpath(a.out), os.path.getsize(a.out) // 1024))
if __name__ == '__main__':
sys.exit(main())