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let csvData = null;
|
| 4 |
+
|
| 5 |
+
// Initialize the map
|
| 6 |
+
function initMap() {
|
| 7 |
+
map = L.map('map').setView([20.5937, 78.9629], 5); // Centered on India
|
| 8 |
+
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
| 9 |
+
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
| 10 |
+
}).addTo(map);
|
| 11 |
+
|
| 12 |
+
// Add click event listener to the map
|
| 13 |
+
map.on('click', onMapClick);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
// Function to handle the map click event
|
| 17 |
+
async function onMapClick(e) {
|
| 18 |
+
const { lat, lng } = e.latlng;
|
| 19 |
+
try {
|
| 20 |
+
const populationDensity = await fetchLULCStatistics(lat, lng);
|
| 21 |
+
// Create popup
|
| 22 |
+
L.popup()
|
| 23 |
+
.setLatLng([lat, lng])
|
| 24 |
+
.setContent(`Latitude: ${lat}<br>Longitude: ${lng}<br>Population Density: ${populationDensity}`)
|
| 25 |
+
.openOn(map);
|
| 26 |
+
} catch (error) {
|
| 27 |
+
console.error("Error fetching data:", error);
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
// Function to fetch LULC Statistics from the Bhuvan API
|
| 32 |
+
async function fetchLULCStatistics(latitude, longitude) {
|
| 33 |
+
const apiUrl = `https://bhuvan-api.gov.in/lulc_statistics?lat=${latitude}&lon=${longitude}&token=${API_KEY}`;
|
| 34 |
+
try {
|
| 35 |
+
const response = await fetch(apiUrl);
|
| 36 |
+
if (!response.ok) {
|
| 37 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 38 |
+
}
|
| 39 |
+
const data = await response.json();
|
| 40 |
+
const populationDensity = data.population_density || "Data not available";
|
| 41 |
+
return populationDensity;
|
| 42 |
+
} catch (error) {
|
| 43 |
+
console.error("Failed to fetch LULC statistics:", error);
|
| 44 |
+
return "Error fetching data";
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// Drag and Drop Functionality
|
| 49 |
+
const dropArea = document.getElementById('drop-area');
|
| 50 |
+
const csvUpload = document.getElementById('csv-upload');
|
| 51 |
+
|
| 52 |
+
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
| 53 |
+
dropArea.addEventListener(eventName, preventDefaults, false)
|
| 54 |
+
})
|
| 55 |
+
|
| 56 |
+
function preventDefaults (e) {
|
| 57 |
+
e.preventDefault()
|
| 58 |
+
e.stopPropagation()
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
['dragenter', 'dragover'].forEach(eventName => {
|
| 62 |
+
dropArea.addEventListener(eventName, highlight, false)
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
['dragleave', 'drop'].forEach(eventName => {
|
| 66 |
+
dropArea.addEventListener(eventName, unhighlight, false)
|
| 67 |
+
})
|
| 68 |
+
|
| 69 |
+
function highlight(e) {
|
| 70 |
+
dropArea.classList.add('highlight')
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
function unhighlight(e) {
|
| 74 |
+
dropArea.classList.remove('highlight')
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
dropArea.addEventListener('drop', handleDrop, false)
|
| 78 |
+
|
| 79 |
+
function handleDrop(e) {
|
| 80 |
+
const dt = e.dataTransfer
|
| 81 |
+
const files = dt.files
|
| 82 |
+
|
| 83 |
+
handleFiles(files)
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
csvUpload.addEventListener('change', function() {
|
| 87 |
+
handleFiles(this.files);
|
| 88 |
+
});
|
| 89 |
+
|
| 90 |
+
function handleFiles(files) {
|
| 91 |
+
const file = files[0];
|
| 92 |
+
if (file && file.type === "text/csv") {
|
| 93 |
+
const reader = new FileReader();
|
| 94 |
+
reader.onload = function(event) {
|
| 95 |
+
csvData = event.target.result;
|
| 96 |
+
plotCsvDataOnMap();
|
| 97 |
+
};
|
| 98 |
+
reader.readAsText(file);
|
| 99 |
+
} else {
|
| 100 |
+
alert("Please upload a valid CSV file.");
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// CSV Parsing and Map Plotting
|
| 105 |
+
function parseCSV(csvText) {
|
| 106 |
+
const lines = csvText.trim().split('\n');
|
| 107 |
+
const headers = lines[0].split(',').map(header => header.trim());
|
| 108 |
+
const data = [];
|
| 109 |
+
|
| 110 |
+
for (let i = 1; i < lines.length; i++) {
|
| 111 |
+
const values = lines[i].split(',').map(value => value.trim());
|
| 112 |
+
if (values.length === headers.length) {
|
| 113 |
+
const entry = {};
|
| 114 |
+
for (let j = 0; j < headers.length; j++) {
|
| 115 |
+
entry[headers[j]] = values[j];
|
| 116 |
+
}
|
| 117 |
+
data.push(entry);
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
return data;
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
// Function to plot CSV data on the map
|
| 124 |
+
function plotCsvDataOnMap() {
|
| 125 |
+
if (!csvData) {
|
| 126 |
+
alert("Please upload a CSV file first.");
|
| 127 |
+
return;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
const data = parseCSV(csvData);
|
| 131 |
+
|
| 132 |
+
// Clear existing markers on the map (if any)
|
| 133 |
+
map.eachLayer(function (layer) {
|
| 134 |
+
if (layer instanceof L.Marker) {
|
| 135 |
+
map.removeLayer(layer);
|
| 136 |
+
}
|
| 137 |
+
});
|
| 138 |
+
|
| 139 |
+
data.forEach(async entry => {
|
| 140 |
+
if (entry.Latitude && entry.Longitude) {
|
| 141 |
+
const latitude = parseFloat(entry.Latitude);
|
| 142 |
+
const longitude = parseFloat(entry.Longitude);
|
| 143 |
+
|
| 144 |
+
// Add marker for each location
|
| 145 |
+
L.marker([latitude, longitude]).addTo(map)
|
| 146 |
+
.bindPopup(`Latitude: ${latitude}<br>Longitude: ${longitude}`);
|
| 147 |
+
}
|
| 148 |
+
});
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
// Initialize map when the window loads
|
| 152 |
+
window.onload = initMap;
|