File size: 5,840 Bytes
af11331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf1f925
 
af11331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf1f925
 
 
af11331
 
 
 
 
 
 
cf1f925
af11331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf1f925
 
 
af11331
cf1f925
af11331
cf1f925
 
 
af11331
 
 
 
 
 
 
cf1f925
 
 
 
 
 
 
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
let polygonDraw = null;
let currentMap = null;

$(document).ready(function() {
    // Location search form submission
    $('#locationForm').on('submit', function(e) {
        e.preventDefault();
        
        $.ajax({
            url: '/search_location',
            method: 'POST',
            data: {
                location: $('#location').val()
            },
            success: function(response) {
                $('#mapContainer').removeClass('d-none');
                loadMap(response.lat, response.lon);
            },
            error: function(xhr) {
                alert('Error: ' + (xhr.responseJSON ? xhr.responseJSON.error : 'Location not found'));
            }
        });
    });

    // File upload form submission
    $('#uploadForm').on('submit', function(e) {
        e.preventDefault();
        
        const formData = new FormData();
        const fileInput = $('#imageUpload')[0];
        
        if (fileInput.files.length === 0) {
            alert('Please select a file to upload');
            return;
        }
        
        formData.append('file', fileInput.files[0]);
        
        $.ajax({
            url: '/upload',
            method: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(response) {
                window.location.href = `/analyze?image=${encodeURIComponent(response.filepath)}`;
            },
            error: function(xhr) {
                alert('Error uploading file: ' + (xhr.responseJSON ? xhr.responseJSON.error : 'Upload failed'));
            }
        });
    });

    // Screenshot capture button click
    $('#captureBtn').on('click', function() {
        if (!polygonDraw) {
            // Initialize polygon drawing if not already initialized
            const mapContainer = document.getElementById('map');
            mapContainer.classList.add('drawing-mode');
            polygonDraw = new PolygonDraw(mapContainer);
            polygonDraw.startDrawing();
            $(this).text('Complete Drawing (Press Enter)');
        } else if (polygonDraw.isDrawing) {
            const points = polygonDraw.completePolygon();
            const mapContainer = document.getElementById('map');
            mapContainer.classList.remove('drawing-mode');
            if (points.length >= 3) {
                captureWithPolygon(points);
            }
            $(this).text('Capture Screenshot');
        } else {
            const mapContainer = document.getElementById('map');
            mapContainer.classList.add('drawing-mode');
            polygonDraw.startDrawing();
            $(this).text('Complete Drawing (Press Enter)');
        }
    });

    // Analyze button click
    $('#analyzeBtn').on('click', function() {
        const screenshotPath = $('#capturedImage').attr('src');
        if (screenshotPath) {
            window.location.href = `/analyze?image=${encodeURIComponent(screenshotPath)}`;
        }
    });

    // Add keyboard event listener for Enter key
    document.addEventListener('keydown', function(event) {
        if (event.key === 'Enter' && polygonDraw && polygonDraw.isDrawing) {
            const points = polygonDraw.completePolygon();
            const mapContainer = document.getElementById('map');
            mapContainer.classList.remove('drawing-mode');
            if (points.length >= 3) {
                captureWithPolygon(points);
            }
            $('#captureBtn').text('Capture Screenshot');
        }
    });
});

function loadMap(lat, lon) {
    const mapDiv = document.getElementById('map');
    mapDiv.innerHTML = ''; // Clear any existing content
    
    // Create a new map instance
    currentMap = L.map('map', {
        center: [lat, lon],
        zoom: 20,
        maxZoom: 18,
        preferCanvas: true
    });
    
    // Add the satellite tile layer
    L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
        attribution: 'Esri',
        maxZoom: 20,
        tileSize: 256
    }).addTo(currentMap);

    // Wait for the map to load completely
    currentMap.whenReady(() => {
        currentMap.invalidateSize();
        currentMap.setView([lat, lon], 20);
    });
}

function captureWithPolygon(points) {
    const mapContainer = document.getElementById('map');
    
    // Get the current map state
    const center = currentMap.getCenter();
    const bounds = currentMap.getBounds();
    const zoom = currentMap.getZoom();
    
    const data = {
        width: mapContainer.offsetWidth,
        height: mapContainer.offsetHeight,
        polygon: points,
        mapState: {
            center: {
                lat: center.lat,
                lng: center.lng
            },
            bounds: {
                north: bounds.getNorth(),
                south: bounds.getSouth(),
                east: bounds.getEast(),
                west: bounds.getWest()
            },
            zoom: zoom
        }
    };

    // Log the map state for debugging
    console.log('Capturing map state:', data.mapState);

    fetch('/capture_screenshot', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(data)
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            $('#screenshotGallery').removeClass('d-none');
            const imagePath = data.cutout_path || data.screenshot_path;
            const imageUrl = imagePath + '?t=' + new Date().getTime();
            $('#capturedImage').attr('src', imageUrl);
        } else {
            alert('Error capturing screenshot');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('Error capturing screenshot');
    });
}