Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add METAR overlay #214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions public_html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
<div class="settingsCheckbox" id="nexrad_checkbox"></div>
<div class="settingsText">NEXRAD Weather</div>
</div>
<div class="settingsOptionContainer">
<div class="settingsCheckbox" id="metar_checkbox"></div>
<div class="settingsText">METARs</div>
</div>
<div class="settingsOptionContainer">
<div class="settingsCheckbox" id="sitepos_checkbox"></div>
<div class="settingsText">Site Position and Range Rings</div>
Expand Down
79 changes: 79 additions & 0 deletions public_html/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,82 @@ function createBaseLayers() {

return layers;
}

function createMetarLayer() {
var metarTileGrid = ol.tilegrid.createXYZ({ minZoom: 3, maxZoom: 16 });

var metarSource = new ol.source.Vector({
// The API doesn't support CORS, so we have to use JSONP with a custom loader
loader: function (extent, resolution, projection, success, failure) {
var lonLat = ol.proj.transformExtent(extent, 'EPSG:3857', 'EPSG:4326');
var url = 'https://www.aviationweather.gov/cgi-bin/json/MetarJSON.php';
$.ajax({
url: url,
data: {
filter: 'prior',
density: 0,
date: new Date().toISOString().replace(/\D/g, '').slice(0, 12),
bbox: lonLat.join(','),
zoom: metarTileGrid.getZForResolution(resolution) - 1
},
dataType: 'jsonp',
jsonpCallback: 'metarCallback',
success: metarCallback(extent, projection, success, failure)
});
},
strategy: ol.loadingstrategy.bbox,
attributions: 'METAR courtesy of <a href="https://www.aviationweather.gov">Aviation Weather Center</a>'
});

var metarCallback = function (extent, projection, success, failure) {
return function (data) {
var format = new ol.format.GeoJSON();
var features = format.readFeatures(data, { extent: extent, featureProjection: projection });
metarSource.addFeatures(features);
if (success !== undefined) {
success(features);
}
}
};

var metar = new ol.layer.Vector({
name: 'metar',
type: 'overlay',
title: 'METAR',
opacity: 0.8,
visible: false,
source: metarSource,
style: function style(feature) {
var cat = feature.get('fltcat');
var color = '#666';
if (cat === 'VFR') {
color = '#00AB42'
} else if (cat === 'MVFR') {
color = '#0016E8'
} else if (cat === 'IFR') {
color = '#FF0007'
} else if (cat === 'LIFR') {
color = '#CA33F9'
}
return new ol.style.Style({
image: new ol.style.Circle({
radius: 5,
fill: new ol.style.Fill({
color: color
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 2
})
})
});
}
});

var refreshMetar = function () {
metar.getSource().refresh();
};
window.setInterval(refreshMetar, 5 * 60000);

return metar;
}
10 changes: 9 additions & 1 deletion public_html/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ function initialize_map() {
})
});

var metarLayer = createMetarLayer();

layers.push(new ol.layer.Group({
title: 'Overlays',
layers: [
Expand All @@ -951,6 +953,8 @@ function initialize_map() {
})
}),

metarLayer,

iconsLayer
]
}));
Expand Down Expand Up @@ -1042,12 +1046,15 @@ function initialize_map() {
}
}
});

OLMap.getView().on('change:resolution', function(event) {
ZoomLvl = localStorage['ZoomLvl'] = OLMap.getView().getZoom();
for (var plane in Planes) {
Planes[plane].updateMarker(false);
};
if (metarLayer.getVisible()) {
metarLayer.getSource().refresh();
}
});

OLMap.on(['click', 'dblclick'], function(evt) {
Expand Down Expand Up @@ -1097,6 +1104,7 @@ function initialize_map() {
// handle the layer settings pane checkboxes
OLMap.once('postrender', function(e) {
toggleLayer('#nexrad_checkbox', 'nexrad');
toggleLayer('#metar_checkbox', 'metar');
toggleLayer('#sitepos_checkbox', 'site_pos');
toggleLayer('#actrail_checkbox', 'ac_trail');
toggleLayer('#acpositions_checkbox', 'ac_positions');
Expand Down