web


git clone https://radroots.dev/git/web.git
Log | Files | Refs | Submodules | README | LICENSE

lib.ts (3519B)


      1 import { handle_err, } from "@radroots/apps-lib";
      2 import type { IGcsLocationFields } from "@radroots/replica-db-schema-bindings";
      3 import type { IError } from "@radroots/types-bindings";
      4 import {
      5     geojson_point_from_geopoint,
      6     geojson_polygon_circle_wgs84,
      7     location_geohash,
      8     type GeocoderReverseResult,
      9     type IClientGeolocationPosition
     10 } from "@radroots/geo";
     11 import { d_tag_create, err_msg } from "@radroots/utils";
     12 import { geoc } from "../app";
     13 
     14 export const geolocation_fields_from_point = async (opts: {
     15     label?: string;
     16     tag_0?: string;
     17     d_tag?: string;
     18     geol_p: IClientGeolocationPosition;
     19 }): Promise<IGcsLocationFields | IError<string>> => {
     20     const { label, geol_p } = opts;
     21     try {
     22         const point = geojson_point_from_geopoint(geol_p);
     23         const polygon = geojson_polygon_circle_wgs84({ geol_p });
     24         const fields: IGcsLocationFields = {
     25             d_tag: opts.d_tag || d_tag_create(),
     26             lat: geol_p.lat,
     27             lng: geol_p.lng,
     28             geohash: location_geohash(geol_p),
     29             point: JSON.stringify(point),
     30             polygon: JSON.stringify(polygon),
     31             tag_0: opts.tag_0,
     32         }
     33         if (label) fields.label = label;
     34         if (geol_p.accuracy !== undefined) fields.accuracy = geol_p.accuracy;
     35         if (geol_p.altitude !== undefined) fields.altitude = geol_p.altitude;
     36         const geoc_rev = await geoc.reverse({ lat: geol_p.lat, lng: geol_p.lng });
     37         if ("err" in geoc_rev) return err_msg(geoc_rev);
     38         else if ("results" in geoc_rev && geoc_rev.results.length > 0) {
     39             const geoc_res = geoc_rev.results[0];
     40             fields.gc_id = geoc_res.id.toString();
     41             fields.gc_name = geoc_res.name;
     42             fields.gc_admin1_id = geoc_res.admin1_id.toString();
     43             fields.gc_admin1_name = geoc_res.admin1_name;
     44             fields.gc_country_id = geoc_res.country_id;
     45             fields.gc_country_name = geoc_res.country_name;
     46         };
     47         return fields;
     48     } catch (e) {
     49         handle_err(e, `geolocation_fields_from_point`);
     50         return err_msg(`*`)
     51     }
     52 };
     53 
     54 export const geolocation_fields_from_point_with_geocode = async (opts: {
     55     label?: string;
     56     tag_0?: string;
     57     geoc_r: GeocoderReverseResult;
     58     d_tag?: string;
     59     geol_p: IClientGeolocationPosition;
     60 }): Promise<IGcsLocationFields | IError<string>> => {
     61     const { label, geoc_r, geol_p } = opts;
     62     try {
     63         const point = geojson_point_from_geopoint(geol_p);
     64         const polygon = geojson_polygon_circle_wgs84({ geol_p });
     65         const fields: IGcsLocationFields = {
     66             d_tag: opts.d_tag || d_tag_create(),
     67             lat: geol_p.lat,
     68             lng: geol_p.lng,
     69             geohash: location_geohash(geol_p),
     70             point: JSON.stringify(point),
     71             polygon: JSON.stringify(polygon),
     72             tag_0: opts.tag_0 || undefined,
     73             gc_id: geoc_r.id.toString(),
     74             gc_name: geoc_r.name,
     75             gc_admin1_id: geoc_r.admin1_id.toString(),
     76             gc_admin1_name: geoc_r.admin1_name,
     77             gc_country_id: geoc_r.country_id,
     78             gc_country_name: geoc_r.country_name,
     79         };
     80         if (label) fields.label = label;
     81         if (geol_p.accuracy !== undefined) fields.accuracy = geol_p.accuracy;
     82         if (geol_p.altitude !== undefined) fields.altitude = geol_p.altitude;
     83         return fields;
     84     } catch (e) {
     85         handle_err(e, `geolocation_fields_from_point_with_geocode`);
     86         return err_msg(`*`)
     87     }
     88 };