#!/usr/bin/env python3 """Map Picker: choose any area for VisorMesh's Map screen and build its basemap (VisorMeshMap.pdb), on macOS, Windows or Linux. Needs only Python 3. python3 tools/map_picker.py [--port 8765] [--cache DIR] [--out-dir DIR] [--no-browser] It opens a page in the browser with a world map. Drag the blue box over the area to cover and the orange one over the part that gets street-level detail; the page shows each zoom's tiles and the file's estimated size. Build fetches the OpenStreetMap data for the area from the Overpass API (cached, so only once), draws the tiles with make_map.py and writes the database to the output folder (default ~/VisorMeshMap), also offered as a download. Install it like any Palm database: `pilot-xfer -p usb: -i VisorMeshMap.pdb` (macOS, Linux; or `make install-map` in palm-client), or Palm Desktop's Install Tool (Windows). It replaces the map that's on the Visor. The server only listens on this computer (127.0.0.1). """ import argparse import json import os import sys import threading import time import webbrowser from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import make_map # noqa: E402 # Rough sizes per tile, from the Philadelphia map: street-level tiles in a # built-up area, and the wider zooms. Countryside comes out smaller. DETAIL_KB = 5.0 OTHER_KB = 3.2 MAX_SPAN = 6.0 # degrees per side: the header's projection tables, and patience MAX_KB = 6000 # the Visor Neo has 8 MB for everything MAX_FETCHES = 400 # cells x layers, to stay polite to the public Overpass server def check(req): """The request's area, detail area and max zoom, or a ValueError for the page.""" try: area = [float(v) for v in req['area']] detail = [float(v) for v in req['detail']] max_zoom = int(req['maxZoom']) except (KeyError, TypeError, ValueError): raise ValueError('Bad request.') s, w, n, e = area if not (-85 <= s < n <= 85 and -180 <= w < e <= 180): raise ValueError('The area must be within latitude 85 and not cross the 180th meridian.') if n - s > MAX_SPAN or e - w > MAX_SPAN: raise ValueError('The area can be at most %g degrees on a side.' % MAX_SPAN) if max_zoom not in (12, 13, 14): raise ValueError('The street-level zoom must be 12, 13 or 14.') # the street-level area, kept inside the whole area ds, dw, dn, de = max(detail[0], s), max(detail[1], w), min(detail[2], n), min(detail[3], e) if ds >= dn or dw >= de: raise ValueError('The street-level area (orange) must overlap the map area (blue).') return (s, w, n, e), (ds, dw, dn, de), max_zoom def plan(cache, area, detail, max_zoom): min_zoom = make_map.auto_min_zoom(area, max_zoom) zooms, total = [], 0.0 for z, d, _, cols, rows in make_map.zoom_plan(area, detail, min_zoom, max_zoom): kb = cols * rows * (DETAIL_KB if d else OTHER_KB) zooms.append({'z': z, 'detail': d, 'cols': cols, 'rows': rows, 'kb': round(kb)}) total += kb jobs, cached = make_map.cell_jobs(cache, area, detail) problems = [] if total > MAX_KB: problems.append('About %.1f MB is too big for the Visor (at most %.0f MB here): make the orange box ' 'smaller or the street-level zoom lower.' % (total / 1024, MAX_KB / 1024)) if len(jobs) - cached > MAX_FETCHES: problems.append('That needs %d downloads from the OpenStreetMap server; pick a smaller area ' '(at most %d at a time).' % (len(jobs) - cached, MAX_FETCHES)) return {'minZoom': min_zoom, 'zooms': zooms, 'totalKb': round(total), 'fetches': len(jobs), 'cached': cached, 'problems': problems, 'area': area, 'detail': detail} class Job: """The one build at a time, and what the page polls for.""" def __init__(self): self.lock = threading.Lock() self.running = False self.cancel = False self.phase = '' self.done = self.total = 0 self.log = [] self.result = None # {'file', 'kb', 'previews'} once built self.error = None def say(self, msg): with self.lock: self.log.append(time.strftime('%H:%M:%S ') + msg) del self.log[:-300] def status(self): with self.lock: return {'running': self.running, 'phase': self.phase, 'done': self.done, 'total': self.total, 'log': list(self.log), 'result': self.result, 'error': self.error} def run_build(job, cache, out_dir, area, detail, max_zoom): try: jobs, _ = make_map.cell_jobs(cache, area, detail) job.phase, job.done, job.total = 'Downloading OpenStreetMap data', 0, len(jobs) failed = 0 for name, cell in jobs: if job.cancel: raise RuntimeError('Stopped.') if not os.path.exists(make_map.cell_path(cache, name, cell)): job.say('fetching %s for %+.2f, %+.2f' % (name[:-5], cell[0], cell[1])) if not make_map.fetch_cell(cache, name, cell, log=job.say): failed += 1 job.done += 1 if failed: raise RuntimeError('%d downloads failed (the server is busy?). Build again later: ' 'what was fetched is kept.' % failed) job.phase, job.done, job.total = 'Reading the data', 0, 0 layers = make_map.cell_layers(cache, area, detail) job.say('%d roads, %d streets, %d water, %d parks and rail' % ( len(layers['major']), len(layers['minor']), len(layers['water']), len(layers['parks']))) min_zoom = make_map.auto_min_zoom(area, max_zoom) job.phase, job.total = 'Drawing the tiles', max_zoom - min_zoom + 1 out = os.path.join(out_dir, 'VisorMeshMap.pdb') preview = os.path.join(out_dir, 'preview') def log(msg): job.say(msg) if msg.startswith('zoom '): job.done += 1 size = make_map.build(area, detail, min_zoom, max_zoom, 2, layers, out, preview, log=log) with job.lock: job.result = {'file': out, 'kb': size // 1024, 'previews': ['z%d.png' % z for z in range(min_zoom, max_zoom + 1)]} job.phase = 'Done' except Exception as e: with job.lock: job.error = str(e) job.phase = 'Stopped' job.say('error: %s' % e) finally: with job.lock: job.running = False def make_handler(cache, out_dir, job): class Handler(BaseHTTPRequestHandler): def log_message(self, fmt, *args): pass def send(self, code, body, ctype='application/json', extra=None): if isinstance(body, (dict, list)): body = json.dumps(body).encode() elif isinstance(body, str): body = body.encode() self.send_response(code) self.send_header('Content-Type', ctype) self.send_header('Content-Length', str(len(body))) self.send_header('Cache-Control', 'no-store') for k, v in (extra or {}).items(): self.send_header(k, v) self.end_headers() self.wfile.write(body) def file(self, path, ctype, name=None): if not os.path.isfile(path): return self.send(404, {'error': 'Not built yet.'}) with open(path, 'rb') as f: data = f.read() extra = {'Content-Disposition': 'attachment; filename="%s"' % name} if name else None self.send(200, data, ctype, extra) def do_GET(self): if self.path == '/': return self.send(200, PAGE, 'text/html; charset=utf-8') if self.path == '/api/status': return self.send(200, job.status()) if self.path == '/api/download': return self.file(os.path.join(out_dir, 'VisorMeshMap.pdb'), 'application/octet-stream', 'VisorMeshMap.pdb') if self.path.startswith('/api/preview/'): name = os.path.basename(self.path) if name.startswith('z') and name.endswith('.png') and name[1:-4].isdigit(): return self.file(os.path.join(out_dir, 'preview', name), 'image/png') self.send(404, {'error': 'Not found.'}) def do_POST(self): # a page on another site can't post JSON here without CORS, but check anyway origin = self.headers.get('Origin') if origin and not origin.startswith(('http://127.0.0.1:', 'http://localhost:')): return self.send(403, {'error': 'Forbidden.'}) try: req = json.loads(self.rfile.read(int(self.headers.get('Content-Length', 0))) or b'{}') except ValueError: return self.send(400, {'error': 'Bad request.'}) if self.path == '/api/cancel': job.cancel = True return self.send(200, {'ok': True}) if self.path not in ('/api/plan', '/api/build'): return self.send(404, {'error': 'Not found.'}) try: area, detail, max_zoom = check(req) except ValueError as e: return self.send(400, {'error': str(e)}) p = plan(cache, area, detail, max_zoom) if self.path == '/api/plan': return self.send(200, p) if p['problems']: return self.send(400, {'error': ' '.join(p['problems'])}) with job.lock: if job.running: return self.send(409, {'error': 'A build is already running.'}) job.running, job.cancel, job.log, job.result, job.error = True, False, [], None, None threading.Thread(target=run_build, args=(job, cache, out_dir, area, detail, max_zoom), daemon=True).start() return self.send(200, {'ok': True}) return Handler def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument('--port', type=int, default=8765) ap.add_argument('--cache', default=os.path.join(os.path.expanduser('~'), '.visormesh', 'map-cache')) ap.add_argument('--out-dir', default=os.path.join(os.path.expanduser('~'), 'VisorMeshMap')) ap.add_argument('--no-browser', action='store_true') a = ap.parse_args() os.makedirs(a.cache, exist_ok=True) os.makedirs(a.out_dir, exist_ok=True) handler = make_handler(a.cache, a.out_dir, Job()) try: server = ThreadingHTTPServer(('127.0.0.1', a.port), handler) except OSError: server = ThreadingHTTPServer(('127.0.0.1', 0), handler) # the port is taken: any free one url = 'http://127.0.0.1:%d/' % server.server_address[1] print('VisorMesh Map Picker at %s' % url, flush=True) print('Maps are saved in %s; OpenStreetMap data is cached in %s' % (a.out_dir, a.cache)) print('Press Ctrl+C to quit.', flush=True) if not a.no_browser: threading.Timer(0.5, webbrowser.open, (url,)).start() try: server.serve_forever() except KeyboardInterrupt: pass PAGE = r'''
Choose the area for the Visor's Map screen.
Map area: every zoom covers it, with the main roads, water and parks.
Street level: the closest zoom, with every street. This is most of the size.
Drag a box's corners to resize it, its centre square to move it. Or pan the map to a place and use Fit to view.
| Zoom | Covers | Tiles | About |
|---|---|---|---|
| Map file | |||
Previews:
Install it (it replaces the map on the Visor):
macOS or Linux: pilot-xfer -p usb: -i VisorMeshMap.pdb, then press HotSync.
Windows: open it with Palm Desktop's Install Tool, then HotSync.
Map data © OpenStreetMap contributors (ODbL). Downloads use the public Overpass API and are cached, so each place is only fetched once.