Skip to content

Commit

Permalink
Merge pull request #745 from akrherz/240423-2
Browse files Browse the repository at this point in the history
Omnibus
  • Loading branch information
akrherz authored Apr 23, 2024
2 parents bb80a4f + 85ccc6d commit f37157e
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 128 deletions.
1 change: 1 addition & 0 deletions cgi-bin/afos/retrieve.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class MyModel(CGIModel):
@field_validator("sdate", "edate", mode="before")
def allow_str_or_none(cls, v):
"""pydantic can't seem to handle this."""
# pydantic/pydantic/issues/9308
if 8 <= len(v) < 10:
# zero pad
return "-".join(f"{int(v):02.0f}" for v in v.split("-"))
Expand Down
62 changes: 62 additions & 0 deletions htdocs/js/olselect-lonlat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// A factory to generate an OpenLayers map with a single marker on the page
// that is dragable, which then callbacks a given function with the lat/lon
// values of the marker.

function olSelectLonLat(div, initialLon, initialLat, callback) {

let marker = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([initialLon, initialLat]))
});
let style = new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 1],
src: '/images/marker.png',
}),
});

// Set the style for the marker
marker.setStyle(style);

// Create a vector source and add the marker to it
let vectorSource = new ol.source.Vector({
features: [marker]
});

// Create a vector layer with the vector source and add it to the map
let vectorLayer = new ol.layer.Vector({
source: vectorSource
});

let map = new ol.Map({
target: div,
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([initialLon, initialLat]),
zoom: 5
})
});

let modify = new ol.interaction.Modify({
hitDetection: vectorLayer,
source: vectorSource
});
map.addInteraction(modify);

// Add a listener to the drag-and-drop interaction
modify.on('modifyend', (e) => {
let coords = e.features.getArray()[0].getGeometry().getCoordinates();
let lonLat = ol.proj.toLonLat(coords);
try {
callback(lonLat[0], lonLat[1]);
} catch (e) {
console.log(e);
}
});

return { map: map, marker: marker };
}
8 changes: 5 additions & 3 deletions htdocs/nws/spc_outlook_search/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ require_once "../../../include/mlib.php";
force_https();
require_once "../../../include/myview.php";
require_once "../../../include/iemprop.php";
$gmapskey = get_iemprop("google.maps.key");
$t = new MyView();
$OL = "9.1.0";

