Allow raw latlng array in latLngToCoords/latLngsToCoords (#7436)

* Add Array traitment into latLngToCoords/latLngsToCoords

add array management into function latLngToCoords and coordsToLatLngs

Add array management into GeoJSON.latLngToCoords andGeoJSON.latLngToCoords

* Add tests

* minify code

* remove redundant code

* revert change

Co-authored-by: Dorian Benedetti <dorian.benedetti@etu.u-bordeaux.fr>
Co-authored-by: Florian Bischof <design.falke@gmail.com>
This commit is contained in:
Relkfaw
2022-04-15 14:27:51 +02:00
committed by GitHub
parent 25546fc04f
commit b3e1b241f1
2 changed files with 12 additions and 0 deletions

View File

@ -752,6 +752,11 @@ describe("L.GeoJSON functions", function () {
}); });
describe("#latLngToCoords", function () { describe("#latLngToCoords", function () {
it("accepts latlng array", function () {
var coords = L.GeoJSON.latLngToCoords([2, 1, 3]);
expect(coords).to.eql([1, 2, 3]);
});
it("returns an array of coordinates and altitude", function () { it("returns an array of coordinates and altitude", function () {
var coords = L.GeoJSON.latLngToCoords(L.latLng(2, 1)); var coords = L.GeoJSON.latLngToCoords(L.latLng(2, 1));
var coordsWithAlt = L.GeoJSON.latLngToCoords(L.latLng(2, 1, 3)); var coordsWithAlt = L.GeoJSON.latLngToCoords(L.latLng(2, 1, 3));
@ -772,6 +777,11 @@ describe("L.GeoJSON functions", function () {
}); });
describe("#latLngsToCoords", function () { describe("#latLngsToCoords", function () {
it("accepts multidimensional latlng array", function () {
var coords = L.GeoJSON.latLngsToCoords([[2, 1, 3], [5, 4, 6]]);
expect(coords).to.eql([[1, 2, 3], [4, 5, 6]]);
});
it("returns a multidimensional array of coordinates", function () { it("returns a multidimensional array of coordinates", function () {
var coords = L.GeoJSON.latLngsToCoords([L.latLng(2, 1), L.latLng(4, 3)]); var coords = L.GeoJSON.latLngsToCoords([L.latLng(2, 1), L.latLng(4, 3)]);
var coordWithAlt = L.GeoJSON.latLngsToCoords([L.latLng(2, 1, 3), L.latLng(5, 4, 6)]); var coordWithAlt = L.GeoJSON.latLngsToCoords([L.latLng(2, 1, 3), L.latLng(5, 4, 6)]);

View File

@ -8,6 +8,7 @@ import {Polyline} from './vector/Polyline';
import {Polygon} from './vector/Polygon'; import {Polygon} from './vector/Polygon';
import {LatLng} from '../geo/LatLng'; import {LatLng} from '../geo/LatLng';
import * as LineUtil from '../geometry/LineUtil'; import * as LineUtil from '../geometry/LineUtil';
import {toLatLng} from '../geo/LatLng';
/* /*
@ -257,6 +258,7 @@ export function coordsToLatLngs(coords, levelsDeep, _coordsToLatLng) {
// Reverse of [`coordsToLatLng`](#geojson-coordstolatlng) // Reverse of [`coordsToLatLng`](#geojson-coordstolatlng)
// Coordinates values are rounded with [`formatNum`](#util-formatnum) function. // Coordinates values are rounded with [`formatNum`](#util-formatnum) function.
export function latLngToCoords(latlng, precision) { export function latLngToCoords(latlng, precision) {
latlng = toLatLng(latlng);
return latlng.alt !== undefined ? return latlng.alt !== undefined ?
[Util.formatNum(latlng.lng, precision), Util.formatNum(latlng.lat, precision), Util.formatNum(latlng.alt, precision)] : [Util.formatNum(latlng.lng, precision), Util.formatNum(latlng.lat, precision), Util.formatNum(latlng.alt, precision)] :
[Util.formatNum(latlng.lng, precision), Util.formatNum(latlng.lat, precision)]; [Util.formatNum(latlng.lng, precision), Util.formatNum(latlng.lat, precision)];