@truelies/osm-dybuf 0.2.0 → 0.2.2

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.
package/grid_index.d.ts CHANGED
@@ -8,6 +8,18 @@ export declare class Gridex {
8
8
  toString(): string;
9
9
  equals(other: Gridex): boolean;
10
10
  }
11
+ export declare class GridexAtLv extends Gridex {
12
+ readonly level: number;
13
+ constructor(unit: GridUnit, londex: number, latdex: number);
14
+ boundsInt(applyTriangle?: boolean): [number, number, number, number];
15
+ boundsFine(applyTriangle?: boolean): [number, number, number, number];
16
+ boundsFloat(applyTriangle?: boolean): {
17
+ minLon: number;
18
+ minLat: number;
19
+ maxLon: number;
20
+ maxLat: number;
21
+ };
22
+ }
11
23
  export declare class GridUnit {
12
24
  readonly unitFine: number;
13
25
  readonly level: number;
@@ -16,7 +28,9 @@ export declare class GridUnit {
16
28
  readonly minLatdex: number;
17
29
  readonly maxLatdex: number;
18
30
  constructor(level: number);
19
- gridexesAt(longitudeInt: number, latitudeInt: number): Gridex[];
31
+ makeGridex(londex: number, latdex: number): GridexAtLv;
32
+ gridexesAt(longitudeInt: number, latitudeInt: number): GridexAtLv[];
33
+ gridexesWithExt(longitudeInt: number, latitudeInt: number, ext?: number): GridexAtLv[];
20
34
  boundsOfGridFine(gridex: Gridex, applyTriangle?: boolean): [number, number, number, number];
21
35
  boundsOfGrid(gridex: Gridex, applyTriangle?: boolean): [number, number, number, number];
22
36
  gridexRangeForBounds(
package/grid_index.mjs CHANGED
@@ -25,6 +25,31 @@ export class Gridex {
25
25
  }
26
26
  }
27
27
 
28
+ export class GridexAtLv extends Gridex {
29
+ constructor(unit, londex, latdex) {
30
+ super(londex, latdex);
31
+ this.unit = unit;
32
+ }
33
+ get level() {
34
+ return this.unit.level;
35
+ }
36
+ boundsInt(applyTriangle = false) {
37
+ return this.unit.boundsOfGrid(this, applyTriangle);
38
+ }
39
+ boundsFine(applyTriangle = false) {
40
+ return this.unit.boundsOfGridFine(this, applyTriangle);
41
+ }
42
+ boundsFloat(applyTriangle = false) {
43
+ const [minLon, minLat, maxLon, maxLat] = this.boundsInt(applyTriangle);
44
+ return {
45
+ minLon: minLon / INT_COORD_SCALE,
46
+ minLat: minLat / INT_COORD_SCALE,
47
+ maxLon: maxLon / INT_COORD_SCALE,
48
+ maxLat: maxLat / INT_COORD_SCALE,
49
+ };
50
+ }
51
+ }
52
+
28
53
  export class GridUnit {
29
54
  constructor(level) {
30
55
  if (!Number.isInteger(level) || level < 0 || level > MAX_LEVEL) {
@@ -38,24 +63,18 @@ export class GridUnit {
38
63
  this.minLatdex = -this.maxLatdex;
39
64
  }
40
65
 
66
+ makeGridex(londex, latdex) {
67
+ return new GridexAtLv(this, londex, latdex);
68
+ }
69
+
41
70
  _axisIndices(valueFine, axis) {
42
71
  const max = axis === "lon" ? this.maxLondex : this.maxLatdex;
43
72
  const min = axis === "lon" ? this.minLondex : this.minLatdex;
44
73
  const unit = this.unitFine;
45
74
 
46
- if (valueFine > MAX_LONGITUDE_FINE || valueFine < MIN_LONGITUDE_FINE) {
47
- throw new Error("value out of range");
48
- }
49
-
50
- if (valueFine === 0) {
51
- return [-1, 1];
52
- }
53
- if (valueFine === max * unit) {
54
- return [max];
55
- }
56
- if (valueFine === min * unit) {
57
- return [min];
58
- }
75
+ if (valueFine === 0) return [-1, 1];
76
+ if (valueFine === max * unit) return [max];
77
+ if (valueFine === min * unit) return [min];
59
78
  if (valueFine % unit === 0) {
60
79
  const idx = valueFine / unit;
61
80
  return [idx, idx + (idx > 0 ? 1 : -1)];
@@ -76,8 +95,8 @@ export class GridUnit {
76
95
  const latdex = latFine > 0 ? this.maxLatdex : this.minLatdex;
77
96
  const res = [];
78
97
  for (let i = 0; i < this.maxLondex; i++) {
79
- res.push(new Gridex(i + 1, latdex));
80
- res.push(new Gridex(-(i + 1), latdex));
98
+ res.push(this.makeGridex(i + 1, latdex));
99
+ res.push(this.makeGridex(-(i + 1), latdex));
81
100
  }
82
101
  return res;
83
102
  }
@@ -86,12 +105,54 @@ export class GridUnit {
86
105
  const res = [];
87
106
  for (const lx of lonIdx) {
88
107
  for (const ly of latIdx) {
89
- res.push(new Gridex(lx, ly));
108
+ res.push(this.makeGridex(lx, ly));
90
109
  }
91
110
  }
92
111
  return res;
93
112
  }
94
113
 
114
+ gridexesWithExt(longitudeInt, latitudeInt, ext = 0) {
115
+ const radius = Number(ext);
116
+ if (!Number.isInteger(radius) || radius < 0) {
117
+ throw new Error("ext must be a non-negative integer");
118
+ }
119
+ const baseLonFine = longitudeInt * GRID_FINE_RES;
120
+ const baseLatFine = latitudeInt * GRID_FINE_RES;
121
+ const seen = new Set();
122
+ const result = [];
123
+
124
+ const wrapLon = (lonFine) => {
125
+ const span = 2 * MAX_LONGITUDE_FINE;
126
+ let v = lonFine;
127
+ while (v > MAX_LONGITUDE_FINE) v -= span;
128
+ while (v < MIN_LONGITUDE_FINE) v += span;
129
+ return v;
130
+ };
131
+ const clampLat = (latFine) => {
132
+ if (latFine > MAX_LATITUDE_FINE) return MAX_LATITUDE_FINE;
133
+ if (latFine < MIN_LATITUDE_FINE) return MIN_LATITUDE_FINE;
134
+ return latFine;
135
+ };
136
+
137
+ for (let dx = -radius; dx <= radius; dx++) {
138
+ for (let dy = -radius; dy <= radius; dy++) {
139
+ const lonFine = wrapLon(baseLonFine + dx * this.unitFine);
140
+ const latFine = clampLat(baseLatFine + dy * this.unitFine);
141
+ const lonInt = lonFine / GRID_FINE_RES;
142
+ const latInt = latFine / GRID_FINE_RES;
143
+ const cells = this.gridexesAt(lonInt, latInt);
144
+ for (const cell of cells) {
145
+ const key = `${cell.level}:${cell.londex},${cell.latdex}`;
146
+ if (!seen.has(key)) {
147
+ seen.add(key);
148
+ result.push(cell);
149
+ }
150
+ }
151
+ }
152
+ }
153
+ return result;
154
+ }
155
+
95
156
  boundsOfGridFine(gridex, applyTriangle = false) {
96
157
  const { londex, latdex } = gridex;
97
158
  const minLonFine = (londex - (londex > 0 ? 1 : 0)) * this.unitFine;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@truelies/osm-dybuf",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Gridex OSM DyBuf parser and schema utilities",
5
5
  "type": "module",
6
6
  "main": "./osm_dybuf.mjs",
package/schema_ids.mjs CHANGED
@@ -1,7 +1,4 @@
1
- import { createRequire } from "node:module";
2
-
3
- const require = createRequire(import.meta.url);
4
- const REGION_CODES_RAW = require("./region_numeric_codes.json");
1
+ import REGION_CODES_RAW from "./region_numeric_codes.js";
5
2
 
6
3
  export const ENTITY_TYPE_IDS = Object.freeze({
7
4
  c: 0,