From 702e3d2a90e67b7e6ff314f4fd840ef0e2a89881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Tue, 9 Jan 2018 03:25:55 -0800 Subject: [PATCH] DMS coordinates --- data/core.yaml | 8 ++++++ dist/locales/en.json | 8 ++++++ modules/ui/feature_list.js | 4 +-- modules/ui/panels/location.js | 8 +++--- modules/ui/panels/measurement.js | 33 ++++++++++++--------- modules/util/units.js | 49 ++++++++++++++++++++++++++++++-- 6 files changed, 88 insertions(+), 22 deletions(-) diff --git a/data/core.yaml b/data/core.yaml index 9795047bf..bfcbc11f6 100644 --- a/data/core.yaml +++ b/data/core.yaml @@ -1067,4 +1067,12 @@ en: square_kilometers: "{quantity} km²" hectares: "{quantity} ha" area_pair: "{area1} ({area2})" + arcdegrees: "{quantity}°" + arcminutes: "{quantity}′" + arcseconds: "{quantity}″" + north: "N" + south: "S" + east: "E" + west: "W" + coordinate: "{coordinate}{direction}" coordinate_pair: "{latitude}, {longitude}" diff --git a/dist/locales/en.json b/dist/locales/en.json index 7f9170fd3..b17e1411c 100644 --- a/dist/locales/en.json +++ b/dist/locales/en.json @@ -1233,6 +1233,14 @@ "square_kilometers": "{quantity} km²", "hectares": "{quantity} ha", "area_pair": "{area1} ({area2})", + "arcdegrees": "{quantity}°", + "arcminutes": "{quantity}′", + "arcseconds": "{quantity}″", + "north": "N", + "south": "S", + "east": "E", + "west": "W", + "coordinate": "{coordinate}{direction}", "coordinate_pair": "{latitude}, {longitude}" }, "presets": { diff --git a/modules/ui/feature_list.js b/modules/ui/feature_list.js index 457940143..f0e2e5a3b 100644 --- a/modules/ui/feature_list.js +++ b/modules/ui/feature_list.js @@ -7,7 +7,7 @@ import { d3keybinding as d3_keybinding } from '../lib/d3.keybinding.js'; import * as sexagesimal from '@mapbox/sexagesimal'; import { t } from '../util/locale'; -import { displayCoordinatePair } from '../util/units'; +import { dmsCoordinatePair } from '../util/units'; import { geoExtent, geoChooseEdge } from '../geo'; import { modeSelect } from '../modes'; import { osmEntity } from '../osm'; @@ -144,7 +144,7 @@ export function uiFeatureList(context) { id: -1, geometry: 'point', type: t('inspector.location'), - name: displayCoordinatePair([loc[1], loc[0]]), + name: dmsCoordinatePair([loc[1], loc[0]]), location: loc }); } diff --git a/modules/ui/panels/location.js b/modules/ui/panels/location.js index 3a866a5bf..e79903c11 100644 --- a/modules/ui/panels/location.js +++ b/modules/ui/panels/location.js @@ -1,6 +1,6 @@ import _debounce from 'lodash-es/debounce'; -import { displayCoordinatePair } from '../../util/units'; +import { decimalCoordinatePair, dmsCoordinatePair } from '../../util/units'; import { t } from '../../util/locale'; import { services } from '../../services'; @@ -21,11 +21,11 @@ export function uiPanelLocation(context) { coord = context.map().center(); } - var coordStr = displayCoordinatePair(coord); - list .append('li') - .text(coordStr); + .text(dmsCoordinatePair(coord)) + .append('li') + .text(decimalCoordinatePair(coord)); // Location Info selection diff --git a/modules/ui/panels/measurement.js b/modules/ui/panels/measurement.js index 4300a55af..a8d269cd7 100644 --- a/modules/ui/panels/measurement.js +++ b/modules/ui/panels/measurement.js @@ -8,7 +8,7 @@ import { } from 'd3-geo'; import { t } from '../../util/locale'; -import { displayArea, displayLength, displayCoordinatePair } from '../../util/units'; +import { displayArea, displayLength, decimalCoordinatePair, dmsCoordinatePair } from '../../util/units'; import { geoExtent } from '../../geo'; import { utilDetect } from '../../util/detect'; @@ -78,14 +78,17 @@ export function uiPanelMeasurement(context) { var list = selection .append('ul'); + var coordItem; // multiple features, just display extent center.. if (!singular) { - list + coordItem = list .append('li') - .text(t('info_panels.measurement.center') + ':') - .append('span') - .text(displayCoordinatePair(center)); + .text(t('info_panels.measurement.center') + ':'); + coordItem.append('span') + .text(dmsCoordinatePair(center)); + coordItem.append('span') + .text(decimalCoordinatePair(center)); return; } @@ -132,11 +135,13 @@ export function uiPanelMeasurement(context) { .append('span') .text(displayLength(length, isImperial)); - list + coordItem = list .append('li') - .text(t('info_panels.measurement.centroid') + ':') - .append('span') - .text(displayCoordinatePair(centroid)); + .text(t('info_panels.measurement.centroid') + ':'); + coordItem.append('span') + .text(dmsCoordinatePair(centroid)); + coordItem.append('span') + .text(decimalCoordinatePair(centroid)); var toggle = isImperial ? 'imperial' : 'metric'; @@ -160,11 +165,13 @@ export function uiPanelMeasurement(context) { .append('span') .text(t('geometry.' + geometry)); - list + coordItem = list .append('li') - .text(centerLabel + ':') - .append('span') - .text(displayCoordinatePair(center)); + .text(centerLabel + ':'); + coordItem.append('span') + .text(dmsCoordinatePair(center)); + coordItem.append('span') + .text(decimalCoordinatePair(center)); } } diff --git a/modules/util/units.js b/modules/util/units.js index c7e2ff1da..b27e7c4b7 100644 --- a/modules/util/units.js +++ b/modules/util/units.js @@ -99,13 +99,56 @@ function clamp(x, min, max) { return Math.max(min, Math.min(x, max)); } +function displayCoordinate(deg, pos, neg) { + var min = (Math.abs(deg) - Math.floor(Math.abs(deg))) * 60, + sec = (min - Math.floor(min)) * 60, + displayDegrees = t('units.arcdegrees', { + quantity: Math.floor(Math.abs(deg)).toLocaleString(locale) + }), + displayCoordinate; + + if (Math.floor(sec) > 0) { + displayCoordinate = displayDegrees + + t('units.arcminutes', { quantity: Math.floor(min).toLocaleString(locale) }) + + t('units.arcseconds', { quantity: Math.round(sec).toLocaleString(locale) }); + } else if (Math.floor(min) > 0) { + displayCoordinate = displayDegrees + + t('units.arcminutes', { quantity: Math.round(min).toLocaleString(locale) }); + } else { + displayCoordinate = t('units.arcdegrees', { + quantity: Math.round(Math.abs(deg)).toLocaleString(locale) + }); + } + + if (deg === 0) { + return displayCoordinate; + } else { + return t('units.coordinate', { + coordinate: displayCoordinate, + direction: t('units.' + (deg > 0 ? pos : neg)) + }); + } +} + /** - * Returns a human-readable representation of the given coordinate pair. + * Returns given coordinate pair in degree-minute-second format. * * @param {Array} coord longitude and latitude */ -export function displayCoordinatePair(coord) { - return t('units.coordinate_pair', { +export function dmsCoordinatePair(coord) { + return t('units.coordinate_pair', { + latitude: displayCoordinate(clamp(coord[1], -90, 90), 'north', 'south'), + longitude: displayCoordinate(wrap(coord[0], -180, 180), 'east', 'west') + }); +} + +/** + * Returns the given coordinate pair in decimal format. + * + * @param {Array} coord longitude and latitude + */ +export function decimalCoordinatePair(coord) { + return t('units.coordinate_pair', { latitude: clamp(coord[1], -90, 90).toLocaleString(locale, { maximumFractionDigits: OSM_PRECISION }), longitude: wrap(coord[0], -180, 180).toLocaleString(locale, { maximumFractionDigits: OSM_PRECISION }) });