@ifc-lite/geo 0.0.0-stage → 0.1.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 (91) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +294 -2
  3. package/dist/diagnostics.d.ts +16 -0
  4. package/dist/diagnostics.d.ts.map +1 -0
  5. package/dist/diagnostics.js +24 -0
  6. package/dist/diagnostics.js.map +1 -0
  7. package/dist/egm96-undulation-data.d.ts +19 -0
  8. package/dist/egm96-undulation-data.d.ts.map +1 -0
  9. package/dist/egm96-undulation-data.js +18 -0
  10. package/dist/egm96-undulation-data.js.map +1 -0
  11. package/dist/egm96-undulation.d.ts +7 -0
  12. package/dist/egm96-undulation.d.ts.map +1 -0
  13. package/dist/egm96-undulation.js +72 -0
  14. package/dist/egm96-undulation.js.map +1 -0
  15. package/dist/engine-state.d.ts +62 -0
  16. package/dist/engine-state.d.ts.map +1 -0
  17. package/dist/engine-state.js +25 -0
  18. package/dist/engine-state.js.map +1 -0
  19. package/dist/index.d.ts +17 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +19 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/loaders.d.ts +56 -0
  24. package/dist/loaders.d.ts.map +1 -0
  25. package/dist/loaders.js +73 -0
  26. package/dist/loaders.js.map +1 -0
  27. package/dist/map-unit.d.ts +21 -0
  28. package/dist/map-unit.d.ts.map +1 -0
  29. package/dist/map-unit.js +29 -0
  30. package/dist/map-unit.js.map +1 -0
  31. package/dist/precision-grid-data.d.ts +83 -0
  32. package/dist/precision-grid-data.d.ts.map +1 -0
  33. package/dist/precision-grid-data.js +44 -0
  34. package/dist/precision-grid-data.js.map +1 -0
  35. package/dist/precision-grid-table.d.ts +29 -0
  36. package/dist/precision-grid-table.d.ts.map +1 -0
  37. package/dist/precision-grid-table.js +177 -0
  38. package/dist/precision-grid-table.js.map +1 -0
  39. package/dist/precision-grids.d.ts +28 -0
  40. package/dist/precision-grids.d.ts.map +1 -0
  41. package/dist/precision-grids.js +173 -0
  42. package/dist/precision-grids.js.map +1 -0
  43. package/dist/proj4-utils.d.ts +73 -0
  44. package/dist/proj4-utils.d.ts.map +1 -0
  45. package/dist/proj4-utils.js +148 -0
  46. package/dist/proj4-utils.js.map +1 -0
  47. package/dist/proj4.d.ts +17 -0
  48. package/dist/proj4.d.ts.map +1 -0
  49. package/dist/proj4.js +20 -0
  50. package/dist/proj4.js.map +1 -0
  51. package/dist/projected-units.d.ts +17 -0
  52. package/dist/projected-units.d.ts.map +1 -0
  53. package/dist/projected-units.js +49 -0
  54. package/dist/projected-units.js.map +1 -0
  55. package/dist/reproject-point.d.ts +40 -0
  56. package/dist/reproject-point.d.ts.map +1 -0
  57. package/dist/reproject-point.js +88 -0
  58. package/dist/reproject-point.js.map +1 -0
  59. package/dist/resolve-projection.d.ts +85 -0
  60. package/dist/resolve-projection.d.ts.map +1 -0
  61. package/dist/resolve-projection.js +230 -0
  62. package/dist/resolve-projection.js.map +1 -0
  63. package/dist/sanitize-proj4.d.ts +16 -0
  64. package/dist/sanitize-proj4.d.ts.map +1 -0
  65. package/dist/sanitize-proj4.js +133 -0
  66. package/dist/sanitize-proj4.js.map +1 -0
  67. package/dist/spatial-wkt.d.ts +28 -0
  68. package/dist/spatial-wkt.d.ts.map +1 -0
  69. package/dist/spatial-wkt.js +132 -0
  70. package/dist/spatial-wkt.js.map +1 -0
  71. package/dist/transformer.d.ts +97 -0
  72. package/dist/transformer.d.ts.map +1 -0
  73. package/dist/transformer.js +93 -0
  74. package/dist/transformer.js.map +1 -0
  75. package/dist/well-known-crs.d.ts +2 -0
  76. package/dist/well-known-crs.d.ts.map +1 -0
  77. package/dist/well-known-crs.js +25 -0
  78. package/dist/well-known-crs.js.map +1 -0
  79. package/dist/wkt-parameters.d.ts +25 -0
  80. package/dist/wkt-parameters.d.ts.map +1 -0
  81. package/dist/wkt-parameters.js +120 -0
  82. package/dist/wkt-parameters.js.map +1 -0
  83. package/dist/wkt-projection.d.ts +60 -0
  84. package/dist/wkt-projection.d.ts.map +1 -0
  85. package/dist/wkt-projection.js +174 -0
  86. package/dist/wkt-projection.js.map +1 -0
  87. package/dist/wkt.d.ts +7 -0
  88. package/dist/wkt.d.ts.map +1 -0
  89. package/dist/wkt.js +10 -0
  90. package/dist/wkt.js.map +1 -0
  91. package/package.json +69 -3
