Spaces:
Running
Running
File size: 20,145 Bytes
42c7a8a |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Farm Geofencing & AI Design</title>
<!-- External CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css" rel="stylesheet" />
<script src="https://cdn.tailwindcss.com"></script>
<style>
#map {
height: 70vh;
width: 100%;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.control-panel {
background: rgba(255,255,255,0.9);
padding: 15px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.loading-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255,255,255,0.8);
display: none;
justify-content: center;
align-items: center;
z-index: 9999;
}
.spinner {
width: 50px;
height: 50px;
border: 5px solid #f3f3f3;
border-top: 5px solid #3498db;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body class="bg-gray-100">
<!-- Loading Overlay -->
<div id="loadingOverlay" class="loading-overlay">
<div class="spinner"></div>
</div>
<div class="container-fluid py-4">
<div class="row mb-4">
<div class="col-12">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="text-3xl font-bold">Farm Geofencing & AI Design</h1>
<div class="d-flex gap-2">
<button id="helpBtn" class="btn btn-info">
<i class="fas fa-question-circle"></i> Help
</button>
<button id="resetBtn" class="btn btn-warning">
<i class="fas fa-redo"></i> Reset
</button>
</div>
</div>
</div>
</div>
<div class="row">
<!-- Controls Panel -->
<div class="col-md-3">
<!-- Search Location (Optional - keeping for map centering) -->
<div class="control-panel mb-4">
<h5 class="mb-3">Search Location</h5>
<div class="mb-3">
<input type="text" id="addressInput" class="form-control mb-2" placeholder="Enter Address">
<button id="addressSearchBtn" class="btn btn-primary w-100">
<i class="fas fa-search"></i> Search Address
</button>
</div>
<div class="mb-3">
<div class="input-group mb-2">
<input type="number" id="latInput" class="form-control" placeholder="Latitude" step="0.000001">
<input type="number" id="lngInput" class="form-control" placeholder="Longitude" step="0.000001">
</div>
<button id="coordSearchBtn" class="btn btn-primary w-100">
<i class="fas fa-map-marker-alt"></i> Search Coordinates
</button>
</div>
</div>
<!-- Drawing Controls -->
<div class="control-panel mb-4">
<h5 class="mb-3">Drawing Tools</h5>
<div class="btn-group w-100 mb-2">
<button id="startDrawingBtn" class="btn btn-success">Start Drawing</button>
<button id="clearDrawingBtn" class="btn btn-danger">Clear</button>
</div>
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" id="snapToGridCheck">
<label class="form-check-label" for="snapToGridCheck">Snap to Grid</label>
</div>
</div>
<!-- Farm Design Inputs -->
<div class="control-panel mb-4">
<h5 class="mb-3">AI Farm Design</h5>
<div class="mb-3">
<label for="farmTypeSelect" class="form-label">Farming Type</label>
<select id="farmTypeSelect" class="form-select">
<option value="Horticulture">Horticulture (Fruits, Vegetables)</option>
<option value="Plantation">Plantation (Trees, Coffee, Tea)</option>
<option value="Poultry">Poultry (Chickens, Ducks)</option>
<option value="Dairy">Dairy (Cows, Goats)</option>
<option value="Mixed Farming">Mixed Farming</option>
</select>
</div>
<div class="mb-3">
<label for="preferencesInput" class="form-label">Additional Preferences</label>
<textarea id="preferencesInput" class="form-control" rows="3" placeholder="e.g., organic methods, specific crop types, number of animals, water source location..."></textarea>
</div>
<button id="generateDesignBtn" class="btn btn-info w-100">
<i class="fas fa-magic"></i> Generate Farm Design
</button>
</div>
</div>
<!-- Map & Geofence Data Display -->
<div class="col-md-9">
<div class="row">
<div class="col-12">
<div id="map"></div>
</div>
</div>
<!-- Tabbed Data Display -->
<div class="row mt-4">
<div class="col-12">
<div class="card">
<div class="card-body">
<ul class="nav nav-tabs" id="dataTabs">
<li class="nav-item">
<a class="nav-link active" data-bs-toggle="tab" href="#geofenceInfo">Geofence Info</a>
</li>
<li class="nav-item">
<a class="nav-link" data-bs-toggle="tab" href="#farmDesignOutput">AI Farm Design</a>
</li>
</ul>
<div class="tab-content mt-3">
<div class="tab-pane fade show active" id="geofenceInfo">
<div id="geofenceInfoContent">
<p>Draw a polygon on the map to see its area and coordinates.</p>
</div>
</div>
<div class="tab-pane fade" id="farmDesignOutput">
<div id="farmDesignContent">
<p>Click "Generate Farm Design" after drawing a geofence and selecting your farming type.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Help Modal -->
<div class="modal fade" id="helpModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">How to Use the Dashboard</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<h6>Drawing a Farm Area (Geofence)</h6>
<ol>
<li>Click "Start Drawing" to activate polygon drawing mode.</li>
<li>Click on the map to add vertices for your farm's boundary.</li>
<li>Double-click (or click the first point you drew) to complete the polygon.</li>
<li>Once drawn, the farm's area and coordinates will appear in the "Geofence Info" tab.</li>
<li>Use "Clear" to remove the drawn area and its data.</li>
<li>Check "Snap to Grid" to align polygon points to a grid for cleaner shapes.</li>
</ol>
<h6>AI Farm Design</h6>
<ol>
<li>First, draw your farm's geofence on the map.</li>
<li>Select your primary farming type (e.g., Horticulture, Poultry) from the dropdown.</li>
<li>Enter any additional preferences or specific requirements in the text area.</li>
<li>Click "Generate Farm Design". The AI will analyze your farm's location (via a satellite image of the geofenced area) and your input to provide a detailed textual design plan.</li>
<li>The AI's design plan will appear in the "AI Farm Design" tab. This plan includes layout recommendations and a descriptive prompt for how an image generation AI could visualize the design.</li>
</ol>
<h6>Searching Locations</h6>
<ul>
<li>Enter an address or latitude/longitude coordinates and click "Search" to center the map.</li>
</ul>
</div>
</div>
</div>
</div>
<!-- External Scripts -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://kit.fontawesome.com/your-fontawesome-kit.js"></script> <!-- Ensure this points to your Font Awesome kit -->
<script src="https://cdn.jsdelivr.net/npm/toastify-js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBvVLjWmCja331H8SuIZ4UlJdZytuYkC6Y&libraries=drawing,places"></script>
<!-- Custom JavaScript -->
<script>
let map, drawingManager, polygon;
let geocoder;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 20.5937, lng: 78.9629 }, // Default to India
zoom: 5,
mapTypeControl: true,
fullscreenControl: true,
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.RIGHT_BOTTOM
}
});
geocoder = new google.maps.Geocoder();
setupDrawingManager();
setupEventListeners();
}
// Toggle draw mode on click
document.getElementById('startDrawingBtn').addEventListener('click', function() {
if (drawingManager.getDrawingMode() == google.maps.drawing.OverlayType.POLYGON) {
drawingManager.setDrawingMode(null);
this.textContent = "Start Drawing";
} else {
// Clear existing polygon before drawing a new one
clearPolygon();
drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYGON);
this.textContent = "Stop Drawing";
}
});
function setupDrawingManager() {
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: null, // Start with no drawing mode enabled
drawingControl: false, // Don't show the default drawing controls
polygonOptions: {
fillColor: '#4CAF50',
fillOpacity: 0.3,
strokeWeight: 2,
strokeColor: '#4CAF50',
editable: true // Allow editing after drawing
}
});
drawingManager.setMap(map);
// Event listener for when a polygon is completed
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(poly) {
// If there was an old polygon, clear it
if (polygon) {
polygon.setMap(null);
}
polygon = poly;
drawingManager.setDrawingMode(null); // Exit drawing mode
document.getElementById('startDrawingBtn').textContent = "Start Drawing";
if (document.getElementById('snapToGridCheck').checked) {
snapPolygonToGrid(polygon);
}
updateGeofenceData();
// Add listener for polygon path changes (for editing)
google.maps.event.addListener(polygon.getPath(), 'set_at', updateGeofenceData);
google.maps.event.addListener(polygon.getPath(), 'insert_at', updateGeofenceData);
});
}
function snapPolygonToGrid(poly) {
const gridSize = 0.0005; // Finer grid for more precision
let path = poly.getPath();
for (let i = 0; i < path.getLength(); i++) {
let pt = path.getAt(i);
let snappedLat = Math.round(pt.lat() / gridSize) * gridSize;
let snappedLng = Math.round(pt.lng() / gridSize) * gridSize;
path.setAt(i, new google.maps.LatLng(snappedLat, snappedLng));
}
}
function setupEventListeners() {
document.getElementById('clearDrawingBtn').addEventListener('click', clearAll);
document.getElementById('helpBtn').addEventListener('click', function() {
new bootstrap.Modal(document.getElementById('helpModal')).show();
});
document.getElementById('resetBtn').addEventListener('click', function() {
clearAll();
map.setCenter({ lat: 20.5937, lng: 78.9629 }); // Reset to India
map.setZoom(5);
});
document.getElementById('addressSearchBtn').addEventListener('click', searchByAddress);
document.getElementById('coordSearchBtn').addEventListener('click', searchByCoordinates);
document.getElementById('snapToGridCheck').addEventListener('change', function() {
if (polygon && this.checked) {
snapPolygonToGrid(polygon);
updateGeofenceData(); // Recalculate area after snapping
}
});
document.getElementById('generateDesignBtn').addEventListener('click', generateFarmDesign);
}
function clearPolygon() {
if (polygon) {
polygon.setMap(null);
polygon = null;
}
document.getElementById('geofenceInfoContent').innerHTML = '<p>Draw a polygon on the map to see its area and coordinates.</p>';
document.getElementById('farmDesignContent').innerHTML = '<p>Click "Generate Farm Design" after drawing a geofence and selecting your farming type.</p>';
}
function clearAll() {
clearPolygon();
// Reset drawing mode if it was active
drawingManager.setDrawingMode(null);
document.getElementById('startDrawingBtn').textContent = "Start Drawing";
}
function searchByAddress() {
let address = document.getElementById('addressInput').value;
if (!address) {
showToast("Please enter an address");
return;
}
geocoder.geocode({ address: address }, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
let location = results[0].geometry.location;
map.setCenter(location);
map.setZoom(12);
} else {
showToast("Geocode was not successful: " + status);
}
});
}
function searchByCoordinates() {
let lat = parseFloat(document.getElementById('latInput').value);
let lng = parseFloat(document.getElementById('lngInput').value);
if (!isNaN(lat) && !isNaN(lng) && lat >= -90 && lat <= 90 && lng >= -180 && lng <= 180) {
map.setCenter({ lat: lat, lng: lng });
map.setZoom(12); // Zoom in on the specified coordinates
} else {
showToast("Invalid coordinates. Latitude must be between -90 and 90, Longitude between -180 and 180.");
}
}
function updateGeofenceData() {
if (!polygon) {
document.getElementById('geofenceInfoContent').innerHTML = '<p>Draw a polygon on the map to see its area and coordinates.</p>';
return;
}
showLoading(true);
const path = polygon.getPath();
const coordinates = [];
path.forEach(function(latLng) {
coordinates.push({ lat: latLng.lat(), lng: latLng.lng() });
});
fetch('/calculate_area', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ coordinates: coordinates }),
})
.then(response => response.json())
.then(data => {
showLoading(false);
const container = document.getElementById('geofenceInfoContent');
if (data.error) {
container.innerHTML = `<p class="text-danger">Error: ${data.error}</p>`;
showToast(`Error: ${data.error}`);
return;
}
let coordsHtml = '<h6>Coordinates:</h6><ul>';
data.coordinates.forEach(coord => {
coordsHtml += `<li>Lat: ${coord.lat.toFixed(6)}, Lng: ${coord.lng.toFixed(6)}</li>`;
});
coordsHtml += '</ul>';
container.innerHTML = `
<h6>Area:</h6>
<ul>
<li><strong>${data.area_hectares.toFixed(2)} hectares</strong></li>
<li>${data.area_sq_meters.toFixed(2)} sq meters</li>
<li>${data.area_sq_km.toFixed(2)} sq km</li>
<li>${data.area_acres.toFixed(2)} acres</li>
</ul>
${coordsHtml}
`;
})
.catch(error => {
console.error('Error fetching area calculation:', error);
showLoading(false);
document.getElementById('geofenceInfoContent').innerHTML = '<p class="text-danger">Failed to calculate area. Please try again.</p>';
showToast("Failed to calculate area.");
});
}
function generateFarmDesign() {
if (!polygon) {
showToast("Please draw a farm area (geofence) on the map first.");
return;
}
showLoading(true);
const path = polygon.getPath();
const coordinates = [];
path.forEach(function(latLng) {
coordinates.push({ lat: latLng.lat(), lng: latLng.lng() });
});
const farmType = document.getElementById('farmTypeSelect').value;
const preferences = document.getElementById('preferencesInput').value;
fetch('/generate_farm_design', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
coordinates: coordinates,
farm_type: farmType,
preferences: preferences
}),
})
.then(response => response.json())
.then(data => {
showLoading(false);
const container = document.getElementById('farmDesignContent');
if (data.error) {
container.innerHTML = `<p class="text-danger">Error: ${data.error}</p>`;
showToast(`Error: ${data.error}`);
return;
}
let designHtml = `<h6>AI-Generated Farm Design Plan for ${farmType} Farm:</h6>
<div class="card p-3 mb-3">
${data.design_plan.split('\n').map(p => `<p>${p}</p>`).join('')}
</div>`;
if (data.visual_design_prompt) {
designHtml += `<h6 class="mt-4">Prompt for Image Generation AI:</h6>
<div class="card p-3 bg-light">
<p class="mb-0"><em>"${data.visual_design_prompt}"</em></p>
<small class="text-muted mt-2">
(Use this prompt with a dedicated text-to-image AI (e.g., DALL-E, Midjourney, Stable Diffusion) to visualize the design.
The current Google Gemini API provides textual advice and prompts for external image generation.)
</small>
</div>`;
}
if (data.map_image_url) {
designHtml += `<h6 class="mt-4">Farm Area Context:</h6>
<img src="${data.map_image_url}" alt="Farm Area Satellite Image" class="img-fluid rounded shadow-sm mt-2" style="max-width: 600px; border: 1px solid #ddd;">
<small class="text-muted d-block mt-1">Satellite image of the geofenced area used for AI analysis.</small>`;
}
container.innerHTML = designHtml;
showToast("Farm design generated successfully!");
// Switch to the AI Farm Design tab
new bootstrap.Tab(document.querySelector('#dataTabs a[href="#farmDesignOutput"]')).show();
})
.catch(error => {
console.error('Error generating farm design:', error);
showLoading(false);
document.getElementById('farmDesignContent').innerHTML = '<p class="text-danger">Failed to generate farm design. Please check console for details and ensure API keys are correct.</p>';
showToast("Failed to generate farm design.");
});
}
function showLoading(show) {
document.getElementById('loadingOverlay').style.display = show ? 'flex' : 'none';
}
function showToast(message) {
Toastify({
text: message,
duration: 3000,
gravity: "top",
position: "right",
backgroundColor: "#333",
stopOnFocus: true
}).showToast();
}
window.onload = initMap;
</script>
</body>
</html> |