feat(gps-tracks): GPX download from public track popup
Some checks failed
CI / lint (push) Failing after 5s
CI / test (push) Failing after 6s
CI / build (push) Has been skipped
CI / lint (pull_request) Failing after 4s
CI / test (pull_request) Failing after 5s
CI / build (pull_request) Has been skipped

Реализация ET-011: кнопка «Скачать GPX» в popup публичного GPS-трека и
новый эндпоинт GET /api/gps-tracks/{track_id}/download (GPX 1.1 +
Content-Disposition с UTF-8 именем по RFC 5987). Реэкспорт защищён
per-source флагом `download_allowed` в `config/gps_sources.yaml`
(default-deny, MVP whitelist = `osm`).

Backend:
- `src/api/gps_tracks/export.py` — чистый stdlib-builder GPX 1.1
  (`build_gpx`) + санитизация имени файла (`safe_filename`, RFC 5987).
- `src/api/gps_tracks/endpoint.py` — новый route с проверками
  400 / 403 / 404 / 413; cap 200 000 точек (REQ-NF-02).
- `src/api/gps_tracks/config.py` — `load_download_allowed_sources()`
  читает YAML, default-deny при отсутствии поля; fallback на `{"osm"}`
  при отсутствии конфига.
- `src/api/main.py` — пробрасывает `GPS_SOURCES_CONFIG_PATH` в router.

Frontend:
- `src/web/gps_tracks.js` — кнопка в `_renderTrackPopupHtml`,
  обработчик `_downloadPublicTrack` (fetch + Blob + a.download — тот же
  паттерн, что в `app.js::downloadGPX`, R-1 митигирован), парсер
  `_parseFilenameFromCD` для RFC 5987, маппинг ошибок
  `_handleDownloadError` (403/404/413/5xx → showToast).
- `src/web/app.css` — стиль кнопки, 32×32 CSS px (REQ-NF-04).

Тесты:
- 13 unit для GPX-builder (UT-01/02/03/05; XSD-валидация против
  `tests/fixtures/gpx-1.1/gpx.xsd`).
- 10 unit для `safe_filename` (UT-04).
- 11 integration для download-эндпоинта (IT-01..08 +
  ANY-rule license check + default-deny без конфига).

ADR-014 (gpx-download-endpoint), ADR-015 (source-redistribution-policy).
Refs: ET-011

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-03 20:59:53 +00:00
parent 6fe2ecf12b
commit eea6c846c2
12 changed files with 2284 additions and 8 deletions

View File

@@ -3,6 +3,20 @@
All notable changes to this project will be documented in this file.
Format: [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
## [Unreleased]
### Added
- ET-011: Скачивание GPX из popup публичного трека. Новый эндпоинт
`GET /api/gps-tracks/{track_id}/download` собирает GPX 1.1 из геометрии
трека и отдаёт с `Content-Disposition: attachment` (UTF-8 имя файла по
RFC 5987). В popup на карте появилась кнопка «Скачать GPX» (32×32 CSS px,
mobile-friendly). Реализация: новый модуль `src/api/gps_tracks/export.py`
(`build_gpx`, `safe_filename`); расширение `config/gps_sources.yaml`
per-source флагом `download_allowed` (default-deny; MVP whitelist = `osm`,
см. ADR-015); helper `load_download_allowed_sources` в `config.py`.
Тесты: 13 unit GPX-builder + 10 unit filename + 11 integration download.
ADR-014, ADR-015. Refs: ET-011.
## [v0.0.2] — 2026-06-02
### Added

View File

@@ -10,6 +10,8 @@ sources:
parser_module: "src.api.gps_tracks.sources.osm"
save_user_field: true
external_url_template: "https://www.openstreetmap.org/user/{user}/traces/{external_id_numeric}"
# ET-011 / ADR-015: ODbL разрешает реэкспорт при атрибуции.
download_allowed: true
- id: enduro_russia
name: "EnduroRussia.ru"
@@ -22,6 +24,8 @@ sources:
parser_module: "src.api.gps_tracks.sources.enduro_russia"
save_user_field: false
source_priority: 80
# ET-011 / ADR-015: ToS не содержит явного разрешения на ре-экспорт.
download_allowed: false
- id: wikiloc
name: "Wikiloc"
@@ -36,6 +40,8 @@ sources:
source_priority: 70
activity_filter: [motorcycle, enduro]
max_tracks_per_run: 50
# ET-011 / ADR-015: proprietary, ToS запрещает массовый ре-экспорт.
download_allowed: false
- id: ttrails
name: "Тропинки.ру"
@@ -47,3 +53,5 @@ sources:
attribution: "ttrails.ru"
parser_module: "src.api.gps_tracks.sources.ttrails"
save_user_field: false
# ET-011 / ADR-015: collection-ADR proposed (blocked), реэкспорт запрещён.
download_allowed: false

View File

@@ -1,6 +1,16 @@
"""Загрузка и валидация конфигурации GPS-источников и регионов (ET-008)."""
import logging
import os
import yaml
logger = logging.getLogger(__name__)
# ET-011 / ADR-015: дефолтный whitelist для скачивания, если конфиг недоступен
# (например, в unit-тестах). Совпадает с production-выбором "только OSM".
DEFAULT_DOWNLOAD_ALLOWED_SOURCES = frozenset({"osm"})
def load_sources_config(path: str) -> list:
"""Загружает конфигурацию источников GPS-треков.
@@ -87,3 +97,47 @@ def load_regions_config(path: str) -> list:
)
return regions
def load_download_allowed_sources(path: str | None) -> set[str]:
"""ET-011 / ADR-015: возвращает whitelist source_id с download_allowed=true.
Семантика default-deny: если поле `download_allowed` отсутствует,
источник **не** добавляется в whitelist.
Args:
path: путь к `config/gps_sources.yaml` либо None.
Returns:
set[str] — id источников, для которых разрешено скачивание.
При path=None / отсутствии файла / ошибке парсинга — возвращает
`DEFAULT_DOWNLOAD_ALLOWED_SOURCES` (`{"osm"}`) и логирует WARNING.
"""
if not path:
return set(DEFAULT_DOWNLOAD_ALLOWED_SOURCES)
if not os.path.exists(path):
logger.warning(
"gps_sources config not found at %s; falling back to default "
"download whitelist=%s",
path,
sorted(DEFAULT_DOWNLOAD_ALLOWED_SOURCES),
)
return set(DEFAULT_DOWNLOAD_ALLOWED_SOURCES)
try:
sources = load_sources_config(path)
except (ValueError, OSError) as exc:
logger.warning(
"failed to load gps_sources config from %s (%s); falling back to "
"default download whitelist=%s",
path,
exc,
sorted(DEFAULT_DOWNLOAD_ALLOWED_SOURCES),
)
return set(DEFAULT_DOWNLOAD_ALLOWED_SOURCES)
allowed: set[str] = set()
for src in sources:
# Дефолт `False` — default-deny (ADR-015 §C).
if src.get("download_allowed") is True:
allowed.add(src["id"])
return allowed

View File

@@ -1,13 +1,17 @@
"""FastAPI router для GPS-треков (ET-008)."""
"""FastAPI router для GPS-треков (ET-008, расширен в ET-011)."""
import json
import logging
import os
from typing import Optional
from fastapi import APIRouter, HTTPException, Query, Response
from fastapi import APIRouter, HTTPException, Path, Query, Response
from src.api.gps_tracks.config import load_download_allowed_sources
from src.api.gps_tracks.db import get_tracks_in_bbox, init_db, open_db
from src.api.gps_tracks.export import build_gpx, safe_filename
from src.api.gps_tracks.mvt import (
_gps_tile_cache,
_wkb_to_coords,
build_gps_mvt,
clear_gps_tile_cache,
get_gps_cached_tile,
@@ -15,6 +19,13 @@ from src.api.gps_tracks.mvt import (
_tile_to_bbox,
)
logger = logging.getLogger("uvicorn.access")
# ET-011 / ADR-014:
ALLOWED_DOWNLOAD_FORMATS = {"gpx"}
MAX_POINTS_FOR_DOWNLOAD = 200_000 # REQ-NF-02
GPX_MEDIA_TYPE = "application/gpx+xml; charset=utf-8"
def _parse_bbox(bbox_str: str) -> tuple:
"""Парсит и валидирует bbox строку 'west,south,east,north'.
@@ -52,8 +63,6 @@ def _parse_bbox(bbox_str: str) -> tuple:
def _row_to_geojson_feature(row) -> dict:
"""Конвертирует sqlite3.Row в GeoJSON Feature."""
from src.api.gps_tracks.mvt import _wkb_to_coords
coords = _wkb_to_coords(row["geom"])
sources = json.loads(row["sources_json"] or "[]")
@@ -94,17 +103,29 @@ def _row_to_geojson_feature(row) -> dict:
}
def create_gps_router(db_path: str) -> APIRouter:
def create_gps_router(
db_path: str,
sources_config_path: Optional[str] = None,
) -> APIRouter:
"""Создаёт FastAPI router для GPS-треков.
Args:
db_path: путь к SQLite БД для GPS-треков
db_path: путь к SQLite БД для GPS-треков.
sources_config_path: путь к ``config/gps_sources.yaml``.
Если None — для ET-011 download-эндпоинта используется
default-deny whitelist ``{"osm"}`` (см. ADR-015).
Returns:
APIRouter с prefix="/api/gps-tracks"
"""
router = APIRouter(prefix="/api/gps-tracks", tags=["gps-tracks"])
# ET-011 / ADR-015: whitelist source_id, для которых разрешено
# скачивание GPX. Читается один раз при старте router'а.
allowed_download_sources: set[str] = load_download_allowed_sources(
sources_config_path
)
def _get_conn():
conn = open_db(db_path)
init_db(conn)
@@ -307,4 +328,110 @@ def create_gps_router(db_path: str) -> APIRouter:
clear_gps_tile_cache()
return {"status": "ok", "cleared": True}
# ─── ET-011: скачивание GPX из popup ──────────────────────────
@router.get("/{track_id}/download")
async def download_track(
track_id: int = Path(..., ge=1),
format: str = Query("gpx", description="Формат файла (только 'gpx' в MVP)"),
):
"""Отдаёт GPX-файл для трека с правильным Content-Disposition.
Реализует ADR-014 / ADR-015 для ET-011.
Порядок проверок (ADR-014 §H):
1. format ∈ whitelist (иначе 400).
2. SELECT по id (иначе 404).
3. points_count <= MAX (иначе 413).
4. licence policy по sources (иначе 403).
5. Сборка GPX → 200.
"""
if format not in ALLOWED_DOWNLOAD_FORMATS:
raise HTTPException(
status_code=400,
detail="unsupported_format",
)
try:
conn = _get_conn()
cur = conn.cursor()
cur.execute(
"""
SELECT id, name, description, activity_type, user, created_at,
length_m, points_count, geom, sources_json,
external_urls_json
FROM tracks
WHERE id = ?
""",
(track_id,),
)
row = cur.fetchone()
conn.close()
except Exception as exc:
raise HTTPException(500, f"DB error: {exc}")
if row is None:
raise HTTPException(status_code=404, detail="track_not_found")
points_count = row["points_count"] or 0
if points_count > MAX_POINTS_FOR_DOWNLOAD:
raise HTTPException(status_code=413, detail="track_too_large")
sources = json.loads(row["sources_json"] or "[]")
external_urls = json.loads(row["external_urls_json"] or "[]")
# ADR-015 §B1: разрешение по принципу ANY — хотя бы один разрешённый.
if not any(s in allowed_download_sources for s in sources):
raise HTTPException(
status_code=403,
detail={
"detail": "source_forbidden",
"external_urls": external_urls,
},
)
coords = _wkb_to_coords(row["geom"]) or []
try:
xml_str = build_gpx(
track_id=row["id"],
name=row["name"],
description=row["description"],
activity_type=row["activity_type"],
user=row["user"],
created_at=row["created_at"],
sources=sources,
external_urls=external_urls,
coords=coords,
)
except Exception as exc:
logger.exception("build_gpx failed for track_id=%s", track_id)
raise HTTPException(500, f"GPX build error: {exc}")
ascii_name, utf8_quoted = safe_filename(row["name"], track_id)
content_disposition = (
f'attachment; filename="{ascii_name}.gpx"; '
f"filename*=UTF-8''{utf8_quoted}.gpx"
)
xml_bytes = xml_str.encode("utf-8")
# REQ-F-07: лёгкое журналирование успешной отдачи.
logger.info(
"track_download id=%d sources=%s size_bytes=%d",
track_id,
",".join(sources) if sources else "",
len(xml_bytes),
)
return Response(
content=xml_bytes,
media_type=GPX_MEDIA_TYPE,
headers={
"Content-Disposition": content_disposition,
"Cache-Control": "private, max-age=3600",
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "Content-Disposition",
},
)
return router

