@itowns/geographic 2.46.1-next.26 → 2.46.1-next.27

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 (3) hide show
  1. package/lib/Crs.js +31 -6
  2. package/package.json +1 -1
  3. package/src/Crs.ts +33 -6
package/lib/Crs.js CHANGED
@@ -160,14 +160,39 @@ export function axisOrder(crs) {
160
160
 
161
161
  /**
162
162
  * Defines a proj4 projection as a named alias.
163
- * This function is a specialized wrapper over the
164
- * [`proj4.defs`](https://github.com/proj4js/proj4js#named-projections)
165
- * function.
163
+ * This function is an alias for the
164
+ * [`proj4.defs`](https://github.com/proj4js/proj4js#named-projections) function.
165
+ */
166
+ export const defs = proj4.defs;
167
+
168
+ /**
169
+ * Fetches a CRS definition from epsg.io and registers it with proj4.
170
+ * If the CRS is already defined, returns the existing definition.
171
+ *
172
+ * @param crs - The EPSG code string (e.g. "EPSG:2154").
173
+ * @returns The proj4 projection definition.
166
174
  *
167
- * @param code - Named alias of the currently defined projection.
168
- * @param proj4def - Proj4 or WKT string of the defined projection.
175
+ * @example
176
+ * // Register EPSG:2154 (RGF93 / Lambert-93)
177
+ * await CRS.fromEPSG('EPSG:2154');
178
+ *
179
+ * // Register EPSG:4269 (NAD83)
180
+ * await CRS.fromEPSG('EPSG:4269');
169
181
  */
170
- export const defs = (code, proj4def) => proj4.defs(code, proj4def);
182
+ export async function fromEPSG(crs) {
183
+ const def = proj4.defs(crs);
184
+ if (def) {
185
+ return def;
186
+ }
187
+ const code = crs.replace(/^EPSG:/i, '');
188
+ const response = await fetch(`https://epsg.io/${code}.proj4`);
189
+ if (!response.ok) {
190
+ throw new Error(`Failed to fetch EPSG:${code} from epsg.io: ${response.status}`);
191
+ }
192
+ const proj4def = await response.text();
193
+ proj4.defs(crs, proj4def);
194
+ return proj4.defs(crs);
195
+ }
171
196
  export function defsFromWkt(wkt) {
172
197
  proj4.defs('unknown', wkt);
173
198
  const proj4Defs = proj4.defs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itowns/geographic",
3
- "version": "2.46.1-next.26",
3
+ "version": "2.46.1-next.27",
4
4
  "description": "Geodesy",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
package/src/Crs.ts CHANGED
@@ -173,14 +173,41 @@ export function axisOrder(crs: ProjectionLike) {
173
173
 
174
174
  /**
175
175
  * Defines a proj4 projection as a named alias.
176
- * This function is a specialized wrapper over the
177
- * [`proj4.defs`](https://github.com/proj4js/proj4js#named-projections)
178
- * function.
176
+ * This function is an alias for the
177
+ * [`proj4.defs`](https://github.com/proj4js/proj4js#named-projections) function.
178
+ */
179
+ export const defs = proj4.defs;
180
+
181
+ /**
182
+ * Fetches a CRS definition from epsg.io and registers it with proj4.
183
+ * If the CRS is already defined, returns the existing definition.
184
+ *
185
+ * @param crs - The EPSG code string (e.g. "EPSG:2154").
186
+ * @returns The proj4 projection definition.
187
+ *
188
+ * @example
189
+ * // Register EPSG:2154 (RGF93 / Lambert-93)
190
+ * await CRS.fromEPSG('EPSG:2154');
179
191
  *
180
- * @param code - Named alias of the currently defined projection.
181
- * @param proj4def - Proj4 or WKT string of the defined projection.
192
+ * // Register EPSG:4269 (NAD83)
193
+ * await CRS.fromEPSG('EPSG:4269');
182
194
  */
183
- export const defs = (code: string, proj4def: string) => proj4.defs(code, proj4def);
195
+ export async function fromEPSG(crs: string): Promise<ProjectionDefinition> {
196
+ const def = proj4.defs(crs);
197
+ if (def) {
198
+ return def;
199
+ }
200
+
201
+ const code = crs.replace(/^EPSG:/i, '');
202
+ const response = await fetch(`https://epsg.io/${code}.proj4`);
203
+ if (!response.ok) {
204
+ throw new Error(`Failed to fetch EPSG:${code} from epsg.io: ${response.status}`);
205
+ }
206
+
207
+ const proj4def = await response.text();
208
+ proj4.defs(crs, proj4def);
209
+ return proj4.defs(crs);
210
+ }
184
211
 
185
212
  export function defsFromWkt(wkt: string) {
186
213
  proj4.defs('unknown', wkt);