File size: 4,074 Bytes
5262418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<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>