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";
const latOffset = 0.0045;
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;
const now = new Date();
const utcOffset = 5.5 * 60;
const istDate = new Date(now.getTime() + utcOffset * 60 * 1000);
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;
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;
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;
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;
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;
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;
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>,
<a target="_blank" href="${gmaps_poi}">GMaps</a>,
<a target="_blank" href="${osm}">OSM Web</a>,
<a target="_blank" href="${geo_uri}">Geo URL</a>