@@ -0,0 +1,97 @@
1
+ import { type CrsDatumShift } from './engine-state.js';
2
+ import type { CrsLoaders } from './loaders.js';
3
+ import { type LinearUnit } from './projected-units.js';
4
+ import { type GridPointFactors } from './proj4-utils.js';
5
+ export type { CrsDatumShift } from './engine-state.js';
6
+ export type { LinearUnit } from './projected-units.js';
7
+ /** What identifies a CRS. At least one field; see {@link resolveCrs} for precedence. */
8
+ export interface CrsInput {
9
+ /** EPSG code: `2056`, `'2056'` or `'EPSG:2056'`. */
10
+ epsg?: number | string;
11
+ /** `EPSG:<code>` or a well-known alias such as `LV95`, `RD New`, `British National Grid`. */
12
+ name?: string;
13
+ /** WKT1 or WKT2 definition, single or compound. */
14
+ wkt?: string;
15
+ }
16
+ /**
17
+ * A point in the transformer's CRS or in WGS84. `x`/`y` are easting/northing
18
+ * in metres for a projected CRS (whatever its native unit, see
19
+ * {@link CrsTransformer.linearUnit}) and longitude/latitude in degrees for a
20
+ * geographic one and for WGS84: east or longitude always comes first.
21
+ */
22
+ export type Coordinate = readonly [x: number, y: number, z?: number];
23
+ export interface CrsTransformer {
24
+ /**
25
+ * The proj4 definition in use. Either a proj4 string or, for a WKT, a
26
+ * handle registered with this package's proj4 instance; treat it as opaque.
27
+ */
28
+ readonly definition: string;
29
+ /** `geographic`: coordinates are degrees. `projected`: metres. */
30
+ readonly kind: 'projected' | 'geographic';
31
+ /**
32
+ * The unit the CRS itself is defined in, and its length in metres: the
33
+ * US survey foot (1200/3937 m) for a State Plane CRS in feet. Coordinates
34
+ * going into and coming out of this transformer are still metres; divide
35
+ * by `toMetres` to get the CRS's own numbers. `null` for a geographic CRS,
36
+ * or when the definition names a unit this package does not know.
37
+ */
38
+ readonly linearUnit: LinearUnit | null;
39
+ /** The EPSG code the input resolved through, when it named one. */
40
+ readonly epsg: number | null;
41
+ /** How the datum shift of {@link definition} is carried out. Fixed for this transformer. */
42
+ readonly datumShift: CrsDatumShift;
43
+ /**
44
+ * CRS coordinates to WGS84 `[longitude, latitude]` in degrees. `null` when
45
+ * the input (a given `z` included) is not finite, lies outside a loaded grid's coverage or the
46
+ * extent the WKT declares, or does not map to a valid position.
47
+ *
48
+ * `z` is returned unchanged: there is no vertical transformation, so a
49
+ * height above the CRS's vertical datum stays exactly that.
50
+ */
51
+ toWgs84(coordinate: Coordinate): [number, number] | [number, number, number] | null;
52
+ /**
53
+ * WGS84 `[longitude, latitude]` in degrees to CRS coordinates. `null` with
54
+ * the same refusals as {@link toWgs84}: a non-finite or out-of-range input,
55
+ * a position outside a loaded grid's coverage or the WKT's declared extent.
56
+ * `z` is returned unchanged.
57
+ */
58
+ fromWgs84(coordinate: Coordinate): [number, number] | [number, number, number] | null;
59
+ /**
60
+ * Grid convergence (`gamma`, radians, counter-clockwise positive: grid
61
+ * north is true north rotated by `+gamma`) and point scale factor
62
+ * (`pointScale`, grid distance over WGS84-ellipsoid distance) at a point of
63
+ * a projected CRS. `null` for a geographic CRS, within 0.1 degrees of a
64
+ * pole, or where the projection cannot be evaluated.
65
+ */
66
+ gridFactors(x: number, y: number): GridPointFactors | null;
67
+ }
68
+ /** Options of {@link createCrsEngine}. */
69
+ export interface CrsEngineOptions {
70
+ /** The loaders this engine may call. Absent: none, so no network request. */
71
+ loaders?: CrsLoaders;
72
+ }
73
+ /** An engine with its own loaders and caches; see {@link createCrsEngine}. */
74
+ export interface CrsEngine {
75
+ resolveCrs(input: CrsInput): Promise<CrsTransformer | null>;
76
+ }
77
+ /**
78
+ * Make an engine with its own loaders and its own caches. Definitions and
79
+ * datum grids it resolves are not shared with the default engine
80
+ * (`resolveCrs`) or any other engine, and theirs are not visible to it, so a
81
+ * server can resolve for several tenants with different loaders side by side.
82
+ * Keep the engine for as long as its loaders apply: each one loads a grid
83
+ * once and keeps it.
84
+ */
85
+ export declare function createCrsEngine(options?: CrsEngineOptions): CrsEngine;
86
+ /**
87
+ * Resolve a CRS to a transformer through the default engine, whose loaders
88
+ * `configureCrsLoaders` sets. `null` when it cannot be resolved.
89
+ *
90
+ * Precedence: a readable `wkt` defines the CRS (an EPSG identifier inside it
91
+ * resolves through the registry); otherwise `epsg`; otherwise `name`.
92
+ *
93
+ * Resolution reads the bundled EPSG index, the name aliases and the WKT. It
94
+ * touches the network only through loaders the host installed.
95
+ */
96
+ export declare function resolveCrs(input: CrsInput): Promise<CrsTransformer | null>;
97
+ //# sourceMappingURL=transformer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformer.d.ts","sourceRoot":"","sources":["../src/transformer.ts"],"names":[],"mappings":"AAUA,OAAO,EAGL,KAAK,aAAa,EAEnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAuB,KAAK,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAA8C,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAIrG,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,YAAY,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAEvD,wFAAwF;AACxF,MAAM,WAAW,QAAQ;IACvB,oDAAoD;IACpD,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,6FAA6F;IAC7F,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mDAAmD;IACnD,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,SAAS,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,EAAE,MAAM,CAAC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1,93 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+ /**
5
+ * The package's main contract: resolve a CRS once, then convert points
6
+ * between it and WGS84 and read the local grid factors.
7
+ */
8
+ import { reportCrsDiagnostic } from './diagnostics.js';
9
+ import { createEngineState, defaultEngineState, } from './engine-state.js';
10
+ import { projectedLinearUnit } from './projected-units.js';
11
+ import { computeGridPointFactors, isGeographicProj4 } from './proj4-utils.js';
12
+ import { coordinatesToLatLon, latLonToCoordinates } from './reproject-point.js';
13
+ import { declaredEpsgCode, resolveDefinition } from './resolve-projection.js';
14
+ function epsgName(epsg) {
15
+ const code = /^(?:EPSG\s*:\s*)?(\d+)$/i.exec(String(epsg).trim())?.[1];
16
+ return code ? `EPSG:${code}` : null;
17
+ }
18
+ function withZ(x, y, z) {
19
+ return z === undefined ? [x, y] : [x, y, z];
20
+ }
21
+ /**
22
+ * Make an engine with its own loaders and its own caches. Definitions and
23
+ * datum grids it resolves are not shared with the default engine
24
+ * (`resolveCrs`) or any other engine, and theirs are not visible to it, so a
25
+ * server can resolve for several tenants with different loaders side by side.
26
+ * Keep the engine for as long as its loaders apply: each one loads a grid
27
+ * once and keeps it.
28
+ */
29
+ export function createCrsEngine(options = {}) {
30
+ const state = createEngineState(options.loaders ?? {});
31
+ return { resolveCrs: (input) => resolveCrsWith(state, input) };
32
+ }
33
+ /**
34
+ * Resolve a CRS to a transformer through the default engine, whose loaders
35
+ * `configureCrsLoaders` sets. `null` when it cannot be resolved.
36
+ *
37
+ * Precedence: a readable `wkt` defines the CRS (an EPSG identifier inside it
38
+ * resolves through the registry); otherwise `epsg`; otherwise `name`.
39
+ *
40
+ * Resolution reads the bundled EPSG index, the name aliases and the WKT. It
41
+ * touches the network only through loaders the host installed.
42
+ */
43
+ export function resolveCrs(input) {
44
+ return resolveCrsWith(defaultEngineState(), input);
45
+ }
46
+ async function resolveCrsWith(state, input) {
47
+ const epsg = input.epsg === undefined ? undefined : epsgName(input.epsg);
48
+ // An unreadable `epsg` is refused, unless a readable WKT defines the CRS:
49
+ // the WKT comes first, and lower-priority input cannot veto it.
50
+ if (epsg === null && !input.wkt)
51
+ return null;
52
+ const descriptor = { name: epsg === null ? undefined : epsg ?? input.name, wellKnownText: input.wkt };
53
+ const resolved = await resolveDefinition(descriptor, state);
54
+ if (!resolved)
55
+ return null;
56
+ const { definition, datumShift } = resolved;
57
+ const code = declaredEpsgCode(descriptor);
58
+ const geographic = isGeographicProj4(definition);
59
+ return {
60
+ definition,
61
+ kind: geographic ? 'geographic' : 'projected',
62
+ linearUnit: geographic ? null : projectedLinearUnit(definition),
63
+ epsg: code === null ? null : Number(code),
64
+ datumShift,
65
+ toWgs84([x, y, z]) {
66
+ if (z !== undefined && !Number.isFinite(z))
67
+ return null;
68
+ const position = coordinatesToLatLon(definition, x, y);
69
+ return position && withZ(position.lon, position.lat, z);
70
+ },
71
+ fromWgs84([lon, lat, z]) {
72
+ if (z !== undefined && !Number.isFinite(z))
73
+ return null;
74
+ const position = latLonToCoordinates(definition, lon, lat);
75
+ return position && withZ(position.x, position.y, z);
76
+ },
77
+ gridFactors(x, y) {
78
+ if (geographic)
79
+ return null;
80
+ const position = coordinatesToLatLon(definition, x, y);
81
+ if (!position || Math.abs(position.lat) >= 89.9)
82
+ return null;
83
+ try {
84
+ return computeGridPointFactors(definition, x, y);
85
+ }
86
+ catch (error) {
87
+ reportCrsDiagnostic('[reproject] grid factors could not be evaluated', error);
88
+ return null;
89
+ }
90
+ },
91
+ };
92
+ }
93
+ //# sourceMappingURL=transformer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformer.js","sourceRoot":"","sources":["../src/transformer.ts"],"names":[],"mappings":"AAAA;;+DAE+D;AAE/D;;;GAGG;AAEH,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EACL,iBAAiB,EACjB,kBAA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AAC,EAAE,CAAC,CAAC,CAAC;YACvD,OAAO,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QAC1D,CAAC;QACD,SAAS,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,CAAC;YACrB,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;gBAAE,OAAO,IAAI,CAAC;YACxD,MAAM,QAAQ,GAAG,mBAAmB,CAAC,UAAU,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;YAC3D,OAAO,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACtD,CAAC;QACD,WAAW,CAAC,CAAC,EAAE,CAAC;YACd,IAAI,UAAU;gBAAE,OAAO,IAAI,CAAC;YAC5B,MAAM,QAAQ,GAAG,mBAAmB,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;YACvD,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;YAC7D,IAAI,CAAC;gBACH,OAAO,uBAAuB,CAAC,UAAU,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;YACnD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,mBAAmB,CAAC,iDAAiD,EAAE,KAAK,CAAC,CAAC;gBAC9E,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function wellKnownCrsCode(name: string): string | undefined;
2
+ //# sourceMappingURL=well-known-crs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"well-known-crs.d.ts","sourceRoot":"","sources":["../src/well-known-crs.ts"],"names":[],"mappings":"AAuBA,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEjE"}
@@ -0,0 +1,25 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+ /** Authoring-tool CRS labels that are explicit aliases, never inferred coordinates. */
5
+ const WELL_KNOWN_CRS = {
6
+ 'wgs 84': '4326', 'wgs84': '4326', 'wgs-84': '4326', 'nad83': '4269', 'nad27': '4267', 'etrs89': '4258',
7
+ 'gcs_wgs_1984': '4326', 'gcs_north_american_1983': '4269',
8
+ 'rd': '28992', 'rd new': '28992', 'amersfoort / rd new': '28992', 'amersfoort rd new': '28992',
9
+ 'stelsel van de rijksdriehoeksmeting': '28992', 'rijksdriehoeksmeting': '28992', 'nl_rd': '28992',
10
+ 'rd new + nap height': '7415', 'amersfoort / rd new + nap height': '7415',
11
+ 'osgb 1936 / british national grid': '27700', 'osgb36 / british national grid': '27700',
12
+ 'british national grid': '27700', 'bng': '27700',
13
+ 'dhdn / gauss-kruger zone 2': '31466', 'dhdn / gauss-kruger zone 3': '31467',
14
+ 'dhdn / gauss-kruger zone 4': '31468', 'dhdn / gauss-kruger zone 5': '31469',
15
+ 'etrs89 / utm zone 32n': '25832', 'etrs89 / utm zone 33n': '25833',
16
+ 'mgi / austria lambert': '31287', 'austria lambert': '31287',
17
+ 'ch1903+ / lv95': '2056', 'lv95': '2056', 'ch1903 / lv03': '21781', 'lv03': '21781',
18
+ 'belge 1972 / belgian lambert 72': '31370', 'belgian lambert 72': '31370',
19
+ 'etrs89 / belgian lambert 2008': '3812',
20
+ 'rgf93 / lambert-93': '2154', 'rgf93 v1 / lambert-93': '2154', 'lambert-93': '2154', 'lambert 93': '2154',
21
+ };
22
+ export function wellKnownCrsCode(name) {
23
+ return WELL_KNOWN_CRS[name.trim().toLowerCase()];
24
+ }
25
+ //# sourceMappingURL=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AAC,IAAY;IAC3C,OAAO,cAAc,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC,CAAC;AACnD,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Give every angular map-projection parameter of a WKT2 `CONVERSION` that
3
+ * states no unit an explicit degree.
4
+ *
5
+ * ISO 19162:2019 (OGC 18-010r7) 9.3.4: when a map projection parameter
6
+ * omits its unit, angles are decimal degrees, lengths metres and scale
7
+ * factors unity. PROJ reads it that way. `wkt-parser` instead passes the bare
8
+ * number through, which proj4 then takes as radians: a central meridian of
9
+ * 7.5 becomes 7.5 rad, 69.7 degrees east. Lengths and scale factors already
10
+ * come out right, so only angles get a unit. WKT1 (`PROJECTION`) is left
11
+ * alone: its reader converts degrees itself.
12
+ */
13
+ export declare function withImpliedParameterUnits(wkt: string): string;
14
+ /** What `wkt-parser` returns, as far as this check reads it. */
15
+ export interface ParsedProjection {
16
+ projName?: string;
17
+ [key: string]: unknown;
18
+ }
19
+ /**
20
+ * The parameters `parsed` lacks for its projection method; empty when it has
21
+ * them all or the method is not checked. A WKT whose parameter names the
22
+ * reader did not recognise ends up here instead of at the wrong place.
23
+ */
24
+ export declare function missingProjectionParameters(parsed: ParsedProjection): string[];
25
+ //# sourceMappingURL=wkt-parameters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wkt-parameters.d.ts","sourceRoot":"","sources":["../src/wkt-parameters.ts"],"names":[],"mappings":"AAwDA;;;;;;;;;;;GAWG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAwB7D;AAmBD,gEAAgE;AAChE,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED;;;;GAIG;AACH,wBAAgB,2BAA2B,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,EAAE,CAI9E"}
@@ -0,0 +1,120 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+ /**
5
+ * Map-projection parameters around proj4's WKT reader (`wkt-parser`): what a
6
+ * WKT2 parameter without a unit means, and whether the reader picked up the
7
+ * parameters a projection method cannot do without.
8
+ */
9
+ /** One degree in radians, as EPSG and PROJ write it. */
10
+ const DEGREE = 'ANGLEUNIT["degree",0.0174532925199433]';
11
+ /**
12
+ * PROJ's reading of a parameter name (`guessUnitForParameter` in
13
+ * `src/iso19111/io.cpp`). Scale comes first because of "Scale factor on
14
+ * pseudo standard parallel".
15
+ */
16
+ const SCALE_NAME = /scale|scaling factor/i;
17
+ const ANGULAR_NAME = /latitude|longitude|meridian|parallel|azimuth|angle|heading|rotation/i;
18
+ const UNIT_NODE = /^(?:ANGLEUNIT|LENGTHUNIT|SCALEUNIT|PARAMETRICUNIT|TIMEUNIT|UNIT)$/i;
19
+ /** The direct child nodes (`KEYWORD[...]`) between `from` and `to`, skipping quoted text. */
20
+ function childNodes(text, from, to) {
21
+ const nodes = [];
22
+ let index = from;
23
+ while (index < to) {
24
+ const char = text[index];
25
+ if (char === '"') {
26
+ index = text.indexOf('"', index + 1) + 1 || to;
27
+ continue;
28
+ }
29
+ const keyword = /^[A-Za-z_][A-Za-z0-9_]*\s*\[/.exec(text.slice(index, Math.min(to, index + 64)));
30
+ const startsWord = !/[A-Za-z0-9_]/.test(text[index - 1] ?? '');
31
+ if (!keyword || !startsWord) {
32
+ index += 1;
33
+ continue;
34
+ }
35
+ const bodyStart = index + keyword[0].length;
36
+ let depth = 1;
37
+ let cursor = bodyStart;
38
+ let quoted = false;
39
+ for (; cursor < to && depth > 0; cursor += 1) {
40
+ if (text[cursor] === '"')
41
+ quoted = !quoted;
42
+ else if (!quoted && text[cursor] === '[')
43
+ depth += 1;
44
+ else if (!quoted && text[cursor] === ']')
45
+ depth -= 1;
46
+ }
47
+ if (depth !== 0)
48
+ break;
49
+ nodes.push({ name: keyword[0].replace(/[\s[]/g, '').toUpperCase(), bodyStart, end: cursor });
50
+ index = cursor;
51
+ }
52
+ return nodes;
53
+ }
54
+ /**
55
+ * Give every angular map-projection parameter of a WKT2 `CONVERSION` that
56
+ * states no unit an explicit degree.
57
+ *
58
+ * ISO 19162:2019 (OGC 18-010r7) 9.3.4: when a map projection parameter
59
+ * omits its unit, angles are decimal degrees, lengths metres and scale
60
+ * factors unity. PROJ reads it that way. `wkt-parser` instead passes the bare
61
+ * number through, which proj4 then takes as radians: a central meridian of
62
+ * 7.5 becomes 7.5 rad, 69.7 degrees east. Lengths and scale factors already
63
+ * come out right, so only angles get a unit. WKT1 (`PROJECTION`) is left
64
+ * alone: its reader converts degrees itself.
65
+ */
66
+ export function withImpliedParameterUnits(wkt) {
67
+ const insertions = [];
68
+ const visit = (from, to) => {
69
+ for (const node of childNodes(wkt, from, to)) {
70
+ if (node.name !== 'CONVERSION') {
71
+ visit(node.bodyStart, node.end - 1);
72
+ continue;
73
+ }
74
+ for (const parameter of childNodes(wkt, node.bodyStart, node.end - 1)) {
75
+ if (parameter.name !== 'PARAMETER')
76
+ continue;
77
+ const body = wkt.slice(parameter.bodyStart, parameter.end - 1);
78
+ if (childNodes(body, 0, body.length).some((child) => UNIT_NODE.test(child.name)))
79
+ continue;
80
+ const head = /^\s*"((?:[^"]|"")*)"\s*,\s*[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/.exec(body);
81
+ if (!head || SCALE_NAME.test(head[1]) || !ANGULAR_NAME.test(head[1]))
82
+ continue;
83
+ insertions.push(parameter.bodyStart + head[0].length);
84
+ }
85
+ }
86
+ };
87
+ visit(0, wkt.length);
88
+ let result = wkt;
89
+ for (const position of insertions.reverse()) {
90
+ result = `${result.slice(0, position)},${DEGREE}${result.slice(position)}`;
91
+ }
92
+ return result;
93
+ }
94
+ /**
95
+ * The proj4 parameters a projection method has no meaningful default for:
96
+ * without them proj4 silently uses 0 (a central meridian of 0 degrees puts
97
+ * the model hundreds of kilometres west) or 1. Keyed by the method name with
98
+ * case, spaces and punctuation removed, in its EPSG, WKT1 and ESRI spellings.
99
+ * A method not listed is not checked.
100
+ */
101
+ const REQUIRED_PARAMETERS = [
102
+ [/^(?:transversemercator|gausskruger|tmerc)$/, ['long0', 'k0']],
103
+ [/^(?:obliquestereographic|doublestereographic|sterea)$/, ['long0', 'lat0', 'k0']],
104
+ [/^(?:lambertconicconformal1sp|lambertconformalconic1sp)$/, ['long0', 'lat0', 'k0']],
105
+ [/^(?:lambertconicconformal2sp|lambertconformalconic2sp)$/, ['long0', 'lat1', 'lat2']],
106
+ // Without a 1SP/2SP suffix a WKT may state one parallel or two.
107
+ [/^(?:lambertconformalconic|lcc)$/, ['long0', 'lat1']],
108
+ [/^(?:mercator|mercator1sp|mercator2sp|mercatorvarianta|mercatorvariantb)$/, ['long0']],
109
+ ];
110
+ /**
111
+ * The parameters `parsed` lacks for its projection method; empty when it has
112
+ * them all or the method is not checked. A WKT whose parameter names the
113
+ * reader did not recognise ends up here instead of at the wrong place.
114
+ */
115
+ export function missingProjectionParameters(parsed) {
116
+ const method = (parsed.projName ?? '').toLowerCase().replace(/[^a-z0-9]/g, '');
117
+ const required = REQUIRED_PARAMETERS.find(([pattern]) => pattern.test(method))?.[1] ?? [];
118
+ return required.filter((key) => typeof parsed[key] !== 'number' || !Number.isFinite(parsed[key]));
119
+ }
120
+ //# sourceMappingURL=wkt-parameters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wkt-parameters.js","sourceRoot":"","sources":["../src/wkt-parameters.ts"],"names":[],"mappings":"AAAA;;+DAE+D;AAE/D;;;;GAIG;AAEH,wDAAwD;AACxD,MAAM,MAAM,GAAG,wCAAwC,CAAC;AAExD;;;;GAIG;AACH,MAAM,UAAU,GAAG,uBAAuB,CAAC;AAC3C,MAAM,YAAY,GAAG,sEAAsE,CAAC;AAC5F,MAAM,SAAS,GAAG,o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AE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACtD,CAAC,0EAA0E,EAAE,CAAC,OAAO,CAAC,CAAC;CACxF,CAAC;AAQF;;;;GAIG;AACH,MAAM,UAAU,2BAA2B,CAAC,MAAwB;IAClE,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC;IAC/E,MAAM,QAAQ,GAAG,mBAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC1F,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACpG,CAAC"}
@@ -0,0 +1,60 @@
1
+ interface ParsedWktDefinition {
2
+ projName?: string;
3
+ a?: number;
4
+ datum_params?: unknown;
5
+ nadgrids?: unknown;
6
+ to_meter?: number;
7
+ units?: string;
8
+ [key: string]: unknown;
9
+ }
10
+ export type WktProjection =
11
+ /** `towgs84`: a datum shift the WKT states itself, to replace the EPSG default. */
12
+ {
13
+ kind: 'epsg';
14
+ code: string;
15
+ towgs84?: string;
16
+ } | {
17
+ kind: 'definition';
18
+ definition: string;
19
+ }
20
+ /**
21
+ * Read, but without a parameter its projection needs: the WKT says where
22
+ * the CRS is and the reader lost it, so nothing else may stand in for it.
23
+ */
24
+ | {
25
+ kind: 'refused';
26
+ };
27
+ export declare function isWktDefinition(definition: string): boolean;
28
+ /** The parsed definition behind a registered name (either unit variant). */
29
+ export declare function registeredWktDefinition(definition: string): ParsedWktDefinition | undefined;
30
+ /** The registered name of the same CRS with metre coordinates. */
31
+ export declare function wktDefinitionInMetres(definition: string): string;
32
+ /**
33
+ * Resolve a CRS WKT (WKT1 or WKT2, single or compound) to a projection.
34
+ *
35
+ * Returns `{ kind: 'epsg' }` when the horizontal CRS carries an EPSG
36
+ * identifier, so the registered definition (with its precision grids and
37
+ * datum handling) wins over re-deriving it. A TOWGS84 the WKT states is
38
+ * returned with it: it is the author's datum shift and replaces the registry
39
+ * default. Otherwise registers the parsed WKT with proj4 (a BOUNDCRS keeps
40
+ * its ABRIDGEDTRANSFORMATION there). The registry projection is preferred
41
+ * because proj4's WKT1 reader mishandles some methods (Mercator_2SP reads
42
+ * standard_parallel_1 as the latitude of origin). `null` when it cannot be read.
43
+ */
44
+ export declare function resolveWktProjection(wkt: string): WktProjection | null;
45
+ /**
46
+ * A registry definition chosen for a WKT that names an EPSG code, wrapped so
47
+ * the WKT's declared extent still guards it: registered under an alias that
48
+ * carries the extent, else returned unchanged when the WKT declares none.
49
+ */
50
+ export declare function withWktExtent(wkt: string, definition: string): string;
51
+ /**
52
+ * Whether a WGS84 position lies within the declared extent of validity
53
+ * (WKT2 `USAGE/BBOX`) of the WKT behind `definition`, widened by
54
+ * `marginDegrees`. `true` for any other definition or a WKT without an extent.
55
+ * This is the guard against a definition that parsed but means something else
56
+ * than its author wrote: such a model lands far outside its own area of use.
57
+ */
58
+ export declare function isWithinWktDefinitionExtent(definition: string, lon: number, lat: number, marginDegrees?: number): boolean;
59
+ export {};
60
+ //# sourceMappingURL=wkt-projection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wkt-projection.d.ts","sourceRoot":"","sources":["../src/wkt-projection.ts"],"names":[],"mappings":"AAqCA,UAAU,mBAAmB;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,MAAM,MAAM,aAAa;AACvB,mFAAmF;AACjF;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAChD;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE;AAC5C;;;GAGG;GACD;IAAE,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC;AAgBxB,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAE3D;AAED,4EAA4E;AAC5E,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,MAAM,GAAG,mBAAmB,GAAG,SAAS,CAE3F;AAMD,kEAAkE;AAClE,wBAAgB,qBAAqB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAMtE;AA2DD;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAarE;AAED;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CAAC,UAAU,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,aAAa,SAAM,GAAG,OAAO,CAmBtH"}
@@ -0,0 +1,174 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+ /**
5
+ * Projection definitions from an `IfcWellKnownText` CRS definition (IFC4X3).
6
+ *
7
+ * The WKT itself is read by proj4's own parser (`wkt-parser`, 1.7 or later,
8
+ * which also maps WKT1-style parameter names such as `central_meridian`
9
+ * inside WKT2). This module selects the horizontal CRS of a compound one,
10
+ * prefers a registered EPSG identifier when the WKT names one, gives WKT2
11
+ * angular parameters without a unit their standard degree, refuses a parse
12
+ * that lacks a parameter its projection needs, and registers the parsed
13
+ * definition with proj4 under a content-addressed name. Every proj4 call
14
+ * then takes that name exactly like a proj4 string.
15
+ */
16
+ import proj4 from 'proj4';
17
+ import parseWkt from 'wkt-parser';
18
+ import { reportCrsDiagnostic } from './diagnostics.js';
19
+ import { extractWktBoundingBox, extractWktHorizontalCrs, extractWktSpatialMetadata, } from './spatial-wkt.js';
20
+ import { missingProjectionParameters, withImpliedParameterUnits } from './wkt-parameters.js';
21
+ /** Prefix of the proj4 definition names registered for WKT CRSs. */
22
+ const WKT_DEFINITION_PREFIX = 'IFC-WKT:';
23
+ /** Suffix of the same definition with its projected unit overridden to metres. */
24
+ const METRES_SUFFIX = '#m';
25
+ /** proj4 datum types (proj4/lib/constants/values.js). */
26
+ const PJD_NODATUM = 5;
27
+ const WGS84_SEMI_MAJOR = 6378137;
28
+ const resolved = new Map();
29
+ const registered = new Map();
30
+ const warnedExtents = new Set();
31
+ /** FNV-1a; a cache key, not a security boundary. */
32
+ function hash(text) {
33
+ let h = 0x811c9dc5;
34
+ for (let i = 0; i < text.length; i++) {
35
+ h ^= text.charCodeAt(i);
36
+ h = Math.imul(h, 0x01000193);
37
+ }
38
+ return (h >>> 0).toString(16).padStart(8, '0');
39
+ }
40
+ export function isWktDefinition(definition) {
41
+ return definition.startsWith(WKT_DEFINITION_PREFIX);
42
+ }
43
+ /** The parsed definition behind a registered name (either unit variant). */
44
+ export function registeredWktDefinition(definition) {
45
+ return registration(definition)?.parsed;
46
+ }
47
+ function registration(definition) {
48
+ return registered.get(definition.endsWith(METRES_SUFFIX) ? definition.slice(0, -METRES_SUFFIX.length) : definition);
49
+ }
50
+ /** The registered name of the same CRS with metre coordinates. */
51
+ export function wktDefinitionInMetres(definition) {
52
+ return definition.endsWith(METRES_SUFFIX) ? definition : `${definition}${METRES_SUFFIX}`;
53
+ }
54
+ /**
55
+ * Resolve a CRS WKT (WKT1 or WKT2, single or compound) to a projection.
56
+ *
57
+ * Returns `{ kind: 'epsg' }` when the horizontal CRS carries an EPSG
58
+ * identifier, so the registered definition (with its precision grids and
59
+ * datum handling) wins over re-deriving it. A TOWGS84 the WKT states is
60
+ * returned with it: it is the author's datum shift and replaces the registry
61
+ * default. Otherwise registers the parsed WKT with proj4 (a BOUNDCRS keeps
62
+ * its ABRIDGEDTRANSFORMATION there). The registry projection is preferred
63
+ * because proj4's WKT1 reader mishandles some methods (Mercator_2SP reads
64
+ * standard_parallel_1 as the latitude of origin). `null` when it cannot be read.
65
+ */
66
+ export function resolveWktProjection(wkt) {
67
+ const text = wkt.trim();
68
+ if (resolved.has(text))
69
+ return resolved.get(text) ?? null;
70
+ const result = resolveUncached(text);
71
+ resolved.set(text, result);
72
+ return result;
73
+ }
74
+ function resolveUncached(wkt) {
75
+ const horizontal = extractWktHorizontalCrs(wkt);
76
+ if (!horizontal) {
77
+ reportCrsDiagnostic('[reproject] IfcWellKnownText has no horizontal CRS (PROJCRS/GEOGCRS); it cannot place the model.');
78
+ return null;
79
+ }
80
+ const epsg = extractWktSpatialMetadata(horizontal).horizontalId?.match(/^EPSG:(\d+)$/)?.[1];
81
+ if (epsg) {
82
+ // The projection comes from the registry; a TOWGS84 the WKT states is the
83
+ // author's datum shift and replaces the registry default.
84
+ const towgs84 = /\bTOWGS84\s*\[([^\]]*)\]/i.exec(horizontal)?.[1]
85
+ ?.split(',').map((value) => Number(value.trim()));
86
+ return towgs84 && (towgs84.length === 3 || towgs84.length === 7) && towgs84.every(Number.isFinite)
87
+ ? { kind: 'epsg', code: epsg, towgs84: towgs84.join(',') }
88
+ : { kind: 'epsg', code: epsg };
89
+ }
90
+ let parsed;
91
+ try {
92
+ parsed = parseWkt(withImpliedParameterUnits(horizontal));
93
+ }
94
+ catch (error) {
95
+ reportCrsDiagnostic('[reproject] IfcWellKnownText could not be parsed; it cannot place the model.', error);
96
+ return null;
97
+ }
98
+ const missing = missingProjectionParameters(parsed);
99
+ if (missing.length > 0) {
100
+ reportCrsDiagnostic(`[reproject] IfcWellKnownText ${parsed.projName} was read without ${missing.join(', ')} `
101
+ + '(a parameter name the WKT reader does not recognise); it cannot place the model.');
102
+ return { kind: 'refused' };
103
+ }
104
+ // Keyed by the whole WKT: its extent (below) belongs to the registration.
105
+ const name = `${WKT_DEFINITION_PREFIX}${hash(wkt)}`;
106
+ try {
107
+ proj4.defs(name, parsed);
108
+ proj4.defs(`${name}${METRES_SUFFIX}`, (parsed.projName === 'longlat'
109
+ ? parsed
110
+ : { ...parsed, units: 'm', to_meter: 1 }));
111
+ // proj4 resolves the projection lazily; make an unsupported method fail here.
112
+ proj4(name, 'WGS84');
113
+ }
114
+ catch (error) {
115
+ reportCrsDiagnostic('[reproject] IfcWellKnownText uses a projection proj4 does not support; it cannot place the model.', error);
116
+ return null;
117
+ }
118
+ registered.set(name, { parsed, extent: extractWktBoundingBox(wkt) });
119
+ const datum = proj4.Proj(name).datum;
120
+ if (datum?.datum_type === PJD_NODATUM && Math.abs((parsed.a ?? WGS84_SEMI_MAJOR) - WGS84_SEMI_MAJOR) > 1) {
121
+ reportCrsDiagnostic('[reproject] IfcWellKnownText declares a non-WGS84 datum without a datum shift (TOWGS84/BOUNDCRS) '
122
+ + 'that proj4 knows; positions may be off by tens to hundreds of metres.');
123
+ }
124
+ return { kind: 'definition', definition: name };
125
+ }
126
+ /**
127
+ * A registry definition chosen for a WKT that names an EPSG code, wrapped so
128
+ * the WKT's declared extent still guards it: registered under an alias that
129
+ * carries the extent, else returned unchanged when the WKT declares none.
130
+ */
131
+ export function withWktExtent(wkt, definition) {
132
+ const extent = extractWktBoundingBox(wkt.trim());
133
+ if (!extent)
134
+ return definition;
135
+ const name = `${WKT_DEFINITION_PREFIX}${hash(`${wkt.trim()}\n${definition}`)}`;
136
+ if (!registered.has(name)) {
137
+ const geographic = /\+proj=longlat\b/.test(definition);
138
+ proj4.defs(name, definition);
139
+ proj4.defs(`${name}${METRES_SUFFIX}`, geographic
140
+ ? definition
141
+ : `${definition.replace(/(?:^|\s)\+(?:units|to_meter)=\S+/g, '').trim()} +units=m`);
142
+ registered.set(name, { parsed: proj4.defs(name), extent });
143
+ }
144
+ return name;
145
+ }
146
+ /**
147
+ * Whether a WGS84 position lies within the declared extent of validity
148
+ * (WKT2 `USAGE/BBOX`) of the WKT behind `definition`, widened by
149
+ * `marginDegrees`. `true` for any other definition or a WKT without an extent.
150
+ * This is the guard against a definition that parsed but means something else
151
+ * than its author wrote: such a model lands far outside its own area of use.
152
+ */
153
+ export function isWithinWktDefinitionExtent(definition, lon, lat, marginDegrees = 0.5) {
154
+ if (!isWktDefinition(definition))
155
+ return true;
156
+ const box = registration(definition)?.extent;
157
+ if (!box)
158
+ return true;
159
+ const withinLat = lat >= box.south - marginDegrees && lat <= box.north + marginDegrees;
160
+ // An extent crossing the antimeridian has west > east.
161
+ const withinLon = box.west <= box.east
162
+ ? lon >= box.west - marginDegrees && lon <= box.east + marginDegrees
163
+ : lon >= box.west - marginDegrees || lon <= box.east + marginDegrees;
164
+ if (withinLat && withinLon)
165
+ return true;
166
+ const key = wktDefinitionInMetres(definition);
167
+ if (!warnedExtents.has(key)) {
168
+ warnedExtents.add(key);
169
+ reportCrsDiagnostic(`[reproject] IfcWellKnownText places the model at ${lat.toFixed(5)}, ${lon.toFixed(5)}, outside the `
170
+ + `extent its WKT declares (BBOX ${box.south}, ${box.west}, ${box.north}, ${box.east}); refusing the position.`);
171
+ }
172
+ return false;
173
+ }
174
+ //# sourceMappingURL=wkt-projection.js.map