Last active
September 3, 2019 16:55
-
-
Save vincentsarago/b27556d08e13852407efee5a8b883a18 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title>Demo</title> | |
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.css' rel='stylesheet' /> | |
<link href="https://api.mapbox.com/mapbox-assembly/v0.23.2/assembly.min.css" rel="stylesheet"> | |
<script src="https://api.mapbox.com/mapbox-assembly/v0.23.2/assembly.js"></script> | |
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script> | |
<style> | |
body { margin:0; padding:0; width:100%; height:100%;} | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
.mosaic-info { | |
z-index: 10; | |
position: absolute; | |
top: 5px; | |
right: 0; | |
padding: 5px; | |
width: auto; | |
height: auto; | |
font-size: 18px; | |
color: #000; | |
} | |
.zoom-info { | |
z-index: 10; | |
position: absolute; | |
bottom: 17px; | |
right: 0; | |
padding: 5px; | |
width: auto; | |
height: auto; | |
font-size: 16px; | |
color: #000; | |
} | |
.loading-map { | |
position: absolute; | |
width: 100%; | |
height: 100%; | |
color: #FFF; | |
background-color: #000; | |
text-align: center; | |
opacity: 0.5; | |
font-size: 45px; | |
} | |
.loading-map.off { | |
opacity: 0; | |
-o-transition: all .5s ease; | |
-webkit-transition: all .5s ease; | |
-moz-transition: all .5s ease; | |
-ms-transition: all .5s ease; | |
transition: all ease .5s; | |
visibility:hidden; | |
} | |
.middle-center { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
} | |
.middle-center * { | |
display: block; | |
padding: 5px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id='map'> | |
<div id='loader' class="loading-map z2"> | |
<div class="middle-center"> | |
<div class="round animation-spin animation--infinite animation--speed-1"> | |
<svg class='icon icon--l inline-block'><use xlink:href='#icon-satellite'/></svg> | |
</div> | |
</div> | |
</div> | |
<div class="mosaic-info"> | |
<span class='py3' style='display: block; text-align: right;' id="name"></span> | |
<span class='py3' style='display: block; text-align: right;' id="min-zoom"></span> | |
<span class='py3' style='display: block; text-align: right;' id="max-zoom"></span> | |
</div> | |
<div class="zoom-info"><span id="zoom"></span></div> | |
</div> | |
<script> | |
const apiEndpoint = 'https://08lazntuxe.execute-api.us-east-1.amazonaws.com/production' | |
mapboxgl.accessToken = 'pk.eyJ1IjoidmluY2VudHNhcmFnbyIsImEiOiJjamxwa3JkaWkwZ3BjM3dudmZmazQwYjI2In0.eUzks_hqH-QVIlnXUKmKsA' | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/basic-v9', | |
center: [0, 0], | |
zoom: 1 | |
}) | |
map.on('zoom', function (e) { | |
const z = (map.getZoom()).toString().slice(0, 6) | |
document.getElementById('zoom').textContent = z | |
}) | |
const addAOI = (bounds) => { | |
const geojson = { | |
"type": "FeatureCollection", | |
"features": [turf.bboxPolygon(bounds)] | |
} | |
map.addSource('aoi', { | |
'type': 'geojson', | |
'data': geojson | |
}) | |
map.addLayer({ | |
id: 'aoi-polygon', | |
type: 'line', | |
source: 'aoi', | |
layout: { | |
'line-cap': 'round', | |
'line-join': 'round' | |
}, | |
paint: { | |
'line-color': '#3bb2d0', | |
'line-width': 2 | |
} | |
}) | |
return | |
} | |
const addMosaic = (mosaicUrl, options) => { | |
return fetch(`${apiEndpoint}/mosaic/info?url=${mosaicUrl}`) | |
.then(res => { | |
if (res.ok) return res.json() | |
throw new Error('Network response was not ok.') | |
}) | |
.then(data => { | |
document.getElementById('name').textContent = `${data.name}` | |
document.getElementById('min-zoom').textContent = `Min Zoom: ${data.minzoom}` | |
document.getElementById('max-zoom').textContent = `Max Zoom: ${data.maxzoom}` | |
const tileParams = Object.keys(options).map(i => `${i}=${options[i]}`).join('&') | |
let url = `${apiEndpoint}/tiles/tilejson.json?url=${mosaicUrl}&tile_format=png&resampling_method=bilinear` | |
if (tileParams !== '') url += `&${tileParams}` | |
map.addSource('raster', { | |
type: 'raster', | |
url: url | |
}) | |
map.addLayer({ | |
id: 'raster', | |
type: 'raster', | |
source: 'raster' | |
}) | |
document.getElementById('loader').classList.toggle('off') | |
const bounds = data.bounds | |
addAOI(bounds) | |
map.fitBounds([[bounds[0], bounds[1]], [bounds[2], bounds[3]]]) | |
}) | |
.catch(err => { | |
console.warn(err) | |
}) | |
} | |
map.on('load', () => { | |
addMosaic('https://s3.amazonaws.com/opendata.remotepixel.ca/noaa/barry/barry.json.gz', {}) | |
.then(() => { | |
fetch('https://gist.githubusercontent.com/vincentsarago/6b342e34495ce9f4de26f39192fb6f8f/raw/b07f278d9cb29423cf7c61b704d453e1d6c3c948/barry.geojson') | |
.then(res => { | |
if (res.ok) return res.json() | |
throw new Error('Network response was not ok.') | |
}) | |
.then(geojson => { | |
map.addSource('borders', { | |
'type': 'geojson', | |
'data': geojson | |
}) | |
map.addLayer({ | |
id: 'borders', | |
type: 'line', | |
source: 'borders', | |
layout: { | |
'line-cap': 'round', | |
'line-join': 'round' | |
}, | |
paint: { | |
'line-color': '#000', | |
'line-width': 2 | |
} | |
}) | |
}) | |
.catch(err => { | |
console.warn(err) | |
}) | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment