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
// bangalore https://thejeshgn-imd_distrcitwise_warnings.web.val.run/?lat=12.9796&lon=77.5906
// lalitpur up https://thejeshgn-imd_distrcitwise_warnings.web.val.run/?lat=24.6936599&lon=78.412621
// vishisha MP https://thejeshgn-imd_distrcitwise_warnings.web.val.run/?lat=23.5210259&lon=77.809569
export default async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const lat = parseFloat(url.searchParams.get("lat") || "12.9796");
const lon = parseFloat(url.searchParams.get("lon") || "77.5906");
const format = url.searchParams.get("format") || "html";
// 1 degree of latitude is roughly 111 kilometers
const latOffset = 0.0045; // ~500 meters
// Adjust the longitude offset by the latitude's cosine to account for the Earth's curvature
const lonOffset = latOffset / Math.cos(lat * (Math.PI / 180));
const minLat = lat - latOffset;
const minLon = lon - lonOffset;
const maxLat = lat + latOffset;
const maxLon = lon + lonOffset;
const bbox = minLon + "," + minLat + "," + maxLon + "," + maxLat;
// crude hack but works for now
const now = new Date();
const utcOffset = 5.5 * 60; // IST is UTC+5:30
const istDate = new Date(now.getTime() + utcOffset * 60 * 1000);
// const today = istDate.toISOString();
const today = istDate.toISOString().replace("Z", "+05:30");
const osmand_web = "http://osmand.net/go.html?z=16&lat=" + lat + "&lon=" + lon;
const gmaps_poi = "https://www.google.com/maps?&hl=en&t=m&z=12&ll=" + lat + "," + lon + "&q=" + lat + "," + lon;
const osm = "http://www.openstreetmap.org/?&zoom=16&layers=M&mlat=" + lat + "&mlon=" + lon;
const geo_uri = "geo:" + lat + "," + lon;
const dist_nowcast_warnings =
"https://reactjs.imd.gov.in/geoserver/imd/wms?service=WMS&request=GetMap&layers=imd%3ANowcast_StateDistrict_Merged&styles=&format=image%2Fsvg&transparent=true&version=1.1.1&width=256&height=256&srs=EPSG%3A4326&bbox="
+ bbox;
// env=day:Day1_Color
const dist_warnings_today =
"https://reactjs.imd.gov.in/geoserver/imd/wms?service=WMS&request=GetMap&layers=imd%3AWarnings_StateDistrict_Merged&styles=&format=image%2Fsvg&transparent=true&version=1.1.1&env=day%3ADay1_Color&width=256&height=256&srs=EPSG%3A4326&bbox="
+ bbox;
// env=day:Day2_Color
const dist_warnings_today_plus_1 =
"https://reactjs.imd.gov.in/geoserver/imd/wms?service=WMS&request=GetMap&layers=imd%3AWarnings_StateDistrict_Merged&styles=&format=image%2Fsvg&transparent=true&version=1.1.1&env=day%3ADay2_Color&width=256&height=256&srs=EPSG%3A4326&bbox="
+ bbox;
// env=day:Day3_Color
const dist_warnings_today_plus_2 =
"https://reactjs.imd.gov.in/geoserver/imd/wms?service=WMS&request=GetMap&layers=imd%3AWarnings_StateDistrict_Merged&styles=&format=image%2Fsvg&transparent=true&version=1.1.1&env=day%3ADay3_Color&width=256&height=256&srs=EPSG%3A4326&bbox="
+ bbox;
// env=day:Day4_Color
const dist_warnings_today_plus_3 =
"https://reactjs.imd.gov.in/geoserver/imd/wms?service=WMS&request=GetMap&layers=imd%3AWarnings_StateDistrict_Merged&styles=&format=image%2Fsvg&transparent=true&version=1.1.1&env=day%3ADay4_Color&width=256&height=256&srs=EPSG%3A4326&bbox="
+ bbox;
// env=day:Day5_Color
const dist_warnings_today_plus_4 =
"https://reactjs.imd.gov.in/geoserver/imd/wms?service=WMS&request=GetMap&layers=imd%3AWarnings_StateDistrict_Merged&styles=&format=image%2Fsvg&transparent=true&version=1.1.1&env=day%3ADay5_Color&width=256&height=256&srs=EPSG%3A4326&bbox="
+ bbox;
/*
//extract color from SVGs
svgElement = document.querySelector('svg');
shapeElement = svgElement.querySelector('g')
rect = shapeElement.querySelector('g')
fillColor = rect.getAttribute('fill');
*/
const r = {
"today": today,
"lat": lat,
"lon": lon,
"bbox": bbox,
"osmand_web": osmand_web,
"gmaps_poi": gmaps_poi,
"osm": osm,
"geo_uri": geo_uri,
"dist_nowcast_warnings": dist_nowcast_warnings,
"dist_warnings_today": dist_warnings_today,
"dist_warnings_today_plus_1": dist_warnings_today_plus_1,
"dist_warnings_today_plus_2": dist_warnings_today_plus_2,
"dist_warnings_today_plus_3": dist_warnings_today_plus_3,
"dist_warnings_today_plus_4": dist_warnings_today_plus_4,
};
if (format == "json") {
return Response.json(r);
} else {
const bboxHtml = `
<html>
<body>
<h1>IMD Warnings</h1>
<p>Center: (${lat}, ${lon})</p>
<p>
<a target="_blank" href="${osmand_web}" >OSMAnd Web</a>,&nbsp;
<a target="_blank" href="${gmaps_poi}">GMaps</a>,&nbsp;
<a target="_blank" href="${osm}">OSM Web</a>,&nbsp;
<a target="_blank" href="${geo_uri}">Geo URL</a>