Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Array traitment into latLngToCoords/latLngsToCoords #7436

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 6 additions & 3 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 All @@ -268,11 +270,12 @@ export function latLngToCoords(latlng, precision) {
// Coordinates values are rounded with [`formatNum`](#util-formatnum) function.
export function latLngsToCoords(latlngs, levelsDeep, closed, precision) {
var coords = [];

for (var i = 0, len = latlngs.length; i < len; i++) {
var latlng;
if (!levelsDeep) { latlng = toLatLng(latlngs[i]); } else { latlng = latlngs[i]; }
coords.push(levelsDeep ?
latLngsToCoords(latlngs[i], levelsDeep - 1, closed, precision) :
latLngToCoords(latlngs[i], precision));
latLngsToCoords(latlng, levelsDeep - 1, closed, precision) :
latLngToCoords(latlng, precision));
}

if (!levelsDeep && closed) {
Expand Down