@procedures/geo 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +21 -2
  3. package/dist/.tsbuildinfo +1 -1
  4. package/dist/distance.d.ts +14 -0
  5. package/dist/distance.d.ts.map +1 -0
  6. package/dist/distance.js +20 -0
  7. package/dist/distance.js.map +1 -0
  8. package/dist/geohash.d.ts +30 -0
  9. package/dist/geohash.d.ts.map +1 -0
  10. package/dist/geohash.js +87 -0
  11. package/dist/geohash.js.map +1 -0
  12. package/dist/index.d.ts +3 -0
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +3 -0
  15. package/dist/index.js.map +1 -1
  16. package/dist/procedures/geo/distance.d.ts +12 -0
  17. package/dist/procedures/geo/distance.d.ts.map +1 -0
  18. package/dist/procedures/geo/distance.js +14 -0
  19. package/dist/procedures/geo/distance.js.map +1 -0
  20. package/dist/procedures/geo/geohash.d.ts +11 -0
  21. package/dist/procedures/geo/geohash.d.ts.map +1 -0
  22. package/dist/procedures/geo/geohash.js +14 -0
  23. package/dist/procedures/geo/geohash.js.map +1 -0
  24. package/dist/procedures/geo/index.d.ts +3 -0
  25. package/dist/procedures/geo/index.d.ts.map +1 -0
  26. package/dist/procedures/geo/index.js +3 -0
  27. package/dist/procedures/geo/index.js.map +1 -0
  28. package/dist/procedures.d.ts +3 -1
  29. package/dist/procedures.d.ts.map +1 -1
  30. package/dist/procedures.js +34 -2
  31. package/dist/procedures.js.map +1 -1
  32. package/dist/types.d.ts +33 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/types.js +29 -0
  35. package/dist/types.js.map +1 -0
  36. package/package.json +5 -3
  37. package/src/distance.ts +23 -0
  38. package/src/geohash.ts +97 -0
  39. package/src/index.ts +3 -0
  40. package/src/procedures/geo/distance.ts +16 -0
  41. package/src/procedures/geo/geohash.ts +16 -0
  42. package/src/procedures/geo/index.ts +2 -0
  43. package/src/procedures.ts +47 -2
  44. package/src/types.ts +67 -0
@@ -0,0 +1,16 @@
1
+ /**
2
+ * geo.geohash procedure
3
+ *
4
+ * Give the geohash of a point
5
+ */
6
+
7
+ import { decodeGeohash, geohash } from "../../geohash.js";
8
+ import type { GeoGeohashInput, GeoGeohashOutput } from "../../types.js";
9
+
10
+ /**
11
+ * This function gives the geohash of a point and the cell of that geohash. It changes nothing.
12
+ */
13
+ export async function geoGeohash(input: GeoGeohashInput): Promise<GeoGeohashOutput> {
14
+ const hash = geohash(input.point, input.precision);
15
+ return { hash, cell: decodeGeohash(hash) };
16
+ }
@@ -0,0 +1,2 @@
1
+ export { geoDistance } from "./distance.js";
2
+ export { geoGeohash } from "./geohash.js";
package/src/procedures.ts CHANGED
@@ -1,12 +1,25 @@
1
1
  /**
2
- * The procedures of `@procedures/geo`: `geo.geocode` and `geo.route`.
2
+ * The procedures of `@procedures/geo`: `geo.geocode`, `geo.route`, `geo.distance` and `geo.geohash`.
3
3
  */
4
4
 
5
5
  import { createProcedure, zodAdapter, type AnyProcedure, type ProcedureContext } from "@procedures/client";
6
6
  import { z } from "zod";
7
7
  import { drivingRoute, geocode, type GeocodeResult, type RouteResult } from "./geo.js";
8
+ import { geoDistance } from "./procedures/geo/distance.js";
9
+ import { geoGeohash } from "./procedures/geo/geohash.js";
10
+ import {
11
+ GeoDistanceInputSchema,
12
+ GeoDistanceOutputSchema,
13
+ GeoGeohashInputSchema,
14
+ GeoGeohashOutputSchema,
15
+ type GeoDistanceInput,
16
+ type GeoDistanceOutput,
17
+ type GeoGeohashInput,
18
+ type GeoGeohashOutput,
19
+ } from "./types.js";
8
20
 
9
21
  const NETWORK_READ = { reads: ["network"], writes: [] };
22
+ const PURE = { reads: [], writes: [] };
10
23
  const point = z.object({ lat: z.number().min(-90).max(90), lon: z.number().min(-180).max(180) });
11
24
  const timeoutMs = z.number().int().positive().max(120_000).default(20_000).describe("The time limit, in milliseconds (default: 20000)");
12
25
 