$t->jsextra = <<<EOF
<script type="text/javascript" src="search.js?v=6"></script>
<script src="https://maps.googleapis.com/maps/api/js?key={$gmapskey}&amp;callback=initialize" type="text/javascript"></script>
<script src='/vendor/openlayers/{$OL}/ol.js'></script>
<script type="text/javascript" src="/js/olselect-lonlat.js"></script>
<script type="text/javascript" src="search.js"></script>
EOF;
$t->headextra = <<<EOF
<link rel="stylesheet" type="text/css" href="/vendor/openlayers/{$OL}/ol.css" />
<style>
.map {
width: 100%;
Expand Down
54 changes: 20 additions & 34 deletions htdocs/nws/spc_outlook_search/search.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var marker;
var map;
let marker = null;
let map = null;

function text(str) {
// XSS
Expand All @@ -21,11 +21,10 @@ function workflow() {

function buildUI() {
$("#manualpt").click(() => {
const la = $("#lat").val();
const lo = $("#lon").val();
const latlng = new google.maps.LatLng(parseFloat(la), parseFloat(lo));
marker.setPosition(latlng);
updateMarkerPosition(latlng);
const la = parseFloat($("#lat").val());
const lo = parseFloat($("#lon").val());
marker.setGeometry(new ol.geom.Point(ol.proj.fromLonLat([lo, la])));
updateMarkerPosition(lon, lat);
});
$('#last').change(() => {
workflow();
Expand All @@ -51,11 +50,10 @@ function updateTableTitle(lon, lat) {
$('#mcds').find("caption").text(`Mesoscale Convective Discussions for ${txt}`);
}

function updateMarkerPosition(latLng) {
$("#lat").val(latLng.lat().toFixed(4));
$("#lon").val(latLng.lng().toFixed(4));
window.location.href = `#bypoint/${latLng.lng().toFixed(4)}/${latLng.lat().toFixed(4)}`;
map.setCenter(latLng);
function updateMarkerPosition(lon, lat) {
$("#lat").val(lat.toFixed(4));
$("#lon").val(lon.toFixed(4));
window.location.href = `#bypoint/${lon.toFixed(4)}/${lat.toFixed(4)}`;
workflow();
}

Expand Down Expand Up @@ -121,37 +119,25 @@ function doWatch(lon, lat) {
}
});
}
// eslint-disable-next-line no-unused-vars
function initialize() { // skipcq: JS-0128
buildUI();
const latLng = new google.maps.LatLng(41.53, -93.653);
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map,
draggable: true
});

google.maps.event.addListener(marker, 'dragend', () => {
updateMarkerPosition(marker.getPosition());
});
$(document).ready(() => {
buildUI();
let res = olSelectLonLat("map", -93.653, 41.53, updateMarkerPosition);
map = res.map;
marker = res.marker;

// Do the anchor tag linking, please
const tokens = window.location.href.split("#");
if (tokens.length === 2) {
const tokens2 = tokens[1].split("/");
if (tokens2.length === 3) {
if (tokens2[0] === 'bypoint') {
const latlng = new google.maps.LatLng(text(tokens2[2]), text(tokens2[1]));
marker.setPosition(latlng);
updateMarkerPosition(latlng);
const lon = parseFloat(tokens2[1]);
const lat = parseFloat(tokens2[2]);
marker.setGeometry(new ol.geom.Point(ol.proj.fromLonLat([lon, lat])));
updateMarkerPosition(lon, lat);
}
}
}

}
});
8 changes: 5 additions & 3 deletions htdocs/nws/sps_search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
force_https();
require_once "../../../include/myview.php";
require_once "../../../include/iemprop.php";
$gmapskey = get_iemprop("google.maps.key");
$t = new MyView();
$OL = "9.1.0";

$t->jsextra = <<<EOF
<script type="text/javascript" src="/js/mapping.js"></script>
<script src="/vendor/jquery-datatables/1.10.20/datatables.min.js"></script>
<script src="/vendor/jquery-ui/1.11.4/jquery-ui.js"></script>
<script src="/vendor/select2/4.1.0rc0/select2.min.js"></script>
<script type="text/javascript" src="search.js?v=2"></script>
<script src="https://maps.googleapis.com/maps/api/js?key={$gmapskey}&callback=_gcb" type="text/javascript"></script>
<script src='/vendor/openlayers/{$OL}/ol.js'></script>
<script type="text/javascript" src="/js/olselect-lonlat.js"></script>
<script type="text/javascript" src="search.js"></script>
EOF;

$t->headextra = <<<EOF
<link rel='stylesheet' href="/vendor/openlayers/{$OL}/ol.css" type='text/css'>
<link rel="stylesheet" href="/vendor/jquery-datatables/1.10.20/datatables.min.css" />
<link rel="stylesheet" href="/vendor/jquery-ui/1.11.4/jquery-ui.min.css" />
<link rel="stylesheet" type="text/css" href="/vendor/select2/4.1.0rc0/select2.min.css"/ >
Expand Down
33 changes: 14 additions & 19 deletions htdocs/nws/sps_search/search.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/* global MapMarkerWidget */
let mapwidget1 = null;
let marker = null;
let table1 = null;
let edate = null;
let sdate = null;
const BACKEND_SPS_BYPOINT = '/json/sps_by_point.py';


function updateMarkerPosition(lon, lat) {
const latLng = new google.maps.LatLng(parseFloat(lat), parseFloat(lon))
$("#lat").val(latLng.lat().toFixed(4));
$("#lon").val(latLng.lng().toFixed(4));
window.location.href = `#bypoint/${latLng.lng().toFixed(4)}/${latLng.lat().toFixed(4)}`;
if (mapwidget1){
mapwidget1.map.setCenter(latLng);
}
$("#lat").val(lat.toFixed(4));
$("#lon").val(lon.toFixed(4));
window.location.href = `#bypoint/${lon.toFixed(4)}/${lat.toFixed(4)}`;
marker.setGeometry(new ol.geom.Point(ol.proj.fromLonLat([lon, lat])));
updateTable();
}

Expand Down Expand Up @@ -87,20 +84,17 @@ function buildUI(){
edate.datepicker("setDate", +1);
// Manual Point Entry
$("#manualpt").click(function(){
const la = $("#lat").val();
const lo = $("#lon").val();
if (la == "" || lo == ""){
const la = parseFloat($("#lat").val());
const lo = parseFloat($("#lon").val());
if (isNaN(la) || isNaN(lo)){
return;
}
const latlng = new google.maps.LatLng(parseFloat(la), parseFloat(lo))
mapwidget1.marker.setPosition(latlng);
marker.setGeometry(new ol.geom.Point(ol.proj.fromLonLat([lo, la])));
updateMarkerPosition(lo, la);
});

};
// Callback from Google Load
// eslint-disable-next-line no-unused-vars
function _gcb() {
$(document).ready(() => {
buildUI();
let default_lon = -93.653;
let default_lat = 41.53;
Expand All @@ -117,7 +111,8 @@ function _gcb() {
}
}
}
let res = olSelectLonLat("map", default_lon, default_lat, updateMarkerPosition);
mapwidget1 = res.map;
marker = res.marker;

mapwidget1 = new MapMarkerWidget("map", default_lon, default_lat);
mapwidget1.register(updateMarkerPosition);
}
});
31 changes: 8 additions & 23 deletions htdocs/sites/site.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
/* global CONFIG */

//callback on when the marker is done moving
function displayCoordinates(pnt) {
const lat = pnt.lat().toFixed(8);
const lng = pnt.lng().toFixed(8);
$("#newlat").val(lat);
$("#newlon").val(lng);
function displayCoordinates(lon, lat) {
$("#newlat").val(lat.toFixed(8));
$("#newlon").val(lon.toFixed(8));
}

// eslint-disable-next-line no-unused-vars
function load() { // skipcq: JS-0128
const mapOptions = {
zoom: 15,
center: new google.maps.LatLng(CONFIG.lat, CONFIG.lon),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
const map = new google.maps.Map(document.getElementById('mymap'),
mapOptions);
const marker = new google.maps.Marker({
position: mapOptions.center,
map,
draggable: true
});
google.maps.event.addListener(marker, 'dragend', () => {
displayCoordinates(marker.getPosition());
});
}
$(document).ready(() => {
const res = olSelectLonLat("mymap", CONFIG.lon, CONFIG.lat, displayCoordinates);
// zoom in
res.map.getView().setZoom(14);
});
9 changes: 6 additions & 3 deletions htdocs/sites/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
require_once "../../include/forms.php";
require_once "../../include/sites.php";
require_once "../../include/iemprop.php";
$gmapskey = get_iemprop("google.maps.key");

$OL = "9.1.0";
$ctx = get_sites_context();
$station = $ctx->station;
$network = $ctx->network;
Expand Down Expand Up @@ -79,15 +78,19 @@

$t = new MyView();
$t->title = sprintf("Site Info: %s %s", $station, $metadata["name"]);
$t->headextra = <<<EOM
<link rel='stylesheet' href="/vendor/openlayers/{$OL}/ol.css" type='text/css'>
EOM;
$t->jsextra = <<<EOF
<script>
var CONFIG = {
lat: {$lat},
lon: {$lon}
};
</script>
<script src='/vendor/openlayers/{$OL}/ol.js'></script>
<script type="text/javascript" src="/js/olselect-lonlat.js"></script>\
<script src="site.js" type="text/javascript"></script>
<script src="https://maps.googleapis.com/maps/api/js?key={$gmapskey}&amp;callback=load&amp;loading=async" type="text/javascript"></script>
EOF;
$t->sites_current = "base";

Expand Down
Loading

0 comments on commit f37157e

Please sign in to comment.