Files
openlayers/src/ol/coordinate.js
Tim Schaub ad62739a6e Use blocked scoped variables
In addition to using const and let, this also upgrades our linter config and removes lint (mostly whitespace).
2018-01-12 00:50:30 -07:00

380 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @module ol/coordinate
*/
import {modulo} from './math.js';
import _ol_string_ from './string.js';
const _ol_coordinate_ = {};
/**
* Add `delta` to `coordinate`. `coordinate` is modified in place and returned
* by the function.
*
* Example:
*
* var coord = [7.85, 47.983333];
* ol.coordinate.add(coord, [-2, 4]);
* // coord is now [5.85, 51.983333]
*
* @param {ol.Coordinate} coordinate Coordinate.
* @param {ol.Coordinate} delta Delta.
* @return {ol.Coordinate} The input coordinate adjusted by the given delta.
* @api
*/
_ol_coordinate_.add = function(coordinate, delta) {
coordinate[0] += delta[0];
coordinate[1] += delta[1];
return coordinate;
};
/**
* Calculates the point closest to the passed coordinate on the passed circle.
*
* @param {ol.Coordinate} coordinate The coordinate.
* @param {ol.geom.Circle} circle The circle.
* @return {ol.Coordinate} Closest point on the circumference
*/
_ol_coordinate_.closestOnCircle = function(coordinate, circle) {
const r = circle.getRadius();
const center = circle.getCenter();
const x0 = center[0];
const y0 = center[1];
const x1 = coordinate[0];
const y1 = coordinate[1];
let dx = x1 - x0;
const dy = y1 - y0;
if (dx === 0 && dy === 0) {
dx = 1;
}
const d = Math.sqrt(dx * dx + dy * dy);
const x = x0 + r * dx / d;
const y = y0 + r * dy / d;
return [x, y];
};
/**
* Calculates the point closest to the passed coordinate on the passed segment.
* This is the foot of the perpendicular of the coordinate to the segment when
* the foot is on the segment, or the closest segment coordinate when the foot
* is outside the segment.
*
* @param {ol.Coordinate} coordinate The coordinate.
* @param {Array.<ol.Coordinate>} segment The two coordinates of the segment.
* @return {ol.Coordinate} The foot of the perpendicular of the coordinate to
* the segment.
*/
_ol_coordinate_.closestOnSegment = function(coordinate, segment) {
const x0 = coordinate[0];
const y0 = coordinate[1];
const start = segment[0];
const end = segment[1];
const x1 = start[0];
const y1 = start[1];
const x2 = end[0];
const y2 = end[1];
const dx = x2 - x1;
const dy = y2 - y1;
const along = (dx === 0 && dy === 0) ? 0 :
((dx * (x0 - x1)) + (dy * (y0 - y1))) / ((dx * dx + dy * dy) || 0);
let x, y;
if (along <= 0) {
x = x1;
y = y1;
} else if (along >= 1) {
x = x2;
y = y2;
} else {
x = x1 + along * dx;
y = y1 + along * dy;
}
return [x, y];
};
/**
* Returns a {@link ol.CoordinateFormatType} function that can be used to format
* a {ol.Coordinate} to a string.
*
* Example without specifying the fractional digits:
*
* var coord = [7.85, 47.983333];
* var stringifyFunc = ol.coordinate.createStringXY();
* var out = stringifyFunc(coord);
* // out is now '8, 48'
*
* Example with explicitly specifying 2 fractional digits:
*
* var coord = [7.85, 47.983333];
* var stringifyFunc = ol.coordinate.createStringXY(2);
* var out = stringifyFunc(coord);
* // out is now '7.85, 47.98'
*
* @param {number=} opt_fractionDigits The number of digits to include
* after the decimal point. Default is `0`.
* @return {ol.CoordinateFormatType} Coordinate format.
* @api
*/
_ol_coordinate_.createStringXY = function(opt_fractionDigits) {
return (
/**
* @param {ol.Coordinate|undefined} coordinate Coordinate.
* @return {string} String XY.
*/
function(coordinate) {
return _ol_coordinate_.toStringXY(coordinate, opt_fractionDigits);
}
);
};
/**
* @param {string} hemispheres Hemispheres.
* @param {number} degrees Degrees.
* @param {number=} opt_fractionDigits The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} String.
*/
_ol_coordinate_.degreesToStringHDMS = function(hemispheres, degrees, opt_fractionDigits) {
const normalizedDegrees = modulo(degrees + 180, 360) - 180;
const x = Math.abs(3600 * normalizedDegrees);
const dflPrecision = opt_fractionDigits || 0;
const precision = Math.pow(10, dflPrecision);
let deg = Math.floor(x / 3600);
let min = Math.floor((x - deg * 3600) / 60);
let sec = x - (deg * 3600) - (min * 60);
sec = Math.ceil(sec * precision) / precision;
if (sec >= 60) {
sec = 0;
min += 1;
}
if (min >= 60) {
min = 0;
deg += 1;
}
return deg + '\u00b0 ' + _ol_string_.padNumber(min, 2) + '\u2032 ' +
_ol_string_.padNumber(sec, 2, dflPrecision) + '\u2033' +
(normalizedDegrees == 0 ? '' : ' ' + hemispheres.charAt(normalizedDegrees < 0 ? 1 : 0));
};
/**
* Transforms the given {@link ol.Coordinate} to a string using the given string
* template. The strings `{x}` and `{y}` in the template will be replaced with
* the first and second coordinate values respectively.
*
* Example without specifying the fractional digits:
*
* var coord = [7.85, 47.983333];
* var template = 'Coordinate is ({x}|{y}).';
* var out = ol.coordinate.format(coord, template);
* // out is now 'Coordinate is (8|48).'
*
* Example explicitly specifying the fractional digits:
*
* var coord = [7.85, 47.983333];
* var template = 'Coordinate is ({x}|{y}).';
* var out = ol.coordinate.format(coord, template, 2);
* // out is now 'Coordinate is (7.85|47.98).'
*
* @param {ol.Coordinate|undefined} coordinate Coordinate.
* @param {string} template A template string with `{x}` and `{y}` placeholders
* that will be replaced by first and second coordinate values.
* @param {number=} opt_fractionDigits The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} Formatted coordinate.
* @api
*/
_ol_coordinate_.format = function(coordinate, template, opt_fractionDigits) {
if (coordinate) {
return template
.replace('{x}', coordinate[0].toFixed(opt_fractionDigits))
.replace('{y}', coordinate[1].toFixed(opt_fractionDigits));
} else {
return '';
}
};
/**
* @param {ol.Coordinate} coordinate1 First coordinate.
* @param {ol.Coordinate} coordinate2 Second coordinate.
* @return {boolean} Whether the passed coordinates are equal.
*/
_ol_coordinate_.equals = function(coordinate1, coordinate2) {
let equals = true;
for (let i = coordinate1.length - 1; i >= 0; --i) {
if (coordinate1[i] != coordinate2[i]) {
equals = false;
break;
}
}
return equals;
};
/**
* Rotate `coordinate` by `angle`. `coordinate` is modified in place and
* returned by the function.
*
* Example:
*
* var coord = [7.85, 47.983333];
* var rotateRadians = Math.PI / 2; // 90 degrees
* ol.coordinate.rotate(coord, rotateRadians);
* // coord is now [-47.983333, 7.85]
*
* @param {ol.Coordinate} coordinate Coordinate.
* @param {number} angle Angle in radian.
* @return {ol.Coordinate} Coordinate.
* @api
*/
_ol_coordinate_.rotate = function(coordinate, angle) {
const cosAngle = Math.cos(angle);
const sinAngle = Math.sin(angle);
const x = coordinate[0] * cosAngle - coordinate[1] * sinAngle;
const y = coordinate[1] * cosAngle + coordinate[0] * sinAngle;
coordinate[0] = x;
coordinate[1] = y;
return coordinate;
};
/**
* Scale `coordinate` by `scale`. `coordinate` is modified in place and returned
* by the function.
*
* Example:
*
* var coord = [7.85, 47.983333];
* var scale = 1.2;
* ol.coordinate.scale(coord, scale);
* // coord is now [9.42, 57.5799996]
*
* @param {ol.Coordinate} coordinate Coordinate.
* @param {number} scale Scale factor.
* @return {ol.Coordinate} Coordinate.
*/
_ol_coordinate_.scale = function(coordinate, scale) {
coordinate[0] *= scale;
coordinate[1] *= scale;
return coordinate;
};
/**
* Subtract `delta` to `coordinate`. `coordinate` is modified in place and
* returned by the function.
*
* @param {ol.Coordinate} coordinate Coordinate.
* @param {ol.Coordinate} delta Delta.
* @return {ol.Coordinate} Coordinate.
*/
_ol_coordinate_.sub = function(coordinate, delta) {
coordinate[0] -= delta[0];
coordinate[1] -= delta[1];
return coordinate;
};
/**
* @param {ol.Coordinate} coord1 First coordinate.
* @param {ol.Coordinate} coord2 Second coordinate.
* @return {number} Squared distance between coord1 and coord2.
*/
_ol_coordinate_.squaredDistance = function(coord1, coord2) {
const dx = coord1[0] - coord2[0];
const dy = coord1[1] - coord2[1];
return dx * dx + dy * dy;
};
/**
* @param {ol.Coordinate} coord1 First coordinate.
* @param {ol.Coordinate} coord2 Second coordinate.
* @return {number} Distance between coord1 and coord2.
*/
_ol_coordinate_.distance = function(coord1, coord2) {
return Math.sqrt(_ol_coordinate_.squaredDistance(coord1, coord2));
};
/**
* Calculate the squared distance from a coordinate to a line segment.
*
* @param {ol.Coordinate} coordinate Coordinate of the point.
* @param {Array.<ol.Coordinate>} segment Line segment (2 coordinates).
* @return {number} Squared distance from the point to the line segment.
*/
_ol_coordinate_.squaredDistanceToSegment = function(coordinate, segment) {
return _ol_coordinate_.squaredDistance(coordinate,
_ol_coordinate_.closestOnSegment(coordinate, segment));
};
/**
* Format a geographic coordinate with the hemisphere, degrees, minutes, and
* seconds.
*
* Example without specifying fractional digits:
*
* var coord = [7.85, 47.983333];
* var out = ol.coordinate.toStringHDMS(coord);
* // out is now '47° 58 60″ N 7° 50 60″ E'
*
* Example explicitly specifying 1 fractional digit:
*
* var coord = [7.85, 47.983333];
* var out = ol.coordinate.toStringHDMS(coord, 1);
* // out is now '47° 58 60.0″ N 7° 50 60.0″ E'
*
* @param {ol.Coordinate|undefined} coordinate Coordinate.
* @param {number=} opt_fractionDigits The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} Hemisphere, degrees, minutes and seconds.
* @api
*/
_ol_coordinate_.toStringHDMS = function(coordinate, opt_fractionDigits) {
if (coordinate) {
return _ol_coordinate_.degreesToStringHDMS('NS', coordinate[1], opt_fractionDigits) + ' ' +
_ol_coordinate_.degreesToStringHDMS('EW', coordinate[0], opt_fractionDigits);
} else {
return '';
}
};
/**
* Format a coordinate as a comma delimited string.
*
* Example without specifying fractional digits:
*
* var coord = [7.85, 47.983333];
* var out = ol.coordinate.toStringXY(coord);
* // out is now '8, 48'
*
* Example explicitly specifying 1 fractional digit:
*
* var coord = [7.85, 47.983333];
* var out = ol.coordinate.toStringXY(coord, 1);
* // out is now '7.8, 48.0'
*
* @param {ol.Coordinate|undefined} coordinate Coordinate.
* @param {number=} opt_fractionDigits The number of digits to include
* after the decimal point. Default is `0`.
* @return {string} XY.
* @api
*/
_ol_coordinate_.toStringXY = function(coordinate, opt_fractionDigits) {
return _ol_coordinate_.format(coordinate, '{x}, {y}', opt_fractionDigits);
};
export default _ol_coordinate_;