@@ -79,4 +92,36 @@ export const geoRouteProcedure: AnyProcedure = createProcedure()
79
92
  )
80
93
  .build() as AnyProcedure;
81
94
 
82
- export const geoProcedures: AnyProcedure[] = [geoGeocodeProcedure, geoRouteProcedure];
95
+ // =============================================================================
96
+ // geo.distance
97
+ // =============================================================================
98
+
99
+ export const geoDistanceProcedure: AnyProcedure = createProcedure()
100
+ .path(["geo", "distance"])
101
+ .input(zodAdapter<GeoDistanceInput>(GeoDistanceInputSchema))
102
+ .output(zodAdapter<GeoDistanceOutput>(GeoDistanceOutputSchema))
103
+ .meta({
104
+ description: "Give the great-circle distance between two points in kilometres (haversine), without the network",
105
+ output: "json",
106
+ effects: PURE,
107
+ })
108
+ .handler(async (input: GeoDistanceInput): Promise<GeoDistanceOutput> => geoDistance(input))
109
+ .build() as AnyProcedure;
110
+
111
+ // =============================================================================
112
+ // geo.geohash
113
+ // =============================================================================
114
+
115
+ export const geoGeohashProcedure: AnyProcedure = createProcedure()
116
+ .path(["geo", "geohash"])
117
+ .input(zodAdapter<GeoGeohashInput>(GeoGeohashInputSchema))
118
+ .output(zodAdapter<GeoGeohashOutput>(GeoGeohashOutputSchema))
119
+ .meta({
120
+ description: "Give the geohash of a point and the cell of that geohash, without the network",
121
+ output: "json",
122
+ effects: PURE,
123
+ })
124
+ .handler(async (input: GeoGeohashInput): Promise<GeoGeohashOutput> => geoGeohash(input))
125
+ .build() as AnyProcedure;
126
+
127
+ export const geoProcedures: AnyProcedure[] = [geoGeocodeProcedure, geoRouteProcedure, geoDistanceProcedure, geoGeohashProcedure];
package/src/types.ts ADDED
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Type definitions of the procedures of this package
3
+ */
4
+
5
+ import { z } from "zod";
6
+ import type { Point } from "./geo.js";
7
+ import type { GeohashCell } from "./geohash.js";
8
+
9
+ const point = z.object({ lat: z.number().min(-90).max(90), lon: z.number().min(-180).max(180) });
10
+
11
+ // =============================================================================
12
+ // geo.distance Types - Give the great-circle distance between two points
13
+ // =============================================================================
14
+
15
+ /** The input of `geo.distance`: two points. */
16
+ export interface GeoDistanceInput {
17
+ from: Point;
18
+ to: Point;
19
+ }
20
+
21
+ /** The output of `geo.distance`: the great-circle distance in kilometres, without rounding. */
22
+ export interface GeoDistanceOutput {
23
+ km: number;
24
+ }
25
+
26
+ export const GeoDistanceInputSchema: z.ZodType<GeoDistanceInput> = z.object({
27
+ from: point.describe("The first point (degrees)"),
28
+ to: point.describe("The second point (degrees)"),
29
+ });
30
+
31
+ export const GeoDistanceOutputSchema: z.ZodType<GeoDistanceOutput> = z.object({
32
+ km: z.number().describe("The great-circle distance in kilometres (haversine, Earth radius 6371 km), without rounding"),
33
+ });
34
+
35
+ // =============================================================================
36
+ // geo.geohash Types - Give the geohash of a point
37
+ // =============================================================================
38
+
39
+ /** The input of `geo.geohash`: a point and the number of characters. */
40
+ export interface GeoGeohashInput {
41
+ point: Point;
42
+ precision: number;
43
+ }
44
+
45
+ /** The output of `geo.geohash`: the geohash and its cell. */
46
+ export interface GeoGeohashOutput {
47
+ hash: string;
48
+ cell: GeohashCell;
49
+ }
50
+
51
+ export const GeoGeohashInputSchema: z.ZodType<GeoGeohashInput, { point: Point; precision?: number | undefined }> = z.object({
52
+ point: point.describe("The point (degrees)"),
53
+ precision: z.number().int().min(1).max(12).default(7).describe("The number of characters, from 1 to 12 (default: 7)"),
54
+ });
55
+
56
+ export const GeoGeohashOutputSchema: z.ZodType<GeoGeohashOutput> = z.object({
57
+ hash: z.string().describe("The geohash"),
58
+ cell: z
59
+ .object({
60
+ south: z.number(),
61
+ west: z.number(),
62
+ north: z.number(),
63
+ east: z.number(),
64
+ center: z.object({ lat: z.number(), lon: z.number() }),
65
+ })
66
+ .describe("The cell of the geohash: its edges and its center (degrees)"),
67
+ });