From 5ab5bf2d628ebf4918eefedae80772b7c687d745 Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Sun, 21 Jul 2019 12:01:53 +1000 Subject: [PATCH 1/6] add MercatorCoordinate toScale method --- src/geo/mercator_coordinate.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/geo/mercator_coordinate.js b/src/geo/mercator_coordinate.js index ee363142420..4272e0ce228 100644 --- a/src/geo/mercator_coordinate.js +++ b/src/geo/mercator_coordinate.js @@ -3,12 +3,16 @@ import LngLat from '../geo/lng_lat'; import type {LngLatLike} from '../geo/lng_lat'; +/* + * The circumference of the world in meters at the equator. + */ +const circumferenceAtEquator = 2 * Math.PI * 6378137; + /* * The circumference of the world in meters at the given latitude. */ function circumferenceAtLatitude(latitude: number) { - const circumference = 2 * Math.PI * 6378137; - return circumference * Math.cos(latitude * Math.PI / 180); + return circumferenceAtEquator * Math.cos(latitude * Math.PI / 180); } export function mercatorXfromLng(lng: number) { @@ -36,6 +40,19 @@ export function altitudeFromMercatorZ(z: number, y: number) { return z * circumferenceAtLatitude(latFromMercatorY(y)); } +/** + * Determine the Mercator scale factor for a given latitude, see + * https://en.wikipedia.org/wiki/Mercator_projection#Scale_factor + * + * At the equator the scale factor will be 1, which increases at higher latitudes. + * + * @param {number} lat Latitude + * @returns {number} scale factor + */ +export function mercatorScale(lat: number) { + return 1 / Math.cos(lat * Math.PI / 180); +} + /** * A `MercatorCoordinate` object represents a projected three dimensional position. * @@ -113,6 +130,16 @@ class MercatorCoordinate { toAltitude() { return altitudeFromMercatorZ(this.z, this.y); } + + /** + * Returns the scale transform to convert real world units (meters) into `MercatorCoordinate`s. + * + * @returns {number} The scale number. + */ + toScale() { + return 1 / circumferenceAtEquator * mercatorScale(latFromMercatorY(this.y)); + } + } export default MercatorCoordinate; From b7c1e1724184cc2e6d417cb082b93b8dd2f956f4 Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Sun, 21 Jul 2019 12:18:13 +1000 Subject: [PATCH 2/6] add unit tests --- test/unit/geo/mercator_coordinate.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unit/geo/mercator_coordinate.test.js b/test/unit/geo/mercator_coordinate.test.js index 4173296b199..e72898e0194 100644 --- a/test/unit/geo/mercator_coordinate.test.js +++ b/test/unit/geo/mercator_coordinate.test.js @@ -1,6 +1,7 @@ import { test } from '../../util/test'; import LngLat from '../../../src/geo/lng_lat'; import MercatorCoordinate from '../../../src/geo/mercator_coordinate'; +import { mercatorScale } from '../../../src/geo/mercator_coordinate'; test('LngLat', (t) => { t.test('#constructor', (t) => { @@ -27,5 +28,17 @@ test('LngLat', (t) => { t.end(); }); + t.test('#mercatorScale', (t) => { + t.equal(mercatorScale(0), 1, 'mercator scale at the equator'); + t.equal(mercatorScale(45), 1.414213562373095, 'mercator scale at 45 degrees latitude'); + t.end(); + }); + + t.test('#toScale', (t) => { + const nullIsland = new LngLat(0, 0); + t.equal(MercatorCoordinate.fromLngLat(nullIsland).toScale(), 2.495320233665337e-8, 'transform scale at the equator'); + t.end(); + }); + t.end(); }); From b24a01c410874905616a1025cfda4a0e17f4777e Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Sun, 21 Jul 2019 12:23:15 +1000 Subject: [PATCH 3/6] fix lint --- test/unit/geo/mercator_coordinate.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/unit/geo/mercator_coordinate.test.js b/test/unit/geo/mercator_coordinate.test.js index e72898e0194..a7e9f1cb19e 100644 --- a/test/unit/geo/mercator_coordinate.test.js +++ b/test/unit/geo/mercator_coordinate.test.js @@ -1,7 +1,6 @@ import { test } from '../../util/test'; import LngLat from '../../../src/geo/lng_lat'; -import MercatorCoordinate from '../../../src/geo/mercator_coordinate'; -import { mercatorScale } from '../../../src/geo/mercator_coordinate'; +import MercatorCoordinate, { mercatorScale } from '../../../src/geo/mercator_coordinate'; test('LngLat', (t) => { t.test('#constructor', (t) => { From 85b05a53d1fea10d80007b234c1e4736e314a185 Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Fri, 26 Jul 2019 14:24:48 +1000 Subject: [PATCH 4/6] rename toScale to meterInMercatorCoordinateUnits --- src/geo/mercator_coordinate.js | 10 +++++++--- test/unit/geo/mercator_coordinate.test.js | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/geo/mercator_coordinate.js b/src/geo/mercator_coordinate.js index 4272e0ce228..76b0e0d41fc 100644 --- a/src/geo/mercator_coordinate.js +++ b/src/geo/mercator_coordinate.js @@ -132,11 +132,15 @@ class MercatorCoordinate { } /** - * Returns the scale transform to convert real world units (meters) into `MercatorCoordinate`s. + * Returns the distance of 1 meter in `MercatorCoordinate` units at this latitude. * - * @returns {number} The scale number. + * For coordinates in real world units using meters, this naturally provides the scale + * to transform into `MercatorCoordinate`s. + * + * @returns {number} Distance of 1 meter in `MercatorCoordinate` units. */ - toScale() { + meterInMercatorCoordinateUnits() { + // 1 meter / circumference at equator in meters * Mercator projection scale factor at this latitude return 1 / circumferenceAtEquator * mercatorScale(latFromMercatorY(this.y)); } diff --git a/test/unit/geo/mercator_coordinate.test.js b/test/unit/geo/mercator_coordinate.test.js index a7e9f1cb19e..d7acb1393da 100644 --- a/test/unit/geo/mercator_coordinate.test.js +++ b/test/unit/geo/mercator_coordinate.test.js @@ -33,9 +33,9 @@ test('LngLat', (t) => { t.end(); }); - t.test('#toScale', (t) => { + t.test('#meterInMercatorCoordinateUnits', (t) => { const nullIsland = new LngLat(0, 0); - t.equal(MercatorCoordinate.fromLngLat(nullIsland).toScale(), 2.495320233665337e-8, 'transform scale at the equator'); + t.equal(MercatorCoordinate.fromLngLat(nullIsland).meterInMercatorCoordinateUnits(), 2.495320233665337e-8, 'length of 1 meter in MercatorCoordinate units at the equator'); t.end(); }); From eb169d750f18812f5766219f3641a1ea8b859520 Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 14 Aug 2019 12:28:44 +1000 Subject: [PATCH 5/6] move docs to external repo --- docs/pages/example/add-3d-model.html | 90 ++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 docs/pages/example/add-3d-model.html diff --git a/docs/pages/example/add-3d-model.html b/docs/pages/example/add-3d-model.html new file mode 100644 index 00000000000..24ec49b537a --- /dev/null +++ b/docs/pages/example/add-3d-model.html @@ -0,0 +1,90 @@ + + +
+ From f8ded799feadd551c5fce710c85f7f1f7815603e Mon Sep 17 00:00:00 2001 From: Andrew Harvey Date: Wed, 14 Aug 2019 12:31:43 +1000 Subject: [PATCH 6/6] move docs to external repo --- docs/pages/example/add-3d-model.html | 90 ---------------------------- 1 file changed, 90 deletions(-) delete mode 100644 docs/pages/example/add-3d-model.html diff --git a/docs/pages/example/add-3d-model.html b/docs/pages/example/add-3d-model.html deleted file mode 100644 index 24ec49b537a..00000000000 --- a/docs/pages/example/add-3d-model.html +++ /dev/null @@ -1,90 +0,0 @@ - - -
-