commit 729c9239718d00a43cb55dd9746cd2f75a6979f8
parent fafc6c4112ff71d127f58350a8d7c0746f043454
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Mon, 28 Apr 2025 03:57:03 +0000
utils: add/edit utils
Diffstat:
2 files changed, 41 insertions(+), 1 deletion(-)
diff --git a/utils/src/*validation.ts b/utils/src/*validation.ts
@@ -1,4 +1,4 @@
-import { type GeolocationAddress, type GeolocationPoint, parse_int, util_rxp } from "$root";
+import { GeocoderReverseResult, type GeolocationAddress, type GeolocationPoint, parse_int, util_rxp } from "$root";
import { z } from "zod";
export const zf_area_unit = z.union([
@@ -20,6 +20,17 @@ export const schema_geolocation_address: z.ZodSchema<GeolocationAddress> = z.obj
country: z.string().regex(util_rxp.country_code_a2)
});
+export const schema_geocode_result: z.ZodSchema<GeocoderReverseResult> = z.object({
+ id: z.number(),
+ name: z.string(),
+ admin1_id: z.union([z.string(), z.number()]),
+ admin1_name: z.string(),
+ country_id: z.string(),
+ country_name: z.string(),
+ latitude: z.number(),
+ longitude: z.number(),
+});
+
export const schema_geolocation_point: z.ZodSchema<GeolocationPoint> = z.object({
lat: z.number().min(-90).max(90),
lng: z.number().min(-180).max(180),
diff --git a/utils/src/geo.ts b/utils/src/geo.ts
@@ -1,6 +1,11 @@
import { type GeolocationPointTuple, type GeometryPoint } from "$root";
import { decodeBase32, encodeBase32 } from "geohashing";
+export type GeolocationBasis = {
+ point: GeolocationPoint;
+ address?: GeolocationAddress;
+}
+
export type GeolocationAddress = {
primary: string;
admin: string;
@@ -233,3 +238,27 @@ export const geo_bounds_calc = (lat: number, lng: number, distance_km: number):
};
};
+export const location_gcs_to_geolocation_basis = ({
+ lat,
+ lng,
+ gc_name: primary,
+ gc_admin1_name: admin,
+ gc_country_id: country,
+}: {
+ lat: number;
+ lng: number;
+ gc_name?: string;
+ gc_admin1_name?: string;
+ gc_country_id?: string;
+}): GeolocationBasis => ({
+ point: {
+ lat,
+ lng,
+ },
+ address: primary && admin && country ? {
+ primary,
+ admin,
+ country,
+ } : undefined
+});
+