-
Notifications
You must be signed in to change notification settings - Fork 1
/
NWS-forecast-map.php
1323 lines (1174 loc) · 49.3 KB
/
NWS-forecast-map.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
#################################################################################
#
# NWS Weather Forecast with Leaflet Map
#
# This program is based on WxForecastMap.php by Curly of ricksturf.com, and has
# been completely rewritten to use Leaflet/OpenStreetMaps and api.weather.gov
# for the data (instead of Google map/forecast.weather.gov/MapClick.php XML)
# by Ken True - [email protected]
#
# NWS-forecast-map.php - 18-May-2019 - initial release
#
# Version 2.00 - 18-May-2019 - initial release
# Version 2.10 - 19-May-2019 - added display of County Zone alerts and map polygons (where provided)
# Version 2.20 - 14-Jul-2020 - switched to geo+json from ld+json calls to api.weather.gov
# Version 2.30 - 20-Oct-2022 - added adjustment of -1,-1 to gridpoint forecast for problem WFOs
# Version 2.40 - 21-Jun-2024 - update for change in icon URLs in api.weather.gov JSON + alert links
# Version 2.41 - 22-Jun-2024 - fix for missing global for $NWSICONLIST+colors for polygon displays
#
#################################################################################
# error_reporting(E_ALL); // uncomment to turn on full error reporting
#
# script available at http://saratoga-weather.org/scripts.php
#
# you may copy/modify/use this script as you see fit,
# no warranty is expressed or implied.
# Usage:
# you can use this webpage standalone (customize the HTML portion below)
# or you can include it in an existing page:
/*
<?php
$doInclude = true;
include("NWS-forecast-map.php");
?>
*/
#
# settings: --------------------------------------------------------------------
# if you are using www.mapbox.com for map tiles, you
# need to acquire an API ke from that service
#
# put this in the CALLING page for NWS-forecast-map.php script:
/*
$setMapboxAPIkey = '-replace-this-with-your-API-key-here-';
*/
# Note: if using the Saratoga template set, put a new entry in Settings.php
/*
$SITE['mapboxAPIkey'] = '-replace-this-with-your-API-key-here-';
*/
# and you won't need to change the $mapAPI value above (nor any of the other
# settings in the script below.
#
# change myLat, myLong to your station latitude/longitude,
# set $ourTZ to your time zone
# other settings are optional
#
# set to station latitude/longitude (decimal degrees)
$myLat = 37.2747; //North=positive, South=negative decimal degrees
$myLong = -122.0229; //East=positive, West=negative decimal degrees
# The above settings are for saratoga-weather.org location
$ourTZ = "America/Los_Angeles"; //NOTE: this *MUST* be set correctly to
# translate UTC times to your LOCAL time for the displays.
# Use https://www.php.net/manual/en/timezones.php to find the timezone suitable for
# your location.
#
# pick a format for the time to display ..uncomment one (or make your own)
# $timeFormat = 'D, Y-m-d H:i:s T'; // Fri, 2006-03-31 14:03:22 TZone
# $timeFormat = 'D, d-M-Y g:i:s a T'; // Fri, 31-Mar-2006 4:03:22 am TZone
$timeFormat = 'g:i a T M d, Y'; // 10:30 am CDT March 31, 2018
# see: http://leaflet-extras.github.io/leaflet-providers/preview/ for additional maps
# select ONE map tile provider by uncommenting the values below.
$mapProvider = 'Esri_WorldTopoMap'; // ESRI topo map - no key needed
#$mapProvider = 'OSM'; // OpenStreetMap - no key needed
#$mapProvider = 'Terrain'; // Terrain map by stamen.com - no key needed
#$mapProvider = 'OpenTopo'; // OpenTopoMap.com - no key needed
#$mapProvider = 'Wikimedia'; // Wikimedia map - no key needed
#
#$mapProvider = 'MapboxSat'; // Maps by Mapbox.com - API KEY needed in $mapboxAPIkey
#$mapProvider = 'MapboxTer'; // Maps by Mapbox.com - API KEY needed in $mapboxAPIkey
$mapboxAPIkey = '--mapbox-API-key--';
# use this for the API key to MapBox
$mapZoomDefault = 11; // =11; default Leaflet Map zoom entry for display (1=world, 14=street)
# end of settings -------------------------------------------------------------
if (isset($_REQUEST['sce']) && strtolower($_REQUEST['sce']) == 'view' ) {
//--self downloader --
$filenameReal = __FILE__;
$download_size = filesize($filenameReal);
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: text/plain;charset=ISO-8859-1");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
header('Connection: close');
readfile($filenameReal);
exit;
}
// overrides from Settings.php if available
// if(file_exists("Settings.php")) {include_once("Settings.php");}
//if(file_exists("common.php")) {include_once("common.php");}
global $SITE;
if (isset($SITE['latitude'])) {$myLat = $SITE['latitude'];}
if (isset($SITE['longitude'])) {$myLong = $SITE['longitude'];}
if (isset($SITE['cityname'])) {$ourLocationName = $SITE['cityname'];}
if (isset($SITE['tz'])) {$ourTZ = $SITE['tz']; }
if (isset($SITE['timeFormat'])) {$timeFormat = $SITE['timeFormat'];}
if (isset($SITE['mapboxAPIkey'])) {$mapboxAPIkey = $SITE['mapboxAPIkey']; }
// end of overrides from Settings.php
// overrides from including page if any
if (isset($setMapZoomDefault)) { $mapZoomDefault = $setMapZoomDefault; }
if (isset($setDoLinkTarget)) { $doLinkTarget = $setDoLinkTarget; }
if (isset($setLatitude)) { $myLat = $setLatitude; }
if (isset($setLongitude)) { $myLong = $setLongitude; }
if (isset($setLocationName)) { $ourLocationName = $setLocationName; }
if (isset($setTimeZone)) { $ourTZ = $setTimeZone; }
if (isset($setTimeFormat)) { $timeFormat = $setTimeFormat; }
if (isset($setMapProvider)) { $mapProvider = $setMapProvider; }
if (isset($setMapboxAPIkey)) { $mapboxAPIkey = $setMapboxAPIkey; }
// ------ start of code -------
define('NWS_DETAIL_PRODUCT_URL','https://alerts-v2.weather.gov/#/?id='); // api site still beta // Jasiu fix 10-May-2020
if(!isset($mapboxAPIkey)) {
$mapboxAPIkey = '--mapbox-API-key--';
}
$includeMode = (isset($doInclude) and $doInclude)?true:false;
# table of available map tile providers
$mapTileProviders = array(
'OSM' => array(
'name' => 'Street',
'URL' =>'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
'attrib' => '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, Points © 2012 LINZ',
'maxzoom' => 18
),
'Wikimedia' => array(
'name' => 'Street2',
'URL' =>'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png',
'attrib' => '<a href="https://wikimediafoundation.org/wiki/Maps_Terms_of_Use">Wikimedia</a>',
'maxzoom' => 18
),
'Esri_WorldTopoMap' => array(
'name' => 'Terrain',
'URL' => 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}',
'attrib' => 'Tiles © <a href="https://www.esri.com/en-us/home" title="Sources: Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community">Esri</a>',
'maxzoom' => 18
),
'Terrain' => array(
'name' => 'Terrain2',
'URL' =>'http://{s}.tile.stamen.com/terrain/{z}/{x}/{y}.jpg',
'attrib' => '<a href="https://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> <a href="https://stamen.com">Stamen.com</a> | Data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors.',
'maxzoom' => 14
),
'OpenTopo' => array(
'name' => 'Topo',
'URL' =>'https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
'attrib' => ' © <a href="https://opentopomap.org/">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>) | Data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors.',
'maxzoom' => 15
),
'MapboxTer' => array(
'name' => 'Terrain3',
'URL' =>'https://api.mapbox.com/styles/v1/mapbox/outdoors-v10/tiles/256/{z}/{x}/{y}?access_token='.
$mapboxAPIkey,
'attrib' => '© <a href="https://mapbox.com">MapBox.com</a> | Data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors.',
'maxzoom' => 18
),
'MapboxSat' => array(
'name' => 'Satellite',
'URL' =>'https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v10/tiles/256/{z}/{x}/{y}?access_token='.
$mapboxAPIkey,
'attrib' => '© <a href="https://mapbox.com">MapBox.com</a> | Data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors.',
'maxzoom' => 18
),
);
if(!$includeMode) { // emit only if full page is generated
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="viewport" content="initial-scale=1.0" />
<link rel="stylesheet" href="NWS-forecast-map.css" />
<title>NWS Forecast Map</title>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.graytitles {
font-size:large;
text-align:center;
margin-top: 5px;
}
-->
</style>
</head>
<body>
<?php } // end !$includeMode ?>
<div>
<p> </p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.5.1/leaflet.js" type="text/javascript"></script>
<?php
$centerlat = $myLat;
$centerlong= $myLong;
$zoom = $mapZoomDefault;
global $Status;
$errorMessage = '';
$Status = "<!-- NWS-forecast-map.php - V2.41 - 22-Jun-2024 -->\n";
if(isset($_REQUEST['zoom']) and is_numeric($_REQUEST['zoom'])) {
$zoom = $_REQUEST['zoom'];
}
if(isset($_REQUEST['llc'])) {
list($centerlat,$centerlong) = explode(',',$_REQUEST['llc']);
}
if(isset($_REQUEST['map'])) {
$reqMap = $_REQUEST['map'];
}
$doDebug = isset($_REQUEST['debug'])?true:false;
# for easy testing (part 1)
if(isset($_REQUEST['latlong'])) {
$t = $_REQUEST['latlong'];
if(preg_match('!^([\d\.]+),-([\d\.]+)$!',$t,$m)) {
$centerlat = $m[1];
$centerlong= $m[2];
$centerlong= 0.0-$centerlong;
$Status .= "<!-- using latlong=$centerlat,$centerlong -->\n";
}
}
# for easy testing (part 2)
if(isset($_REQUEST['lat']) and is_numeric($_REQUEST['lat']) and
isset($_REQUEST['lon']) and is_numeric($_REQUEST['lon']) ) {
$centerlat = $_REQUEST['lat'];
$centerlong= $_REQUEST['lon'];
$Status .= "<!-- using lat=$centerlat and long=$centerlong -->\n";
}
# for easy testing (part 3) - fudge the gridpoint number
if(isset($_REQUEST['adj'])) {
$t = $_REQUEST['adj'];
if(preg_match('!^([\-\d\.]+),([\-\d\.]+)$!',$t,$m)) {
$adj1 = $m[1];
$adj2 = $m[2];
$Status .= "<!-- using adj=$adj1,$adj2 -->\n";
}
} else {
$adj1 = 0;
$adj2 = 0;
}
# listing of current WFO gridpoint forecasts with +1,+1 offsets to correct 20-Oct-2022
#$offByOne = array('AFC','AER','ALU','AFG','AJK','BOX','CAE','DLH','FSD','HGX','HNX','LIX','LWX','MAF','MFR','MLB','MRX','MTR');
$offByOne = array('NIL');
$pointHTML = WXmap_fetchUrlWithoutHanging('https://api.weather.gov/points/'.$centerlat.','.$centerlong);
$long = round($centerlong,4);
$lat = round($centerlat,4);
$stuff = explode("\r\n\r\n",$pointHTML); // maybe we have more than one header due to redirects.
$content = (string)array_pop($stuff); // last one is the content
$headers = (string)array_pop($stuff); // next-to-last-one is the headers
preg_match('/HTTP\/\S+ (\d+)/', $headers, $m);
//$Status .= "<!-- m=".print_r($m,true)." -->\n";
//$Status .= "<!-- html=".print_r($html,true)." -->\n";
if(isset($m[1])) {$lastRC = (string)$m[1];} else {$lastRC = '0';}
if($lastRC == '200') { // got a good return .. process
$rawpointJSON = json_decode($content,true);
$pointJSON = $rawpointJSON['properties'];
$gridPointMeta = $pointJSON['gridId'].'/'.$pointJSON['gridX'].','.$pointJSON['gridY'];
if($doDebug) {$Status .= "<!-- Point content\n".var_export($pointJSON,true)." -->\n";}
$fcstURL = $pointJSON['forecast'];
$ngpID = $pointJSON['gridId'];
if(in_array($ngpID,$offByOne)) {
print "<!-- WFO=$ngpID is +1,+1 offset, adjusted down by -1,-1 -->\n";
$adj1 = -1;
$adj2 = -1;
}
$ngpX = $pointJSON['gridX']+$adj1;
$ngpY = $pointJSON['gridY']+$adj2;
$fcstURL = "https://api.weather.gov/gridpoints/$ngpID/$ngpX,$ngpY/forecast";
$gridPointMeta = $ngpID.'/'.$ngpX.','.$ngpY;
$fcstZoneURL = $pointJSON['forecastZone'];
$fcstZone = get_zone($fcstZoneURL);
$countyZoneURL = $pointJSON['county'];
$countyZone = get_zone($countyZoneURL);
$fireZoneURL = $pointJSON['fireWeatherZone'];
$fireZone = get_zone($fireZoneURL);
$cityname = $pointJSON['relativeLocation']['properties']['city'];
$statename= $pointJSON['relativeLocation']['properties']['state'];
$TZ = $pointJSON['timeZone'];
date_default_timezone_set($TZ);
$distanceFrom = '';
if (isset($pointJSON['relativeLocation']['properties']['distance']['value'])) {
$distance = $pointJSON['relativeLocation']['properties']['distance']['value'];
$distance = floor(0.000621371 * $distance); // truncate to nearest whole mile
$Status.= "<!-- distance=$distance from " . $cityname . " -->\n";
if ($distance >= 2) {
$angle = $pointJSON['relativeLocation']['properties']['bearing']['value'];
$compass = array('N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW');
$direction = $compass[round($angle / 22.5) % 16];
$t = $distance . ' ';
$t.= ($distance > 1) ? "Miles" : "Mile";
$t.= " $direction ";
$distanceFrom = $t. ' from ';
}
}
$Status .= "<!-- fcstURL='$fcstURL' -->\n";
} else { //not a good return
$errorMessage .= "<h2>Oops... unable to fetch the point forecast data RC=$lastRC.<br/>" .
"Please try again later</h2>";
$distanceFrom = '';
$cityname = 'n/a';
$statename = 'n/a';
$updateTime= 'n/a';
$lat=$centerlat;
$long=$centerlong;
}
// now get the actual forecast from the gridpoint URL
if(isset($fcstURL)) { // try only if the gridpoint URL was found
$gridpointHTML = WXmap_fetchUrlWithoutHanging($fcstURL);
$stuff = explode("\r\n\r\n",$gridpointHTML); // maybe we have more than one header due to redirects.
$content = (string)array_pop($stuff); // last one is the content
$headers = (string)array_pop($stuff); // next-to-last-one is the headers
preg_match('/HTTP\/\S+ (\d+)/', $headers, $m);
//$Status .= "<!-- m=".print_r($m,true)." -->\n";
//$Status .= "<!-- html=".print_r($html,true)." -->\n";
if(isset($m[1])) {$lastRC = (string)$m[1]; } else { $lastRC = '0'; }
if($lastRC == '200') {
$rawgridpointJSON = json_decode($content,true);
$gridpointJSON = $rawgridpointJSON['properties'];
if($doDebug) {$Status .= "<!-- gridpoint content\n".var_export($gridpointJSON,true)." -->\n";}
$updateTime = date($timeFormat,strtotime($gridpointJSON["updateTime"]));
/* ld+json extract
if(isset($gridpointJSON['geometry'])) {
$g = $gridpointJSON['geometry'];
# "geometry": "GEOMETRYCOLLECTION(POINT(-122.0220167 37.2668315),POLYGON((-122.0385572 37.275548,-122.0330058 37.2536879,-122.005479 37.2581132,-122.011025 37.2799738,-122.0385572 37.275548)))",
if(preg_match('|POINT\(([^\)]+)\)|i',$g,$m)) {
list($long,$lat) = explode(' ',$m[1]);
$long = round($long,4);
$lat = round($lat,4);
$centerlat = $lat;
$centerlong = $long;
}
if(preg_match('|POLYGON\(\(([^\)]+)\)\)|i',$g,$m)) {
$poly = explode(',',$m[1]);
}
*/
}
$poly = array();
if(isset($rawgridpointJSON['geometry']['coordinates'][0])) {
if($doDebug) {$Status .= "<!-- gridpoint geometry\n".var_export($rawgridpointJSON['geometry']['coordinates'][0],true)." -->\n";}
foreach ($rawgridpointJSON['geometry']['coordinates'][0] as $i => $coords) {
$poly[] = $coords[0].' '.$coords[1];
}
if($doDebug) {$Status .= "<!-- lat=$lat long=$long poly=".var_export($poly,true)." -->\n"; }
} else {
$errorMessage .= "<h2>Oops... unable to fetch the gridpoint forecast data RC=$lastRC.<br/>" .
"Please try again later</h2>";
$updateTime= 'n/a';
$lat=$centerlat;
$long=$centerlong;
}
}
list($alertsList,$alertsJS) = WXmap_get_alerts($countyZoneURL,$fcstZone,$countyZone,$fireZone,$centerlat,$centerlong,
"$cityname $statename");
if(!isset($zoom)) {$zoom = 11;}
print $Status;
?>
<div style="width:621px; margin:0px auto 0px auto;">
<table cellspacing="0" cellpadding="0"
style="width:100%; margin:0px auto 0px auto; border:1px solid black; background-color:#F5F8FE">
<tr>
<td style="text-align:center; padding:5px 0px 5px 0px">Land forecasts for the United States from the National Weather Service</td>
</tr>
</table>
<p> </p>
<table width="611" cellspacing="3" cellpadding="3" style="color:black; line-height:1.2em; margin: 0px auto 0px auto; border:solid 4px #006699; background-color:#FFF">
<tr>
<td colspan="3">
<div style="margin: 6px auto 0px auto; text-align:center; width:570px; height:360px; border: outset #777 3px;" id="map_canvas">
<noscript>
<p><span style="font-size:14px;">Your JavaScript is disabled and preventing the map to load for you.</span></p>
<p>Enable your JavaScript in your browser to view the map and select a forecast for any US location.</p>
</noscript>
<?php echo $errorMessage; ?>
</div>
<div style="text-align:center; font-size:12px;margin-top: 5px;">
<span style="color:#009900;font-size:large;font-weight:bolder;">☐ </span> Green box outlines the forecast area</div>
</td>
</tr>
<tr>
<td style="font-size:12px; padding-left:16px; color:#030">Zoom or drag the map for a precise location<br />Double-click the location for the latest forecast.</td>
<td style="width:32%; text-align:left; font-size:10px">LAT: <span id="latspan"><?php echo $lat; ?></span><br />LON: <span id="lngspan"><?php echo $long;?></span></td>
<td style="float:right">
<form action="" method="get">
<div>
<input type="hidden" id="latlongclicked" name="llc" />
<input type="hidden" id="currentzoom" name="zoom" />
<input type="hidden" id="currentmap" name="map" />
<input style="visibility:hidden;" id="theSubmitButton" type="submit" value=""/>
</div>
</form>
</td>
</tr>
<tr>
<td colspan="3" style="padding: 2px 10px 2px 10px"><hr /></td>
</tr>
<tr>
<td colspan="3" style="text-align:center; font-size: 1.5em;"><b><?php echo "$distanceFrom$cityname, $statename"; ?></b></td>
</tr>
<tr>
<td colspan="3" style="padding-bottom: 6px; text-align:center; font-size: 0.8em">Updated <?php echo $updateTime;?></td>
</tr>
</table>
<?php if(!empty($alertsList)) { // show the alerts box ?>
<table border="0" width="622" style="margin:5px auto 0px auto;border: thick solid red; background-color: #FF9;">
<tr>
<td class="alertbox" style="text-align: center;">
<?php echo $alertsList; ?>
</td>
</tr>
</table>
<?php } // end alert summary display ?>
<?php
// Generate the detailed forecasts from the gridpoint info:
/*
{
"number": 1,
"name": "Today",
"startTime": "2019-05-16T10:00:00-07:00",
"endTime": "2019-05-16T18:00:00-07:00",
"isDaytime": true,
"temperature": 62,
"temperatureUnit": "F",
"temperatureTrend": null,
"windSpeed": "10 to 15 mph",
"windDirection": "WSW",
"icon": "https://api.weather.gov/icons/land/day/tsra,50/tsra,60?size=medium",
"shortForecast": "Showers And Thunderstorms Likely",
"detailedForecast": "Showers and thunderstorms likely. Mostly cloudy, with a high near 62. West southwest wind 10 to 15 mph, with gusts as high as 20 mph. Chance of precipitation is 60%. New rainfall amounts between a tenth and quarter of an inch possible."
},
*/
$dayStyle = 'vertical-align: top; border:solid 4px #006699; width: 311px; background-color:#FFFEE8; padding-bottom: 10px';
$nightStyle = 'vertical-align: top; border:solid 4px #006699; width: 311px; background-color:#F2F2F2; padding-bottom: 10px;';
if(isset($gridpointJSON['periods'][0])) { // only generate if we have data
global $NWSICONLIST;
load_lookups();
for ($i=0;$i<count($gridpointJSON['periods']);$i=$i+2) {
$Pa = $gridpointJSON['periods'][$i];
$Pb = $gridpointJSON['periods'][$i+1];
?>
<table border="0" width="622" style="margin:0px auto 0px auto;">
<tr>
<td class="graybox" style="width: 311px;">
<div class="graytitles"><?php echo $Pa['name']; ?></div>
</td>
<td> </td>
<td class="graybox" style="width: 311px;">
<div class="graytitles"><?php echo $Pb['name']; ?></div>
</td>
</tr>
<tr>
<?php if($Pa['isDaytime']) {
// Daytime:
$color = '#800';
$loworhigh = 'HIGH';
$tdStyle = $dayStyle;
} else {
// nighttime
$tdStyle = $nightStyle;
$loworhigh = 'LOW';
$color = '#008';
}?>
<td style="<?php echo $tdStyle; ?>">
<div style="text-align: center; font-size:110%; padding: 6px 0px 16px 0px;"><i>"<?php echo $Pa['shortForecast']; ?>"</i></div>
<div style="vertical-align: top; padding: 0px 8px 0px 8px; text-align: justify">
<?php
if(substr($Pa['icon'],0,4) !== 'http') {$Pa['icon'] = convert_icon($Pa['icon']); }
if(substr($Pb['icon'],0,4) !== 'http') {$Pb['icon'] = convert_icon($Pb['icon']); }
?>
<img style="float: left; border-style: none; padding-right: 8px" src="<?php echo $Pa['icon']; ?>" height="86" width="86"
alt="<?php echo $Pa['shortForecast']; ?>" title="<?php echo $Pa['detailedForecast']; ?>"/>
<div>
<span style="color:<?php echo $color;?>;"><?php echo $loworhigh; ?> </span><span style="color:<?php echo $color;?>; font-size:1.5em;">
<b><?php echo $Pa['temperature'];?>°</b>
</span>
</div>
<span style="font-size:.9em"><?php echo $Pa['detailedForecast']; ?></span>
</div>
</td>
<td> </td>
<?php if($Pb['isDaytime']) {
// Daytime:
$color = '#800';
$loworhigh = 'HIGH';
$tdStyle = $dayStyle;
} else {
// nighttime
$tdStyle = $nightStyle;
$loworhigh = 'LOW';
$color = '#008';
}?>
<td style="<?php echo $tdStyle; ?>">
<div style="text-align: center; font-size:110%; padding: 6px 0px 16px 0px;"><i>"<?php echo $Pb['shortForecast']; ?>"</i></div>
<div style="vertical-align: top; padding: 0px 8px 0px 8px; text-align: justify">
<img style="float: left; border-style: none; padding-right: 8px" src="<?php echo $Pb['icon']; ?>" height="86" width="86"
alt="<?php echo $Pb['shortForecast']; ?>" title="<?php echo $Pb['detailedForecast']; ?>"/>
<div>
<span style="color:<?php echo $color;?>;"><?php echo $loworhigh; ?> </span><span style="color:<?php echo $color;?>; font-size:1.5em;">
<b><?php echo $Pb['temperature'];?>°</b>
</span>
</div>
<span style="font-size:.9em"><?php echo $Pb['detailedForecast']; ?></span>
</div>
</td>
</tr>
</table>
<?php
} // end of loop to print
?>
<script type="text/javascript">
// <![CDATA[
<?php
print '// Leaflet/OpenStreetMap+other tile providers MAP production code
';
// Generate map options
$mOpts = array();
$mList = '';
$mFirstMap = '';
$mFirstMapName = '';
$mSelMap = '';
$mSelMapName = '';
$swxAttrib = ' | Script by <a href="https://saratoga-weather.org/">Saratoga-weather.org</a>';
$mScheme = $_SERVER['SERVER_PORT']==443?'https':'http';
foreach ($mapTileProviders as $n => $M ) {
$name = $M['name'];
$vname = 'M'.strtolower($name);
if(empty($mFirstMap)) {$mFirstMap = $vname; $mFirstMapName = $name;} // default map is first in list
if(strpos($n,'Mapbox') !== false and
strpos($mapboxAPIkey,'-API-key-') !== false) {
$mList .= "\n".'// skipping Mapbox - '.$name.' since $mapboxAPIkey is not set'."\n\n";
continue;
}
if($mScheme == 'https' and parse_url($M['URL'],PHP_URL_SCHEME) == 'http') {
$mList .= "\n".'// skipping '.$name.' due to http only map tile link while our page is https'."\n\n";
continue;
}
if($mapProvider == $n) {$mSelMap = $vname; $mSelMapName = $name;}
$mList .= 'var '.$vname.' = L.tileLayer(\''.$M['URL'].'\', {
maxZoom: '.$M['maxzoom'].',
attribution: \''.$M['attrib'].$swxAttrib.'\',
mapname: "'.$name.'"
});
';
$mOpts[$name] = $vname;
}
print "// Map tile providers:\n";
print $mList;
print "// end of map tile providers\n\n";
print "var baseLayers = {\n";
$mtemp = '';
foreach ($mOpts as $n => $v) {
$mtemp .= ' "'.$n.'": '.$v.",\n";
}
$mtemp = substr($mtemp,0,strlen($mtemp)-2)."\n";
print $mtemp;
print "}; \n";
if(empty($mSelMap)) {$mSelMap = $mFirstMap; $mSelMapName = $mFirstMapName;}
if(isset($reqMap) and isset($mOpts[$reqMap])) {
$mSelMap = $mOpts[$reqMap];
$mSelMapName = $reqMap;
}
// end Generate map tile options
?>
var map = L.map('map_canvas', {
center: new L.latLng([<?php echo $centerlat;?>,<?php echo $centerlong;?>]),
zoom: <?php echo $zoom; ?>,
layers: [<?php echo $mSelMap; ?>],
doubleClickZoom: false,
scrollWheelZoom: false
});
var selMap = '<?php echo $mSelMapName; ?>';
// console.log('initial selMap='+selMap);
L.control.scale().addTo(map);
L.control.layers(baseLayers).addTo(map);
// draw the gridpoint forecast area as a polygon
var polyfa = [
<?php
foreach ($poly as $i => $coords) {
list($longP,$latP) = explode(' ',$coords);
print " [$latP,$longP],\n";
}
?>
];
var mapolyfa = new L.polygon(polyfa,{
opacity: 1.0,
color: "#009900",
strokeOpacity: 0.9,
weight: 2.5,
fillColor: "#7FF378",
fillOpacity: 0.20,
title: "Forecast Area"
}).addTo(map);
mapolyfa.bindTooltip("Forecast Area for <?php echo "$distanceFrom$cityname"; ?>",
{ sticky: true,
direction: "auto"
});
var markerImageGreen = new L.icon({
iconUrl: "./ajax-images/mma_20_green.png",
iconSize: [12, 20],
iconAnchor: [6, 20]
});
var marker = new L.marker(new L.LatLng(<?php echo $centerlat;?>,<?php echo $centerlong;?>),{
clickable: true,
draggable: false,
icon: markerImageGreen,
title: "Selected location at <?php echo $centerlat;?>,<?php echo $centerlong;?>"+
"\n(/gridpoints/<?php echo $gridPointMeta; ?>/forecast bounding box shown)"+
"\nadjustment of <?php echo $adj1; ?>,<?php echo $adj2; ?> gridpoint used"
});
map.addLayer(marker);
<?php if(!empty($alertsJS)) { print $alertsJS; } ?>
// display mouse lat/long in page as mouse moves
function mouseMove (e) {
document.getElementById('latspan').innerHTML = e.latlng.lat.toFixed(4)
document.getElementById('lngspan').innerHTML = e.latlng.lng.toFixed(4)
}
map.on('mousemove',mouseMove);
// do 'submit' with mouse lat/long+current zoom as args
function mouseDoubleClicked (e) {
document.getElementById('latlongclicked').value = e.latlng.lat.toFixed(4) + ',' + e.latlng.lng.toFixed(4);
document.getElementById('currentzoom').value = map.getZoom();
document.getElementById('currentmap').value = selMap;
document.getElementById('theSubmitButton').click();
// console.log('submit selMap='+selMap)
}
map.on('dblclick',mouseDoubleClicked);
function mapbasechg (e) {
document.getElementById('currentmap').value = e.name;
selMap = e.name;
//console.log('mapchange selMap='+e.name)
}
map.on('baselayerchange',mapbasechg);
// ]]>
</script>
<?php } // end of got data to print ?>
<p style="text-align:center;"><small>Script by <a href="https://saratoga-weather.org/">Saratoga-weather.org</a>.
Data provided by <a href="https://www.weather.gov/">NOAA/NWS</a></small> </p>
</div>
</div>
<?php if(!$includeMode) { ?>
</body>
</html>
<?php } // end !$includeMode ?>
<?php
// ------------------------------------------------------------------------------------------
// FUNCTIONS
function WXmap_fetchUrlWithoutHanging($inurl)
{
// get contents from one URL and return as string
global $Status, $needCookie /*, $URLcache */;
$useFopen = false;
$overall_start = time();
if (!$useFopen) {
// Set maximum number of seconds (can have floating-point) to wait for feed before displaying page without feed
$numberOfSeconds = 6;
$url = $inurl;
// Thanks to Curly from ricksturf.com for the cURL fetch functions
$data = '';
$domain = parse_url($url, PHP_URL_HOST);
$theURL = str_replace('nocache', '?' . $overall_start, $url); // add cache-buster to URL if needed
$Status.= "<!-- curl fetching '$theURL' -->\n";
$ch = curl_init(); // initialize a cURL session
curl_setopt($ch, CURLOPT_URL, $theURL); // connect to provided URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // don't verify peer certificate
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (wxForecastMap.php (JSON) - saratoga-weather.org)');
// curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0');
curl_setopt($ch, CURLOPT_HTTPHEADER, // request LD-JSON format
array(
"Accept: application/geo+json",
"Cache-control: no-cache",
"Pragma: akamai-x-cache-on, akamai-x-get-request-id"
));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $numberOfSeconds); // connection timeout
curl_setopt($ch, CURLOPT_TIMEOUT, $numberOfSeconds); // data timeout
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the data transfer
curl_setopt($ch, CURLOPT_NOBODY, false); // set nobody
curl_setopt($ch, CURLOPT_HEADER, true); // include header information
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow Location: redirect
curl_setopt($ch, CURLOPT_MAXREDIRS, 1); // but only one time
if (isset($needCookie[$domain])) {
curl_setopt($ch, $needCookie[$domain]); // set the cookie for this request
curl_setopt($ch, CURLOPT_COOKIESESSION, true); // and ignore prior cookies
$Status.= "<!-- cookie used '" . $needCookie[$domain] . "' for GET to $domain -->\n";
}
$data = curl_exec($ch); // execute session
if (curl_error($ch) <> '') { // IF there is an error
$Status.= "<!-- curl Error: " . curl_error($ch) . " -->\n"; // display error notice
}
$cinfo = curl_getinfo($ch); // get info on curl exec.
/*
curl info sample
Array
(
[url] => http://saratoga-weather.net/clientraw.txt
[content_type] => text/plain
[http_code] => 200
[header_size] => 266
[request_size] => 141
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.125
[namelookup_time] => 0.016
[connect_time] => 0.063
[pretransfer_time] => 0.063
[size_upload] => 0
[size_download] => 758
[speed_download] => 6064
[speed_upload] => 0
[download_content_length] => 758
[upload_content_length] => -1
[starttransfer_time] => 0.125
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 74.208.149.102
[certinfo] => Array
(
)
[primary_port] => 80
[local_ip] => 192.168.1.104
[local_port] => 54156
)
*/
if($url !== $cinfo['url'] and $cinfo['http_code'] == 200 and
strpos($url,'/points/') > 0 and strpos($cinfo['url'],'/gridpoints/') > 0) {
# only cache point forecast->gridpoint forecast successful redirects
$Status .= "<!-- note: fetched '".$cinfo['url']."' after redirect was followed. -->\n";
//$URLcache[$inurl] = $cinfo['url'];
//$Status .= "<!-- $inurl added to URLcache -->\n";
}
$Status.= "<!-- HTTP stats: " . " RC=" . $cinfo['http_code'];
if (isset($cinfo['primary_ip'])) {
$Status .= " dest=" . $cinfo['primary_ip'];
}
if (isset($cinfo['primary_port'])) {
$Status .= " port=" . $cinfo['primary_port'];
}
if (isset($cinfo['local_ip'])) {
$Status.= " (from sce=" . $cinfo['local_ip'] . ")";
}
$Status.= "\n Times:" .
" dns=" . sprintf("%01.3f", round($cinfo['namelookup_time'], 3)) .
" conn=" . sprintf("%01.3f", round($cinfo['connect_time'], 3)) .
" pxfer=" . sprintf("%01.3f", round($cinfo['pretransfer_time'], 3));
if ($cinfo['total_time'] - $cinfo['pretransfer_time'] > 0.0000) {
$Status.= " get=" . sprintf("%01.3f", round($cinfo['total_time'] - $cinfo['pretransfer_time'], 3));
}
$Status.= " total=" . sprintf("%01.3f", round($cinfo['total_time'], 3)) . " secs -->\n";
// $Status .= "<!-- curl info\n".print_r($cinfo,true)." -->\n";
curl_close($ch); // close the cURL session
// $Status .= "<!-- raw data\n".$data."\n -->\n";
$stuff = explode("\r\n\r\n",$data); // maybe we have more than one header due to redirects.
$content = (string)array_pop($stuff); // last one is the content
$headers = (string)array_pop($stuff); // next-to-last-one is the headers
if ($cinfo['http_code'] <> '200') {
$Status.= "<!-- headers returned:\n" . $headers . "\n -->\n";
}
return $data; // return headers+contents
}
else {
// print "<!-- using file_get_contents function -->\n";
$STRopts = array(
'http' => array(
'method' => "GET",
'protocol_version' => 1.1,
'header' => "Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (NWS-forecast-map.php - saratoga-weather.org)\r\n" .
"Accept: application/geo+json\r\n",
"Cache-control: no-cache\r\n",
"Pragma: akamai-x-cache-on, akamai-x-get-request-id\r\n"
) ,
'ssl' => array(
'method' => "GET",
'protocol_version' => 1.1,
'verify_peer' => false,
'header' => "Cache-Control: no-cache, must-revalidate\r\n" .
"Cache-control: max-age=0\r\n" .
"Connection: close\r\n" .
"User-agent: Mozilla/5.0 (NWS-forecast-map.php - saratoga-weather.org)\r\n" .
"Accept: application/geo+json\r\n",
"Cache-control: no-cache\r\n",
"Pragma: akamai-x-cache-on, akamai-x-get-request-id\r\n"
)
);
$STRcontext = stream_context_create($STRopts);
$T_start = WXmap_fetch_microtime();
$xml = file_get_contents($inurl, false, $STRcontext);
$T_close = WXmap_fetch_microtime();
$headerarray = get_headers($url, 0);
$theaders = join("\r\n", $headerarray);
$xml = $theaders . "\r\n\r\n" . $xml;
$ms_total = sprintf("%01.3f", round($T_close - $T_start, 3));
$Status.= "<!-- file_get_contents() stats: total=$ms_total secs -->\n";
$Status.= "<-- get_headers returns\n" . $theaders . "\n -->\n";
// print " file() stats: total=$ms_total secs.\n";
$overall_end = time();
$overall_elapsed = $overall_end - $overall_start;
$Status.= "<!-- fetch function elapsed= $overall_elapsed secs. -->\n";
// print "fetch function elapsed= $overall_elapsed secs.\n";
return ($xml);
}
} // end WXmap_fetchUrlWithoutHanging
// ------------------------------------------------------------------
function WXmap_fetch_microtime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
// ------------------------------------------------------------------------------------------
function WXmap_get_alerts($countyURL,$warnZone,$countyZone,$fireZone,$lat,$long,$locName) {
# https://forecast.weather.gov/showsigwx.php?warnzone=MNZ083&warncounty=MNC013&firewxzone=MNZ083&lat=44.1661&lon=-94.0054
# get/decode the alerts (if any) and return
# strings $alertsList for display and $alertsJS for polygons on the map
$alertURL = str_replace('zones/county','alerts/active/zone',$countyURL);
global $Status,$doDebug;
$Status .= "<!-- fetching County alerts -->\n";
$linkURL = "https://forecast.weather.gov/showsigwx.php?warnzone={$warnZone}&warncounty={$countyZone}&firewxzone={$fireZone}&lat={$lat}&lon={$long}&local_place1=".urlencode($locName);
$alertHTML = WXmap_fetchUrlWithoutHanging($alertURL);
$stuff = explode("\r\n\r\n",$alertHTML); // maybe we have more than one header due to redirects.
$content = (string)array_pop($stuff); // last one is the content
$headers = (string)array_pop($stuff); // next-to-last-one is the headers
preg_match('/HTTP\/\S+ (\d+)/', $headers, $m);
//$Status .= "<!-- m=".print_r($m,true)." -->\n";
//$Status .= "<!-- html=".print_r($html,true)." -->\n";
if(!isset($m[1])) {
$Status .= "<!-- failed to fetch $alertURL to process -->\n";
return(array('Error: alert data not available',''));
}
$lastRC = (string)$m[1];
if($lastRC !== '200') { // no data to process
$Status .= "<!-- no alerts to process -->\n";
return(array('',''));
}
$AJSON = json_decode($content,true);
if($doDebug) {$Status .= "<!-- alert JSON\n".var_export($AJSON,true)." -->\n"; }
if(!isset($AJSON['features'][0])) {
$Status .= "<!-- no alerts found at $alertURL -->\n";
return(array('',''));
}
$A = "";
$JS = '';
$colors = array('#DF0101','#F79F81','#F2F5A9','#0174DF','#58FAD0',
'#FACC2E','#01DF01','#F7BE81','#FE2E64','#E0F8EC',
'#DF0101','#F79F81','#F2F5A9','#0174DF','#58FAD0',
'#FACC2E','#01DF01','#F7BE81','#FE2E64','#E0F8EC',
'#DF0101','#F79F81','#F2F5A9','#0174DF','#58FAD0',
'#FACC2E','#01DF01','#F7BE81','#FE2E64','#E0F8EC'
);
foreach($AJSON['features'] as $i => $rJ) {
$J = $rJ['properties'];
if(isset($J['expires'])) {
$exp = strtotime($J['expires']);
$Status .= "<!-- expires '".$J['expires']. "' ";
if(time() > $exp) {
$Status .= " EXPIRED. -->\n";
continue;
} else {
$Status .= " still active. -->\n";
}
}
// generate the text and link
if(isset($J['headline']) and isset($J['description'])) {
$desc = $J['description'];
$headline = preg_replace('| by .*$|i','',$J['headline']);
$product = $J['id'];
# $url = NWS_DETAIL_PRODUCT_URL.$product;
$url = $linkURL;
$A .= '<b><a href="'.$url.'" title="'.$desc.'">'.$headline."</a></b><br/>\n";
$Status .= "<!-- alert $i='".$J['headline']."' found expires='".$J['expires']."' -->\n";
}
// process map defs
if(isset($rJ['geometry']['coordinates'][0])) {
if(true or preg_match('|POLYGON\(\(([^\)]+)\)\)|i',$J['geometry'],$m)) {
$poly = array();
foreach ($rJ['geometry']['coordinates'][0] as $ii => $coords) {
$poly[] = $coords[0].' '.$coords[1];
}
$Status .= "<!-- alert poly cs=$i \n".var_export($poly,true)." -->\n";
$cs = $i;
$cc = count($colors);
if(!isset($colors[$cs])) {$tr = range(0,$cs); shuffle($tr); $cs = $tr[0];}
$rc = isset($colors[$cs])?$colors[$cs]:$colors[1];
$id = $i+1;
$JS .= '
var poly'.$id.' = [
';
foreach ($poly as $i => $coords) {
list($longP,$latP) = explode(' ',$coords);
$JS .= " [$latP,$longP],\n";
}
$tooltip = '<b>'.str_replace(' issued ',"</b><br>issued ",$J['headline']);
$tooltip = str_replace(' until ','<br/> until ',$tooltip);
$tooltip = str_replace(' by ','<br/> by ',$tooltip);
$JS .= '
];
var mapoly'.$id.' = new L.polygon(poly'.$id.',{
opacity: 1.0,
color: "'.$rc.'",
strokeOpacity: 0.9,
weight: 2.5,
fillColor: "'.$rc.'",
fillOpacity: 0.20,
title: "'.$J['event'].'"
}).addTo(map);