-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit map bounds to focus area when focusing
- Loading branch information
1 parent
9f91ed3
commit 615db10
Showing
4 changed files
with
58 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function getMaxArrayDepth(value) { | ||
return Array.isArray(value) ? | ||
1 + Math.max(0, ...value.map(getMaxArrayDepth)) : | ||
0; | ||
} | ||
|
||
function bboxOfGeoJson(geojson) { | ||
|
||
if (!geojson?.geometry?.coordinates?.length) return; | ||
|
||
var depth = getMaxArrayDepth(geojson.geometry.coordinates); | ||
var coords = geojson.geometry.coordinates.flat(depth - 2); | ||
var bbox = [ Infinity, Infinity, -Infinity, -Infinity]; | ||
|
||
bbox = coords.reduce(function(prev, coord) { | ||
return [ | ||
Math.min(coord[0], prev[0]), | ||
Math.min(coord[1], prev[1]), | ||
Math.max(coord[0], prev[2]), | ||
Math.max(coord[1], prev[3]) | ||
]; | ||
}, bbox); | ||
if (bbox[0].isNaN) return; | ||
return bbox; | ||
}; | ||
|
||
function extendBbox(bbox, buffer) { | ||
bbox = bbox.slice(); | ||
bbox[0] -= buffer; // west | ||
bbox[1] -= buffer; // south | ||
bbox[2] += buffer; // east | ||
bbox[3] += buffer; // north | ||
return bbox; | ||
} |