View File

@@ -0,0 +1,265 @@
"""GPX-экспорт публичных GPS-треков (ET-011, ADR-014).
Сборка GPX 1.1 из метаданных трека + санитизация имени файла для
HTTP Content-Disposition с поддержкой RFC 5987 (UTF-8 filename*).
Чистый stdlib-модуль, без I/O — легко тестируется юнитами.
"""
from __future__ import annotations
import urllib.parse
import xml.etree.ElementTree as ET
from datetime import datetime, timezone
# OSM-license URL для блока <copyright> (ADR-014 §G, ODbL).
_OSM_LICENSE_URL = "https://www.openstreetmap.org/copyright"
# Запрещённые в FAT/NTFS символы (ADR-014 §F.2).
_FORBIDDEN_NAME_CHARS = set('/\\:*?"<>|')
# Лимит длины ASCII-fallback по байтам UTF-8 (ADR-014 §F.5; RFC 5987 — 254
# на параметр, минус префикс "filename*=UTF-8''" и расширение).
_MAX_NAME_BYTES = 80
def _format_utc(iso_str: str | None) -> str | None:
"""Нормализует ISO-8601 datetime → 'YYYY-MM-DDTHH:MM:SSZ' (UTC).
Поддерживает входные строки с/без таймзоны. None / нераспарсимое — None.
"""
if not iso_str:
return None
try:
# Python 3.11+ fromisoformat понимает 'Z'-суффикс; для надёжности
# делаем явную замену.
normalized = iso_str.replace("Z", "+00:00")
dt = datetime.fromisoformat(normalized)
except (ValueError, TypeError):
return None
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
else:
dt = dt.astimezone(timezone.utc)
return dt.strftime("%Y-%m-%dT%H:%M:%SZ")
def build_gpx(
*,
track_id: int,
name: str | None,
description: str | None,
activity_type: str | None,
user: str | None,
created_at: str | None,
sources: list[str],
external_urls: list[str],
coords: list[tuple[float, float]],
) -> str:
"""Собирает GPX 1.1 как XML-строку (с XML-declaration).
Args:
track_id: id трека (используется только в fallback-имени).
name: tracks.name (если пусто — в `<name>` ставится «Без названия»).
description: tracks.description (если пусто — `<desc>` опускается).
activity_type: tracks.activity_type, попадает в `<trk><type>`.
user: tracks.user — попадает в `<metadata><author><name>`.
created_at: ISO-8601 строка → нормализуется в UTC `<metadata><time>`.
sources: список source_id (для `<copyright>` и `<link><text>`).
external_urls: список внешних URL → `<metadata><link>` по одному.
coords: список (lon, lat) — точки трека.
Returns:
XML-строка (включает `<?xml …?>`-декларацию).
"""
# GPX namespace должен быть default — иначе ET создаёт префикс ns0:gpx.
gpx_ns = "http://www.topografix.com/GPX/1/1"
xsi_ns = "http://www.w3.org/2001/XMLSchema-instance"
ET.register_namespace("", gpx_ns)
ET.register_namespace("xsi", xsi_ns)
root = ET.Element(
f"{{{gpx_ns}}}gpx",
{
"version": "1.1",
"creator": "Enduro Trails",
f"{{{xsi_ns}}}schemaLocation": (
"http://www.topografix.com/GPX/1/1 "
"http://www.topografix.com/GPX/1/1/gpx.xsd"
),
},
)
# ─── <metadata> ───────────────────────────────────────────────
# Порядок дочерних элементов фиксирован XSD-схемой GPX 1.1:
# name, desc, author, copyright, link*, time, keywords, bounds, extensions.
# Любое отклонение → DocumentInvalid (см. UT-03).
metadata = ET.SubElement(root, f"{{{gpx_ns}}}metadata")
meta_name = ET.SubElement(metadata, f"{{{gpx_ns}}}name")
meta_name.text = (name or "").strip() or "Без названия"
desc_clean = (description or "").strip()
if desc_clean:
desc_el = ET.SubElement(metadata, f"{{{gpx_ns}}}desc")
desc_el.text = desc_clean
user_clean = (user or "").strip()
if user_clean:
author_el = ET.SubElement(metadata, f"{{{gpx_ns}}}author")
author_name = ET.SubElement(author_el, f"{{{gpx_ns}}}name")
author_name.text = user_clean
# <copyright>: OSM → официальная ODbL-ссылка (ADR-014 §G).
# Для не-OSM источников: license = первый external_url (если есть),
# иначе блок опускаем.
sources_list = list(sources or [])
if "osm" in sources_list:
cr_el = ET.SubElement(
metadata, f"{{{gpx_ns}}}copyright", {"author": "Enduro Trails"}
)
lic_el = ET.SubElement(cr_el, f"{{{gpx_ns}}}license")
lic_el.text = _OSM_LICENSE_URL
elif external_urls:
first_url = next((u for u in external_urls if u), None)
if first_url:
cr_el = ET.SubElement(
metadata, f"{{{gpx_ns}}}copyright", {"author": "Enduro Trails"}
)
lic_el = ET.SubElement(cr_el, f"{{{gpx_ns}}}license")
lic_el.text = first_url
# <link> на каждый external_url; <text> = "Источник: <source_id>".
# ADR-014 §G: по одному `<link>` на каждый элемент external_urls.
src_for_link = list(sources or [])
for idx, url in enumerate(external_urls or []):
if not url:
continue
link_el = ET.SubElement(metadata, f"{{{gpx_ns}}}link", {"href": url})
text_el = ET.SubElement(link_el, f"{{{gpx_ns}}}text")
src_label = src_for_link[idx] if idx < len(src_for_link) else (
src_for_link[0] if src_for_link else ""
)
text_el.text = (
f"Источник: {src_label}" if src_label else "Источник"
)
time_str = _format_utc(created_at)
if time_str:
time_el = ET.SubElement(metadata, f"{{{gpx_ns}}}time")
time_el.text = time_str
# ─── <trk> ────────────────────────────────────────────────────
trk = ET.SubElement(root, f"{{{gpx_ns}}}trk")
trk_name = ET.SubElement(trk, f"{{{gpx_ns}}}name")
trk_name.text = (name or "").strip() or "Без названия"
act_clean = (activity_type or "").strip()
if act_clean:
trk_type = ET.SubElement(trk, f"{{{gpx_ns}}}type")
trk_type.text = act_clean
trkseg = ET.SubElement(trk, f"{{{gpx_ns}}}trkseg")
# Координаты приходят как (lon, lat) из _wkb_to_coords (см. mvt.py).
# GPX: lat/lon атрибуты с фиксированной точностью 6 знаков
# (~0.11 м, ADR-014 §G).
for lon, lat in coords or []:
ET.SubElement(
trkseg,
f"{{{gpx_ns}}}trkpt",
{"lat": f"{lat:.6f}", "lon": f"{lon:.6f}"},
)
# ET.tostring с xml_declaration=True даёт нужный prolog.
xml_bytes = ET.tostring(
root,
encoding="utf-8",
xml_declaration=True,
)
return xml_bytes.decode("utf-8")
def _sanitize_for_filesystem(name: str) -> str:
"""Заменяет запрещённые / управляющие символы на '_'.
Затем триммит пробелы и точки по краям (Windows-нюанс).
"""
out_chars: list[str] = []
for ch in name:
code = ord(ch)
if ch in _FORBIDDEN_NAME_CHARS:
out_chars.append("_")
elif code < 0x20 or code == 0x7F:
out_chars.append("_")
else:
out_chars.append(ch)
return "".join(out_chars).strip(" .")
def _truncate_utf8(name: str, max_bytes: int) -> str:
"""Триммит строку так, чтобы её UTF-8-длина не превышала max_bytes."""
encoded = name.encode("utf-8")
if len(encoded) <= max_bytes:
return name
# Декодируем с ignore, чтобы не обрезать середину code-point'а.
return encoded[:max_bytes].decode("utf-8", errors="ignore")
def _ascii_fallback(name: str) -> str:
"""ASCII-fallback для параметра `filename=` (без `*`).
ADR-014 §F.7: транслитерации **не делаем**; non-ASCII / non-printable
символы заменяем на '_'. Если результат пуст — caller подставит
'track-<id>'.
"""
out: list[str] = []
for ch in name:
code = ord(ch)
# 0x20..0x7E — printable ASCII, исключая запрещённые ФС-символы
# (они уже подменены в _sanitize_for_filesystem, но на всякий случай).
if 0x20 <= code <= 0x7E and ch not in _FORBIDDEN_NAME_CHARS:
out.append(ch)
else:
out.append("_")
return "".join(out).strip(" .")
def safe_filename(name: str | None, track_id: int) -> tuple[str, str]:
"""Возвращает (ascii_fallback, utf8_percent_quoted) без расширения.
Алгоритм (ADR-014 §F):
1. Пустой/None → 'track-<id>'.
2. Запрещённые / управляющие символы → '_'.
3. Триммим пробелы и точки.
4. Триммим до 80 байт UTF-8.
5. Пустой результат → 'track-<id>'.
6. ASCII-fallback: только printable ASCII; non-ASCII → '_'.
7. UTF-8 quoted: urllib.parse.quote(name, safe='', encoding='utf-8').
Args:
name: исходное имя (tracks.name) — может быть None / пустым.
track_id: id трека для fallback-имени.
Returns:
Кортеж (ascii_fallback, utf8_percent_quoted). Оба без расширения.
"""
fallback = f"track-{track_id}"
raw = (name or "").strip()
if not raw:
return fallback, urllib.parse.quote(fallback, safe="", encoding="utf-8")
sanitized = _sanitize_for_filesystem(raw)
if not sanitized:
return fallback, urllib.parse.quote(fallback, safe="", encoding="utf-8")
truncated = _truncate_utf8(sanitized, _MAX_NAME_BYTES).strip(" .")
if not truncated:
return fallback, urllib.parse.quote(fallback, safe="", encoding="utf-8")
utf8_quoted = urllib.parse.quote(truncated, safe="", encoding="utf-8")
ascii_ok = _ascii_fallback(truncated)
if not ascii_ok:
ascii_ok = fallback
return ascii_ok, utf8_quoted

View File

@@ -1252,7 +1252,14 @@ async def terrain_tile(layer: str, z: int, x: int, y: int):
# ─── Static files ─────────────────────────────────────────────────────────────
from src.api.gps_tracks.endpoint import create_gps_router
gps_router = create_gps_router(GPS_TRACKS_DB_PATH)
# ET-011 / ADR-015: путь к config/gps_sources.yaml — содержит per-source
# флаг `download_allowed`, который router читает один раз при старте.
GPS_SOURCES_CONFIG_PATH = os.environ.get(
"GPS_SOURCES_CONFIG_PATH",
os.path.join(os.path.dirname(__file__), "../../config/gps_sources.yaml"),
)
gps_router = create_gps_router(GPS_TRACKS_DB_PATH, GPS_SOURCES_CONFIG_PATH)
app.include_router(gps_router)
if os.path.exists(STATIC_DIR):

View File

@@ -1300,3 +1300,44 @@ body.satellite-active #btn-basemap {
.track-popup-sources a:hover {
text-decoration: underline;
}
/* ET-011: кнопка «Скачать GPX» в popup публичного трека (REQ-NF-04) */
.track-popup-actions {
margin-top: 8px;
display: flex;
gap: 8px;
}
.track-popup-download-btn {
display: inline-flex;
align-items: center;
justify-content: center;
width: 32px;
height: 32px;
border: none;
border-radius: 6px;
cursor: pointer;
background: var(--accent, #ff8c1a);
color: #fff;
padding: 0;
transition: opacity 0.15s ease;
}
.track-popup-download-btn:hover {
opacity: 0.9;
}
.track-popup-download-btn:focus {
outline: 2px solid var(--accent, #ff8c1a);
outline-offset: 2px;
}
.track-popup-download-btn svg {
width: 18px;
height: 18px;
}
.track-popup-download-btn.is-loading {
opacity: 0.6;
pointer-events: none;
}

View File

@@ -460,6 +460,10 @@ async function fetchAndUpdateGpsGeoJson(bounds) {
// ─── Popup при клике ──────────────────────────────────────────────
// ET-011: SVG-иконка «download», копия из index.html sheet-route::downloadGPX
// (см. ADR-014 §3.a). Inline-SVG, чтобы popup не зависел от внешнего ассета.
const _GPS_DOWNLOAD_ICON_SVG = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>';
function _renderTrackPopupHtml(props) {
const name = props.name || 'Без названия';
const activity = props.activity_type || props.activity || 'other';
@@ -488,6 +492,22 @@ function _renderTrackPopupHtml(props) {
}
} catch(e) {}
// ET-011 / REQ-F-01: кнопка «Скачать» в popup публичного трека.
// Безопасно используем числовой id (FastAPI Path int ge=1 на сервере),
// но всё равно делаем явный Number() — на случай, если MVT отдал строку.
const trackId = Number(props.id);
const actionsHtml = Number.isFinite(trackId) && trackId > 0
? `<div class="track-popup-actions">
<button type="button"
class="track-popup-download-btn"
aria-label="Скачать GPX"
title="Скачать GPX"
data-track-id="${trackId}">
${_GPS_DOWNLOAD_ICON_SVG}
</button>
</div>`
: '';
return `
<div class="track-popup">
<div class="track-popup-name">${name}</div>
@@ -495,11 +515,109 @@ function _renderTrackPopupHtml(props) {
<div class="track-popup-row">📏 ${lengthKm} км · ${points} точек</div>
${dateStr ? `<div class="track-popup-row">📅 ${dateStr}</div>` : ''}
${user ? `<div class="track-popup-row">👤 ${user}</div>` : ''}
${actionsHtml}
${sourcesHtml}
</div>
`;
}
// ─── ET-011: Скачивание GPX из popup ─────────────────────────────
/**
* ET-011 (ADR-014 §3): парсит заголовок Content-Disposition и возвращает имя
* файла. Приоритет — `filename*=UTF-8''<percent-encoded>` (RFC 5987);
* fallback — `filename="…"`; при отсутствии обоих — null.
*
* @param {string|null} cd
* @returns {string|null}
*/
function _parseFilenameFromCD(cd) {
if (!cd) return null;
// RFC 5987: filename*=UTF-8''<encoded>
const ext = cd.match(/filename\*=UTF-8''([^;]+)/i);
if (ext && ext[1]) {
try {
return decodeURIComponent(ext[1].trim());
} catch (_) {
// битый percent-encoding — упадём в обычный filename
}
}
const plain = cd.match(/filename="([^"]+)"/i) || cd.match(/filename=([^;]+)/i);
if (plain && plain[1]) return plain[1].trim();
return null;
}
/**
* ET-011 (ADR-014 §3.b): человекочитаемое сообщение по HTTP-статусу.
*
* @param {number} status
* @param {object} body уже распарсенный JSON ответа (может быть пустым)
*/
function _handleDownloadError(status, body) {
if (typeof showToast !== 'function') return;
if (status === 403) {
// ADR-015 §G: backend кладёт external_urls в detail.
const urls = (body && body.detail && body.detail.external_urls)
|| (body && body.external_urls);
const firstUrl = Array.isArray(urls) && urls.length ? urls[0] : null;
if (firstUrl) {
showToast(`Источник запрещает скачивание. Откройте трек на сайте источника: ${firstUrl}`);
} else {
showToast('Источник запрещает скачивание. Откройте трек на сайте источника.');
}
} else if (status === 404) {
showToast('Трек не найден.');
} else if (status === 413) {
showToast('Трек слишком большой для скачивания.');
} else if (status === 400) {
showToast('Неподдерживаемый формат файла.');
} else {
showToast('Не удалось скачать. Попробуйте ещё раз.');
}
}
/**
* ET-011: скачивает GPX для трека с публичного слоя.
* Использует тот же паттерн (fetch → Blob → URL.createObjectURL → a.download),
* что и app.js::downloadGPX(), — он уже отлажен на iOS Safari (BRD R-1).
*
* @param {number|string} trackId
* @param {HTMLElement|null} btnEl кнопка, на которой показываем индикатор
*/
async function _downloadPublicTrack(trackId, btnEl) {
if (btnEl) btnEl.classList.add('is-loading');
const basePath = window.location.pathname.replace(/\/[^/]*$/, '') || '';
const url = `${basePath}/api/gps-tracks/${encodeURIComponent(trackId)}/download`;
try {
const resp = await fetch(url);
if (!resp.ok) {
let body = {};
try { body = await resp.json(); } catch (_) {}
_handleDownloadError(resp.status, body);
return;
}
const blob = await resp.blob();
const filename = _parseFilenameFromCD(resp.headers.get('Content-Disposition'))
|| `track-${trackId}.gpx`;
const objectUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = objectUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
// Освобождаем blob чуть позже — Safari иногда отменяет скачивание,
// если revoke сработал синхронно с click().
setTimeout(() => URL.revokeObjectURL(objectUrl), 1000);
} catch (err) {
if (typeof showToast === 'function') {
showToast('Не удалось скачать. Попробуйте ещё раз.');
}
} finally {
if (btnEl) btnEl.classList.remove('is-loading');
}
}
function _setupGpsClickHandler(map) {
const layerIds = [window.gpsTracksLayer.layerId, window.gpsTracksLayer.layerGeoId];
@@ -511,10 +629,26 @@ function _setupGpsClickHandler(map) {
const feature = e.features && e.features[0];
if (!feature) return;
new maplibregl.Popup({ closeOnClick: true, maxWidth: '300px' })
const popup = new maplibregl.Popup({ closeOnClick: true, maxWidth: '300px' })
.setLngLat(e.lngLat)
.setHTML(_renderTrackPopupHtml(feature.properties))
.addTo(map);
// ET-011 / ADR-014 §3.b: делегированный обработчик клика на
// кнопку «Скачать». Popup в проекте перерисовывается при каждом
// открытии, так что листенер живёт ровно столько, сколько popup.
const popupEl = popup.getElement && popup.getElement();
if (popupEl) {
popupEl.addEventListener('click', (ev) => {
const btn = ev.target.closest && ev.target.closest('.track-popup-download-btn');
if (!btn) return;
ev.preventDefault();
ev.stopPropagation();
const tid = btn.getAttribute('data-track-id');
if (!tid) return;
_downloadPublicTrack(tid, btn);
});
}
});
map.on('mouseenter', layerId, () => { map.getCanvas().style.cursor = 'pointer'; });

View File

@@ -0,0 +1,412 @@
"""Integration-тесты ET-011 download-эндпоинта.
Покрывает test-plan: IT-01..IT-07 (+ IT-05 license-фильтр).
"""
from __future__ import annotations
import json
import os
import sqlite3
import urllib.parse
import pytest
from fastapi import FastAPI
from httpx import ASGITransport, AsyncClient
from lxml import etree as lxml_et
from shapely import wkb as shp_wkb
from shapely.geometry import LineString
from src.api.gps_tracks.db import init_db, open_db, upsert_track
from src.api.gps_tracks.dedup import compute_dedup_key
from src.api.gps_tracks.endpoint import create_gps_router
from src.api.gps_tracks.models import TrackInsert
GPX_NS = "http://www.topografix.com/GPX/1/1"
_FIXTURES_DIR = os.path.join(
os.path.dirname(__file__), "..", "fixtures", "gpx-1.1"
)
_GPX_XSD_PATH = os.path.abspath(os.path.join(_FIXTURES_DIR, "gpx.xsd"))
# ─── Helpers ──────────────────────────────────────────────────────────────
def _make_track(
*,
external_id: str = "T1",
source_id: str = "osm",
name: str | None = "Test trail",
description: str | None = None,
activity_type: str | None = "enduro",
user: str | None = None,
created_at: str | None = "2024-05-12T10:00:00Z",
n_points: int = 10,
length_m: float = 5000.0,
external_url: str | None = "https://www.openstreetmap.org/way/1",
source_priority: int = 50,
base_lon: float = 37.60,
base_lat: float = 55.74,
) -> TrackInsert:
"""Создаёт TrackInsert с реальной WKB-геометрией."""
coords = [(base_lon + i * 0.001, base_lat + i * 0.001) for i in range(n_points)]
line = LineString(coords)
geom_wkb = shp_wkb.dumps(line)
min_lon = min(c[0] for c in coords)
max_lon = max(c[0] for c in coords)
min_lat = min(c[1] for c in coords)
max_lat = max(c[1] for c in coords)
return TrackInsert(
external_id=external_id,
source_id=source_id,
external_url=external_url,
name=name,
description=description,
activity_type=activity_type,
user=user,
created_at=created_at,
length_m=length_m,
points_count=n_points,
geom_wkb=geom_wkb,
min_lon=min_lon,
min_lat=min_lat,
max_lon=max_lon,
max_lat=max_lat,
tags=[],
source_priority=source_priority,
)
def _insert_track(conn: sqlite3.Connection, track: TrackInsert) -> int:
"""Вставляет трек и возвращает его id."""
dedup = compute_dedup_key(
(track.min_lon, track.min_lat, track.max_lon, track.max_lat),
{"length_m": track.length_m, "created_at": track.created_at},
)
upsert_track(conn, track, dedup, source_priority=track.source_priority or 50)
cur = conn.cursor()
cur.execute("SELECT id FROM tracks WHERE dedup_key = ?", (dedup,))
return cur.fetchone()["id"]
def _make_app(db_path: str, sources_config_path: str | None = None) -> FastAPI:
app = FastAPI()
router = create_gps_router(db_path, sources_config_path)
app.include_router(router)
return app
def _config_with(sources: dict[str, bool], tmp_path) -> str:
"""Создаёт временный gps_sources.yaml с заданными download_allowed."""
lines = ["sources:"]
for sid, allowed in sources.items():
lines.append(f" - id: {sid}")
lines.append(f" name: \"{sid}\"")
lines.append(" enabled: true")
lines.append(f" license_adr: \"docs/test-{sid}.md\"")
lines.append(f" base_url: \"https://example.com/{sid}\"")
lines.append(f" download_allowed: {'true' if allowed else 'false'}")
path = tmp_path / "gps_sources.yaml"
path.write_text("\n".join(lines), encoding="utf-8")
return str(path)
@pytest.fixture
def osm_db(tmp_path):
"""БД с одним OSM-треком из 10 точек."""
db_path = str(tmp_path / "osm.sqlite")
conn = open_db(db_path)
init_db(conn)
track_id = _insert_track(conn, _make_track())
conn.close()
return db_path, track_id
@pytest.fixture
def osm_db_app(osm_db, tmp_path):
"""App, где OSM разрешён для скачивания (default)."""
db_path, track_id = osm_db
cfg = _config_with({"osm": True, "wikiloc": False}, tmp_path)
return _make_app(db_path, cfg), track_id
# ─── IT-01: happy path ─────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_it01_download_happy_path(osm_db_app):
"""IT-01: GET /api/gps-tracks/{id}/download → 200 + правильные хедеры."""
app, track_id = osm_db_app
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(f"/api/gps-tracks/{track_id}/download")
assert resp.status_code == 200
assert resp.headers["content-type"].startswith("application/gpx+xml")
cd = resp.headers["content-disposition"]
assert "attachment" in cd
assert "filename*=UTF-8''" in cd
assert resp.text.startswith("<?xml")
assert "<gpx" in resp.text
assert 'version="1.1"' in resp.text
assert resp.text.count("<trkpt") == 10
# ─── IT-02: 404 ────────────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_it02_track_not_found(osm_db_app):
app, _ = osm_db_app
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get("/api/gps-tracks/99999999/download")
assert resp.status_code == 404
detail = resp.json().get("detail", "")
assert "not_found" in str(detail).lower() or "track_not_found" in str(detail)
# ─── IT-03: невалидный format ──────────────────────────────────────────────
@pytest.mark.asyncio
async def test_it03_invalid_format(osm_db_app):
app, track_id = osm_db_app
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(
f"/api/gps-tracks/{track_id}/download", params={"format": "fit"}
)
assert resp.status_code == 400
@pytest.mark.asyncio
async def test_it03_explicit_gpx_format_ok(osm_db_app):
"""format=gpx синонимично default'у."""
app, track_id = osm_db_app
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(
f"/api/gps-tracks/{track_id}/download", params={"format": "gpx"}
)
assert resp.status_code == 200
# ─── IT-04: 413 patho-трек ─────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_it04_track_too_large(tmp_path):
"""IT-04: points_count > 200000 → 413 (без чтения geom)."""
db_path = str(tmp_path / "huge.sqlite")
conn = open_db(db_path)
init_db(conn)
# Используем upsert обычным путём, потом подменим points_count
track_id = _insert_track(conn, _make_track(name="Huge"))
cur = conn.cursor()
cur.execute("UPDATE tracks SET points_count = 300000 WHERE id = ?", (track_id,))
conn.commit()
conn.close()
cfg = _config_with({"osm": True}, tmp_path)
app = _make_app(db_path, cfg)
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(f"/api/gps-tracks/{track_id}/download")
assert resp.status_code == 413
# ─── IT-05: license-фильтр (403) ───────────────────────────────────────────
@pytest.mark.asyncio
async def test_it05_source_forbidden_403(tmp_path):
"""IT-05: трек только с wikiloc → 403 если wikiloc нет в whitelist."""
db_path = str(tmp_path / "wikiloc.sqlite")
conn = open_db(db_path)
init_db(conn)
track_id = _insert_track(
conn,
_make_track(
external_id="W1",
source_id="wikiloc",
external_url="https://www.wikiloc.com/abc",
),
)
conn.close()
# Whitelist только osm → wikiloc запрещён
cfg = _config_with({"osm": True, "wikiloc": False}, tmp_path)
app = _make_app(db_path, cfg)
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(f"/api/gps-tracks/{track_id}/download")
assert resp.status_code == 403
body = resp.json()
# FastAPI обёртывает наш detail-dict в {"detail": {...}}.
detail = body.get("detail", body)
if isinstance(detail, dict):
assert detail.get("detail") == "source_forbidden"
assert detail.get("external_urls") == ["https://www.wikiloc.com/abc"]
else:
# Если FastAPI отдал просто строку — должно содержать source_forbidden
assert "source_forbidden" in str(detail)
@pytest.mark.asyncio
async def test_it05_dual_source_with_osm_passes(tmp_path):
"""ADR-015 §B1: ANY-rule — трек с sources=[osm, wikiloc] разрешён."""
db_path = str(tmp_path / "dual.sqlite")
conn = open_db(db_path)
init_db(conn)
# Создаём трек один раз как osm, затем upsert-мерж с wikiloc
t1 = _make_track(
external_id="X1", source_id="osm",
external_url="https://www.openstreetmap.org/way/1",
)
t2 = _make_track(
external_id="X1", source_id="wikiloc",
external_url="https://www.wikiloc.com/x",
source_priority=70,
)
_insert_track(conn, t1)
track_id = _insert_track(conn, t2)
# Проверяем, что записалось два source'а
cur = conn.cursor()
cur.execute("SELECT sources_json FROM tracks WHERE id = ?", (track_id,))
sources = json.loads(cur.fetchone()["sources_json"])
assert "osm" in sources and "wikiloc" in sources
conn.close()
cfg = _config_with({"osm": True, "wikiloc": False}, tmp_path)
app = _make_app(db_path, cfg)
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(f"/api/gps-tracks/{track_id}/download")
assert resp.status_code == 200
# ─── IT-06: UTF-8 имя ──────────────────────────────────────────────────────
@pytest.mark.asyncio
async def test_it06_utf8_filename_in_cd(tmp_path):
db_path = str(tmp_path / "ru.sqlite")
conn = open_db(db_path)
init_db(conn)
track_id = _insert_track(
conn,
_make_track(name="По грязи к Чёрному озеру"),
)
conn.close()
cfg = _config_with({"osm": True}, tmp_path)
app = _make_app(db_path, cfg)
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(f"/api/gps-tracks/{track_id}/download")
assert resp.status_code == 200
cd = resp.headers["content-disposition"]
assert "filename*=UTF-8''" in cd
# Декодируем RFC 5987 часть
star = cd.split("filename*=UTF-8''", 1)[1]
encoded = star.split(";", 1)[0].strip()
decoded = urllib.parse.unquote(encoded, encoding="utf-8")
assert decoded == "По грязи к Чёрному озеру.gpx"
# ASCII fallback — без кириллицы (проверим, что filename="..." есть)
assert 'filename="' in cd
plain = cd.split('filename="', 1)[1].split('"', 1)[0]
assert all(ord(c) < 128 for c in plain)
# ─── IT-07: валидация GPX по XSD ───────────────────────────────────────────
@pytest.mark.asyncio
async def test_it07_response_validates_against_xsd(osm_db_app):
if not os.path.exists(_GPX_XSD_PATH):
pytest.skip("GPX XSD not present")
app, track_id = osm_db_app
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp = await client.get(f"/api/gps-tracks/{track_id}/download")
assert resp.status_code == 200
schema = lxml_et.XMLSchema(lxml_et.parse(_GPX_XSD_PATH))
doc = lxml_et.fromstring(resp.content)
schema.assertValid(doc)
# ─── IT-08: регрессия — существующие эндпоинты живы ───────────────────────
@pytest.mark.asyncio
async def test_it08_existing_endpoints_smoke(osm_db_app):
"""IT-08: добавление download не сломало /api/gps-tracks и /health."""
app, _ = osm_db_app
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
resp_bbox = await client.get(
"/api/gps-tracks", params={"bbox": "37.5,55.7,37.9,55.9"}
)
resp_health = await client.get("/api/gps-tracks/health")
assert resp_bbox.status_code == 200
body = resp_bbox.json()
assert body["type"] == "FeatureCollection"
assert isinstance(body["features"], list)
assert resp_health.status_code == 200
health = resp_health.json()
assert health["status"] == "ok"
assert "tracks_total" in health
# ─── Дополнительно: проверка default-deny при отсутствии конфига ──────────
@pytest.mark.asyncio
async def test_default_deny_without_config(tmp_path):
"""Без sources_config_path whitelist = {osm} (см. ADR-015 §F)."""
db_path = str(tmp_path / "noconfig.sqlite")
conn = open_db(db_path)
init_db(conn)
# OSM трек — должен пройти даже без конфига
osm_id = _insert_track(conn, _make_track(source_id="osm"))
# Wikiloc трек в другом регионе — должен быть отдельной записью с другим
# dedup_key и запрещён к скачиванию.
wiki_id = _insert_track(
conn,
_make_track(
external_id="W1",
source_id="wikiloc",
external_url="https://www.wikiloc.com/y",
base_lon=40.0,
base_lat=50.0,
created_at="2025-01-01T00:00:00Z",
length_m=8888.0,
),
)
conn.close()
# Sanity: треки должны быть разными записями
assert osm_id != wiki_id, (
"test setup: tracks merged into one record via dedup_key"
)
app = _make_app(db_path, sources_config_path=None)
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
r_osm = await client.get(f"/api/gps-tracks/{osm_id}/download")
r_wiki = await client.get(f"/api/gps-tracks/{wiki_id}/download")
assert r_osm.status_code == 200
assert r_wiki.status_code == 403

View File

@@ -0,0 +1,95 @@
"""Unit-тесты для ET-011 sanitize/safe_filename (UT-04, REQ-F-04)."""
from __future__ import annotations
import urllib.parse
from src.api.gps_tracks.export import safe_filename
def test_ut04_cyrillic_utf8():
"""UT-04: кириллическое имя → ascii-fallback пустой и читается из utf-8."""
name = "По грязи к Чёрному озеру"
ascii_fb, utf8_quoted = safe_filename(name, 42)
# ascii_fallback содержит только ASCII (после санитизации
# нелатинские символы стали '_'), длина ≤ 80
assert all(ord(c) < 128 for c in ascii_fb)
assert len(ascii_fb) <= 80
# decoded utf-8 совпадает с исходным именем (до триммирования по 80 байтам)
decoded = urllib.parse.unquote(utf8_quoted, encoding="utf-8")
assert decoded == name
def test_ut04_forbidden_chars_replaced():
"""UT-04: запрещённые ФС-символы → '_'."""
name = 'Trail/with:bad*chars?"<>|'
ascii_fb, _ = safe_filename(name, 1)
for ch in '/\\:*?"<>|':
assert ch not in ascii_fb
# должно быть несколько подчёркиваний (хотя бы один на запрещённый символ)
assert "_" in ascii_fb
def test_ut04_empty_name_fallback_track_id():
"""UT-04: пустое имя → 'track-<id>'."""
ascii_fb, utf8_q = safe_filename("", 42)
assert ascii_fb == "track-42"
assert urllib.parse.unquote(utf8_q) == "track-42"
def test_ut04_none_name_fallback_track_id():
ascii_fb, utf8_q = safe_filename(None, 7)
assert ascii_fb == "track-7"
assert urllib.parse.unquote(utf8_q) == "track-7"
def test_ut04_truncated_to_80_bytes():
"""UT-04: длинное ASCII-имя триммится до 80 байт."""
name = "X" * 200
ascii_fb, utf8_q = safe_filename(name, 1)
assert len(ascii_fb.encode("utf-8")) <= 80
# utf8_q после percent-decoding тоже не должен превышать лимит
decoded = urllib.parse.unquote(utf8_q, encoding="utf-8")
assert len(decoded.encode("utf-8")) <= 80
def test_ut04_truncated_utf8_no_broken_codepoints():
"""UT-04: триммирование multibyte-строки не ломает code-point."""
# 200 русских букв = 400 байт UTF-8; триммим до 80 байт → ~40 букв
name = "Я" * 200
ascii_fb, utf8_q = safe_filename(name, 1)
decoded = urllib.parse.unquote(utf8_q, encoding="utf-8")
# должно успешно декодироваться
assert len(decoded) > 0
assert len(decoded.encode("utf-8")) <= 80
def test_ut04_only_forbidden_chars_fallback():
"""UT-04: имя из одних запрещённых символов после strip → fallback."""
ascii_fb, utf8_q = safe_filename("...", 5)
# точки страйпятся, остаётся пустота → fallback
assert ascii_fb == "track-5"
def test_ut04_whitespace_only_fallback():
ascii_fb, _ = safe_filename(" ", 8)
assert ascii_fb == "track-8"
def test_ut04_control_chars_replaced():
"""Управляющие символы (0x00..0x1F, 0x7F) → '_'."""
name = "abc\x00\x01\x1fdef\x7f"
ascii_fb, _ = safe_filename(name, 1)
assert "\x00" not in ascii_fb
assert "\x1f" not in ascii_fb
assert "\x7f" not in ascii_fb
assert "abc" in ascii_fb and "def" in ascii_fb
def test_ut04_ascii_clean_kept_as_is():
"""ASCII-чистое имя сохраняется в ascii-fallback."""
name = "OSM Trail 42"
ascii_fb, utf8_q = safe_filename(name, 1)
assert ascii_fb == "OSM Trail 42"
assert urllib.parse.unquote(utf8_q) == "OSM Trail 42"

View File

@@ -0,0 +1,331 @@
"""Unit-тесты для ET-011 GPX-builder (`src/api/gps_tracks/export.py`).
Покрывает test-plan: UT-01, UT-02, UT-03, UT-05.
"""
from __future__ import annotations
import os
import xml.etree.ElementTree as ET
import pytest
from lxml import etree as lxml_et
from src.api.gps_tracks.export import build_gpx
GPX_NS = "http://www.topografix.com/GPX/1/1"
GPX = "{%s}" % GPX_NS
_FIXTURES_DIR = os.path.join(
os.path.dirname(__file__), "..", "fixtures", "gpx-1.1"
)
_GPX_XSD_PATH = os.path.abspath(os.path.join(_FIXTURES_DIR, "gpx.xsd"))
@pytest.fixture(scope="module")
def gpx_schema() -> lxml_et.XMLSchema:
"""Загружает GPX 1.1 XSD-схему (см. tests/fixtures/gpx-1.1/gpx.xsd)."""
if not os.path.exists(_GPX_XSD_PATH):
pytest.skip(f"GPX XSD not found at {_GPX_XSD_PATH}")
return lxml_et.XMLSchema(lxml_et.parse(_GPX_XSD_PATH))
def _validate_gpx(xml_str: str, schema: lxml_et.XMLSchema) -> None:
"""Валидирует GPX-строку по schema; падает с диагностикой при ошибке."""
doc = lxml_et.fromstring(xml_str.encode("utf-8"))
schema.assertValid(doc)
# ─── UT-01: корректная структура GPX 1.1 ──────────────────────────────────
def test_ut01_build_gpx_basic_structure():
"""UT-01: 5 точек, name/description/external_urls — все элементы на месте."""
xml_str = build_gpx(
track_id=1,
name="Test trail",
description="A short description",
activity_type="enduro",
user="testuser",
created_at="2024-05-12T10:00:00Z",
sources=["osm"],
external_urls=["https://www.openstreetmap.org/way/1"],
coords=[
(37.60, 55.74),
(37.61, 55.75),
(37.62, 55.76),
(37.63, 55.77),
(37.64, 55.78),
],
)
# ET-парсинг — используем тот же ElementTree namespace
root = ET.fromstring(xml_str)
assert root.tag == f"{GPX}gpx"
assert root.attrib["version"] == "1.1"
assert root.attrib["creator"] == "Enduro Trails"
metadata = root.find(f"{GPX}metadata")
assert metadata is not None
name_el = metadata.find(f"{GPX}name")
assert name_el is not None and name_el.text == "Test trail"
link_el = metadata.find(f"{GPX}link")
assert link_el is not None
assert link_el.attrib["href"] == "https://www.openstreetmap.org/way/1"
trks = root.findall(f"{GPX}trk")
assert len(trks) == 1
trk = trks[0]
segs = trk.findall(f"{GPX}trkseg")
assert len(segs) == 1
pts = segs[0].findall(f"{GPX}trkpt")
assert len(pts) == 5
for pt in pts:
# lat/lon — float-парсебельные
lat = float(pt.attrib["lat"])
lon = float(pt.attrib["lon"])
assert -90 <= lat <= 90
assert -180 <= lon <= 180
def test_ut01_metadata_link_text_includes_source():
"""UT-01: text <link> = 'Источник: <source_id>'."""
xml_str = build_gpx(
track_id=1,
name="x",
description=None,
activity_type=None,
user=None,
created_at=None,
sources=["osm"],
external_urls=["https://www.openstreetmap.org/way/42"],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
link = root.find(f"{GPX}metadata/{GPX}link")
text_el = link.find(f"{GPX}text")
assert text_el is not None
assert text_el.text == "Источник: osm"
def test_ut01_osm_copyright_present():
"""UT-01 / AC-10: для OSM-источника присутствует <copyright> с OSM license."""
xml_str = build_gpx(
track_id=1,
name="osm track",
description=None,
activity_type=None,
user=None,
created_at=None,
sources=["osm"],
external_urls=["https://www.openstreetmap.org/way/123"],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
cr = root.find(f"{GPX}metadata/{GPX}copyright")
assert cr is not None
assert cr.attrib["author"] == "Enduro Trails"
lic = cr.find(f"{GPX}license")
assert lic is not None
assert lic.text == "https://www.openstreetmap.org/copyright"
# ─── UT-02: пустые / NULL поля ────────────────────────────────────────────
def test_ut02_empty_fields_no_elements():
"""UT-02: <desc>, <time>, <author>, <link> отсутствуют, а не пустые."""
xml_str = build_gpx(
track_id=99,
name=None,
description=None,
activity_type=None,
user=None,
created_at=None,
sources=[],
external_urls=[],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
metadata = root.find(f"{GPX}metadata")
assert metadata.find(f"{GPX}desc") is None
assert metadata.find(f"{GPX}time") is None
assert metadata.find(f"{GPX}author") is None
assert metadata.find(f"{GPX}link") is None
assert metadata.find(f"{GPX}copyright") is None
name_el = metadata.find(f"{GPX}name")
assert name_el is not None
assert name_el.text == "Без названия"
def test_ut02_empty_name_in_trk_too():
xml_str = build_gpx(
track_id=99,
name="",
description="",
activity_type="",
user="",
created_at=None,
sources=[],
external_urls=[],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
trk_name = root.find(f"{GPX}trk/{GPX}name")
assert trk_name.text == "Без названия"
# type отсутствует, потому что activity_type пустой
assert root.find(f"{GPX}trk/{GPX}type") is None
# ─── UT-03: соответствие XSD-схеме ───────────────────────────────────────
def test_ut03_xsd_minimal(gpx_schema):
"""UT-03: минимальный трек — без metadata-полей и без activity_type."""
xml_str = build_gpx(
track_id=1,
name=None,
description=None,
activity_type=None,
user=None,
created_at=None,
sources=[],
external_urls=[],
coords=[(37.0, 55.0), (37.1, 55.1)],
)
_validate_gpx(xml_str, gpx_schema)
def test_ut03_xsd_typical(gpx_schema):
"""UT-03: типичный OSM-трек со всеми полями."""
xml_str = build_gpx(
track_id=10,
name="OSM trail in Moscow",
description="A nice trail",
activity_type="enduro",
user="alice",
created_at="2024-05-12T10:00:00Z",
sources=["osm"],
external_urls=["https://www.openstreetmap.org/way/1"],
coords=[(37.6 + i / 100, 55.7 + i / 100) for i in range(20)],
)
_validate_gpx(xml_str, gpx_schema)
def test_ut03_xsd_utf8_name(gpx_schema):
"""UT-03: UTF-8 имя/описание не ломают XSD-валидацию."""
xml_str = build_gpx(
track_id=42,
name="По грязи к Чёрному озеру",
description="Тестовое описание с & < > символами",
activity_type="enduro",
user="ivan",
created_at="2025-06-01T12:34:56+03:00",
sources=["osm", "enduro_russia"],
external_urls=[
"https://www.openstreetmap.org/way/9",
"https://endurorussia.ru/tracks/9",
],
coords=[(37.6, 55.7), (37.7, 55.8), (37.8, 55.9)],
)
_validate_gpx(xml_str, gpx_schema)
# ─── UT-05: smoke for wkb_to_coords boundary (2 точки) ──────────────────
def test_ut05_two_point_coords():
"""UT-05: минимальный LineString (2 точки) собирается корректно."""
xml_str = build_gpx(
track_id=1,
name="two-pt",
description=None,
activity_type=None,
user=None,
created_at=None,
sources=[],
external_urls=[],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
pts = root.findall(f"{GPX}trk/{GPX}trkseg/{GPX}trkpt")
assert len(pts) == 2
# ─── Дополнительные проверки структуры ──────────────────────────────────
def test_xml_declaration_present():
xml_str = build_gpx(
track_id=1,
name="x",
description=None,
activity_type=None,
user=None,
created_at=None,
sources=[],
external_urls=[],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
assert xml_str.startswith("<?xml")
def test_trkpt_coordinate_precision_6_digits():
"""ADR-014 §G: lat/lon с фиксированной точностью 6 знаков."""
xml_str = build_gpx(
track_id=1,
name="x",
description=None,
activity_type=None,
user=None,
created_at=None,
sources=[],
external_urls=[],
coords=[(37.123456789, 55.987654321)],
)
root = ET.fromstring(xml_str)
pt = root.find(f"{GPX}trk/{GPX}trkseg/{GPX}trkpt")
# 6 знаков после точки
assert pt.attrib["lon"] == "37.123457"
assert pt.attrib["lat"] == "55.987654"
def test_non_osm_source_no_osm_copyright():
"""ADR-014 §G: для не-OSM источников нет OSM-license в <copyright>."""
xml_str = build_gpx(
track_id=1,
name="wikiloc-only",
description=None,
activity_type=None,
user=None,
created_at=None,
sources=["wikiloc"],
external_urls=["https://www.wikiloc.com/x"],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
cr = root.find(f"{GPX}metadata/{GPX}copyright")
# либо <copyright> отсутствует, либо license != OSM URL
if cr is not None:
lic = cr.find(f"{GPX}license")
assert lic is None or lic.text != "https://www.openstreetmap.org/copyright"
def test_time_normalized_to_utc():
"""ADR-014 §G: <metadata><time> приводится к UTC с суффиксом Z."""
xml_str = build_gpx(
track_id=1,
name="x",
description=None,
activity_type=None,
user=None,
created_at="2024-05-12T13:00:00+03:00",
sources=[],
external_urls=[],
coords=[(0.0, 0.0), (1.0, 1.0)],
)
root = ET.fromstring(xml_str)
time_el = root.find(f"{GPX}metadata/{GPX}time")
assert time_el is not None
# +03:00 → UTC = 10:00:00Z
assert time_el.text == "2024-05-12T10:00:00Z"

788
tests/fixtures/gpx-1.1/gpx.xsd vendored Normal file
View File

@@ -0,0 +1,788 @@
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.topografix.com/GPX/1/1"
targetNamespace="http://www.topografix.com/GPX/1/1"
elementFormDefault="qualified">
<xsd:annotation>
<xsd:documentation>
GPX schema version 1.1 - For more information on GPX and this schema, visit http://www.topografix.com/gpx.asp
GPX uses the following conventions: all coordinates are relative to the WGS84 datum. All measurements are in metric units.
</xsd:documentation>
</xsd:annotation>
<xsd:element name="gpx" type="gpxType">
<xsd:annotation>
<xsd:documentation>
GPX is the root element in the XML file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:complexType name="gpxType">
<xsd:annotation>
<xsd:documentation>
GPX documents contain a metadata header, followed by waypoints, routes, and tracks. You can add your own elements
to the extensions section of the GPX document.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="metadata" type="metadataType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Metadata about the file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="wpt" type="wptType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A list of waypoints.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="rte" type="rteType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A list of routes.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="trk" type="trkType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A list of tracks.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="extensions" type="extensionsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="version" type="xsd:string" use="required" fixed="1.1">
<xsd:annotation>
<xsd:documentation>
You must include the version number in your GPX document.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="creator" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>
You must include the name or URL of the software that created your GPX document. This allows others to
inform the creator of a GPX instance document that fails to validate.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="metadataType">
<xsd:annotation>
<xsd:documentation>
Information about the GPX file, author, and copyright restrictions goes in the metadata section. Providing rich,
meaningful information about your GPX files allows others to search for and use your GPS data.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The name of the GPX file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="desc" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
A description of the contents of the GPX file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="author" type="personType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The person or organization who created the GPX file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="copyright" type="copyrightType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Copyright and license information governing use of the file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
URLs associated with the location described in the file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="time" type="xsd:dateTime" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The creation date of the file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="keywords" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Keywords associated with the file. Search engines or databases can use this information to classify the data.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="bounds" type="boundsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Minimum and maximum coordinates which describe the extent of the coordinates in the file.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="extensions" type="extensionsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="wptType">
<xsd:annotation>
<xsd:documentation>
wpt represents a waypoint, point of interest, or named feature on a map.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<!-- Position info -->
<xsd:element name="ele" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Elevation (in meters) of the point.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="time" type="xsd:dateTime" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Creation/modification timestamp for element. Date and time in are in Univeral Coordinated Time (UTC), not local time! Conforms to ISO 8601 specification for date/time representation. Fractional seconds are allowed for millisecond timing in tracklogs.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="magvar" type="degreesType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Magnetic variation (in degrees) at the point
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="geoidheight" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Height (in meters) of geoid (mean sea level) above WGS84 earth ellipsoid. As defined in NMEA GGA message.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- Description info -->
<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The GPS name of the waypoint. This field will be transferred to and from the GPS. GPX does not place restrictions on the length of this field or the characters contained in it. It is up to the receiving application to validate the field before sending it to the GPS.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="cmt" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS waypoint comment. Sent to GPS as comment.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="desc" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
A text description of the element. Holds additional information about the element intended for the user, not the GPS.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="src" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Source of data. Included to give user some idea of reliability and accuracy of data. "Garmin eTrex", "USGS quad Boston North", e.g.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
Link to additional information about the waypoint.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="sym" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Text of GPS symbol name. For interchange with other programs, use the exact spelling of the symbol as displayed on the GPS. If the GPS abbreviates words, spell them out.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="type" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Type (classification) of the waypoint.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<!-- Accuracy info -->
<xsd:element name="fix" type="fixType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Type of GPX fix.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="sat" type="xsd:nonNegativeInteger" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Number of satellites used to calculate the GPX fix.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="hdop" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Horizontal dilution of precision.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="vdop" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Vertical dilution of precision.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="pdop" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Position dilution of precision.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="ageofdgpsdata" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Number of seconds since last DGPS update.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="dgpsid" type="dgpsStationType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
ID of DGPS station used in differential correction.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="extensions" type="extensionsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="lat" type="latitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The latitude of the point. This is always in decimal degrees, and always in WGS84 datum.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="lon" type="longitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The longitude of the point. This is always in decimal degrees, and always in WGS84 datum.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="rteType">
<xsd:annotation>
<xsd:documentation>
rte represents route - an ordered list of waypoints representing a series of turn points leading to a destination.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS name of route.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="cmt" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS comment for route.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="desc" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Text description of route for user. Not sent to GPS.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="src" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Source of data. Included to give user some idea of reliability and accuracy of data.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
Links to external information about the route.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="number" type="xsd:nonNegativeInteger" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS route number.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="type" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Type (classification) of route.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="extensions" type="extensionsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="rtept" type="wptType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A list of route points.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="trkType">
<xsd:annotation>
<xsd:documentation>
trk represents a track - an ordered list of points describing a path.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS name of track.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="cmt" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS comment for track.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="desc" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
User description of track.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="src" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Source of data. Included to give user some idea of reliability and accuracy of data.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="link" type="linkType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
Links to external information about track.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="number" type="xsd:nonNegativeInteger" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
GPS track number.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="type" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Type (classification) of track.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="extensions" type="extensionsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="trkseg" type="trksegType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A Track Segment holds a list of Track Points which are logically connected in order. To represent a single GPS track where GPS reception was lost, or the GPS receiver was turned off, start a new Track Segment for each continuous span of track data.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="extensionsType">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:any namespace="##other" processContents="lax" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:any>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="trksegType">
<xsd:annotation>
<xsd:documentation>
A Track Segment holds a list of Track Points which are logically connected in order. To represent a single GPS track where GPS reception was lost, or the GPS receiver was turned off, start a new Track Segment for each continuous span of track data.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="trkpt" type="wptType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
A Track Point holds the coordinates, elevation, timestamp, and metadata for a single point in a track.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="extensions" type="extensionsType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
You can add extend GPX by adding your own elements from another schema here.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="copyrightType">
<xsd:annotation>
<xsd:documentation>
Information about the copyright holder and any license governing use of this file. By linking to an appropriate license,
you may place your data into the public domain or grant additional usage rights.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="year" type="xsd:gYear" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Year of copyright.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="license" type="xsd:anyURI" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Link to external file containing license text.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="author" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>
Copyright holder (TopoSoft, Inc.)
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="linkType">
<xsd:annotation>
<xsd:documentation>
A link to an external resource (Web page, digital photo, video clip, etc) with additional information.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="text" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Text of hyperlink.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="type" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Mime type of content (image/jpeg)
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="href" type="xsd:anyURI" use="required">
<xsd:annotation>
<xsd:documentation>
URL of hyperlink.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="emailType">
<xsd:annotation>
<xsd:documentation>
An email address. Broken into two parts (id and domain) to help prevent email harvesting.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>
id half of email address (billgates2004)
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="domain" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation>
domain half of email address (hotmail.com)
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="personType">
<xsd:annotation>
<xsd:documentation>
A person or organization.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="name" type="xsd:string" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Name of person or organization.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="email" type="emailType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Email address.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="link" type="linkType" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
Link to Web site or other external information about person.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ptType">
<xsd:annotation>
<xsd:documentation>
A geographic point with optional elevation and time. Available for use by other schemas.
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="ele" type="xsd:decimal" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The elevation (in meters) of the point.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="time" type="xsd:dateTime" minOccurs="0">
<xsd:annotation>
<xsd:documentation>
The time that the point was recorded.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="lat" type="latitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The latitude of the point. Decimal degrees, WGS84 datum.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="lon" type="longitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The latitude of the point. Decimal degrees, WGS84 datum.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="ptsegType">
<xsd:annotation>
<xsd:documentation>
An ordered sequence of points. (for polygons or polylines, e.g.)
</xsd:documentation>
</xsd:annotation>
<xsd:sequence> <!-- elements must appear in this order -->
<xsd:element name="pt" type="ptType" minOccurs="0" maxOccurs="unbounded">
<xsd:annotation>
<xsd:documentation>
Ordered list of geographic points.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="boundsType">
<xsd:annotation>
<xsd:documentation>
Two lat/lon pairs defining the extent of an element.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="minlat" type="latitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The minimum latitude.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="minlon" type="longitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The minimum longitude.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="maxlat" type="latitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The maximum latitude.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="maxlon" type="longitudeType" use="required">
<xsd:annotation>
<xsd:documentation>
The maximum longitude.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="latitudeType">
<xsd:annotation>
<xsd:documentation>
The latitude of the point. Decimal degrees, WGS84 datum.
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:decimal">
<xsd:minInclusive value="-90.0"/>
<xsd:maxInclusive value="90.0"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="longitudeType">
<xsd:annotation>
<xsd:documentation>
The longitude of the point. Decimal degrees, WGS84 datum.
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:decimal">
<xsd:minInclusive value="-180.0"/>
<xsd:maxExclusive value="180.0"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="degreesType">
<xsd:annotation>
<xsd:documentation>
Used for bearing, heading, course. Units are decimal degrees, true (not magnetic).
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:decimal">
<xsd:minInclusive value="0.0"/>
<xsd:maxExclusive value="360.0"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="fixType">
<xsd:annotation>
<xsd:documentation>
Type of GPS fix. none means GPS had no fix. To signify "the fix info is unknown, leave out fixType entirely. pps = military signal used
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="none"/>
<xsd:enumeration value="2d"/>
<xsd:enumeration value="3d"/>
<xsd:enumeration value="dgps"/>
<xsd:enumeration value="pps"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="dgpsStationType">
<xsd:annotation>
<xsd:documentation>
Represents a differential GPS station.
</xsd:documentation>
</xsd:annotation>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="1023"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>