Last active
September 20, 2016 14:24
-
-
Save dbazile/b4571814da01485e832b163a9da5443e to your computer and use it in GitHub Desktop.
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
const WGS84: ol.proj.ProjectionLike = 'EPSG:4326' | |
const WEB_MERCATOR: ol.proj.ProjectionLike = 'EPSG:3857' | |
function getDatelineAwareExtent(geometry: ol.geom.Geometry) { | |
if (geometry instanceof ol.geom.MultiPolygon && crossesDateline(geometry)) { | |
const extents = geometry.getPolygons().map(g => ol.proj.transformExtent(g.getExtent(), WEB_MERCATOR, WGS84)) | |
let [, minY, , maxY] = ol.proj.transformExtent(geometry.getExtent(), WEB_MERCATOR, WGS84) | |
let width = 0 | |
let minX = 180 | |
for (const [polygonMinX, , polygonMaxX] of extents) { | |
width += polygonMaxX - polygonMinX | |
if (polygonMaxX > 0) { | |
minX -= polygonMaxX - polygonMinX | |
} | |
} | |
return ol.proj.transformExtent([minX, minY, minX + width, maxY], WGS84, WEB_MERCATOR) | |
} | |
return geometry.getExtent() // Use as-is | |
} | |
function crossesDateline(geometry: ol.geom.Geometry) { | |
const [minX, , maxX] = ol.proj.transformExtent(geometry.getExtent(), WEB_MERCATOR, WGS84) | |
return minX === -180 && maxX === 180 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment