-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
528 lines (461 loc) · 20.1 KB
/
script.js
File metadata and controls
528 lines (461 loc) · 20.1 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
let routeData = null;
let totalLength = 0;
let routePoints = [];
let calculatedCoordinates = []; // Hesaplanan koordinatları saklamak için dizi
const canvas = document.getElementById('routeCanvas');
const ctx = canvas.getContext('2d');
// ITRF96 için sabit değerler
const ITRF96 = {
a: 6378137.0, // Yarı büyük eksen (metre)
f: 1/298.257222101, // Düzleşme
e2: 0.00669438002290, // Birinci eksantrisite karesi
e4: 0.00004472387211, // İkinci eksantrisite karesi
zone: 36 // UTM bölgesi (Türkiye için)
};
document.getElementById('routeFile').addEventListener('change', function(e) {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const content = e.target.result;
routePoints = parseRouteFile(content);
routeData = routePoints; // routeData'yı güncelle
drawRoute();
document.getElementById('routeInfo').textContent = `Toplam ${routePoints.length} nokta yüklendi.`;
};
reader.readAsText(file);
}
});
function parseRouteFile(content) {
try {
const lines = content.split('\n');
const horizontalData = [];
let isHorizontalSection = false;
totalLength = 0;
let lineCount = 0;
const MAX_LINES = 10000; // Güvenlik limiti
console.log('Dosya işleniyor...');
for (let line of lines) {
lineCount++;
if (lineCount > MAX_LINES) {
console.warn('Maksimum satır sayısı aşıldı!');
break;
}
line = line.trim();
if (line === '$YATAY') {
isHorizontalSection = true;
console.log('Yatay bölüm başladı');
continue;
}
if (line === '$SON') {
isHorizontalSection = false;
console.log('Yatay bölüm bitti');
continue;
}
if (isHorizontalSection && line) {
const parts = line.split(/\s+/).filter(part => part !== '');
if (parts.length < 4) continue;
if (parts[0] === 'PB' || parts[0].startsWith('S') || parts[0] === 'PS') {
const x = parseFloat(parts[1]);
const y = parseFloat(parts[2]);
const radius = parseFloat(parts[3]);
if (isNaN(x) || isNaN(y)) {
console.warn(`Geçersiz koordinat: ${line}`);
continue;
}
horizontalData.push({
type: parts[0],
x: x,
y: y,
radius: radius,
isCurve: !isNaN(radius) && radius !== 0 && isFinite(radius)
});
}
}
}
console.log('Toplam nokta:', horizontalData.length);
// Sadece PB ile başlasın, PS ile bitsin
let startIdx = horizontalData.findIndex(p => p.type === 'PB');
let endIdx = horizontalData.map(p => p.type).lastIndexOf('PS');
if (startIdx === -1) {
console.error('PB (başlangıç) noktası bulunamadı!');
return [];
}
if (endIdx === -1) {
console.error('PS (bitiş) noktası bulunamadı!');
return [];
}
if (endIdx <= startIdx) {
console.error('PS, PB\'den önce geliyor veya arada hiç nokta yok!');
return [];
}
const points = horizontalData.slice(startIdx, endIdx + 1);
console.log('İşlenecek nokta sayısı:', points.length);
totalLength = 0;
for (let i = 0; i < points.length - 1; i++) {
const curr = points[i];
const next = points[i + 1];
const dx = next.x - curr.x;
const dy = next.y - curr.y;
const chord = Math.sqrt(dx * dx + dy * dy);
let segmentLength = chord;
if (curr.radius !== 0 && !isNaN(curr.radius) && isFinite(curr.radius) && Math.abs(curr.radius) > 1e-3 && chord > 0) {
let ratio = chord / (2 * Math.abs(curr.radius));
if (ratio > 1) ratio = 1;
if (ratio < -1) ratio = -1;
const delta = 2 * Math.asin(ratio);
const arcLength = Math.abs(curr.radius) * delta;
if (isFinite(arcLength) && !isNaN(arcLength) && arcLength > 0) {
segmentLength = arcLength;
}
}
if (!isFinite(segmentLength) || isNaN(segmentLength) || segmentLength <= 0) {
console.warn(`Geçersiz segment uzunluğu: ${segmentLength}, düz hat kullanılıyor`);
segmentLength = chord;
}
totalLength += segmentLength;
next.cumulativeLength = totalLength;
console.log(`Nokta ${i}: (${curr.x},${curr.y}) -> (${next.x},${next.y}) | R: ${curr.radius} | Segment: ${segmentLength.toFixed(3)} | Toplam: ${totalLength.toFixed(3)}`);
}
if (points.length > 0) {
points[0].cumulativeLength = 0;
}
console.log('İşlenen nokta sayısı:', points.length, 'Toplam uzunluk:', totalLength.toFixed(3));
return points;
} catch (error) {
console.error('Güzergah dosyası işlenirken hata oluştu:', error);
return [];
}
}
function calculateCurveCoordinates(startPoint, endPoint, radius, distance, totalLength) {
if (!startPoint || !endPoint || isNaN(radius) || radius === 0 || radius === Infinity) {
return null;
}
const dx = endPoint.x - startPoint.x;
const dy = endPoint.y - startPoint.y;
const chordLength = Math.sqrt(dx * dx + dy * dy);
if (chordLength < 0.001) {
return null;
}
try {
// Kurp açısını hesapla
const delta = 2 * Math.asin(chordLength / (2 * Math.abs(radius)));
// Mesafeye göre açıyı hesapla
const ratio = distance / totalLength;
const angle = delta * ratio;
// Başlangıç açısını hesapla
const startAngle = Math.atan2(dy, dx);
// Kurp yönünü belirle
const direction = radius > 0 ? 1 : -1;
// Koordinatları hesapla
const x = startPoint.x + Math.abs(radius) * (Math.sin(startAngle + direction * angle) - Math.sin(startAngle));
const y = startPoint.y + Math.abs(radius) * (-Math.cos(startAngle + direction * angle) + Math.cos(startAngle));
if (isNaN(x) || isNaN(y)) {
return null;
}
return { x, y, angle: startAngle + direction * angle };
} catch (error) {
console.warn('Kurp hesaplama hatası:', error);
return null;
}
}
function interpolateCoordinates(startPoint, endPoint, ratio) {
// Hassas interpolasyon için
const x = startPoint.x + (endPoint.x - startPoint.x) * ratio;
const y = startPoint.y + (endPoint.y - startPoint.y) * ratio;
return { x, y };
}
function calculateCoordinates() {
if (!routeData || routeData.length === 0) {
alert('Lütfen önce güzergah dosyasını yükleyin!');
return;
}
const distance = parseFloat(document.getElementById('distance').value);
const offset = parseFloat(document.getElementById('offset').value);
const vertical = parseFloat(document.getElementById('vertical').value) || 0;
if (isNaN(distance) || isNaN(offset)) {
alert('Lütfen geçerli mesafe ve ofset değerleri girin!');
return;
}
if (distance > totalLength) {
alert(`Girilen mesafe değeri güzergah sınırları dışında! Maksimum değer: ${totalLength.toFixed(3)} m`);
return;
}
// Koordinat hesaplama mantığı
let axisX = 0, axisY = 0;
let offsetX = 0, offsetY = 0;
let found = false;
for (let i = 0; i < routeData.length - 1; i++) {
const current = routeData[i];
const next = routeData[i + 1];
if (distance >= current.cumulativeLength && distance <= next.cumulativeLength) {
const segmentLength = next.cumulativeLength - current.cumulativeLength;
const localDistance = distance - current.cumulativeLength;
try {
if (current.isCurve || next.isCurve) {
// Kurp hesaplaması
const curveResult = calculateCurveCoordinates(
current,
next,
current.radius || next.radius,
localDistance,
segmentLength
);
if (curveResult) {
axisX = curveResult.x;
axisY = curveResult.y;
// Ofset hesaplama
const perpendicularAngle = curveResult.angle + (Math.PI / 2);
offsetX = axisX + (offset * Math.cos(perpendicularAngle));
offsetY = axisY + (offset * Math.sin(perpendicularAngle));
} else {
// Kurp hesaplanamazsa düz hat olarak hesapla
const ratio = localDistance / segmentLength;
const interpolated = interpolateCoordinates(current, next, ratio);
axisX = interpolated.x;
axisY = interpolated.y;
const dx = next.x - current.x;
const dy = next.y - current.y;
const angle = Math.atan2(dy, dx);
const perpendicularAngle = angle + (Math.PI / 2);
offsetX = axisX + (offset * Math.cos(perpendicularAngle));
offsetY = axisY + (offset * Math.sin(perpendicularAngle));
}
} else {
// Düz hat hesaplaması
const ratio = localDistance / segmentLength;
const interpolated = interpolateCoordinates(current, next, ratio);
axisX = interpolated.x;
axisY = interpolated.y;
// Ofset hesaplama
const dx = next.x - current.x;
const dy = next.y - current.y;
const angle = Math.atan2(dy, dx);
const perpendicularAngle = angle + (Math.PI / 2);
offsetX = axisX + (offset * Math.cos(perpendicularAngle));
offsetY = axisY + (offset * Math.sin(perpendicularAngle));
}
found = true;
break;
} catch (error) {
console.warn('Koordinat hesaplama hatası:', error);
continue;
}
}
}
if (!found) {
alert('Koordinat hesaplanamadı!');
return;
}
// Sonuçları göster (3 ondalık basamak)
if (isNaN(axisX) || isNaN(axisY) || isNaN(offsetX) || isNaN(offsetY)) {
alert('Koordinat hesaplanırken bir hata oluştu!');
return;
}
document.getElementById('axisXCoord').textContent = axisX.toFixed(4);
document.getElementById('axisYCoord').textContent = axisY.toFixed(4);
document.getElementById('axisZCoord').textContent = vertical.toFixed(4);
document.getElementById('offsetXCoord').textContent = offsetX.toFixed(4);
document.getElementById('offsetYCoord').textContent = offsetY.toFixed(4);
document.getElementById('offsetZCoord').textContent = vertical.toFixed(4);
// Hesaplanan koordinatları listeye ekle
const coordinateData = {
distance: distance.toFixed(4),
offset: offset.toFixed(4),
vertical: vertical.toFixed(4),
axisX: axisX.toFixed(4),
axisY: axisY.toFixed(4),
offsetX: offsetX.toFixed(4),
offsetY: offsetY.toFixed(4)
};
calculatedCoordinates.push(coordinateData);
updateCoordinatesList();
}
function updateCoordinatesList() {
const listContainer = document.getElementById('coordinatesList');
listContainer.innerHTML = '';
calculatedCoordinates.forEach((coord, index) => {
const coordElement = document.createElement('div');
coordElement.className = 'coordinate-item';
coordElement.innerHTML = `
<span><b>${index + 1}:</b> Mesafe: ${coord.distance} m | Ofset: ${coord.offset} m | Z: ${coord.vertical} m |
TS: ${coord.tangentStation} m | TO: ${coord.tangentOrigin} m | TF: ${coord.tangentFinal} m |
R: ${coord.curveRadius} m | Ofset X: ${coord.offsetX} | Ofset Y: ${coord.offsetY}</span>
`;
listContainer.appendChild(coordElement);
});
}
function exportCoordinates() {
if (calculatedCoordinates.length === 0) {
alert('Dışa aktarılacak koordinat bulunamadı!');
return;
}
let content = 'Nokta Adı\tMesafe (m)\tY\tX\tZ\n';
calculatedCoordinates.forEach((coord, index) => {
content += `Nokta ${index + 1}\t${coord.distance}\t${coord.offsetY}\t${coord.offsetX}\t${coord.vertical}\n`;
});
const blob = new Blob([content], { type: 'text/plain' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'guzergah_koordinatlari.txt';
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}
function calculateDistance(point1, point2) {
const dx = point2.x - point1.x;
const dy = point2.y - point1.y;
return Math.sqrt(dx * dx + dy * dy);
}
function findNearestNeighborPath(points) {
if (points.length <= 1) return points;
const path = [points[0]]; // İlk noktayı başlangıç olarak al
const remainingPoints = [...points.slice(1)]; // Kalan noktalar
while (remainingPoints.length > 0) {
const currentPoint = path[path.length - 1];
let nearestIndex = 0;
let minDistance = Infinity;
// En yakın noktayı bul
for (let i = 0; i < remainingPoints.length; i++) {
const distance = calculateDistance(currentPoint, remainingPoints[i]);
if (distance < minDistance) {
minDistance = distance;
nearestIndex = i;
}
}
// En yakın noktayı yola ekle
path.push(remainingPoints[nearestIndex]);
remainingPoints.splice(nearestIndex, 1);
}
return path;
}
function drawRoute() {
try {
if (!routePoints || routePoints.length === 0) {
console.warn('Güzergah noktaları bulunamadı!');
return;
}
// PB ve PS noktalarını bul
const startIdx = routePoints.findIndex(p => p.type === 'PB');
const endIdx = routePoints.map(p => p.type).lastIndexOf('PS');
if (startIdx === -1 || endIdx === -1 || endIdx <= startIdx) {
console.warn('PB (proje başı) veya PS (proje sonu) noktası bulunamadı ya da sıralama hatalı!');
return;
}
const points = routePoints.slice(startIdx, endIdx + 1);
// Canvas ayarları
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Koordinat sınırları ve ölçek
const minX = Math.min(...points.map(p => p.x));
const maxX = Math.max(...points.map(p => p.x));
const minY = Math.min(...points.map(p => p.y));
const maxY = Math.max(...points.map(p => p.y));
const padding = 50;
const scaleX = (canvas.width - 2 * padding) / (maxX - minX);
const scaleY = (canvas.height - 2 * padding) / (maxY - minY);
const scale = Math.min(scaleX, scaleY) * 0.9;
ctx.strokeStyle = '#4a90e2';
ctx.lineWidth = 2;
let toplamUzunluk = 0;
for (let i = 0; i < points.length - 1; i++) {
const curr = points[i];
const next = points[i + 1];
const dx = next.x - curr.x;
const dy = next.y - curr.y;
const chord = Math.sqrt(dx * dx + dy * dy);
if (next.radius === 0 || !isFinite(next.radius)) {
// Düz hat
toplamUzunluk += chord;
const x1 = (curr.x - minX) * scale + padding;
const y1 = canvas.height - ((curr.y - minY) * scale + padding);
const x2 = (next.x - minX) * scale + padding;
const y2 = canvas.height - ((next.y - minY) * scale + padding);
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
} else {
// Kurp (yay)
let ratio = chord / (2 * Math.abs(next.radius));
if (ratio > 1) ratio = 1;
if (ratio < -1) ratio = -1;
const delta = 2 * Math.asin(ratio);
const arcLength = Math.abs(next.radius) * delta;
toplamUzunluk += arcLength;
// Kurp merkezi ve açıları
const chordMidX = (curr.x + next.x) / 2;
const chordMidY = (curr.y + next.y) / 2;
const chordAngle = Math.atan2(dy, dx);
const h = Math.abs(next.radius) * Math.cos(delta / 2);
const direction = next.radius > 0 ? 1 : -1;
const perpAngle = chordAngle + direction * Math.PI / 2;
const centerX = chordMidX + h * Math.cos(perpAngle);
const centerY = chordMidY + h * Math.sin(perpAngle);
const angle1 = Math.atan2(curr.y - centerY, curr.x - centerX);
const angle2 = Math.atan2(next.y - centerY, next.x - centerX);
let sweep = angle2 - angle1;
if (direction === 1 && sweep < 0) sweep += 2 * Math.PI;
if (direction === -1 && sweep > 0) sweep -= 2 * Math.PI;
ctx.beginPath();
ctx.arc(
(centerX - minX) * scale + padding,
canvas.height - ((centerY - minY) * scale + padding),
Math.abs(next.radius) * scale,
-angle1,
-angle1 + sweep,
direction === -1
);
ctx.stroke();
}
}
// Noktaları işaretle
points.forEach((point, index) => {
const x = (point.x - minX) * scale + padding;
const y = canvas.height - ((point.y - minY) * scale + padding);
ctx.beginPath();
ctx.arc(x, y, 4, 0, 2 * Math.PI);
ctx.fillStyle = point.type === 'PB' ? '#e74c3c' :
point.type === 'PS' ? '#2ecc71' : '#f1c40f';
ctx.fill();
ctx.strokeStyle = '#fff';
ctx.lineWidth = 1;
ctx.stroke();
ctx.font = 'bold 11px Arial';
ctx.fillStyle = '#2c3e50';
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
ctx.fillText(index + 1, x, y - 6);
});
// Bilgi paneli
const totalKm = (toplamUzunluk / 1000).toFixed(3);
const infoText = [
`Toplam Nokta: ${points.length}`,
`Uzunluk: ${totalKm} km (Hedef: 12.123 km)`,
`PB: ${points.filter(p => p.type === 'PB').length}`,
`S: ${points.filter(p => p.type.startsWith('S')).length}`,
`Kurp: ${points.filter(p => p.radius !== 0).length}`
];
ctx.fillStyle = 'rgba(255, 255, 255, 0.95)';
ctx.strokeStyle = '#e9ecef';
ctx.lineWidth = 1;
const panelWidth = 220;
const panelHeight = 110;
ctx.beginPath();
ctx.roundRect(10, 10, panelWidth, panelHeight, 8);
ctx.fill();
ctx.stroke();
ctx.font = '12px Arial';
ctx.fillStyle = '#2c3e50';
ctx.textAlign = 'left';
infoText.forEach((text, index) => {
ctx.fillText(text, 20, 30 + (index * 20));
});
console.log('Güzergah çizimi tamamlandı. Toplam uzunluk:', toplamUzunluk.toFixed(3), 'm');
} catch (error) {
console.error('Güzergah çizilirken hata oluştu:', error);
}
}