Skip to content

Commit

Permalink
Merge pull request #143 from Azure/users/v-pnizetic/bounding-box-vali…
Browse files Browse the repository at this point in the history
…dation

Add validation when calculating bounding box
  • Loading branch information
nizetic authored Jul 3, 2024
2 parents f49e2dc + 4bc9cbe commit ea2d9db
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/pages/conversion/places-preview-map/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,52 @@ export function getTextStyle(category) {
return style;
}

function isValidLatitude(lat) {
return lat >= -90 && lat <= 90;
}

function isValidLongitude(lon) {
return lon >= -180 && lon <= 180;
}

export const flattenCoordinates = arr => {
if (!Array.isArray(arr)) return [];

const flattened = [];
arr.forEach(item => {
if (typeof item[0] === 'number') {
flattened.push(item);
return;
}
flattened.push(...flattenCoordinates(item));
});

return flattened;
};

export function calculateBoundingBox(levels) {
let minLat = Infinity;
let maxLat = -Infinity;
let minLon = Infinity;
let maxLon = -Infinity;
let hasCalulated = false;
let minLat = 90;
let maxLat = -90;
let minLon = 180;
let maxLon = -180;
let hasCalculated = false;

levels.features.forEach(feature => {
const { coordinates } = feature.geometry;
coordinates[0].forEach(([lon, lat]) => {
const flattenedCoordinates = flattenCoordinates(coordinates);

flattenedCoordinates.forEach(([lon, lat]) => {
if (!isValidLongitude(lon) || !isValidLatitude(lat)) return;

minLat = Math.min(minLat, lat);
maxLat = Math.max(maxLat, lat);
minLon = Math.min(minLon, lon);
maxLon = Math.max(maxLon, lon);
hasCalulated = true;
hasCalculated = true;
});
});

if (!hasCalulated) return undefined;
if (!hasCalculated) return undefined;

return [minLon, minLat, maxLon, maxLat];
}
Expand Down

0 comments on commit ea2d9db

Please sign in to comment.