File size: 4,833 Bytes
35ea519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// filepath: /gis-tools-app/gis-tools-app/frontend/script.js
document.addEventListener('DOMContentLoaded', function() {
    // Initialize tool-specific functionalities
    if (document.getElementById('calculate-distance')) {
        setupDistanceCalculator();
    }
    if (document.getElementById('calculate-area')) {
        setupAreaCalculator();
    }
    if (document.getElementById('convert-coordinates')) {
        setupCoordinateConverter();
    }
    if (document.getElementById('extract-terrain')) {
        setupTerrainDataExtractor();
    }
    if (document.getElementById('calculate-elevation')) {
        setupElevationCalculator();
    }
});

function setupDistanceCalculator() {
    document.getElementById('calculate-distance').addEventListener('click', async function() {
        const lat1 = parseFloat(document.getElementById('latitude-1').value);
        const lon1 = parseFloat(document.getElementById('longitude-1').value);
        const lat2 = parseFloat(document.getElementById('latitude-2').value);
        const lon2 = parseFloat(document.getElementById('longitude-2').value);
        
        const response = await fetch('/api/distance', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ lat1, lon1, lat2, lon2 })
        });
        
        const data = await response.json();
        document.getElementById('distance-result').innerText = `Distance: ${data.distance} km`;
    });
}

function setupAreaCalculator() {
    document.getElementById('calculate-area').addEventListener('click', async function() {
        const coordinates = getPolygonCoordinates();
        
        const response = await fetch('/api/area', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ coordinates })
        });
        
        const data = await response.json();
        document.getElementById('area-result').innerText = `Area: ${data.area} sq km`;
    });
}

function getPolygonCoordinates() {
    const coords = [];
    const coordElements = document.querySelectorAll('.coordinate-input');
    coordElements.forEach(input => {
        const lat = parseFloat(input.querySelector('.latitude-input').value);
        const lon = parseFloat(input.querySelector('.longitude-input').value);
        if (!isNaN(lat) && !isNaN(lon)) {
            coords.push([lat, lon]);
        }
    });
    return coords;
}

function setupCoordinateConverter() {
    document.getElementById('convert-coordinates').addEventListener('click', async function() {
        const lat = parseFloat(document.getElementById('coordinate-lat').value);
        const lon = parseFloat(document.getElementById('coordinate-lon').value);
        const sourceCRS = document.getElementById('source-crs').value;
        const targetCRS = document.getElementById('target-crs').value;
        
        const response = await fetch('/api/coordinate-convert', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ lat, lon, sourceCRS, targetCRS })
        });
        
        const data = await response.json();
        document.getElementById('converted-coords').innerText = `Converted: ${data.lat}, ${data.lon}`;
    });
}

function setupTerrainDataExtractor() {
    document.getElementById('extract-terrain').addEventListener('click', async function() {
        const lat = parseFloat(document.getElementById('terrain-lat').value);
        const lon = parseFloat(document.getElementById('terrain-lon').value);
        
        const response = await fetch('/api/terrain', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ lat, lon })
        });
        
        const data = await response.json();
        document.getElementById('terrain-result').innerText = `Slope: ${data.slope}, Aspect: ${data.aspect}`;
    });
}

function setupElevationCalculator() {
    document.getElementById('calculate-elevation').addEventListener('click', async function() {
        const lat = parseFloat(document.getElementById('elevation-lat').value);
        const lon = parseFloat(document.getElementById('elevation-lon').value);
        
        const response = await fetch('/api/elevation', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ latitude: lat, longitude: lon })
        });
        
        const data = await response.json();
        document.getElementById('elevation-result').innerText = `Elevation: ${data.elevation} meters`;
    });
}