Skip to content

Commit

Permalink
Different unit types to use for metric measurement configurable.
Browse files Browse the repository at this point in the history
Precision to use for each unit type configurable.
  • Loading branch information
boris-arkenaar committed Jan 3, 2017
1 parent 121c2ca commit 7d2cf43
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 36 deletions.
29 changes: 23 additions & 6 deletions src/draw/handler/Draw.Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ L.Draw.Polygon = L.Draw.Polyline.extend({

options: {
showArea: false,
showLength: false,
shapeOptions: {
stroke: true,
color: '#3388ff',
Expand All @@ -22,7 +23,14 @@ L.Draw.Polygon = L.Draw.Polyline.extend({
fillOpacity: 0.2,
clickable: true
},
metric: true // Whether to use the metric measurement system or imperial
// Whether to use the metric measurement system (truthy) or not (falsy).
// Also defines the units to use for the metric system as an array of
// strings (e.g. `['ha', 'm']`).
metric: false,
feet: true, // When not metric, to use feet instead of yards for display.
nautic: false, // When not metric, not feet use nautic mile for display
// Defines the precision for each type of unit (e.g. {km: 2, ft: 0}
precision: {}
},

// @method initialize(): void
Expand Down Expand Up @@ -58,6 +66,7 @@ L.Draw.Polygon = L.Draw.Polyline.extend({
text = L.drawLocal.draw.handlers.polygon.tooltip.start;
} else if (this._markers.length < 3) {
text = L.drawLocal.draw.handlers.polygon.tooltip.cont;
subtext = this._getMeasurementString();
} else {
text = L.drawLocal.draw.handlers.polygon.tooltip.end;
subtext = this._getMeasurementString();
Expand All @@ -70,15 +79,23 @@ L.Draw.Polygon = L.Draw.Polyline.extend({
},

_getMeasurementString: function () {
var area = this._area;
var area = this._area,
measurementString = '';


if (!area) {
if (!area && !this.options.showLength) {
return null;
}

var distanceStr = L.Draw.Polyline.prototype._getMeasurementString.call(this);
var areaStr = L.GeometryUtil.readableArea(area, this.options.metric);
return distanceStr + '<br>' + areaStr;
if (this.options.showLength) {
measurementString = L.Draw.Polyline.prototype._getMeasurementString.call(this);
}

if (area) {
measurementString += '<br>' + L.GeometryUtil.readableArea(area, this.options.metric, this.options.precision);
}

return measurementString;
},

_shapeIsValid: function () {
Expand Down
4 changes: 2 additions & 2 deletions src/draw/handler/Draw.Polyline.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ L.Draw.Polyline = L.Draw.Feature.extend({
fill: false,
clickable: true
},
metric: true, // Whether to use the metric measurement system or imperial
metric: false, // Whether to use the metric measurement system or imperial
feet: true, // When not metric, to use feet instead of yards for display.
nautic: false, // When not metric, not feet use nautic mile for display
showLength: true, // Whether to display distance in the tooltip
Expand Down Expand Up @@ -512,7 +512,7 @@ L.Draw.Polyline = L.Draw.Feature.extend({
// calculate the distance from the last fixed point to the mouse position
distance = this._measurementRunningTotal + currentLatLng.distanceTo(previousLatLng);

return L.GeometryUtil.readableDistance(distance, this.options.metric, this.options.feet, this.options.nautic);
return L.GeometryUtil.readableDistance(distance, this.options.metric, this.options.feet, this.options.nautic, this.options.precision);
},

_showErrorTooltip: function () {
Expand Down
93 changes: 65 additions & 28 deletions src/ext/GeometryUtil.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
(function() {

var defaultPrecision = {
km: 2,
ha: 2,
m: 0,
mi: 2,
ac: 2,
yd: 0,
ft: 0,
nm: 2
};


/**
* @class L.GeometryUtil
* @aka GeometryUtil
Expand Down Expand Up @@ -27,24 +41,36 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, {

// @method readableArea(area, isMetric): string
// Returns a readable area string in yards or metric
readableArea: function (area, isMetric) {
var areaStr;
readableArea: function (area, isMetric, precision) {
var areaStr,
units,
precision = L.Util.extend({}, defaultPrecision, precision);

if (isMetric) {
if (area >= 10000) {
areaStr = (area * 0.0001).toFixed(2) + ' ha';
units = ['ha', 'm'];
type = typeof isMetric;
if (type === 'string') {
units = [isMetric];
} else if (type !== 'boolean') {
units = isMetric;
}

if (area >= 1000000 && units.indexOf('km') !== -1) {
areaStr = _round(area * 0.000001, precision['km']) + ' km&sup2;';
} else if (area >= 10000 && units.indexOf('ha') !== -1) {
areaStr = _round(area * 0.0001, precision['ha']) + ' ha';
} else {
areaStr = area.toFixed(2) + ' m&sup2;';
areaStr = _round(area, precision['m']) + ' m&sup2;';
}
} else {
area /= 0.836127; // Square yards in 1 meter

if (area >= 3097600) { //3097600 square yards in 1 square mile
areaStr = (area / 3097600).toFixed(2) + ' mi&sup2;';
} else if (area >= 4840) {//48040 square yards in 1 acre
areaStr = (area / 4840).toFixed(2) + ' acres';
areaStr = _round(area / 3097600, precision['mi']) + ' mi&sup2;';
} else if (area >= 4840) { //4840 square yards in 1 acre
areaStr = _round(area / 4840, precision['ac']) + ' acres';
} else {
areaStr = Math.ceil(area) + ' yd&sup2;';
areaStr = _round(area, precision['yd']) + ' yd&sup2;';
}
}

Expand All @@ -57,53 +83,64 @@ L.GeometryUtil = L.extend(L.GeometryUtil || {}, {
// @alternative
// @method readableDistance(distance, isMetric, useFeet, isNauticalMile): string
// Converts metric distance to distance string.
readableDistance: function (distance, isMetric, isFeet, isNauticalMile) {
readableDistance: function (distance, isMetric, isFeet, isNauticalMile, precision) {
var distanceStr,
units;
units,
precision = L.Util.extend({}, defaultPrecision, precision);

if (typeof isMetric == "string") {
units = isMetric;
if (isMetric) {
units = typeof isMetric == 'string' ? isMetric : 'metric';
} else if (isFeet) {
units = 'feet';
} else if (isNauticalMile) {
units = 'nauticalMile';
} else {
if (isFeet) {
units = 'feet';
} else if (isNauticalMile) {
units = 'nauticalMile';
} else if (isMetric) {
units = 'metric';
} else {
units = 'yards';
}
units = 'yards';
}

switch (units) {
case 'metric':
// show metres when distance is < 1km, then show km
if (distance > 1000) {
distanceStr = (distance / 1000).toFixed(2) + ' km';
distanceStr = _round(distance / 1000, precision['km']) + ' km';
} else {
distanceStr = Math.ceil(distance) + ' m';
distanceStr = _round(distance, precision['m']) + ' m';
}
break;
case 'feet':
distance *= 1.09361 * 3;
distanceStr = Math.ceil(distance) + ' ft';
distanceStr = _round(distance, precision['ft']) + ' ft';

break;
case 'nauticalMile':
distance *= 0.53996;
distanceStr = (distance / 1000).toFixed(2) + ' nm';
distanceStr = _round(distance / 1000, precision['nm']) + ' nm';
break;
case 'yards':
default:
distance *= 1.09361;

if (distance > 1760) {
distanceStr = (distance / 1760).toFixed(2) + ' miles';
distanceStr = _round(distance / 1760, precision['mi']) + ' miles';
} else {
distanceStr = Math.ceil(distance) + ' yd';
distanceStr = _round(distance, precision['yd']) + ' yd';
}
break;
}
return distanceStr;
}
});

function _round(value, precision) {
var rounded;

if (precision) {
rounded = value.toFixed(precision);
} else {
rounded = Math.ceil(value);
}

return rounded;
}

})();

0 comments on commit 7d2cf43

Please sign in to comment.