File size: 7,047 Bytes
c2cbf85
 
 
 
 
 
e41c65d
c2cbf85
e41c65d
c2cbf85
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
e41c65d
c2cbf85
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
e41c65d
 
c2cbf85
 
 
 
 
 
e41c65d
c2cbf85
e41c65d
 
c2cbf85
 
 
 
e41c65d
c2cbf85
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e41c65d
 
c2cbf85
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
 
 
e41c65d
c2cbf85
 
 
 
e41c65d
c2cbf85
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Pest GeoSpatial Analytics</title>
  <style>
    /* Import a Google Font for a modern look */
    @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');

    @keyframes fadeIn {
      from { opacity: 0; transform: translateY(20px); }
      to { opacity: 1; transform: translateY(0); }
    }

    body {
      margin: 0;
      padding: 0;
      font-family: 'Roboto', sans-serif;
      background: linear-gradient(135deg, #e8f5e9, #c8e6c9);
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }

    .container {
      width: 95%;
      max-width: 800px;
      background: #fff;
      padding: 40px;
      margin: 20px;
      border-radius: 12px;
      box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
      animation: fadeIn 1s ease-out;
    }

    h1 {
      color: #2e7d32;
      text-align: center;
      margin-bottom: 30px;
      font-size: 2.8em;
    }

    form {
      display: flex;
      flex-direction: column;
      gap: 20px;
    }

    label {
      font-weight: 500;
      margin-bottom: 8px;
      color: #333;
    }

    select, button {
      width: 100%;
      padding: 14px 18px;
      border: 1px solid #ccc;
      border-radius: 8px;
      font-size: 1em;
      transition: all 0.3s ease;
    }

    select:focus, button:focus {
      outline: none;
      border-color: #2e7d32;
      box-shadow: 0 0 8px rgba(46, 125, 50, 0.5);
    }

    button {
      background-color: #2e7d32;
      color: #fff;
      font-weight: 700;
      cursor: pointer;
      margin-top: 10px;
    }

    button:hover {
      background-color: #276a2e;
      transform: scale(1.02);
    }

    .note {
      font-style: italic;
      color: #555;
      margin-top: 10px;
      text-align: center;
    }

    .map {
      margin-top: 40px;
      text-align: center;
    }

    .map h2 {
      color: #2e7d32;
      margin-bottom: 20px;
      font-size: 2em;
    }

    .map img {
      max-width: 100%;
      border: 3px solid #2e7d32;
      border-radius: 8px;
      transition: opacity 0.5s ease;
    }

    .error {
      color: red;
      font-weight: 700;
      margin-top: 20px;
      font-size: 1.2em;
    }
  </style>
</head>
<body>
<div class="container">
  <h1>Pest GeoSpatial Analytics</h1>

  <form method="GET" action="/">
    <!-- Crop selection -->
    <label for="crop">Crop:</label>
    <select id="crop" name="crop" onchange="updatePestDropdown()">
      <option value="">--Select Crop--</option>
      {% for c in crops %}
        <option value="{{ c }}" {% if selected_crop == c %}selected{% endif %}>{{ c }}</option>
      {% endfor %}
    </select>

    <!-- Pest selection (populated dynamically) -->
    <label for="pest">Pest:</label>
    <select id="pest" name="pest">
      <option value="">--Select Pest--</option>
      <!-- Options will be set by JavaScript based on selected crop -->
    </select>

    <!-- Year selection -->
    <label for="year">Year:</label>
    <select id="year" name="year" onchange="fetchWeeks()">
      <option value="">--Select Year--</option>
      {% for y in years %}
        <option value="{{ y }}" {% if selected_year == y %}selected{% endif %}>{{ y }}</option>
      {% endfor %}
    </select>

    <!-- Week selection (populated dynamically) -->
    <label for="week">Week:</label>
    <select id="week" name="week">
      <option value="">--Select Week--</option>
      {% if selected_week %}
        <option value="{{ selected_week }}" selected>{{ selected_week }}</option>
      {% endif %}
    </select>

    <!-- Parameter selection -->
    <label for="param">Parameter:</label>
    <select id="param" name="param">
      <option value="">--Select Parameter--</option>
      {% for code, label in params.items() %}
        <option value="{{ code }}" {% if selected_param == code %}selected{% endif %}>{{ label }}</option>
      {% endfor %}
    </select>

    <button type="submit">Load Map</button>
    <p class="note">Select a Crop, Pest, Year, Week, and Parameter to load the corresponding map.</p>
  </form>

  {% if image_url %}
  <div class="map" id="mapContainer">
    <h2>Fetched Results</h2>
    <!-- Use the proxy URL for the image -->
    <img src="{{ image_url }}" alt="Map for {{ selected_param }} parameter" onerror="handleImageError(this)">
    <div id="dataNotAvailable" class="error" style="display:none;">Data Not Available. Cannot plot map.</div>
  </div>
  {% endif %}
</div>

<script>
  // Crop-to-Pest mapping provided from the server
  const cropToPests = {{ crop_to_pests | tojson }};

  // Update the pest dropdown based on the selected crop
  function updatePestDropdown() {
    const cropSelect = document.getElementById("crop");
    const pestSelect = document.getElementById("pest");
    const selectedCrop = cropSelect.value;

    pestSelect.innerHTML = "";
    const defaultOption = document.createElement("option");
    defaultOption.value = "";
    defaultOption.textContent = "--Select Pest--";
    pestSelect.appendChild(defaultOption);

    if (selectedCrop && cropToPests[selectedCrop]) {
      const pests = cropToPests[selectedCrop];
      pests.forEach(function(p) {
        const option = document.createElement("option");
        option.value = p;
        option.textContent = p;
        if (p === "{{ selected_pest }}") {
          option.selected = true;
        }
        pestSelect.appendChild(option);
      });
    }
    if (document.getElementById("year").value) {
      fetchWeeks();
    }
  }

  // Fetch week options dynamically from the /fetch_weeks endpoint
  function fetchWeeks() {
    const crop = document.getElementById("crop").value;
    const pest = document.getElementById("pest").value;
    const year = document.getElementById("year").value;
    if (!crop || !pest || !year) return;

    fetch(`/fetch_weeks?crop=${crop}&pest=${pest}&year=${year}`)
      .then(response => response.json())
      .then(data => {
        const weekSelect = document.getElementById("week");
        weekSelect.innerHTML = "";
        const defaultOption = document.createElement("option");
        defaultOption.value = "";
        defaultOption.textContent = "--Select Week--";
        weekSelect.appendChild(defaultOption);

        data.weeks.forEach(function(week) {
          const option = document.createElement("option");
          option.value = week;
          option.textContent = week;
          if (week === "{{ selected_week }}") {
            option.selected = true;
          }
          weekSelect.appendChild(option);
        });
      })
      .catch(err => console.error("Error fetching weeks:", err));
  }

  function handleImageError(img) {
    img.style.display = "none";
    document.getElementById("dataNotAvailable").style.display = "block";
  }

  window.onload = function() {
    updatePestDropdown();
    if ("{{ selected_year }}" && "{{ selected_crop }}" && "{{ selected_pest }}") {
      fetchWeeks();
    }
  };
</script>

</body>
</html>