bhuvan / geojson.html
Zeel's picture
Upload folder using huggingface_hub
5262418 verified
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://unpkg.com/@turf/[email protected]/turf.min.js"></script>
<link rel="stylesheet" href="style.css"> <!-- Link to the external style.css file -->
</head>
<body>
<header>Interactive GeoJSON Map Viewer</header>
<div id="map"></div>
<div id="stats">
<h3>GeoJSON Statistics</h3>
<div class="stat-item">
<div class="stat-label"><i>πŸ“</i> Area (sq meters):</div>
<div id="area_sqm" class="value">Loading...</div>
</div>
<div class="stat-item">
<div class="stat-label"><i>🌍</i> Area (hectares):</div>
<div id="area_hectares" class="value">Loading...</div>
</div>
<div class="stat-item">
<div class="stat-label"><i>πŸ“</i> Centroid:</div>
<div id="centroid" class="value">Loading...</div>
</div>
<div class="stat-item">
<span class="stat-label"><i>Summary:</i></span>
<div class="value" id="summary">Loading...</div>
</div>
</div>
<footer>&copy; 2024 GeoJSON Map Viewer by Sustainability Lab IIT Gandhinagar</footer>
<script src="geostats.js"></script> <!-- Custom script for area and centroid calculations -->
<script type="text/javascript">
// Initialize the map
const map = L.map('map').setView([58.4, 43.0], 11);
// Add a base map layer
new L.TileLayer('http://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}').addTo(map);
// Function to load GeoJSON and calculate statistics
function loadGeoJSON(url) {
fetch(url)
.then(response => response.json())
.then(data => {
const geoJsonLayer = L.geoJSON(data);
geoJsonLayer.addTo(map);
// Fit map to GeoJSON bounds
map.fitBounds(geoJsonLayer.getBounds());
// Call functions from gestats.js
const areaStats = calculateTotalArea(data);
const centroid = calculateCentroid(data);
const geometryCounts = countGeometryTypes(data);
const featureNames = getFeatureNames(data);
// Display area, centroid, and geometry summary in HTML
document.getElementById('area_sqm').textContent = areaStats.areaSqMeters.toFixed(2);
document.getElementById('area_hectares').textContent = areaStats.areaHectares.toFixed(2);
document.getElementById('centroid').textContent =
`Lat: ${centroid.geometry.coordinates[1].toFixed(5)},
Lng: ${centroid.geometry.coordinates[0].toFixed(5)}`;
document.getElementById('summary').innerHTML = `
<strong>Polygons:</strong> ${geometryCounts.Polygon} <br>
<strong>Points:</strong> ${geometryCounts.Point} <br>
<strong>LineStrings:</strong> ${geometryCounts.LineString} <br>
<strong>MultiPolygons:</strong> ${geometryCounts.MultiPolygon} <br>
<strong>Feature Names:</strong> ${featureNames.length > 0 ? featureNames.join(', ') : 'N/A'}
`;
})
.catch(error => {
console.error('Error loading GeoJSON:', error);
alert('Failed to load GeoJSON file. Please check the URL.');
});
}
// Get the GeoJSON URL from the query parameter 'geojson_url'
const urlParams = new URLSearchParams(window.location.search);
const geojsonUrl = urlParams.get('geojson_url');
// Load GeoJSON if a URL is provided
if (geojsonUrl) {
loadGeoJSON(geojsonUrl);
} else {
alert('Please provide a valid GeoJSON URL in the "geojson_url" query parameter.');
}
</script>
</body>
</html>