Skip to content

Commit

Permalink
Allow raw latlng array in latLngToCoords/latLngsToCoords (#7436)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people authored Apr 15, 2022
1 parent 25546fc commit b3e1b24
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/suites/layer/GeoJSONSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,11 @@ describe("L.GeoJSON functions", 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 () {
var coords = L.GeoJSON.latLngToCoords(L.latLng(2, 1));
var coordsWithAlt = L.GeoJSON.latLngToCoords(L.latLng(2, 1, 3));
Expand All @@ -772,6 +777,11 @@ describe("L.GeoJSON functions", 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 () {
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)]);
Expand Down
2 changes: 2 additions & 0 deletions src/layer/GeoJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Polyline} from './vector/Polyline';
import {Polygon} from './vector/Polygon';
import {LatLng} from '../geo/LatLng';
import * as LineUtil from '../geometry/LineUtil';
import {toLatLng} from '../geo/LatLng';


/*
Expand Down Expand Up @@ -257,6 +258,7 @@ export function coordsToLatLngs(coords, levelsDeep, _coordsToLatLng) {
// Reverse of [`coordsToLatLng`](#geojson-coordstolatlng)
// Coordinates values are rounded with [`formatNum`](#util-formatnum) function.
export function latLngToCoords(latlng, precision) {
latlng = toLatLng(latlng);
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)];
Expand Down

0 comments on commit b3e1b24

Please sign in to comment.