@futdevpro/fsm-dynamo 1.20.205 → 1.20.206

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.
@@ -0,0 +1,61 @@
1
+ import HungaryBoundaries from './assets/subdivision-boundaries/hungary.json';
2
+ /**
3
+ * BFR-HELOCIA-077 — coordinates ⇒ ISO 3166-2 subdivision code, without network: the simplified boundaries ship in the
4
+ * package, and the lookup is a point-in-polygon test.
5
+ *
6
+ * Covered countries: Hungary (the 19 counties and Budapest; a city with county rights is counted in its county).
7
+ * Source: the OpenStreetMap boundary relations (© OpenStreetMap contributors, ODbL 1.0: the boundary files are under
8
+ * the ODbL, not the MIT License, and a product showing results from them credits OpenStreetMap). ⚠️ The boundaries
9
+ * are simplified to about 100 m: within about 100 m of a county border a point can fall on the other side. For a
10
+ * statistic per county that is fine; for anything legal (a court, an authority) use the address, not this.
11
+ */
12
+ export class DyFM_SubdivisionLocator {
13
+ /** The boundary sets, one per covered country. */
14
+ static boundaries = [HungaryBoundaries];
15
+ /** ISO 3166-1 alpha-2 codes of the covered countries. */
16
+ static countries = DyFM_SubdivisionLocator.boundaries
17
+ .map((set) => set.country);
18
+ /**
19
+ * The ISO 3166-2 code of the subdivision the point lies in (Hungary: the county, `HU-BU` for Budapest), or `null`
20
+ * outside every covered country or for an invalid point.
21
+ *
22
+ * @param coordinates the point (WGS 84 degrees)
23
+ * @param options.cities a point inside a city with county rights gets the city's own code (e.g. `HU-DE`), not its
24
+ * county's (`HU-HB`)
25
+ */
26
+ static getSubdivisionCode(coordinates, options) {
27
+ const longitude = coordinates?.longitude;
28
+ const latitude = coordinates?.latitude;
29
+ // a missing, non-finite or out-of-range point is no place
30
+ if (!Number.isFinite(longitude) || !Number.isFinite(latitude) || Math.abs(latitude) > 90 ||
31
+ Math.abs(longitude) > 180) {
32
+ return null;
33
+ }
34
+ for (const set of DyFM_SubdivisionLocator.boundaries) {
35
+ // the areas are ordered: the first one that holds the point is the most specific
36
+ const area = set.areas.find((candidate) => candidate.polygons.some((polygon) => DyFM_SubdivisionLocator.isInPolygon(longitude, latitude, polygon)));
37
+ if (area) {
38
+ return options?.cities ? area.code : area.county;
39
+ }
40
+ }
41
+ return null;
42
+ }
43
+ /**
44
+ * Whether the point is inside a GeoJSON polygon: the even-odd ray casting over every ring, so a hole (a city cut out
45
+ * of its county) is outside.
46
+ */
47
+ static isInPolygon(longitude, latitude, polygon) {
48
+ let inside = false;
49
+ for (const ring of polygon) {
50
+ // every edge the eastward ray from the point crosses flips the side
51
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
52
+ const [xi, yi] = ring[i];
53
+ const [xj, yj] = ring[j];
54
+ if ((yi > latitude) !== (yj > latitude) && longitude < (xj - xi) * (latitude - yi) / (yj - yi) + xi) {
55
+ inside = !inside;
56
+ }
57
+ }
58
+ }
59
+ return inside;
60
+ }
61
+ }
@@ -7,6 +7,7 @@ export * from './_collections/loc-country-divisions.const';
7
7
  export * from './_collections/loc-country-isos.const';
8
8
  export * from './_collections/loc.util';
9
9
  export * from './_collections/loc-regions.util';
10
+ export * from './_collections/loc-subdivision-locator.util';
10
11
  // INTERFACES
11
12
  export * from './_models/loc-country-division.interface';
12
13
  export * from './_models/loc-country-iso.interface';
@@ -15,3 +16,4 @@ export * from './_models/loc-division-collection.interface';
15
16
  export * from './_models/loc-division-region-data.interface';
16
17
  export * from './_models/loc-geo-ip-location.interface';
17
18
  export * from './_models/loc-coordinates.interface';
19
+ export * from './_models/loc-subdivision-boundaries.interface';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@futdevpro/fsm-dynamo",
3
- "version": "1.20.205",
3
+ "version": "1.20.206",
4
4
  "description": "Full Stack Model Collection for Dynamic (NodeJS-Typescript) Framework called Dynamo, by Future Development Ltd.",
5
5
  "DyBu_settings": {
6
6
  "packageType": "full-stack-package",