mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 07:26:18 -05:00
Map search added to global search. Preview cards are displayed for all results in new Map tab.
Map viewer has simple search. Map search databases can be downloaded from CDN, or built locally. Map searches can be built offline, but will not have enriched things like state names downloaded from Wikidata.
This commit is contained in:
parent
aaa3c0aff7
commit
c726654de9
21 changed files with 1831 additions and 62 deletions
|
|
@ -4,7 +4,7 @@ from sanic import Request, response, Blueprint
|
|||
from sanic_ext import validate
|
||||
from sanic_ext.extensions.openapi import openapi
|
||||
|
||||
from modules.map import lib, schema
|
||||
from modules.map import lib, schema, search
|
||||
from modules.map.pins import get_map_pins_config
|
||||
from wrolpi.api_utils import json_response
|
||||
from wrolpi.common import wrol_mode_check
|
||||
|
|
@ -98,3 +98,63 @@ async def update_pin(_: Request, pin_id: int, body: schema.MapPinUpdateRequest):
|
|||
if get_map_pins_config().update_pin(pin_id, body.label, body.color):
|
||||
return response.empty(HTTPStatus.NO_CONTENT)
|
||||
return response.json({'error': 'Pin not found'}, HTTPStatus.NOT_FOUND)
|
||||
|
||||
|
||||
@map_bp.get('/search')
|
||||
@openapi.description('Search for places in map search indexes')
|
||||
async def search_places(request: Request):
|
||||
q = request.args.get('q', '')
|
||||
if not q or len(q.strip()) < 1:
|
||||
return json_response({'error': 'Query parameter "q" is required'}, HTTPStatus.BAD_REQUEST)
|
||||
|
||||
try:
|
||||
limit = int(request.args.get('limit', 12))
|
||||
offset = int(request.args.get('offset', 0))
|
||||
lat = float(request.args.get('lat')) if request.args.get('lat') is not None else None
|
||||
lon = float(request.args.get('lon')) if request.args.get('lon') is not None else None
|
||||
except (ValueError, TypeError):
|
||||
return json_response({'error': 'Invalid query parameters'}, HTTPStatus.BAD_REQUEST)
|
||||
|
||||
data = search.search_places(q.strip(), limit=limit, offset=offset, lat=lat, lon=lon)
|
||||
return json_response(data, HTTPStatus.OK)
|
||||
|
||||
|
||||
@map_bp.get('/search/estimate')
|
||||
@openapi.description('Get estimated count of map search results')
|
||||
async def get_search_estimate(request: Request):
|
||||
q = request.args.get('q', '')
|
||||
if not q or len(q.strip()) < 1:
|
||||
return json_response({'map_places': 0}, HTTPStatus.OK)
|
||||
count = search.search_places_count(q.strip())
|
||||
return json_response({'map_places': count}, HTTPStatus.OK)
|
||||
|
||||
|
||||
@map_bp.get('/search/status')
|
||||
@openapi.description('Get status of map search indexes')
|
||||
async def get_search_status(_: Request):
|
||||
status = search.get_search_status()
|
||||
return json_response(status, HTTPStatus.OK)
|
||||
|
||||
|
||||
@map_bp.post('/search/rebuild')
|
||||
@openapi.description('Rebuild all map search indexes')
|
||||
@wrol_mode_check
|
||||
async def rebuild_search_indexes(_: Request):
|
||||
started = search.rebuild_all_search_indexes()
|
||||
if started:
|
||||
return json_response({'message': 'Search index rebuild started'}, HTTPStatus.ACCEPTED)
|
||||
return json_response({'error': 'No PMTiles files found'}, HTTPStatus.NOT_FOUND)
|
||||
|
||||
|
||||
@map_bp.post('/search/rebuild/<filename:str>')
|
||||
@openapi.description('Rebuild search index for a single PMTiles file')
|
||||
@wrol_mode_check
|
||||
async def rebuild_single_search_index(_: Request, filename: str):
|
||||
try:
|
||||
started = search.rebuild_search_index(filename)
|
||||
except ValueError as e:
|
||||
return response.json({'error': str(e)}, HTTPStatus.BAD_REQUEST)
|
||||
|
||||
if started:
|
||||
return json_response({'message': f'Search index rebuild started for {filename}'}, HTTPStatus.ACCEPTED)
|
||||
return json_response({'error': 'File not found'}, HTTPStatus.NOT_FOUND)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ from wrolpi.common import logger, verify_gpg_signature
|
|||
from wrolpi.db import get_db_session
|
||||
from wrolpi.downloader import Downloader, Download, DownloadResult, download_manager
|
||||
|
||||
__all__ = ['MapCatalogDownloader', 'MapExtractDownloader', 'map_catalog_downloader', 'map_extract_downloader']
|
||||
__all__ = ['MapCatalogDownloader', 'MapExtractDownloader', 'MapSearchIndexDownloader',
|
||||
'map_catalog_downloader', 'map_extract_downloader', 'map_search_index_downloader']
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
|
|
@ -66,6 +67,7 @@ class MapCatalogDownloader(Downloader):
|
|||
|
||||
version = manifest.get('version', '')
|
||||
manifest_regions = manifest.get('regions', {})
|
||||
search_indexes = manifest.get('search_indexes', {})
|
||||
|
||||
if not manifest_regions:
|
||||
return DownloadResult(success=False, error='Manifest does not contain regions')
|
||||
|
|
@ -74,7 +76,13 @@ class MapCatalogDownloader(Downloader):
|
|||
if not regions:
|
||||
return DownloadResult(success=True)
|
||||
|
||||
map_directory = lib.get_map_directory()
|
||||
|
||||
created = 0
|
||||
# Defer search-index downloads so they are all inserted AFTER every pmtiles
|
||||
# download. Downloads process in insertion order; this prevents a search-index
|
||||
# download from running before its companion pmtiles file exists on disk.
|
||||
pending_search_downloads = []
|
||||
with get_db_session(commit=True) as session:
|
||||
for r in regions:
|
||||
region = r.get('region', '')
|
||||
|
|
@ -83,6 +91,12 @@ class MapCatalogDownloader(Downloader):
|
|||
logger.warning(f'Subscribed region {region!r} not found in manifest — skipping')
|
||||
continue
|
||||
|
||||
# Compute the expected PMTiles filename.
|
||||
if version:
|
||||
pmtiles_name = f'{region}-{version}.pmtiles'
|
||||
else:
|
||||
pmtiles_name = f'{region}.pmtiles'
|
||||
|
||||
# Delete stale download for this region if the URL changed (new version).
|
||||
old = session.query(Download).filter(
|
||||
Download.downloader == 'map_extract',
|
||||
|
|
@ -101,14 +115,34 @@ class MapCatalogDownloader(Downloader):
|
|||
child.settings = {
|
||||
'region': region,
|
||||
'version': version,
|
||||
'search_index_url': search_indexes.get(region, ''),
|
||||
}
|
||||
child.renew(reset_attempts=True)
|
||||
created += 1
|
||||
|
||||
# Queue search index download if the CDN provides one and the
|
||||
# .search.db file does not already exist on disk.
|
||||
search_url = search_indexes.get(region)
|
||||
search_db_path = (map_directory / pmtiles_name).with_suffix('.search.db')
|
||||
if search_url and not search_db_path.is_file():
|
||||
pending_search_downloads.append((region, search_url, pmtiles_name))
|
||||
|
||||
# Insert all search-index downloads after every pmtiles download, so they
|
||||
# process in insertion order behind their companion pmtiles.
|
||||
for region, search_url, pmtiles_name in pending_search_downloads:
|
||||
search_child = download_manager.get_or_create_download(
|
||||
session, search_url, reset_attempts=True, override_skip=True)
|
||||
search_child.downloader = 'map_search_index'
|
||||
search_child.location = '/map/manage'
|
||||
search_child.settings = {'region': region, 'pmtiles_name': pmtiles_name}
|
||||
search_child.renew(reset_attempts=True)
|
||||
logger.info(f'Created search index download for {region}')
|
||||
|
||||
if not created:
|
||||
return DownloadResult(success=False, error='No matching region URLs found in manifest')
|
||||
|
||||
logger.info(f'Created {created} map region downloads')
|
||||
|
||||
return DownloadResult(success=True, location='/map/manage')
|
||||
|
||||
|
||||
|
|
@ -201,7 +235,86 @@ class MapExtractDownloader(Downloader):
|
|||
size = output_path.stat().st_size
|
||||
logger.warning(f'Successfully downloaded {region} ({size} bytes) to {output_path}')
|
||||
|
||||
# Build search index locally if tippecanoe is available and no CDN
|
||||
# search index download already exists for this region.
|
||||
has_search_download = False
|
||||
with get_db_session() as session:
|
||||
has_search_download = session.query(Download).filter(
|
||||
Download.downloader == 'map_search_index',
|
||||
Download.settings['region'].astext == region,
|
||||
).count() > 0
|
||||
if not has_search_download:
|
||||
try:
|
||||
from modules.map.search import rebuild_search_index
|
||||
rebuild_search_index(output_path.name)
|
||||
except Exception as e:
|
||||
logger.error(f'Failed to start search index build for {region}: {e}')
|
||||
|
||||
return DownloadResult(success=True, location='/map')
|
||||
|
||||
|
||||
map_extract_downloader = MapExtractDownloader()
|
||||
|
||||
|
||||
class MapSearchIndexDownloader(Downloader):
|
||||
"""Downloads a pre-built .search.db file from the CDN via aria2c with meta4 verification.
|
||||
|
||||
Created by MapCatalogDownloader alongside map_extract downloads for each
|
||||
subscribed region. Will wait (fail + retry) until the PMTiles file exists.
|
||||
"""
|
||||
|
||||
name = 'map_search_index'
|
||||
listable = False
|
||||
|
||||
async def do_download(self, download: Download) -> DownloadResult:
|
||||
settings = download.settings or {}
|
||||
region = settings.get('region', '')
|
||||
pmtiles_name = settings.get('pmtiles_name', '')
|
||||
|
||||
if not region or not pmtiles_name:
|
||||
return DownloadResult(success=False, error='Missing region or pmtiles_name in settings')
|
||||
|
||||
map_directory = lib.get_map_directory()
|
||||
pmtiles_path = map_directory / pmtiles_name
|
||||
|
||||
if not pmtiles_path.is_file():
|
||||
return DownloadResult(success=False, error=f'PMTiles file not found: {pmtiles_name}')
|
||||
|
||||
# Name the search.db to match the local PMTiles filename.
|
||||
search_db_path = pmtiles_path.with_suffix('.search.db')
|
||||
|
||||
if search_db_path.is_file():
|
||||
logger.info(f'Search index already exists for {region}, skipping')
|
||||
return DownloadResult(success=True, location='/map')
|
||||
|
||||
# Fetch and verify meta4 (GPG-signed hash).
|
||||
meta4_contents = await self.get_meta4_contents(download.url)
|
||||
if meta4_contents:
|
||||
if not await verify_meta4_signature(meta4_contents):
|
||||
return DownloadResult(success=False,
|
||||
error=f'meta4 signature verification failed for {region} search index')
|
||||
logger.info(f'Search index meta4 verified for {region}')
|
||||
|
||||
# Download via aria2c with meta4 hash verification.
|
||||
logger.warning(f'Downloading search index for {region} from {download.url}')
|
||||
downloaded_path = None
|
||||
try:
|
||||
downloaded_path = await self.download_file(
|
||||
download, download.url, map_directory,
|
||||
check_for_meta4=False, concurrent=1, meta4_xml=meta4_contents,
|
||||
)
|
||||
# Rename to match the local PMTiles filename.
|
||||
downloaded_path.rename(search_db_path)
|
||||
except Exception as e:
|
||||
logger.error(f'Search index download failed for {region}: {e}')
|
||||
if downloaded_path:
|
||||
downloaded_path.unlink(missing_ok=True)
|
||||
search_db_path.unlink(missing_ok=True)
|
||||
return DownloadResult(success=False, error=f'Download failed: {str(e)[:500]}')
|
||||
|
||||
size = search_db_path.stat().st_size
|
||||
logger.warning(f'Downloaded search index for {region} ({size} bytes)')
|
||||
return DownloadResult(success=True, location='/map')
|
||||
|
||||
|
||||
map_search_index_downloader = MapSearchIndexDownloader()
|
||||
|
|
|
|||
|
|
@ -33,13 +33,16 @@ def get_pmtiles_files() -> List[dict]:
|
|||
path=str(path.relative_to(get_media_directory())),
|
||||
size=stat.st_size,
|
||||
mtime=stat.st_mtime,
|
||||
has_search_index=path.with_suffix('.search.db').is_file(),
|
||||
))
|
||||
|
||||
return files
|
||||
|
||||
|
||||
def delete_pmtiles_file(filename: str) -> bool:
|
||||
"""Delete a PMTiles file from the map directory. Returns True if the file was deleted."""
|
||||
"""Delete a PMTiles file from the map directory. Returns True if the file was deleted.
|
||||
|
||||
Also deletes the companion .search.db file if it exists."""
|
||||
map_directory = get_map_directory()
|
||||
# Prevent path traversal.
|
||||
path = (map_directory / filename).resolve()
|
||||
|
|
@ -49,6 +52,11 @@ def delete_pmtiles_file(filename: str) -> bool:
|
|||
if path.is_file() and path.suffix == '.pmtiles':
|
||||
path.unlink()
|
||||
logger.warning(f'Deleted PMTiles file: {path}')
|
||||
# Also delete companion search index.
|
||||
search_db = path.with_suffix('.search.db')
|
||||
if search_db.is_file():
|
||||
search_db.unlink()
|
||||
logger.warning(f'Deleted search index: {search_db}')
|
||||
return True
|
||||
|
||||
return False
|
||||
|
|
@ -73,7 +81,6 @@ def _get_or_create_map_download(session: Session) -> Download:
|
|||
download = download_manager.get_or_create_download(session, MANIFEST_URL, reset_attempts=True,
|
||||
override_skip=True)
|
||||
download.downloader = 'map_catalog'
|
||||
download.sub_downloader = 'map_extract'
|
||||
download.frequency = DownloadFrequency.days180
|
||||
download.settings = {'regions': []}
|
||||
download.location = '/map/manage'
|
||||
|
|
|
|||
295
modules/map/search.py
Normal file
295
modules/map/search.py
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
import asyncio
|
||||
import contextlib
|
||||
import math
|
||||
import shutil
|
||||
import sqlite3
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from modules.map.lib import get_map_directory
|
||||
from wrolpi.common import logger, background_task
|
||||
from wrolpi.events import Events
|
||||
from wrolpi.flags import map_search_building
|
||||
|
||||
logger = logger.getChild(__name__)
|
||||
|
||||
BUILD_SCRIPT = Path(__file__).parent.parent.parent / 'wrolpi' / 'scripts' / 'build_map_search.py'
|
||||
|
||||
|
||||
def get_search_db_files() -> List[Path]:
|
||||
"""Find all .search.db files in the map directory."""
|
||||
map_directory = get_map_directory()
|
||||
return sorted(map_directory.glob('*.search.db'))
|
||||
|
||||
|
||||
def search_places(query: str, limit: int = 12, offset: int = 0, lat: float = None, lon: float = None) -> dict:
|
||||
"""Search all .search.db files for places matching the query.
|
||||
|
||||
Results are ranked by importance (min_zoom) and optionally by proximity to lat/lon.
|
||||
Returns {"results": [...], "total": N}.
|
||||
"""
|
||||
db_files = get_search_db_files()
|
||||
if not db_files:
|
||||
return {'results': [], 'total': 0}
|
||||
|
||||
results = []
|
||||
|
||||
# Cap the number of rows fetched per DB to bound memory on large regional indexes
|
||||
# (e.g. a prefix query like "S*" can match hundreds of thousands of rows). The
|
||||
# cap is a multiple of (offset + limit) to keep enough headroom for ranking and
|
||||
# cross-DB deduplication before pagination.
|
||||
per_db_cap = max((offset + limit) * 5, 100)
|
||||
|
||||
# FTS5 match query with prefix matching. Escape embedded double quotes.
|
||||
escaped = query.replace('"', '""')
|
||||
fts_query = f'"{escaped}"*'
|
||||
|
||||
for db_path in db_files:
|
||||
try:
|
||||
with contextlib.closing(sqlite3.connect(f'file:{db_path}?mode=ro', uri=True)) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
rows = conn.execute("""
|
||||
SELECT p.name, p.kind, p.lat, p.lon, p.min_zoom, p.source,
|
||||
p.kind_detail, p.population, p.region,
|
||||
places_fts.rank AS fts_rank
|
||||
FROM places_fts
|
||||
JOIN places p ON p.id = places_fts.rowid
|
||||
WHERE places_fts MATCH ?
|
||||
ORDER BY places_fts.rank
|
||||
LIMIT ?
|
||||
""", (fts_query, per_db_cap)).fetchall()
|
||||
|
||||
for row in rows:
|
||||
result = dict(row)
|
||||
if lat is not None and lon is not None:
|
||||
dlat = result['lat'] - lat
|
||||
dlon = result['lon'] - lon
|
||||
result['distance'] = math.sqrt(dlat ** 2 + dlon ** 2)
|
||||
results.append(result)
|
||||
except sqlite3.OperationalError as e:
|
||||
logger.warning(f'Search error in {db_path.name}: {e}')
|
||||
continue
|
||||
|
||||
# Fuzzy fallback: if FTS5 found nothing, try LIKE substring matching.
|
||||
if not results:
|
||||
like_pattern = f'%{query}%'
|
||||
for db_path in db_files:
|
||||
try:
|
||||
with contextlib.closing(sqlite3.connect(f'file:{db_path}?mode=ro', uri=True)) as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
|
||||
rows = conn.execute("""
|
||||
SELECT p.name, p.kind, p.lat, p.lon, p.min_zoom, p.source,
|
||||
p.kind_detail, p.population, p.region, 0 AS fts_rank
|
||||
FROM places p
|
||||
WHERE p.name LIKE ?
|
||||
ORDER BY p.min_zoom, p.population DESC
|
||||
LIMIT ?
|
||||
""", (like_pattern, per_db_cap)).fetchall()
|
||||
|
||||
for row in rows:
|
||||
result = dict(row)
|
||||
if lat is not None and lon is not None:
|
||||
dlat = result['lat'] - lat
|
||||
dlon = result['lon'] - lon
|
||||
result['distance'] = math.sqrt(dlat ** 2 + dlon ** 2)
|
||||
results.append(result)
|
||||
except sqlite3.OperationalError as e:
|
||||
logger.warning(f'LIKE search error in {db_path.name}: {e}')
|
||||
continue
|
||||
|
||||
# Sort by importance (lower min_zoom = more important), then by proximity or FTS rank.
|
||||
# Coerce NULL min_zoom (possible from older schemas) to a default; `dict.get` returns
|
||||
# None rather than the default for explicit NULL values.
|
||||
if lat is not None and lon is not None:
|
||||
results.sort(key=lambda r: (r.get('min_zoom') or 10, r.get('distance') or 0))
|
||||
else:
|
||||
results.sort(key=lambda r: (r.get('min_zoom') or 10, r.get('fts_rank') or 0))
|
||||
|
||||
# Deduplicate across sources: same name+kind at ~same location keeps highest population.
|
||||
seen = {}
|
||||
deduped = []
|
||||
for r in results:
|
||||
key = (r['name'], r['kind'], round(r['lat'], 2), round(r['lon'], 2))
|
||||
if key not in seen:
|
||||
seen[key] = len(deduped)
|
||||
deduped.append(r)
|
||||
else:
|
||||
# Keep the entry with higher population.
|
||||
existing = deduped[seen[key]]
|
||||
if (r.get('population') or 0) > (existing.get('population') or 0):
|
||||
deduped[seen[key]] = r
|
||||
|
||||
# Use the DB-wide count for `total` so pagination isn't capped by `per_db_cap`.
|
||||
# Fall back to the deduped length if the count query fails (e.g. the per-DB cap
|
||||
# was never hit, in which case they agree anyway).
|
||||
if len(results) >= per_db_cap:
|
||||
total = search_places_count(query)
|
||||
else:
|
||||
total = len(deduped)
|
||||
|
||||
# Remove internal ranking fields before returning.
|
||||
final = []
|
||||
for r in deduped[offset:offset + limit]:
|
||||
r.pop('fts_rank', None)
|
||||
r.pop('distance', None)
|
||||
final.append(r)
|
||||
|
||||
return {'results': final, 'total': total}
|
||||
|
||||
|
||||
def search_places_count(query: str) -> int:
|
||||
"""Return the count of places matching the query across all search indexes."""
|
||||
db_files = get_search_db_files()
|
||||
if not db_files:
|
||||
return 0
|
||||
|
||||
escaped = query.replace('"', '""')
|
||||
fts_query = f'"{escaped}"*'
|
||||
total = 0
|
||||
|
||||
for db_path in db_files:
|
||||
try:
|
||||
with contextlib.closing(sqlite3.connect(f'file:{db_path}?mode=ro', uri=True)) as conn:
|
||||
row = conn.execute("""
|
||||
SELECT COUNT(*) FROM places_fts WHERE places_fts MATCH ?
|
||||
""", (fts_query,)).fetchone()
|
||||
total += row[0] if row else 0
|
||||
except sqlite3.OperationalError:
|
||||
continue
|
||||
|
||||
# LIKE fallback count if FTS5 found nothing.
|
||||
if total == 0:
|
||||
like_pattern = f'%{query}%'
|
||||
for db_path in db_files:
|
||||
try:
|
||||
with contextlib.closing(sqlite3.connect(f'file:{db_path}?mode=ro', uri=True)) as conn:
|
||||
row = conn.execute("SELECT COUNT(*) FROM places WHERE name LIKE ?",
|
||||
(like_pattern,)).fetchone()
|
||||
total += row[0] if row else 0
|
||||
except sqlite3.OperationalError:
|
||||
continue
|
||||
|
||||
return total
|
||||
|
||||
|
||||
def get_search_status() -> dict:
|
||||
"""Return status of search indexes: which PMTiles files have indexes and which don't."""
|
||||
map_directory = get_map_directory()
|
||||
|
||||
pmtiles_files = sorted(map_directory.glob('*.pmtiles'))
|
||||
# Strip the double .search.db extension to get the PMTiles stem.
|
||||
search_db_files = {p.name.removesuffix('.search.db') for p in get_search_db_files()}
|
||||
|
||||
indexed = []
|
||||
missing = []
|
||||
|
||||
for pmtiles_path in pmtiles_files:
|
||||
info = {'name': pmtiles_path.name, 'stem': pmtiles_path.stem}
|
||||
if pmtiles_path.stem in search_db_files:
|
||||
db_path = pmtiles_path.with_suffix('.search.db')
|
||||
info['search_db_size'] = db_path.stat().st_size
|
||||
indexed.append(info)
|
||||
else:
|
||||
missing.append(info)
|
||||
|
||||
return {
|
||||
'indexed': indexed,
|
||||
'missing': missing,
|
||||
}
|
||||
|
||||
|
||||
def _check_tippecanoe() -> bool:
|
||||
"""Return True if tippecanoe-decode is on PATH. If missing, log and send an Event."""
|
||||
if shutil.which('tippecanoe-decode'):
|
||||
return True
|
||||
msg = 'tippecanoe-decode not found on PATH — map search index cannot be built'
|
||||
logger.error(msg)
|
||||
Events.send_tippecanoe_missing(msg)
|
||||
return False
|
||||
|
||||
|
||||
def _should_enrich() -> bool:
|
||||
"""Return True if Wikidata enrichment should be attempted (WROL mode is not active)."""
|
||||
try:
|
||||
from wrolpi.common import get_wrolpi_config
|
||||
return not get_wrolpi_config().wrol_mode
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
async def _run_build(cmd: list, description: str):
|
||||
"""Run a build subprocess with the map_search_building flag set.
|
||||
|
||||
The flag is automatically cleared when the build finishes (success or failure).
|
||||
"""
|
||||
with map_search_building:
|
||||
logger.warning(f'Starting search index build: {description}')
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.STDOUT,
|
||||
)
|
||||
stdout, _ = await proc.communicate()
|
||||
output = stdout.decode()[-500:] if stdout else ''
|
||||
if proc.returncode == 0:
|
||||
logger.warning(f'Search index build completed: {description}')
|
||||
Events.send_map_search_complete(f'Search index built for {description}')
|
||||
else:
|
||||
logger.error(f'Search index build failed for {description} (exit {proc.returncode}):\n{output}')
|
||||
Events.send_map_search_failed(f'Search index build failed for {description}')
|
||||
|
||||
|
||||
def rebuild_search_index(pmtiles_name: str, max_zoom: int = 10) -> bool:
|
||||
"""Launch the build script as a background task for a single PMTiles file.
|
||||
|
||||
Returns True if the build was started, False if the file doesn't exist.
|
||||
"""
|
||||
map_directory = get_map_directory()
|
||||
pmtiles_path = (map_directory / pmtiles_name).resolve()
|
||||
|
||||
if not pmtiles_path.is_relative_to(map_directory.resolve()):
|
||||
raise ValueError(f'Invalid filename: {pmtiles_name}')
|
||||
|
||||
if not pmtiles_path.is_file():
|
||||
return False
|
||||
|
||||
if map_search_building.is_set():
|
||||
logger.warning(f'Search index build already running — ignoring request for {pmtiles_name}')
|
||||
return False
|
||||
|
||||
if not _check_tippecanoe():
|
||||
return False
|
||||
|
||||
cmd = [sys.executable, str(BUILD_SCRIPT), str(pmtiles_path), '--max-zoom', str(max_zoom)]
|
||||
if _should_enrich():
|
||||
cmd.append('--enrich')
|
||||
|
||||
background_task(_run_build(cmd, pmtiles_name))
|
||||
return True
|
||||
|
||||
|
||||
def rebuild_all_search_indexes(max_zoom: int = 10) -> bool:
|
||||
"""Launch the build script for the entire map directory as a background task."""
|
||||
map_directory = get_map_directory()
|
||||
|
||||
if not any(map_directory.glob('*.pmtiles')):
|
||||
return False
|
||||
|
||||
if map_search_building.is_set():
|
||||
logger.warning('Search index build already running — ignoring rebuild-all request')
|
||||
return False
|
||||
|
||||
if not _check_tippecanoe():
|
||||
return False
|
||||
|
||||
cmd = [sys.executable, str(BUILD_SCRIPT), str(map_directory), '--max-zoom', str(max_zoom)]
|
||||
if _should_enrich():
|
||||
cmd.append('--enrich')
|
||||
|
||||
background_task(_run_build(cmd, 'all map files'))
|
||||
return True
|
||||
|
||||
|
||||
66
modules/map/static/embed.html
Normal file
66
modules/map/static/embed.html
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="/libs/maplibre-gl.css"/>
|
||||
<style>* { margin: 0; padding: 0; } html, body, #map { width: 100%; height: 100%; }</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="map"></div>
|
||||
<script src="/libs/pmtiles.js"></script>
|
||||
<script src="/libs/maplibre-gl.js"></script>
|
||||
<script src="/libs/protomaps-themes-base.js"></script>
|
||||
<script>
|
||||
(function() {
|
||||
var protocol = new pmtiles.Protocol();
|
||||
maplibregl.addProtocol("pmtiles", protocol.tile);
|
||||
|
||||
var hash = window.location.hash.replace("#", "");
|
||||
var parts = hash ? hash.split(",") : [];
|
||||
var lat = parseFloat(parts[0]) || 0;
|
||||
var lon = parseFloat(parts[1]) || 0;
|
||||
var zoom = parseFloat(parts[2]) || 4;
|
||||
|
||||
var dark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||
var flavor = dark ? "dark" : "light";
|
||||
|
||||
fetch("/api/map/files").then(function(r) { return r.json(); }).then(function(data) {
|
||||
var sources = {};
|
||||
var allLayers = [];
|
||||
|
||||
// Add user's PMTiles files (skip terrain). Includes map-overview if present.
|
||||
var files = (data.files || []).filter(function(f) {
|
||||
return f.name.indexOf("terrain") !== 0;
|
||||
});
|
||||
files.forEach(function(f, i) {
|
||||
sources[f.name] = {type: "vector", url: "pmtiles:///data/" + f.name};
|
||||
var layers = protomaps_themes_base.default(f.name, flavor);
|
||||
if (i > 0) {
|
||||
layers.forEach(function(l) { l.id = f.name + ":" + l.id; });
|
||||
}
|
||||
allLayers = allLayers.concat(layers);
|
||||
});
|
||||
|
||||
new maplibregl.Map({
|
||||
container: "map",
|
||||
style: {
|
||||
version: 8,
|
||||
glyphs: "/fonts/{fontstack}/{range}.pbf",
|
||||
sprite: window.location.origin + "/sprites/" + flavor,
|
||||
sources: sources,
|
||||
layers: allLayers,
|
||||
},
|
||||
center: [lon, lat],
|
||||
zoom: zoom,
|
||||
attributionControl: false,
|
||||
interactive: false,
|
||||
});
|
||||
}).catch(function(e) {
|
||||
console.error("Embed map error:", e);
|
||||
document.getElementById("map").textContent = "Map unavailable";
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -124,6 +124,43 @@
|
|||
user-select: none;
|
||||
}
|
||||
#context-menu .action:hover { background: #f0f0f0; }
|
||||
|
||||
/* Search */
|
||||
#search-container {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
z-index: 1000;
|
||||
width: 320px;
|
||||
max-width: calc(100% - 100px);
|
||||
}
|
||||
#search-input {
|
||||
width: 100%;
|
||||
padding: 8px 12px;
|
||||
border: 1px solid #ccc;
|
||||
border-radius: 4px;
|
||||
font-size: 14px;
|
||||
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
|
||||
}
|
||||
#search-results {
|
||||
display: none;
|
||||
background: white;
|
||||
border: 1px solid #ccc;
|
||||
border-top: none;
|
||||
border-radius: 0 0 4px 4px;
|
||||
max-height: 300px;
|
||||
overflow-y: auto;
|
||||
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
|
||||
}
|
||||
.search-result {
|
||||
padding: 8px 12px;
|
||||
cursor: pointer;
|
||||
border-bottom: 1px solid #eee;
|
||||
}
|
||||
.search-result:hover { background: #f0f0f0; }
|
||||
.search-result-name { font-weight: 500; }
|
||||
.search-result-kind { font-size: 11px; color: #888; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
@ -146,6 +183,11 @@
|
|||
|
||||
<div id="status"></div>
|
||||
|
||||
<div id="search-container">
|
||||
<input type="text" id="search-input" placeholder="Search places...">
|
||||
<div id="search-results"></div>
|
||||
</div>
|
||||
|
||||
<div id="context-menu">
|
||||
<div class="coords" id="context-coords"></div>
|
||||
<div class="action" id="context-copy">Copy GPS coordinates</div>
|
||||
|
|
@ -417,40 +459,39 @@
|
|||
var longPressTimer = null;
|
||||
var touchStartPos = null;
|
||||
var canvas = map.getCanvas();
|
||||
canvas.addEventListener("touchstart", function(e) {
|
||||
if (e.touches.length !== 1) return;
|
||||
touchStartPos = {x: e.touches[0].clientX, y: e.touches[0].clientY};
|
||||
longPressTimer = setTimeout(function() {
|
||||
var rect = canvas.getBoundingClientRect();
|
||||
var px = touchStartPos.x - rect.left;
|
||||
var py = touchStartPos.y - rect.top;
|
||||
var lngLat = map.unproject([px, py]);
|
||||
contextMenuData = {lat: lngLat.lat, lon: lngLat.lng};
|
||||
var menu = document.getElementById("context-menu");
|
||||
var z = map.getZoom();
|
||||
var d = z < 4 ? 1 : z < 8 ? 2 : z < 12 ? 3 : 4;
|
||||
document.getElementById("context-coords").textContent =
|
||||
contextMenuData.lat.toFixed(d) + ", " + contextMenuData.lon.toFixed(d);
|
||||
menu.style.left = px + "px";
|
||||
menu.style.top = py + "px";
|
||||
menu.style.display = "block";
|
||||
}, 500);
|
||||
}, {passive: true});
|
||||
canvas.addEventListener("touchmove", function(e) {
|
||||
if (longPressTimer && touchStartPos) {
|
||||
var dx = e.touches[0].clientX - touchStartPos.x;
|
||||
var dy = e.touches[0].clientY - touchStartPos.y;
|
||||
if (Math.abs(dx) > 10 || Math.abs(dy) > 10) {
|
||||
clearTimeout(longPressTimer);
|
||||
longPressTimer = null;
|
||||
canvas.addEventListener("touchstart", function(e) {
|
||||
if (e.touches.length !== 1) return;
|
||||
touchStartPos = {x: e.touches[0].clientX, y: e.touches[0].clientY};
|
||||
longPressTimer = setTimeout(function() {
|
||||
var rect = canvas.getBoundingClientRect();
|
||||
var px = touchStartPos.x - rect.left;
|
||||
var py = touchStartPos.y - rect.top;
|
||||
var lngLat = map.unproject([px, py]);
|
||||
contextMenuData = {lat: lngLat.lat, lon: lngLat.lng};
|
||||
var menu = document.getElementById("context-menu");
|
||||
var z = map.getZoom();
|
||||
var d = z < 4 ? 1 : z < 8 ? 2 : z < 12 ? 3 : 4;
|
||||
document.getElementById("context-coords").textContent =
|
||||
contextMenuData.lat.toFixed(d) + ", " + contextMenuData.lon.toFixed(d);
|
||||
menu.style.left = px + "px";
|
||||
menu.style.top = py + "px";
|
||||
menu.style.display = "block";
|
||||
}, 500);
|
||||
}, {passive: true});
|
||||
canvas.addEventListener("touchmove", function(e) {
|
||||
if (longPressTimer && touchStartPos) {
|
||||
var dx = e.touches[0].clientX - touchStartPos.x;
|
||||
var dy = e.touches[0].clientY - touchStartPos.y;
|
||||
if (Math.abs(dx) > 10 || Math.abs(dy) > 10) {
|
||||
clearTimeout(longPressTimer);
|
||||
longPressTimer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
canvas.addEventListener("touchend", function() {
|
||||
clearTimeout(longPressTimer);
|
||||
longPressTimer = null;
|
||||
});
|
||||
|
||||
});
|
||||
canvas.addEventListener("touchend", function() {
|
||||
clearTimeout(longPressTimer);
|
||||
longPressTimer = null;
|
||||
});
|
||||
map.on("load", function() {
|
||||
buildLayerControls();
|
||||
});
|
||||
|
|
@ -731,6 +772,117 @@
|
|||
if (map) reloadMap();
|
||||
});
|
||||
|
||||
// --- Search ---
|
||||
var searchInput = document.getElementById("search-input");
|
||||
var searchResults = document.getElementById("search-results");
|
||||
var searchTimeout = null;
|
||||
var searchMarker = null;
|
||||
var coordRegex = /^\s*(-?\d+\.?\d*)\s*[,\s]\s*(-?\d+\.?\d*)\s*$/;
|
||||
|
||||
searchInput.addEventListener("input", function() {
|
||||
clearTimeout(searchTimeout);
|
||||
var query = this.value.trim();
|
||||
if (query.length < 2) {
|
||||
searchResults.style.display = "none";
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for coordinate input.
|
||||
var coordMatch = query.match(coordRegex);
|
||||
if (coordMatch) {
|
||||
var cLat = parseFloat(coordMatch[1]);
|
||||
var cLon = parseFloat(coordMatch[2]);
|
||||
if (cLat >= -90 && cLat <= 90 && cLon >= -180 && cLon <= 180) {
|
||||
searchResults.innerHTML = "";
|
||||
var div = document.createElement("div");
|
||||
div.className = "search-result";
|
||||
var nameEl = document.createElement("div");
|
||||
nameEl.className = "search-result-name";
|
||||
nameEl.textContent = cLat + ", " + cLon;
|
||||
div.appendChild(nameEl);
|
||||
var kindEl = document.createElement("div");
|
||||
kindEl.className = "search-result-kind";
|
||||
kindEl.textContent = "coordinates";
|
||||
div.appendChild(kindEl);
|
||||
div.onclick = function() { goToResult(cLat, cLon, cLat + ", " + cLon); };
|
||||
searchResults.appendChild(div);
|
||||
searchResults.style.display = "block";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
searchTimeout = setTimeout(function() {
|
||||
var url = "/api/map/search?q=" + encodeURIComponent(query) + "&limit=8";
|
||||
if (map) {
|
||||
var center = map.getCenter();
|
||||
url += "&lat=" + center.lat + "&lon=" + center.lng;
|
||||
}
|
||||
fetch(url).then(function(r) { return r.json(); }).then(function(data) {
|
||||
searchResults.innerHTML = "";
|
||||
var results = data.results || [];
|
||||
if (results.length > 0) {
|
||||
results.forEach(function(r) {
|
||||
var div = document.createElement("div");
|
||||
div.className = "search-result";
|
||||
var parts = [];
|
||||
if (r.region) parts.push(r.region);
|
||||
var detail = r.kind_detail || r.kind || "";
|
||||
if (detail && !r.region) parts.push(detail.replace(/_/g, " "));
|
||||
if (r.population) {
|
||||
var pop = r.population >= 1000000 ? (r.population / 1000000).toFixed(1) + "M"
|
||||
: r.population >= 1000 ? Math.round(r.population / 1000) + "K"
|
||||
: "" + r.population;
|
||||
parts.push("pop. " + pop);
|
||||
}
|
||||
var subtitle = parts.join(" \u00b7 ");
|
||||
var nameEl = document.createElement("div");
|
||||
nameEl.className = "search-result-name";
|
||||
nameEl.textContent = r.name;
|
||||
div.appendChild(nameEl);
|
||||
if (subtitle) {
|
||||
var kindEl = document.createElement("div");
|
||||
kindEl.className = "search-result-kind";
|
||||
kindEl.textContent = subtitle;
|
||||
div.appendChild(kindEl);
|
||||
}
|
||||
div.onclick = function() { goToResult(r.lat, r.lon, r.name); };
|
||||
searchResults.appendChild(div);
|
||||
});
|
||||
searchResults.style.display = "block";
|
||||
} else {
|
||||
searchResults.style.display = "none";
|
||||
}
|
||||
}).catch(function() { searchResults.style.display = "none"; });
|
||||
}, 300);
|
||||
});
|
||||
|
||||
searchInput.addEventListener("keydown", function(e) {
|
||||
if (e.key === "Escape") {
|
||||
searchResults.style.display = "none";
|
||||
searchInput.blur();
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("click", function(e) {
|
||||
if (!document.getElementById("search-container").contains(e.target)) {
|
||||
searchResults.style.display = "none";
|
||||
}
|
||||
});
|
||||
|
||||
function goToResult(lat, lon, name) {
|
||||
if (!map) return;
|
||||
if (searchMarker) { searchMarker.remove(); searchMarker = null; }
|
||||
var zoom = Math.max(map.getZoom(), 12);
|
||||
map.flyTo({center: [lon, lat], zoom: zoom});
|
||||
searchMarker = new maplibregl.Marker({color: "#6435c9"})
|
||||
.setLngLat([lon, lat])
|
||||
.setPopup(new maplibregl.Popup({offset: 25}).setText(name))
|
||||
.addTo(map);
|
||||
searchMarker.togglePopup();
|
||||
searchResults.style.display = "none";
|
||||
searchInput.value = name;
|
||||
}
|
||||
|
||||
// --- Start ---
|
||||
scanFiles();
|
||||
})();
|
||||
|
|
|
|||
166
modules/map/test/test_search.py
Normal file
166
modules/map/test/test_search.py
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
import sqlite3
|
||||
from http import HTTPStatus
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def _create_test_search_db(path: Path, features: list):
|
||||
"""Create a minimal .search.db file with given features for testing."""
|
||||
conn = sqlite3.connect(str(path))
|
||||
conn.execute('PRAGMA journal_mode=WAL')
|
||||
conn.execute("""
|
||||
CREATE TABLE places (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
kind TEXT,
|
||||
lat REAL NOT NULL,
|
||||
lon REAL NOT NULL,
|
||||
min_zoom REAL,
|
||||
source TEXT,
|
||||
kind_detail TEXT,
|
||||
population INTEGER,
|
||||
wikidata TEXT,
|
||||
region TEXT
|
||||
)
|
||||
""")
|
||||
conn.execute("""
|
||||
CREATE VIRTUAL TABLE places_fts USING fts5(
|
||||
name, kind, content=places, content_rowid=id
|
||||
)
|
||||
""")
|
||||
conn.executemany(
|
||||
'INSERT INTO places (name, kind, lat, lon, min_zoom, source, kind_detail, population, region)'
|
||||
' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
||||
features,
|
||||
)
|
||||
conn.execute("INSERT INTO places_fts(places_fts) VALUES('rebuild')")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def search_db(test_directory):
|
||||
"""Create a test search database with known features."""
|
||||
map_dir = test_directory / 'map'
|
||||
map_dir.mkdir(exist_ok=True)
|
||||
|
||||
features = [
|
||||
('Portland', 'locality', 45.5217, -122.6733, 4.0, 'test', 'city', 652503, 'Oregon'),
|
||||
('Portland', 'locality', 43.6559, -70.2576, 5.0, 'test', 'city', 78408, 'Maine'),
|
||||
('San Francisco', 'locality', 37.7881, -122.4097, 3.0, 'test', 'city', 873965, 'California'),
|
||||
('Springfield', 'locality', 39.7990, -89.6436, 6.0, 'test', 'city', 114230, 'Illinois'),
|
||||
('Springfield', 'locality', 37.2085, -93.2918, 6.0, 'test', 'city', 169176, 'Missouri'),
|
||||
('Yellowstone National Park', 'nature_reserve', 44.6222, -110.5616, 8.0, 'test', '', None, None),
|
||||
]
|
||||
db_path = map_dir / 'test.search.db'
|
||||
_create_test_search_db(db_path, features)
|
||||
return db_path
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_requires_query(async_client, test_directory):
|
||||
"""Search endpoint requires a query parameter."""
|
||||
request, response = await async_client.get('/api/map/search')
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
assert 'error' in response.json
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_empty_query(async_client, test_directory):
|
||||
"""Search endpoint rejects empty query."""
|
||||
request, response = await async_client.get('/api/map/search?q=')
|
||||
assert response.status_code == HTTPStatus.BAD_REQUEST
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_no_indexes(async_client, test_directory):
|
||||
"""Search returns empty results when no search indexes exist."""
|
||||
request, response = await async_client.get('/api/map/search?q=portland')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert response.json['results'] == []
|
||||
assert response.json['total'] == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_returns_results(async_client, test_directory, search_db):
|
||||
"""Search returns matching results from the index."""
|
||||
request, response = await async_client.get('/api/map/search?q=Portland')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
results = response.json['results']
|
||||
assert len(results) >= 1
|
||||
assert response.json['total'] >= 1
|
||||
assert results[0]['name'] == 'Portland'
|
||||
assert 'lat' in results[0]
|
||||
assert 'lon' in results[0]
|
||||
assert 'kind' in results[0]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_with_coordinates(async_client, test_directory, search_db):
|
||||
"""Search with lat/lon returns results sorted by proximity."""
|
||||
# Bias toward Portland, OR (45.5, -122.6)
|
||||
request, response = await async_client.get('/api/map/search?q=Portland&lat=45.5&lon=-122.6')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
results = response.json['results']
|
||||
assert len(results) >= 2
|
||||
# Portland, OR should be first (closest to bias point).
|
||||
assert results[0]['lat'] == pytest.approx(45.5217, abs=0.1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_prefix_matching(async_client, test_directory, search_db):
|
||||
"""Search supports prefix matching."""
|
||||
request, response = await async_client.get('/api/map/search?q=Spring')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
results = response.json['results']
|
||||
assert any(r['name'] == 'Springfield' for r in results)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_status_no_files(async_client, test_directory):
|
||||
"""Status endpoint returns empty when no map files exist."""
|
||||
request, response = await async_client.get('/api/map/search/status')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert response.json['indexed'] == []
|
||||
assert response.json['missing'] == []
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_status_with_index(async_client, test_directory, search_db, make_files_structure):
|
||||
"""Status endpoint shows which files have indexes."""
|
||||
# Create a PMTiles file that matches the search.db stem.
|
||||
make_files_structure(['map/test.pmtiles'])
|
||||
|
||||
request, response = await async_client.get('/api/map/search/status')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['indexed']) == 1
|
||||
assert response.json['indexed'][0]['stem'] == 'test'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_status_missing_index(async_client, test_directory, make_files_structure):
|
||||
"""Status endpoint shows PMTiles files without search indexes."""
|
||||
make_files_structure(['map/usa.pmtiles'])
|
||||
|
||||
request, response = await async_client.get('/api/map/search/status')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['missing']) == 1
|
||||
assert response.json['missing'][0]['stem'] == 'usa'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_pmtiles_also_deletes_search_db(async_client, test_directory, search_db, make_files_structure):
|
||||
"""Deleting a PMTiles file also deletes its companion search.db."""
|
||||
make_files_structure(['map/test.pmtiles'])
|
||||
|
||||
# Both files exist.
|
||||
assert search_db.exists()
|
||||
|
||||
# Delete the PMTiles file.
|
||||
request, response = await async_client.delete('/api/map/files/test.pmtiles')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
|
||||
# Search DB should also be deleted.
|
||||
assert not search_db.exists()
|
||||
Loading…
Add table
Add a link
Reference in a new issue