Publish, edit, renew and delete listings from the web UI
- Form at #/mine/new and #/mine/<id>/edit: coordinates pasted and encoded to a precision-6 geohash in the browser (about 1 km, OSM link to check, no map tiles), euro price parsing, optional exact address saved on the node only - Region guard: refuses places the node does not follow; region fixed on edit - Owner view: days left with a warning under 5 days, manual Renew (30 days), Edit, Delete with confirmation - Tests (jsdom and pure logic), docs, rebuilt web/dist Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -33,3 +33,67 @@ export function decodeGeohash(hash: string): Area | null {
|
||||
lonErr: (lon[1] - lon[0]) / 2,
|
||||
};
|
||||
}
|
||||
|
||||
/** Encodes a position as a geohash of `precision` characters (6 is about 1.2 km by 0.6 km). */
|
||||
export function encodeGeohash(lat: number, lon: number, precision = 6): string {
|
||||
let latR: [number, number] = [-90, 90];
|
||||
let lonR: [number, number] = [-180, 180];
|
||||
let out = "";
|
||||
let bits = 0;
|
||||
let value = 0;
|
||||
let evenBit = true;
|
||||
while (out.length < precision) {
|
||||
const range = evenBit ? lonR : latR;
|
||||
const x = evenBit ? lon : lat;
|
||||
const mid = (range[0] + range[1]) / 2;
|
||||
value <<= 1;
|
||||
if (x >= mid) {
|
||||
value |= 1;
|
||||
range[0] = mid;
|
||||
} else {
|
||||
range[1] = mid;
|
||||
}
|
||||
evenBit = !evenBit;
|
||||
if (++bits === 5) {
|
||||
out += ALPHABET[value];
|
||||
bits = 0;
|
||||
value = 0;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const NUM = String.raw`[-+]?\d{1,3}(?:\.\d+)?`;
|
||||
|
||||
/**
|
||||
* Reads a position out of what people copy from a map: "53.5511, 9.9937",
|
||||
* "53,5511; 9,9937", "53.5511 9.9937", or a link from OpenStreetMap or Google
|
||||
* Maps. Returns null if there is no valid latitude/longitude pair in it.
|
||||
*/
|
||||
export function parseCoordinates(input: string): { lat: number; lon: number } | null {
|
||||
const s = input.trim();
|
||||
let a: string | undefined;
|
||||
let b: string | undefined;
|
||||
let m: RegExpExecArray | null;
|
||||
if ((m = new RegExp(String.raw`#map=\d+/(${NUM})/(${NUM})`).exec(s))) [, a, b] = m; // openstreetmap.org
|
||||
else if ((m = new RegExp(String.raw`mlat=(${NUM})&(?:amp;)?mlon=(${NUM})`).exec(s))) [, a, b] = m;
|
||||
else if ((m = new RegExp(String.raw`@(${NUM}),(${NUM})`).exec(s))) [, a, b] = m; // google.com/maps
|
||||
else if ((m = new RegExp(String.raw`^(${NUM})\s*[,;]\s*(${NUM})$`).exec(s))) [, a, b] = m;
|
||||
else if ((m = /^([-+]?\d{1,3},\d+)\s*;\s*([-+]?\d{1,3},\d+)$/.exec(s))) [, a, b] = m.map((x) => x?.replace(",", ".")); // 53,55; 9,99
|
||||
else if ((m = /^([-+]?\d{1,3}[.,]\d+)\s+([-+]?\d{1,3}[.,]\d+)$/.exec(s))) [, a, b] = m.map((x) => x?.replace(",", "."));
|
||||
if (a === undefined || b === undefined) return null;
|
||||
const lat = Number(a);
|
||||
const lon = Number(b);
|
||||
if (!Number.isFinite(lat) || !Number.isFinite(lon) || Math.abs(lat) > 90 || Math.abs(lon) > 180) return null;
|
||||
return { lat, lon };
|
||||
}
|
||||
|
||||
/** The size of a geohash cell in metres, for showing how coarse a published location is. */
|
||||
export function cellMetres(hash: string): { width: number; height: number } | null {
|
||||
const a = decodeGeohash(hash);
|
||||
if (!a) return null;
|
||||
return {
|
||||
height: Math.round(a.latErr * 2 * 111_320),
|
||||
width: Math.round(a.lonErr * 2 * 111_320 * Math.cos((a.lat * Math.PI) / 180)),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user