Skip to content

Instantly share code, notes, and snippets.

@dbazile
Last active September 20, 2016 14:24
Show Gist options
  • 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
{
"type": "Feature",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-177.923871074119,
-74.658719907701
],
[
-180,
-74.2997270848435
],
[
-180,
-72.2833460271375
],
[
-179.989812508872,
-72.2788352565441
],
[
-174.93969397016,
-73.1281022620665
],
[
-177.923871074119,
-74.658719907701
]
]
],
[
[
[
180,
-74.2997270848435
],
[
176.725005606037,
-73.7334330230079
],
[
180,
-72.2833460271375
],
[
180,
-74.2997270848435
]
]
]
]
},
"properties": {
"acquiredDate": "2016-09-14T20:13:03.088769+00:00",
"bands": {
"blue": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B2.TIF",
"cirrus": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B9.TIF",
"coastal": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B1.TIF",
"green": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B3.TIF",
"nir": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B5.TIF",
"panchromatic": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B8.TIF",
"red": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B4.TIF",
"swir1": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B6.TIF",
"swir2": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B7.TIF",
"tirs1": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B10.TIF",
"tirs2": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_B11.TIF"
},
"cloudCover": 1.92,
"fileFormat": "geotiff",
"path": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/index.html",
"resolution": 30,
"sensorName": "Landsat8",
"thumb_large": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_thumb_large.jpg",
"thumb_small": "https://landsat-pds.s3.amazonaws.com/L8/053/112/LC80531122016258LGN00/LC80531122016258LGN00_thumb_small.jpg"
},
"id": "LC80531122016258LGN00",
"bbox": [
176.725005606037,
-74.658719907701,
-174.93969397016,
-72.2788352565441
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment