Navigation: Add Support for Sharing from Waze

This commit is contained in:
Danny
2026-11-10 12:26:02 -08:00
parent 3adb73f702
commit 33236b62fc
4 changed files with 62 additions and 4 deletions
@@ -9,6 +9,12 @@ for (const text of ['https://maps.google.com/@12.3,-45.6,15z', 'https://maps.goo
test('address search', () => assert.equal(parse('geo:0,0?q=10+Main+Street').query, '10 Main Street'))
test('invalid coordinate does not route', () => assert.equal(parse('geo:123,456').destination, undefined))
test('encoded coordinates', () => assert.equal(parse('https://maps.google.com/?q=12.3%2C-45.6').destination.longitude, -45.6))
test('waze links', () => {
assert.deepEqual(parse('https://waze.com/ul/h9q8yy4').destination, { name: 'Shared destination', latitude: 37.762756, longitude: -122.426147 })
assert.equal(parse('https://waze.com/live-map/directions?to=ll.37.77%2C-122.41').destination.longitude, -122.41)
assert.equal(parse('https://waze.com/ul?q=Starbucks%20Main%20St&navigate=yes').query, 'Starbucks Main St')
assert.ok(isMapLink('https://waze.com/ul?q=Starbucks&navigate=yes'))
})
test('pasted map links open their place; words search', () => {
assert.ok(isMapLink('Bellagio Hotel https://maps.app.goo.gl/AbC123'))
assert.ok(isMapLink('https://goo.gl/maps/xyz'))
@@ -1,11 +1,30 @@
// Destination coordinates take precedence over Google's viewport (@lat,lng).
const GEOHASH = '0123456789bcdefghjkmnpqrstuvwxyz'
const WAZE_SHORT = /https:\/\/(?:www\.)?waze\.com\/ul\/h([0-9b-hjkmnp-z]{4,12})(?![0-9a-z])/i
function wazeGeohash(code) {
const latitude = [-90, 90], longitude = [-180, 180]
let even = true
for (const char of code.toLowerCase()) {
const bits = GEOHASH.indexOf(char)
for (let shift = 4; shift >= 0; shift--) {
const span = even ? longitude : latitude
span[bits >> shift & 1 ? 0 : 1] = (span[0] + span[1]) / 2
even = !even
}
}
return [(latitude[0] + latitude[1]) / 2, (longitude[0] + longitude[1]) / 2].map((value) => Math.round(value * 1e6) / 1e6)
}
export function parseSharedDestination(input) {
let text = String(input || '').slice(0, 8192).trim()
try { text = decodeURIComponent(text.replace(/\+/g, ' ')) } catch { /* Keep malformed shares searchable. */ }
const waze = text.match(WAZE_SHORT)
if (waze) { const [latitude, longitude] = wazeGeohash(waze[1]); return { destination: { name: 'Shared destination', latitude, longitude } } }
const number = '(-?\\d+(?:\\.\\d+)?)'
const patterns = [new RegExp(`!3d${number}!4d${number}`),
new RegExp(`(?:[?&:](?:q|query|daddr|destination)=)${number},\\s*${number}`),
new RegExp(`geo:${number},\\s*${number}`), new RegExp(`@${number},\\s*${number}`)]
new RegExp(`geo:${number},\\s*${number}`), new RegExp(`to=ll\\.${number},\\s*${number}`), new RegExp(`@${number},\\s*${number}`)]
for (const pattern of patterns) {
const match = text.match(pattern)
if (!match) continue
@@ -20,7 +39,7 @@ export function parseSharedDestination(input) {
return { query: query.slice(0, 200) }
}
export const SHORT_MAP_LINK = /https:\/\/(?:maps\.app\.goo\.gl|goo\.gl\/maps)\//
export const SHORT_MAP_LINK = /https:\/\/(?:maps\.app\.goo\.gl|goo\.gl\/maps|(?:[a-z0-9-]+\.)?waze\.com\/ul)/
// A pasted or shared map link opens its place; anything else is a search.
export function isMapLink(input) {
+19 -2
View File
@@ -4,11 +4,13 @@ from urllib.parse import parse_qs, unquote_plus, urlsplit
# Same rules as galaxy/web/js/share-destination.js: destination coordinates before Google's viewport (@lat,lng).
NUMBER = r'(-?\d+(?:\.\d+)?)'
COORDINATES = (re.compile(rf'!3d{NUMBER}!4d{NUMBER}'), re.compile(rf'[?&:](?:q|query|daddr|destination|ll|coordinate)={NUMBER},\s*{NUMBER}'),
re.compile(rf'geo:{NUMBER},\s*{NUMBER}'), re.compile(rf'@{NUMBER},\s*{NUMBER}'))
re.compile(rf'geo:{NUMBER},\s*{NUMBER}'), re.compile(rf'to=ll\.{NUMBER},\s*{NUMBER}'), re.compile(rf'@{NUMBER},\s*{NUMBER}'))
WAZE_SHORT = re.compile(r'https://(?:www\.)?waze\.com/ul/h([0-9b-hjkmnp-z]{4,12})(?![0-9a-z])', re.I)
GEOHASH = '0123456789bcdefghjkmnpqrstuvwxyz'
QUERY = re.compile(r'[?&:](?:q|query|daddr|destination)=([^&#]+)')
QUERY_KEYS = ('q', 'query', 'daddr', 'destination')
LINK = re.compile(r'https://[^\s<>"]+')
MAP_HOST = re.compile(r'(?:[a-z0-9-]+\.)*google\.com|(?:maps\.app\.)?goo\.gl|maps\.apple(?:\.com)?')
MAP_HOST = re.compile(r'(?:[a-z0-9-]+\.)*google\.com|(?:maps\.app\.)?goo\.gl|maps\.apple(?:\.com)?|(?:[a-z0-9-]+\.)?waze\.com')
def place(label: str, latitude: float, longitude: float, address: str | None = None) -> dict:
@@ -17,7 +19,22 @@ def place(label: str, latitude: float, longitude: float, address: str | None = N
'address': address or rest.strip() or None}
def waze_geohash(code: str) -> tuple[float, float]:
latitude, longitude, even = [-90.0, 90.0], [-180.0, 180.0], True
for char in code.lower():
bits = GEOHASH.index(char)
for shift in range(4, -1, -1):
span = longitude if even else latitude
middle = (span[0] + span[1]) / 2
span[0 if bits >> shift & 1 else 1] = middle
even = not even
return round((latitude[0] + latitude[1]) / 2, 6), round((longitude[0] + longitude[1]) / 2, 6)
def shared_coordinates(text: str) -> dict | None:
waze = WAZE_SHORT.search(text)
if waze:
return place('Shared destination', *waze_geohash(waze[1]))
decoded = unquote_plus(text.strip())
for pattern in COORDINATES:
match = pattern.search(decoded)
@@ -0,0 +1,16 @@
from openpilot.starpilot.navigation.share import map_link_query, shared_coordinates
def test_waze_short_link_decodes_geohash():
found = shared_coordinates('https://waze.com/ul/h9q8yy4')
assert (found['latitude'], found['longitude']) == (37.762756, -122.426147)
def test_waze_coordinate_links():
assert shared_coordinates('https://waze.com/ul?ll=37.7749,-122.4194&navigate=yes')['longitude'] == -122.4194
assert shared_coordinates('https://waze.com/live-map/directions?to=ll.37.77%2C-122.41')['latitude'] == 37.77
def test_waze_named_link_is_a_search():
assert shared_coordinates('https://waze.com/ul?q=Starbucks%20Main%20St&navigate=yes') is None
assert map_link_query('https://waze.com/ul?q=Starbucks%20Main%20St&navigate=yes') == 'Starbucks Main St'