-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
230 lines (190 loc) · 7.18 KB
/
main.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const map = new maplibregl.Map({
container: 'map',
style: 'https://tiles.openfreemap.org/styles/liberty',
center: [0, 0],
zoom: 2
});
let geojsonSource = null; // Keep this, it's fine for the source.
// No need for global layer variables. We'll manage layer existence directly on the map.
function updateFeatureDropdown(geojson) {
// ... (Your existing code, this part is correct) ...
const dropdown = document.getElementById('feature-select');
dropdown.innerHTML = '';
const zoomAllOption = document.createElement('option');
zoomAllOption.value = 'all';
zoomAllOption.text = 'Zoom to All';
dropdown.appendChild(zoomAllOption);
if (geojson && geojson.features && geojson.features.length > 0) {
geojson.features.forEach((feature, index) => {
const option = document.createElement('option');
option.value = index;
option.text = feature.properties.name || `Feature ${index + 1}`;
dropdown.appendChild(option);
});
document.getElementById('feature-dropdown').style.display = 'block';
} else {
document.getElementById('feature-dropdown').style.display = 'none';
}
}
function zoomToFeature(featureIndex) {
// ... (Your existing code, this part is correct) ...
if (!geojsonSource || !geojsonSource.data || !geojsonSource.data.features) {
console.warn("GeoJSON data not loaded or invalid.");
return;
}
const flyDuration = parseInt(document.getElementById('fly-duration').value, 10);
if (featureIndex === "all") {
const bounds = new maplibregl.LngLatBounds();
geojsonSource.data.features.forEach(feature => {
const processCoordinates = (coords) => {
if (Array.isArray(coords) && coords.length > 0) {
if (Array.isArray(coords[0])) {
coords.forEach(processCoordinates);
} else {
bounds.extend(coords);
}
}
};
processCoordinates(feature.geometry.coordinates);
});
if (!bounds.isEmpty()) {
map.fitBounds(bounds, { padding: 20, duration: flyDuration });
}
} else {
const feature = geojsonSource.data.features[parseInt(featureIndex, 10)];
if (!feature) {
console.warn("Feature not found at index:", featureIndex);
return;
}
const bounds = new maplibregl.LngLatBounds();
const processCoordinates = (coords) => {
if (Array.isArray(coords) && coords.length > 0) {
if (Array.isArray(coords[0])) {
coords.forEach(processCoordinates);
}
else {
bounds.extend(coords);
}
}
};
processCoordinates(feature.geometry.coordinates);
if (!bounds.isEmpty()) {
map.fitBounds(bounds, { padding: 20, duration: flyDuration });
}
}
}
function handleFile(file) {
const reader = new FileReader();
reader.onload = function(e) {
try {
const geojson = JSON.parse(e.target.result);
// Check if the source exists *before* removing layers and source.
if (map.getSource('geojson-data')) {
// Check if the layers exist *before* removing them.
if (map.getLayer('geojson-layer-fill')) {
map.removeLayer('geojson-layer-fill');
}
if (map.getLayer('geojson-layer-line')) {
map.removeLayer('geojson-layer-line');
}
map.removeSource('geojson-data');
}
geojsonSource = {
type: 'geojson',
data: geojson
};
map.addSource('geojson-data', geojsonSource);
addLayers(); // addLayers() is now safe.
zoomToFeature("all");
updateFeatureDropdown(geojson);
} catch (error) {
console.error("Error parsing GeoJSON:", error);
alert("Invalid GeoJSON file.");
}
document.getElementById('drop-area').style.display = 'none';
};
reader.readAsText(file);
}
function addLayers() {
// No need to remove layers here. We handle removal in handleFile.
const geojsonLayerFill = {
'id': 'geojson-layer-fill',
'type': 'fill',
'source': 'geojson-data',
'paint': {
'fill-color': document.getElementById('fill-color').value,
'fill-opacity': 0.5
},
'filter': ['==', '$type', 'Polygon']
};
map.addLayer(geojsonLayerFill);
const geojsonLayerLine = {
'id': 'geojson-layer-line',
'type': 'line',
'source': 'geojson-data',
'paint': {
'line-color': document.getElementById('stroke-color').value,
'line-width': parseInt(document.getElementById('stroke-width').value, 10),
'line-opacity': 0.5
}
};
map.addLayer(geojsonLayerLine);
}
function updateStyle() {
if (!map.getSource('geojson-data')) {
return;
}
map.setPaintProperty('geojson-layer-line', 'line-color', document.getElementById('stroke-color').value);
map.setPaintProperty('geojson-layer-line', 'line-width', parseInt(document.getElementById('stroke-width').value, 10));
if (map.getLayer('geojson-layer-fill')) {
map.setPaintProperty('geojson-layer-fill', 'fill-color', document.getElementById('fill-color').value);
}
}
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('file-input');
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
document.body.addEventListener(eventName, preventDefaults, false);
});
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
dropArea.addEventListener('drop', handleDrop, false);
// dropArea.addEventListener('click', () => fileInput.click()); // Trigger file input on click. This line is fine.
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
function highlight(e) {
dropArea.classList.add('active');
}
function unhighlight(e) {
dropArea.classList.remove('active');
}
function handleDrop(e) {
const dt = e.dataTransfer;
const files = dt.files;
if (files.length > 0) {
handleFile(files[0]);
}
}
function handleFileInput(files) {
if (files.length > 0) {
handleFile(files[0]);
}
}
// Initial load: Don't add the layers until a file is loaded.
// This prevents trying to remove them before they exist.
map.on('load', () => {
// Initialize an empty source, but *don't* add the layers yet.
geojsonSource = { type: 'geojson', data: { type: "FeatureCollection", features: [] } };
map.addSource('geojson-data', geojsonSource);
// addLayers(); // REMOVE THIS LINE
updateFeatureDropdown(null); // This is correct, show no features initially.
if (!map.getStyle()) { // wait for map loading
return;
}
});