proj4 2.16.1 → 2.17.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.
package/lib/nadgrid.js CHANGED
@@ -7,10 +7,17 @@
7
7
  var loadedNadgrids = {};
8
8
 
9
9
  /**
10
- * Load a binary NTv2 file (.gsb) to a key that can be used in a proj string like +nadgrids=<key>. Pass the NTv2 file
11
- * as an ArrayBuffer.
10
+ * Load either a NTv2 file (.gsb) or a Geotiff (.tif) to a key that can be used in a proj string like +nadgrids=<key>. Pass the NTv2 file
11
+ * as an ArrayBuffer. Pass Geotiff as a GeoTIFF instance from the geotiff.js library.
12
12
  */
13
13
  export default function nadgrid(key, data, options) {
14
+ if (data instanceof ArrayBuffer) {
15
+ return readNTV2Grid(key, data, options);
16
+ }
17
+ return { ready: readGeotiffGrid(key, data) };
18
+ }
19
+
20
+ function readNTV2Grid(key, data, options) {
14
21
  var includeErrorFields = true;
15
22
  if (options !== undefined && options.includeErrorFields === false) {
16
23
  includeErrorFields = false;
@@ -24,6 +31,51 @@ export default function nadgrid(key, data, options) {
24
31
  return nadgrid;
25
32
  }
26
33
 
34
+ async function readGeotiffGrid(key, tiff) {
35
+ var subgrids = [];
36
+ var subGridCount = await tiff.getImageCount();
37
+ // proj produced tiff grid shift files appear to organize lower res subgrids first, higher res/ child subgrids last.
38
+ for (var subgridIndex = subGridCount - 1; subgridIndex >= 0; subgridIndex--) {
39
+ var image = await tiff.getImage(subgridIndex);
40
+
41
+ var rasters = await image.readRasters();
42
+ var data = rasters;
43
+ var lim = [image.getWidth(), image.getHeight()];
44
+ var imageBBoxRadians = image.getBoundingBox().map(degreesToRadians);
45
+ var del = [image.fileDirectory.ModelPixelScale[0], image.fileDirectory.ModelPixelScale[1]].map(degreesToRadians);
46
+
47
+ var maxX = imageBBoxRadians[0] + (lim[0] - 1) * del[0];
48
+ var minY = imageBBoxRadians[3] - (lim[1] - 1) * del[1];
49
+
50
+ var latitudeOffsetBand = data[0];
51
+ var longitudeOffsetBand = data[1];
52
+ var nodes = [];
53
+
54
+ for (let i = lim[1] - 1; i >= 0; i--) {
55
+ for (let j = lim[0] - 1; j >= 0; j--) {
56
+ var index = i * lim[0] + j;
57
+ nodes.push([-secondsToRadians(longitudeOffsetBand[index]), secondsToRadians(latitudeOffsetBand[index])]);
58
+ }
59
+ }
60
+
61
+ subgrids.push({
62
+ del: del,
63
+ lim: lim,
64
+ ll: [-maxX, minY],
65
+ cvs: nodes
66
+ });
67
+ }
68
+
69
+ var tifGrid = {
70
+ header: {
71
+ nSubgrids: subGridCount
72
+ },
73
+ subgrids: subgrids
74
+ };
75
+ loadedNadgrids[key] = tifGrid;
76
+ return tifGrid;
77
+ };
78
+
27
79
  /**
28
80
  * Given a proj4 value for nadgrids, return an array of loaded grids
29
81
  */
@@ -55,6 +107,10 @@ function parseNadgridString(value) {
55
107
  };
56
108
  }
57
109
 
110
+ function degreesToRadians(degrees) {
111
+ return (degrees) * Math.PI / 180;
112
+ }
113
+
58
114
  function secondsToRadians(seconds) {
59
115
  return (seconds / 3600) * Math.PI / 180;
60
116
  }
@@ -115,6 +171,10 @@ function readSubgrids(view, header, isLittleEndian, includeErrorFields) {
115
171
  return grids;
116
172
  }
117
173
 
174
+ /**
175
+ * @param {*} nodes
176
+ * @returns Array<Array<number>>
177
+ */
118
178
  function mapNodes(nodes) {
119
179
  return nodes.map(function (r) {
120
180
  return [secondsToRadians(r.longitudeShift), secondsToRadians(r.latitudeShift)];
package/package.json CHANGED
@@ -1,14 +1,10 @@
1
1
  {
2
2
  "name": "proj4",
3
- "version": "2.16.1",
3
+ "version": "2.17.0",
4
4
  "description": "Proj4js is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations.",
5
5
  "homepage": "https://proj4js.github.io/proj4js/",
6
6
  "main": "dist/proj4-src.js",
7
7
  "module": "lib/index.js",
8
- "directories": {
9
- "test": "test",
10
- "doc": "docs"
11
- },
12
8
  "scripts": {
13
9
  "prepare": "npm run build && npm run test:all",
14
10
  "build": "grunt && npm run rollup",
@@ -38,6 +34,7 @@
38
34
  "@stylistic/eslint-plugin": "^4.2.0",
39
35
  "chai": "^5.0.0",
40
36
  "eslint": "^9.25.1",
37
+ "geotiff": "^2.1.3",
41
38
  "grunt": "^1.6.1",
42
39
  "grunt-cli": "^1.4.3",
43
40
  "mocha": "^10.2.0",
@@ -48,6 +45,6 @@
48
45
  "dependencies": {
49
46
  "geographiclib-geodesic": "^2.1.1",
50
47
  "mgrs": "1.0.0",
51
- "wkt-parser": "^1.5.0"
48
+ "wkt-parser": "^1.5.1"
52
49
  }
53
50
  }
package/scripts/readme.md DELETED
@@ -1,10 +0,0 @@
1
- The script in this directory can be used to update `lib/constants/Datum.js` with data from a [`proj.db`](https://proj.org/en/stable/resource_files.html#proj-db) file placed in the root of the repository.
2
-
3
- After running the script, the formatting needs to be fixed.
4
-
5
- A complete command sequence (issued from the root of the repository) would look like this:
6
-
7
- npm install sqlite3
8
- node scripts/updateDatums.mjs
9
- npm uninstall sqlite3
10
- npm run format
@@ -1,119 +0,0 @@
1
- import sqlite3 from 'sqlite3';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { fileURLToPath } from 'url';
5
- import datums from '../lib/constants/Datum.js'; // Import the datums object directly
6
-
7
- const DATUM_OVERRIDES = {
8
- EPSG_4149: {
9
- towgs84: '674.374,15.056,405.346'
10
- },
11
- EPSG_4267: {
12
- towgs84: '-8.0,160.0,176.0'
13
- },
14
- EPSG_4818: {
15
- towgs84: '589,76,480'
16
- }
17
- };
18
-
19
- // Get the current file's directory
20
- const __filename = fileURLToPath(import.meta.url);
21
- const __dirname = path.dirname(__filename);
22
-
23
- // Open proj.db
24
- const dbPath = path.resolve(__dirname, '../proj.db');
25
- const db = new sqlite3.Database(dbPath);
26
-
27
- for (const key in datums) {
28
- if (key === datums[key].datumName && datums[datums[key].datumName] === datums[key]) {
29
- delete datums[key];
30
- }
31
- }
32
-
33
- const databaseDatumNames = new Set();
34
-
35
- // Query proj.db for Helmert transforms with method_code = 9606
36
- db.all(
37
- `SELECT ht.source_crs_auth_name,
38
- ht.source_crs_code,
39
- ht.tx, ht.ty, ht.tz,
40
- ht.rx, ht.ry, ht.rz,
41
- ht.scale_difference,
42
- ht.accuracy,
43
- gd.name AS datum_name,
44
- cv.name AS datum_code,
45
- e.name AS ellipse_name,
46
- ht.method_code
47
- FROM helmert_transformation ht
48
- JOIN crs_view cv
49
- ON ht.source_crs_auth_name = cv.auth_name
50
- AND ht.source_crs_code = cv.code
51
- JOIN geodetic_crs gcrs
52
- ON cv.table_name = 'geodetic_crs'
53
- AND cv.auth_name = gcrs.auth_name
54
- AND cv.code = gcrs.code
55
- JOIN geodetic_datum gd
56
- ON gcrs.datum_auth_name = gd.auth_name
57
- AND gcrs.datum_code = gd.code
58
- JOIN ellipsoid e
59
- ON gd.ellipsoid_auth_name = e.auth_name
60
- AND gd.ellipsoid_code = e.code
61
- WHERE ht.deprecated = 0
62
- AND ht.method_code IN (9606, 9607, 9603)
63
- AND (ht.target_crs_auth_name = 'EPSG' AND ht.target_crs_code IN ('4326', '7019', '4258')) -- WGS84, GRS80, ETRS89
64
- AND cv.type != 'vertical' -- Exclude vertical datums
65
- ORDER BY
66
- ht.accuracy ASC,
67
- CASE ht.method_code
68
- WHEN 9606 THEN 1
69
- WHEN 9607 THEN 2
70
- WHEN 9603 THEN 3
71
- ELSE 4
72
- END ASC`, // Sort by method_code (9606 first, then 9607), then by accuracy`, // Assuming lower accuracy values are better
73
- (err, rows) => {
74
- if (err) {
75
- console.error('Error querying proj.db:', err);
76
- return;
77
- }
78
-
79
- rows.forEach((row) => {
80
- const normalizedDatumName = row.datum_name.toLowerCase();
81
-
82
- // Skip if the datumName already exists (case-insensitive)
83
- if (databaseDatumNames.has(normalizedDatumName)) {
84
- return;
85
- }
86
-
87
- if (row.method_code === 9607 && row.rx) {
88
- row.rx = -row.rx;
89
- row.ry = -row.ry;
90
- row.rz = -row.rz;
91
- }
92
-
93
- // Construct the towgs84 string from tx, ty, tz, rx, ry, rz, and scale_difference
94
- const towgs84 = row.rx ? `${row.tx},${row.ty},${row.tz},${row.rx},${row.ry},${row.rz},${row.scale_difference}` : `${row.tx},${row.ty},${row.tz}`;
95
-
96
- datums[`${row.source_crs_auth_name}_${row.source_crs_code}`] = {
97
- towgs84
98
- };
99
-
100
- databaseDatumNames.add(normalizedDatumName);
101
- });
102
-
103
- Object.assign(datums, DATUM_OVERRIDES);
104
-
105
- // Write updated datums back to Datum.js
106
- const datumFilePath = path.resolve(__dirname, '../lib/constants/Datum.js');
107
- const updatedContent = `var datums = ${JSON.stringify(datums, null, 2)};\n\n`
108
- + `for (var key in datums) {\n`
109
- + ` var datum = datums[key];\n`
110
- + ` if (!datum.datumName) {\n`
111
- + ` continue;\n`
112
- + ` }\n`
113
- + ` datums[datum.datumName] = datum;\n`
114
- + `}\n\n`
115
- + `export default datums;`;
116
- fs.writeFileSync(datumFilePath, updatedContent, 'utf-8');
117
- console.log('Datum.js updated successfully!');
118
- }
119
- );
@@ -1,70 +0,0 @@
1
- import sqlite3 from 'sqlite3';
2
- import fs from 'fs';
3
- import path from 'path';
4
- import datums from '../lib/constants/Datum.js'; // Import datums directly
5
- import ellipsoids from '../lib/constants/Ellipsoid.js'; // Import ellipsoids directly
6
- import { fileURLToPath } from 'url';
7
-
8
- // Get the current file's directory
9
- const __filename = fileURLToPath(import.meta.url);
10
- const __dirname = path.dirname(__filename);
11
-
12
- // Extract ellipsoid keys and ellipseName values from Ellipsoid.js
13
- const ellipsoidKeys = Object.keys(ellipsoids);
14
- const ellipsoidNames = Object.values(ellipsoids).map(ellipsoid => ellipsoid.ellipseName);
15
-
16
- // Combine ellipsoid keys and ellipseName values into a single set
17
- const ellipsoidSet = new Set([...ellipsoidKeys, ...ellipsoidNames]);
18
-
19
- // Extract ellipse values from Datum.js
20
- const datumEllipses = new Set(Object.values(datums).map(datum => datum.ellipse));
21
-
22
- // Find missing ellipses
23
- const missingEllipses = [...datumEllipses].filter(ellipse => !ellipsoidSet.has(ellipse));
24
-
25
- // If no missing ellipses, exit early
26
- if (missingEllipses.length === 0) {
27
- console.log('No missing ellipses found.');
28
- process.exit(0);
29
- }
30
-
31
- // Open proj.db
32
- const dbPath = path.resolve(__dirname, '../proj.db');
33
- const db = new sqlite3.Database(dbPath);
34
-
35
- // Query the ellipsoid table for missing ellipses
36
- const placeholders = missingEllipses.map(() => '?').join(',');
37
- const query = `
38
- SELECT e.auth_name || ':' || e.code AS key,
39
- e.semi_major_axis AS a,
40
- e.inv_flattening AS rf,
41
- e.name AS ellipseName
42
- FROM ellipsoid e
43
- WHERE e.name IN (${placeholders})
44
- `;
45
-
46
- db.all(query, missingEllipses, (err, rows) => {
47
- if (err) {
48
- console.error('Error querying proj.db:', err);
49
- return;
50
- }
51
-
52
- // Add missing ellipsoids to the ellipsoids object
53
- rows.forEach((row) => {
54
- const key = row.key.replace(/:/g, '_'); // Replace ":" with "_" for valid JavaScript keys
55
- ellipsoids[key] = {
56
- a: row.a,
57
- rf: row.rf,
58
- ellipseName: row.ellipseName
59
- };
60
- });
61
-
62
- // Write the updated ellipsoids object back to Ellipsoid.js
63
- const ellipsoidFilePath = path.resolve(__dirname, '../lib/constants/Ellipsoid.js');
64
- const ellipsoidContent = `var ellipsoids = ${JSON.stringify(ellipsoids, null, 2)};\n\nexport default ellipsoids;`;
65
- fs.writeFileSync(ellipsoidFilePath, ellipsoidContent, 'utf-8');
66
- console.log(`Ellipsoid.js updated successfully with ${rows.length} missing ellipsoids.`);
67
- });
68
-
69
- // Close the database connection
70
- db.close();
package/test/BETA2007.gsb DELETED
Binary file
Binary file
package/test/opt.html DELETED
@@ -1,28 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8">
5
- <title>Mocha Tests</title>
6
- <link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
7
- </head>
8
- <body><div id="mocha"></div>
9
- <script src="../node_modules/mocha/mocha.js"></script>
10
- <script src="../node_modules/chai/chai.js" type="module"></script>
11
- <script src="testData.js"></script>
12
- <script src="../dist/proj4.js"></script>
13
- <script src="test.js"></script>
14
- <script type="module">
15
- import * as chai from '../node_modules/chai/chai.js';
16
-
17
- mocha.setup({
18
- reporter: "JSON",
19
- ui: 'bdd',
20
- globals: [],
21
- timeout: 300000,
22
- checkLeaks: false
23
- });
24
- startTests(chai,proj4,testPoints);
25
- mocha.run();
26
- </script>
27
- </body>
28
- </html>
@@ -1,110 +0,0 @@
1
- import puppeteer from 'puppeteer';
2
- import http from 'http';
3
- import fs from 'fs';
4
- import path from 'path';
5
- import assert from 'assert';
6
-
7
- const hostname = process.env.HOST || '127.0.0.1'; // use hostname 127.0.0.1 unless there exists a preconfigured port
8
- const port = process.env.PORT || 8080; // use port 8080 unless there exists a preconfigured port
9
-
10
- const server = http.createServer(function (request, response) {
11
- let filePath = request.url;
12
-
13
- if (filePath === '/') {
14
- filePath = 'index.html';
15
- } else {
16
- filePath = './' + request.url;
17
- }
18
-
19
- let extname = String(path.extname(filePath)).toLowerCase();
20
- let mimeTypes = {
21
- '.html': 'text/html',
22
- '.js': 'text/javascript',
23
- '.css': 'text/css',
24
- '.json': 'application/json',
25
- '.png': 'image/png',
26
- '.jpg': 'image/jpg',
27
- '.gif': 'image/gif',
28
- '.svg': 'image/svg+xml',
29
- '.wav': 'audio/wav',
30
- '.mp4': 'video/mp4',
31
- '.woff': 'application/font-woff',
32
- '.ttf': 'application/font-ttf',
33
- '.eot': 'application/vnd.ms-fontobject',
34
- '.otf': 'application/font-otf',
35
- '.wasm': 'application/wasm',
36
- '.ico': 'image/x-icon'
37
- };
38
-
39
- let contentType = mimeTypes[extname] || 'application/octet-stream';
40
-
41
- fs.readFile(filePath, function (error, content) {
42
- if (error) {
43
- if (error.code === 'ENOENT') {
44
- fs.readFile('public/404.html', function (error, content) {
45
- response.writeHead(404, { 'Content-Type': 'text/html' });
46
- response.end(content, 'utf-8');
47
- });
48
- } else {
49
- response.writeHead(500);
50
- response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
51
- }
52
- } else {
53
- response.writeHead(200, { 'Content-Type': contentType });
54
- response.end(content, 'utf-8');
55
- }
56
- });
57
- });
58
- function timeoutPromise(timeout, callback) {
59
- return new Promise((resolve, reject) => {
60
- // Set up the timeout
61
- const timer = setTimeout(() => {
62
- reject(new Error(`Promise timed out after ${timeout} ms`));
63
- }, timeout);
64
-
65
- // Set up the real work
66
- callback(
67
- (value) => {
68
- clearTimeout(timer);
69
- resolve(value);
70
- },
71
- (error) => {
72
- clearTimeout(timer);
73
- reject(error);
74
- }
75
- );
76
- });
77
- }
78
- (async () => {
79
- server.listen(port, hostname);
80
-
81
- // Launch the browser and open a new blank page
82
-
83
- const browser = await puppeteer.launch({ headless: 'new', args: ['--no-sandbox'] });
84
- const page = await browser.newPage();
85
-
86
- // Navigate the page to a URL
87
- await page.goto('http://' + hostname + ':' + port + '/test/opt.html');
88
-
89
- // Set screen size
90
- await page.setViewport({ width: 1080, height: 1024 });
91
-
92
- // Type into search box
93
- const testResult = await timeoutPromise(10000, (resolve) => {
94
- page.on('console', (consoleMessage) => {
95
- if (consoleMessage.type() === 'log') {
96
- const res = JSON.parse(consoleMessage.text());
97
- if (res.stats !== undefined) {
98
- resolve(res.stats);
99
- }
100
- }
101
- });
102
- });
103
-
104
- assert.strictEqual(testResult.failures, 0, 'Tests: passed: ' + testResult.passes + ', fail: ' + testResult.failures + ', total:' + testResult.tests);
105
- assert.strictEqual(testResult.tests, testResult.passes, 'Tests: ' + testResult.passes + '/' + testResult.tests);
106
- console.log('Tests: ' + testResult.passes + '/' + testResult.tests);
107
-
108
- await browser.close();
109
- server.close();
110
- })();
package/test/test-ci.mjs DELETED
@@ -1,10 +0,0 @@
1
- import * as chai from 'chai';
2
- import proj4 from '../dist/proj4-src.js';
3
- import testData from './testData.js';
4
- import startTests from './test.js';
5
-
6
- if (typeof process !== 'undefined' && process.toString() === '[object process]') {
7
- (async function () {
8
- startTests(chai, proj4, testData);
9
- })();
10
- }