Interactive interface for browsing global, full-resolution satellite imagery
use geographiclib library to calculate area
Jason Kent97a4a2f08a43
diff --git a/package-lock.json b/package-lock.jsonindex 0b2a7e264..877b597fb 100644--- a/package-lock.json+++ b/package-lock.json@@ -8339,6 +8339,11 @@}}},+ "geographiclib": {+ "version": "1.50.0",+ "resolved": "https://registry.npmjs.org/geographiclib/-/geographiclib-1.50.0.tgz",+ "integrity": "sha512-7cVNdWTMuDbj0GQyg5DRAH2w/NGVeIFNmgT//Yi1IuPpWP0aib1T3T8sduIAtpVBe9OLSlRvK0i71OrQg5StFA=="+ },"geojson-equality": {"version": "0.1.6","resolved": "https://registry.npmjs.org/geojson-equality/-/geojson-equality-0.1.6.tgz",diff --git a/package.json b/package.jsonindex 3dca59b07..0e545666c 100644--- a/package.json+++ b/package.json@@ -162,6 +162,7 @@"canvg-browser": "^1.0.0","date-fns": "^1.29.0","file-saver": "^2.0.2",+ "geographiclib": "^1.50.0","history": "^4.7.2","icheck": "^1.0.2","immutability-helper": "^2.9.0",diff --git a/web/js/map/measure/util.js b/web/js/map/measure/util.jsindex 2e48b30f0..8c96a76de 100644--- a/web/js/map/measure/util.js+++ b/web/js/map/measure/util.js@@ -1,14 +1,19 @@-import { MultiLineString as OlGeomMultiLineString, Polygon as OlGeomPolygon } from 'ol/geom';-import { getArea as OlSphereGetArea, getLength as OlSphereGetLength } from 'ol/sphere';import {- area as TurfArea,- polygon as TurfPolygon,+ MultiLineString as OlGeomMultiLineString,+ Polygon as OlGeomPolygon+} from 'ol/geom';+import {+ getLength as OlSphereGetLength+} from 'ol/sphere';+import {rhumbDistance as TurfRhumbDistance,point as TurfPoint} from '@turf/turf';import arc from 'arc';+import geographiclib from 'geographiclib';-const referenceProjection = 'EPSG:4326';+const geod = geographiclib.Geodesic.WGS84;+const geographicProj = 'EPSG:4326';const metersPerKilometer = 1000;const ftPerMile = 5280;const sqFtPerSqMile = 27878400;@@ -23,7 +28,8 @@ const getArcLine = (p1, p2) => {const start = { x: p1[0], y: p1[1] };const end = { x: p2[0], y: p2[1] };const arcGen = new arc.GreatCircle(start, end);- return arcGen.Arc(arcLineResolution, { offset: 10 });+ // Offset is the number of degrees within dateline to split the line+ return arcGen.Arc(arcLineResolution, { offset: 30 });};/**@@ -33,14 +39,14 @@ const getArcLine = (p1, p2) => {*/export function transformLineStringArc(geom, projection) {const coords = [];- const transformedGeom = geom.clone().transform(projection, referenceProjection);+ const transformedGeom = geom.clone().transform(projection, geographicProj);transformedGeom.forEachSegment((segStart, segEnd) => {getArcLine(segStart, segEnd).geometries.forEach((arcGeom) => {coords.push(arcGeom.coords);});});- return new OlGeomMultiLineString(coords).transform(referenceProjection, projection);+ return new OlGeomMultiLineString(coords).transform(geographicProj, projection);};/**@@ -50,7 +56,7 @@ export function transformLineStringArc(geom, projection) {*/export function transformPolygonArc(geom, projection) {let coords = [];- const transformedGeom = geom.clone().transform(projection, referenceProjection);+ const transformedGeom = geom.clone().transform(projection, geographicProj);const polyCoords = transformedGeom.getCoordinates()[0];for (let i = 0; i < polyCoords.length - 1; i++) {const arcLine = getArcLine(polyCoords[i], polyCoords[i + 1]);@@ -58,7 +64,7 @@ export function transformPolygonArc(geom, projection) {coords = coords.concat(arcGeom.coords);});}- return new OlGeomPolygon([coords]).transform(referenceProjection, projection);+ return new OlGeomPolygon([coords]).transform(geographicProj, projection);};/**@@ -90,9 +96,8 @@ export function getFormattedLength(line, projection, unitOfMeasure, useGreatCirc* @return {String} - The formatted area measurement*/export function getFormattedArea(polygon, projection, unitOfMeasure, useGreatCircle) {- const metricArea = useGreatCircle- ? OlSphereGetArea(polygon, { projection })- : getRhumbLineArea(polygon, projection);+ const transformedPoly = polygon.clone().transform(projection, geographicProj);+ const metricArea = getGeographicLibArea(transformedPoly);if (unitOfMeasure === 'km') {return metricArea > 10000@@ -107,14 +112,31 @@ export function getFormattedArea(polygon, projection, unitOfMeasure, useGreatCir}};+/**+ * Calculate area of a polygon with GeographicLib library+ * @param {*} polygon+ * @param {*} projection+ */+export function getGeographicLibArea(polygon) {+ const coordinates = polygon.getCoordinates()[0];+ if (coordinates.length < 3) return 0;+ const geoPoly = geod.Polygon(false);+ coordinates.forEach(coord => {+ // flip lat/lon position+ geoPoly.AddPoint(coord[1], coord[0]);+ });+ const { area } = geoPoly.Compute(false, true);+ return Math.abs(area);+}+/****/export function getRhumbLineDistance(lineString, projection) {let distance = 0;let transformedLine = lineString;- if (projection !== referenceProjection) {- transformedLine = lineString.clone().transform(projection, referenceProjection);+ if (projection !== geographicProj) {+ transformedLine = lineString.clone().transform(projection, geographicProj);}transformedLine.forEachSegment((segStart, segEnd) => {const p1 = TurfPoint(segStart);@@ -124,22 +146,6 @@ export function getRhumbLineDistance(lineString, projection) {return distance * metersPerKilometer;};-/**- *- */-export function getRhumbLineArea(polygon, projection) {- let transformedPoly = polygon;- if (projection !== referenceProjection) {- transformedPoly = polygon.clone().transform(projection, referenceProjection);- }- const coords = polygon.getCoordinates()[0];- if (coords.length < 4) {- return 0;- }- const poly = TurfPolygon(transformedPoly.getCoordinates());- return TurfArea(poly);-}-/*** Convert and format raw measurements to two decimal points* @param {*} measurementdiff --git a/web/js/map/measure/util.test.js b/web/js/map/measure/util.test.jsnew file mode 100644index 000000000..d56fd0e60--- /dev/null+++ b/web/js/map/measure/util.test.js@@ -0,0 +1,73 @@+import {+ getFormattedArea+ // getFormattedLength+} from './util';+// import { getArea as OlSphereGetArea } from 'ol/sphere';+import { Polygon } from 'ol/geom';+import { registerProjections } from '../../fixtures';++beforeEach(registerProjections);++// const miles = 'mi';+const kilos = 'km';++/**+ * Should be able to handle area calculation made crossing the anti-meridian in polar projection+ * where coordinates do NOT extend beyond +/- 180, +/- 90+ */+test('two different triangles of the same size return the same measurement', () => {+ // triangle crosses anti-meridian+ const tri1 = [[0, 90], [-150, 80], [150, 80], [0, 90]];+ // triangle does NOT anti-meridian+ const tri2 = [[0, 90], [-150, 80], [-90, 80], [0, 90]];+ const greatCircleMeasure = false;+ const tri1poly = new Polygon([tri1]);+ const tri2poly = new Polygon([tri2]);++ const tri1Area = getFormattedArea(tri1poly, 'EPSG:4326', kilos, greatCircleMeasure);+ const tri2Area = getFormattedArea(tri2poly, 'EPSG:4326', kilos, greatCircleMeasure);+ expect(tri1Area).toBe(tri2Area);+});++/**+ * Should be able to handle area calculation made crossing the anti-meridian in geographic projection+ * where coordinates DO extend beyond +/- 180, +/- 90 due to map repeating+ */+test('area measurement that includes coordinates outisde of "normal" extents return correct result', () => {+ const normalTri = [[0, 90], [-150, 80], [-90, 80], [0, 90]];+ // partially outside normal longitudinal extents+ const geoTri1 = [[-180, -90], [-150, -80], [-210, -80], [-180, -90]];+ // entirely outside normal longitudinal extents+ const shiftedTri1 = [[-210, -90], [-180, -80], [-240, -80], [-210, -90]];++ const normalTriPoly = new Polygon([normalTri]);+ const geoTriPoly = new Polygon([geoTri1]);+ const shiftedTriPoly = new Polygon([shiftedTri1]);++ const normalTriArea = getFormattedArea(normalTriPoly, 'EPSG:4326', kilos, true);+ const geoTriArea = getFormattedArea(geoTriPoly, 'EPSG:4326', kilos, true);+ const shiftedTriArea = getFormattedArea(shiftedTriPoly, 'EPSG:4326', kilos, true);+ expect(normalTriArea).toBe(geoTriArea);+ expect(normalTriArea).toBe(shiftedTriArea);+ console.log(normalTriArea);+});++// test('two triangles of the same size return the same Rhumb line area measurement', () => {+// const tri1poly = new Polygon([tri1]);+// const tri2poly = new Polygon([tri2]);++// const tri1Area = getRhumbLineArea(tri1poly, 'EPSG:4326');+// const tri2Area = getRhumbLineArea(tri2poly, 'EPSG:4326');+// console.log('Triangle 1: ', tri1Area);+// console.log('Triangle 2: ', tri2Area);++// expect(tri1Area).toBe(tri2Area);+// });++// test('polygon with same coordinates in different projections should return the same measurement', () => {+// const greatCircleMeasure = true;+// const tri1poly = new Polygon([tri1]);+// const proj1Area = getFormattedArea(tri1poly, 'EPSG:3413', kilos, greatCircleMeasure);+// const proj2Area = getFormattedArea(tri1poly, 'EPSG:4326', kilos, greatCircleMeasure);+// expect(proj1Area).toBe(proj2Area);+// });