Skip to content

Instantly share code, notes, and snippets.

@dbazile
Last active September 20, 2016 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbazile/b4571814da01485e832b163a9da5443e to your computer and use it in GitHub Desktop.
Save dbazile/b4571814da01485e832b163a9da5443e to your computer and use it in GitHub Desktop.
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
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment