Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
1628 lines (1442 loc) · 64.8 KB
/
Copy pathapp.js
File metadata and controls
1628 lines (1442 loc) · 64.8 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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ============================================================
// BLOCKCHAIN SURVEYOR — Sovereign P2P Geodetic Surveyor & Cadastre
// app.js — Full Feature Implementation
// ============================================================
// ── Global State ─────────────────────────────────────────────
const state = {
wallet: { connected: false, address: null, nsurvey: 0 },
rover: { connected: false, satellites: 0, rmsError: 0, ageOfCorrections: 0, fixType: 'No Fix' },
ntrip: { streaming: false, mountpoint: null, packets: 0 },
sms: { verified: false, phone: null },
map: { instance: null, topoLayer: null, satelliteLayer: null },
boundary: { points: [], markers: [], polygon: null, area: 0, perimeter: 0 },
neighbor: { active: false, polygon: null },
cadastral: { lguApproved: false, surveyorVerified: false, neighborsSigned: 0, totalNeighbors: 0 },
subdivision: { active: false },
workflow: { step: 0 },
myLots: [],
intervals: { gnss: null, ntrip: null },
};
// ── Constants ─────────────────────────────────────────────────
const MANILA = [14.5995, 120.9842];
const DEFAULT_ZOOM = 18;
// ── Cadastre Math Engine ──────────────────────────────────────
const latRefDefault = 14.5995;
const lonRefDefault = 120.9842;
function projectToMeters(lat, lng, lat0 = latRefDefault, lon0 = lonRefDefault) {
const rLat0 = lat0 * Math.PI / 180;
const y = (lat - lat0) * 111132.95;
const x = (lng - lon0) * 111319.9 * Math.cos(rLat0);
return { x, y };
}
function checkLineIntersection(a1, a2, b1, b2) {
const pA1 = projectToMeters(a1.lat, a1.lng);
const pA2 = projectToMeters(a2.lat, a2.lng);
const pB1 = projectToMeters(b1.lat, b1.lng);
const pB2 = projectToMeters(b2.lat, b2.lng);
const det = (pA2.x - pA1.x) * (pB2.y - pB1.y) - (pA2.y - pA1.y) * (pB2.x - pB1.x);
if (det === 0) return false;
const t = ((pB1.x - pA1.x) * (pB2.y - pB1.y) - (pB1.y - pA1.y) * (pB2.x - pB1.x)) / det;
const u = ((pB1.x - pA1.x) * (pA2.y - pA1.y) - (pB1.y - pA1.y) * (pA2.x - pA1.x)) / det;
return (t >= 0 && t <= 1 && u >= 0 && u <= 1);
}
function hasSelfIntersection(coords) {
const n = coords.length;
if (n < 4) return false;
for (let i = 0; i < n; i++) {
const a1 = coords[i];
const a2 = coords[(i + 1) % n];
for (let j = i + 2; j < n; j++) {
if ((j + 1) % n === i) continue;
const b1 = coords[j];
const b2 = coords[(j + 1) % n];
if (checkLineIntersection(a1, a2, b1, b2)) {
return true;
}
}
}
return false;
}
function calculateArea(coords) {
const meters = coords.map(c => projectToMeters(c.lat, c.lng));
const n = meters.length;
let areaSum = 0;
for (let i = 0; i < n; i++) {
const nextIdx = (i + 1) % n;
areaSum += meters[i].x * meters[nextIdx].y;
areaSum -= meters[nextIdx].x * meters[i].y;
}
return Math.abs(areaSum) / 2;
}
const WORKFLOW_STEPS = [
{ id: 'connect', label: 'Connect Rover', icon: 'fa-satellite' },
{ id: 'ntrip', label: 'Stream NTRIP', icon: 'fa-tower-broadcast' },
{ id: 'plot', label: 'Plot Boundary', icon: 'fa-draw-polygon' },
{ id: 'signatures',label: 'Get Signatures', icon: 'fa-signature' },
{ id: 'notarize', label: 'Notarize', icon: 'fa-stamp' },
{ id: 'register', label: 'Register', icon: 'fa-cloud-arrow-up' },
];
// ── Bootstrap ─────────────────────────────────────────────────
document.addEventListener('DOMContentLoaded', () => {
loadMyLots();
initMap();
initWorkflowStepper();
renderSatelliteBars(0, []);
bindWallet();
bindSMS();
bindRover();
bindNTRIP();
bindTitleVectorizer();
bindMapControls();
bindBoundaryTools();
bindSubdivision();
bindNeighborConsensus();
bindNotarizations();
bindRegistration();
bindEncroachmentResolution();
bindLedgerSearch();
bindMyLots();
bindModals();
renderMyLots();
showOnboarding();
});
// ============================================================
// MAP
// ============================================================
function initMap() {
const topo = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
maxZoom: 22,
});
const satellite = L.tileLayer(
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
{ attribution: 'Tiles © Esri — Source: Esri, Maxar, Earthstar Geographics', maxZoom: 22 }
);
const map = L.map('map', { center: MANILA, zoom: DEFAULT_ZOOM, layers: [topo], zoomControl: true });
map.on('click', (e) => { if (!state.subdivision.active) addBoundaryPoint(e.latlng); });
state.map.instance = map;
state.map.topoLayer = topo;
state.map.satelliteLayer = satellite;
// Layer toggle
document.getElementById('btn-layer-topo').addEventListener('click', () => {
map.removeLayer(satellite); map.addLayer(topo);
document.getElementById('btn-layer-topo').classList.add('active');
document.getElementById('btn-layer-satellite').classList.remove('active');
});
document.getElementById('btn-layer-satellite').addEventListener('click', () => {
map.removeLayer(topo); map.addLayer(satellite);
document.getElementById('btn-layer-satellite').classList.add('active');
document.getElementById('btn-layer-topo').classList.remove('active');
});
}
// ============================================================
// WORKFLOW STEPPER
// ============================================================
function initWorkflowStepper() {
const wrap = document.getElementById('workflow-stepper');
wrap.innerHTML = WORKFLOW_STEPS.map((s, i) => `
<div class="step-item" id="step-${s.id}">
<div class="step-circle"><i class="fa-solid ${s.icon}"></i></div>
<span class="step-label">${s.label}</span>
</div>
${i < WORKFLOW_STEPS.length - 1 ? `<div class="step-connector" id="conn-${i}"></div>` : ''}
`).join('');
setStep(0);
}
function setStep(n) {
if (n < state.workflow.step) return; // never go backward
state.workflow.step = n;
WORKFLOW_STEPS.forEach((s, i) => {
const el = document.getElementById(`step-${s.id}`);
if (!el) return;
el.classList.toggle('completed', i < n);
el.classList.toggle('active', i === n);
const conn = document.getElementById(`conn-${i}`);
if (conn) conn.classList.toggle('filled', i < n);
});
}
function advanceTo(n) { if (n > state.workflow.step) setStep(n); }
// ============================================================
// GNSS LIVE DISPLAY
// ============================================================
function renderSatelliteBars(count, signals) {
const el = document.getElementById('gnss-sat-bars');
if (!el) return;
const total = 12;
let html = '<div class="sat-grid">';
for (let i = 0; i < total; i++) {
const active = i < count;
const snr = active ? (signals[i] ?? Math.floor(Math.random() * 35 + 20)) : 0;
const pct = active ? Math.min(100, (snr / 55) * 100) : 8;
const colour = snr > 40 ? '#10b981' : snr > 25 ? '#f59e0b' : '#ef4444';
html += `<div class="sat-col">
<div class="sat-bar-bg">
<div class="sat-bar-fill" style="height:${pct}%;background:${active ? colour : 'rgba(255,255,255,0.08)'}"></div>
</div>
<span class="sat-bar-snr">${active ? snr : '·'}</span>
</div>`;
}
html += '</div>';
el.innerHTML = html;
}
function startGNSS() {
if (state.intervals.gnss) clearInterval(state.intervals.gnss);
state.rover.ageOfCorrections = 0;
state.intervals.gnss = setInterval(() => {
// Satellite ramp-up simulation
const target = 14 + Math.floor(Math.random() * 8);
if (state.rover.satellites < target) state.rover.satellites++;
else if (Math.random() < 0.1) state.rover.satellites = Math.max(4, state.rover.satellites - 1);
// RTK precision depends on NTRIP
const sats = state.rover.satellites;
const rtk = state.ntrip.streaming;
state.rover.rmsError = rtk
? Math.max(0.006, 0.012 + (Math.random() - 0.5) * 0.004)
: Math.max(0.1, 0.8 - sats * 0.04 + (Math.random() - 0.5) * 0.05);
state.rover.fixType = rtk && sats > 8 ? 'RTK Fixed'
: rtk ? 'RTK Float'
: sats > 6 ? 'DGNSS'
: 'Autonomous';
state.rover.ageOfCorrections += rtk ? 1 : 0;
// DOM updates
setText('rover-sats-count', `${sats} / 32`);
const rmsEl = document.getElementById('rover-rms-error');
if (rmsEl) {
rmsEl.textContent = `${state.rover.rmsError.toFixed(4)} m`;
rmsEl.style.color = state.rover.rmsError < 0.02 ? '#10b981' : state.rover.rmsError < 0.1 ? '#f59e0b' : '#ef4444';
}
const precEl = document.getElementById('rtk-precision-status');
if (precEl) {
precEl.textContent = state.rover.fixType;
precEl.className = `status-pill ${state.rover.fixType === 'RTK Fixed' ? 'connected' : state.rover.fixType.includes('Float') ? 'partial' : 'disconnected'}`;
}
const aocEl = document.getElementById('gnss-age-of-corrections');
if (aocEl && rtk) {
aocEl.textContent = `${state.rover.ageOfCorrections}s`;
aocEl.style.color = state.rover.ageOfCorrections > 10 ? '#f59e0b' : '#10b981';
}
// Satellite bars
const sigs = Array.from({ length: sats }, () => Math.floor(Math.random() * 35 + 20));
renderSatelliteBars(sats, sigs);
// Auto-advance workflow
if (sats >= 4 && state.workflow.step === 0) advanceTo(1);
}, 1200);
}
// ============================================================
// WALLET & CADASTRAL SMART CONTRACT
// ============================================================
const CADASTRAL_CONTRACT_ABI = [
"constructor()",
"event LotRegistered(uint256 indexed id, string indexed spatialHash, address indexed owner, uint256 area)",
"event BoundarySigned(uint256 indexed id, address indexed neighbor)",
"event SurveyorVerified(uint256 indexed id, address indexed surveyor)",
"event LGUApproved(uint256 indexed id, address indexed LGU)",
"event LotSubdivided(uint256 indexed parentId, uint256[] childIds)",
"event RoleAuthorizationChanged(string role, address indexed account, bool status)",
"function admin() view returns (address)",
"function lotCount() view returns (uint256)",
"function lots(uint256) view returns (uint256 id, string spatialHash, string coordsJson, uint256 area, address owner, address surveyor, address LGU, uint256 timestamp, bool isVerified)",
"function spatialHashExists(string) view returns (bool)",
"function neighborApprovals(uint256, address) view returns (bool)",
"function requiredNeighbors(uint256, uint256) view returns (address)",
"function authorizedSurveyors(address) view returns (bool)",
"function authorizedLGUs(address) view returns (bool)",
"function setSurveyorStatus(address _surveyor, bool _status)",
"function setLGUStatus(address _lgu, bool _status)",
"function registerLot(string _spatialHash, string _coordsJson, uint256 _area, address[] _neighbors) returns (uint256)",
"function signBoundary(uint256 _id)",
"function verifySurveyor(uint256 _id)",
"function approveLGU(uint256 _id)",
"function subdivideLot(uint256 _parentId, string[] _childHashes, string[] _childCoordsJsons, uint256[] _childAreas, address[] _siblings) returns (uint256[])",
"function getRequiredNeighbors(uint256 _id) view returns (address[])",
"function isArchived(uint256) view returns (bool)",
"function lotChildren(uint256, uint256) view returns (uint256)"
];
async function checkRoleAuthorizations() {
const address = document.getElementById('deployed-contract-address').value.trim();
if (!address || !ethers.isAddress(address) || !window.ethereum || !state.wallet.address) {
document.getElementById('badge-surveyor-auth')?.remove();
document.getElementById('badge-lgu-auth')?.remove();
return;
}
try {
const provider = new ethers.BrowserProvider(window.ethereum);
const contract = new ethers.Contract(address, CADASTRAL_CONTRACT_ABI, provider);
const isSurveyor = await contract.authorizedSurveyors(state.wallet.address);
const isLGU = await contract.authorizedLGUs(state.wallet.address);
document.getElementById('badge-surveyor-auth')?.remove();
document.getElementById('badge-lgu-auth')?.remove();
const displaySpan = document.getElementById('wallet-address-display');
if (isSurveyor) {
displaySpan.insertAdjacentHTML('afterend', ' <span id="badge-surveyor-auth" style="font-size:0.55rem;background:#10b981;color:white;padding:2px 5px;border-radius:4px;font-weight:bold;margin-left:4px;">Surveyor</span>');
showToast('You are an authorized Geodetic Surveyor on-chain.', 'info');
}
if (isLGU) {
displaySpan.insertAdjacentHTML('afterend', ' <span id="badge-lgu-auth" style="font-size:0.55rem;background:#3b82f6;color:white;padding:2px 5px;border-radius:4px;font-weight:bold;margin-left:4px;">LGU Official</span>');
showToast('You are an authorized LGU official on-chain.', 'info');
}
} catch (err) {
console.warn('Failed checking roles:', err.message);
}
}
function bindWallet() {
document.getElementById('btn-connect-wallet').addEventListener('click', async () => {
if (!window.ethereum) { showToast('No Web3 wallet found. Please install MetaMask.', 'warning'); return; }
try {
const provider = new ethers.BrowserProvider(window.ethereum);
const accounts = await provider.send('eth_requestAccounts', []);
state.wallet.address = accounts[0];
state.wallet.connected = true;
state.wallet.nsurvey = parseFloat((Math.random() * 120 + 5).toFixed(2));
setText('wallet-address-display', `${state.wallet.address.slice(0,6)}…${state.wallet.address.slice(-4)}`);
setText('survey-balance', `${state.wallet.nsurvey} SURVEY`);
document.getElementById('survey-balance-wrap').classList.remove('hidden');
const btn = document.getElementById('btn-connect-wallet');
btn.innerHTML = '<i class="fa-solid fa-circle-check"></i> Connected';
btn.style.background = 'rgba(16,185,129,0.2)';
btn.style.borderColor = 'rgba(16,185,129,0.4)';
checkRegisterReady();
renderMyLots();
checkRoleAuthorizations();
showToast('Wallet connected!', 'success');
} catch (err) {
showToast('Connection failed: ' + err.message, 'error');
}
});
document.getElementById('btn-apply-manual-wallet').addEventListener('click', () => {
const address = document.getElementById('manual-wallet-address').value.trim();
if (!address) {
showToast('Please enter a wallet address.', 'warning');
return;
}
// Basic Ethereum address format check
if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
showToast('Invalid Ethereum address format.', 'error');
return;
}
try {
state.wallet.address = address;
state.wallet.connected = true;
if (!state.wallet.nsurvey) {
state.wallet.nsurvey = parseFloat((Math.random() * 120 + 5).toFixed(2));
}
setText('wallet-address-display', `${state.wallet.address.slice(0,6)}…${state.wallet.address.slice(-4)}`);
setText('survey-balance', `${state.wallet.nsurvey} SURVEY`);
document.getElementById('survey-balance-wrap').classList.remove('hidden');
// Change button state to indicate success
const btn = document.getElementById('btn-connect-wallet');
btn.innerHTML = '<i class="fa-solid fa-circle-check"></i> Wallet Linked';
btn.style.background = 'rgba(16,185,129,0.2)';
btn.style.borderColor = 'rgba(16,185,129,0.4)';
checkRegisterReady();
renderMyLots();
checkRoleAuthorizations();
showToast('Manual wallet connected!', 'success');
} catch (err) {
showToast('Failed to connect: ' + err.message, 'error');
}
});
const contractInput = document.getElementById('deployed-contract-address');
if (contractInput) {
contractInput.value = localStorage.getItem('cheese_surveyor_contract_address') || '';
contractInput.addEventListener('input', () => {
localStorage.setItem('cheese_surveyor_contract_address', contractInput.value.trim());
checkRoleAuthorizations();
});
// Check if already connected
if (state.wallet.connected) {
checkRoleAuthorizations();
}
}
}
// ============================================================
// SMS BRIDGE
// ============================================================
function bindSMS() {
document.getElementById('btn-request-sms').addEventListener('click', () => {
const ph = document.getElementById('sms-phone-number').value.trim();
if (!ph) { showToast('Enter a mobile number first.', 'warning'); return; }
state.sms.phone = ph;
document.getElementById('sms-otp-container').classList.remove('hidden');
showToast(`OTP sent to ${ph}`, 'info');
});
document.getElementById('btn-verify-otp').addEventListener('click', () => {
const otp = document.getElementById('sms-otp-input').value.trim();
if (!/^\d{6}$/.test(otp)) { showToast('Enter a valid 6-digit OTP.', 'warning'); return; }
state.sms.verified = true;
const btn = document.getElementById('btn-verify-otp');
btn.innerHTML = '<i class="fa-solid fa-check"></i> Verified';
btn.style.background = 'rgba(16,185,129,0.25)';
showToast('Mobile number verified!', 'success');
});
}
// ============================================================
// ROVER
// ============================================================
function bindRover() {
document.getElementById('btn-connect-rover').addEventListener('click', () => {
if (state.rover.connected) {
clearInterval(state.intervals.gnss);
state.rover.connected = false;
state.rover.satellites = 0;
state.rover.fixType = 'No Fix';
setText('rover-link-status', 'Disconnected');
setClass('rover-link-status', 'status-pill disconnected');
document.getElementById('btn-connect-rover').innerHTML = '<i class="fa-solid fa-bluetooth"></i> Connect External Rover';
renderSatelliteBars(0, []);
setText('gnss-age-of-corrections', '—');
return;
}
state.rover.connected = true;
setText('rover-link-status', 'Connected');
setClass('rover-link-status', 'status-pill connected');
const btn = document.getElementById('btn-connect-rover');
btn.innerHTML = '<i class="fa-solid fa-bluetooth"></i> Disconnect Rover';
btn.style.borderColor = 'rgba(16,185,129,0.4)';
showToast('GNSS rover connected via Bluetooth!', 'success');
startGNSS();
});
}
// ============================================================
// NTRIP
// ============================================================
function bindNTRIP() {
const presets = {
'government': { url: 'agn.namria.gov.ph:2101', mount: 'MANILA_RTCM3' },
'depin-manila': { url: 'depin.blockchainsurveyor.io:2101', mount: 'CHZ_RTCM3' },
'depin-cebu': { url: 'cebu.blockchainsurveyor.io:2101', mount: 'CHZ_CEBU3' },
'depin-davao': { url: 'davao.blockchainsurveyor.io:2101', mount: 'CHZ_DAVAO3' },
};
document.getElementById('ntrip-caster-select').addEventListener('change', (e) => {
const p = presets[e.target.value] || presets.government;
document.getElementById('ntrip-caster').value = p.url;
document.getElementById('ntrip-mountpoint').value = p.mount;
});
document.getElementById('btn-connect-ntrip').addEventListener('click', () => {
if (state.ntrip.streaming) {
stopNTRIP(); return;
}
if (!state.rover.connected) { showToast('Connect a GNSS rover first.', 'warning'); return; }
startNTRIP();
});
}
function startNTRIP() {
state.ntrip.streaming = true;
state.ntrip.packets = 0;
document.getElementById('depin-node-rewards').classList.remove('hidden');
document.getElementById('btn-connect-ntrip').innerHTML = '<i class="fa-solid fa-plug-circle-xmark"></i> Stop NTRIP Stream';
advanceTo(2);
showToast('NTRIP correction stream active!', 'success');
state.intervals.ntrip = setInterval(() => {
state.ntrip.packets++;
state.wallet.nsurvey = parseFloat((state.wallet.nsurvey + 0.001).toFixed(4));
setText('survey-balance', `${state.wallet.nsurvey} SURVEY`);
const ctr = document.getElementById('ntrip-packet-counter');
if (ctr) ctr.textContent = `${state.ntrip.packets} pkts`;
}, 3000);
}
function stopNTRIP() {
state.ntrip.streaming = false;
clearInterval(state.intervals.ntrip);
document.getElementById('depin-node-rewards').classList.add('hidden');
document.getElementById('btn-connect-ntrip').innerHTML = '<i class="fa-solid fa-plug"></i> Stream NAMRIA Corrections';
showToast('NTRIP stream stopped.', 'info');
}
// ============================================================
// BOUNDARY PLOTTING
// ============================================================
function addBoundaryPoint(latlng) {
const tempPoints = [...state.boundary.points, latlng];
if (tempPoints.length >= 4 && hasSelfIntersection(tempPoints)) {
showToast('Boundary self-intersection detected! Node rejected.', 'error');
return;
}
const idx = state.boundary.points.length;
const icon = L.divIcon({
className: '',
html: `<div class="b-node" data-i="${idx}"><span>${idx + 1}</span></div>`,
iconSize: [26, 26], iconAnchor: [13, 13],
});
const marker = L.marker(latlng, { icon, draggable: true })
.addTo(state.map.instance);
marker.on('dragend', () => {
const i = state.boundary.markers.indexOf(marker);
if (i >= 0) {
const oldLatLng = state.boundary.points[i];
state.boundary.points[i] = marker.getLatLng();
if (state.boundary.points.length >= 4 && hasSelfIntersection(state.boundary.points)) {
showToast('Invalid drag: boundary self-intersection! Reverted.', 'error');
state.boundary.points[i] = oldLatLng;
marker.setLatLng(oldLatLng);
}
refresh();
}
});
marker.on('contextmenu', () => {
const i = state.boundary.markers.indexOf(marker);
if (i < 0) return;
state.map.instance.removeLayer(marker);
state.boundary.points.splice(i, 1);
state.boundary.markers.splice(i, 1);
// Re-index marker labels
state.boundary.markers.forEach((m, j) => {
m.setIcon(L.divIcon({
className: '',
html: `<div class="b-node"><span>${j + 1}</span></div>`,
iconSize: [26, 26], iconAnchor: [13, 13],
}));
});
refresh();
});
marker.on('click', () => {
if (state.subdivision && state.subdivision.active) {
handleSubdivisionNodeClick(marker);
}
});
state.boundary.points.push(latlng);
state.boundary.markers.push(marker);
refresh();
if (state.boundary.points.length >= 3) advanceTo(3);
}
function refresh() {
drawPolygon();
updateCoordsLog();
calcStats();
checkEncroachment();
}
function drawPolygon() {
const map = state.map.instance;
if (state.boundary.polygon) map.removeLayer(state.boundary.polygon);
const pts = state.boundary.points;
if (pts.length < 2) { state.boundary.polygon = null; return; }
state.boundary.polygon = L.polygon(pts, {
color: '#F59E0B',
fillColor: 'rgba(245,158,11,0.12)',
weight: 2.5,
dashArray: pts.length < 3 ? '6 4' : null,
}).addTo(map);
}
function undoPoint() {
if (!state.boundary.points.length) return;
state.map.instance.removeLayer(state.boundary.markers.pop());
state.boundary.points.pop();
refresh();
}
function clearBoundary() {
state.boundary.markers.forEach(m => state.map.instance.removeLayer(m));
state.boundary.markers = [];
state.boundary.points = [];
if (state.boundary.polygon) { state.map.instance.removeLayer(state.boundary.polygon); state.boundary.polygon = null; }
updateCoordsLog();
calcStats();
document.getElementById('encroachment-warning').classList.add('hidden');
}
function updateCoordsLog() {
const el = document.getElementById('coordinates-log-list');
const pts = state.boundary.points;
if (!pts.length) {
el.innerHTML = '<span class="text-muted" style="text-align:center;margin-top:2rem;display:block;">No boundary points added yet.</span>';
return;
}
el.innerHTML = pts.map((p, i) => `
<div style="display:flex;justify-content:space-between;padding:2px 0;border-bottom:1px solid rgba(255,255,255,0.05);gap:0.5rem;">
<span style="color:var(--cheese-gold);font-weight:700;flex-shrink:0;">P${i + 1}</span>
<span style="font-size:0.65rem;">${p.lat.toFixed(8)}°N</span>
<span style="font-size:0.65rem;">${p.lng.toFixed(8)}°E</span>
</div>`).join('');
}
function calcStats() {
const pts = state.boundary.points;
if (pts.length < 3) {
setText('metric-area', '0.00');
setText('metric-perimeter', '0.00');
state.boundary.area = 0;
state.boundary.perimeter = 0;
document.getElementById('btn-subdivide-mode').disabled = true;
checkRegisterReady();
return;
}
// Shoelace for area (spherical approximation)
const R = 6371000;
const rad = d => d * Math.PI / 180;
let area = 0, perim = 0;
for (let i = 0; i < pts.length; i++) {
const j = (i + 1) % pts.length;
const la1 = rad(pts[i].lat), la2 = rad(pts[j].lat);
const lo1 = rad(pts[i].lng), lo2 = rad(pts[j].lng);
area += (lo2 - lo1) * (2 + Math.sin(la1) + Math.sin(la2));
const dLa = la2 - la1, dLo = lo2 - lo1;
const a = Math.sin(dLa / 2) ** 2 + Math.cos(la1) * Math.cos(la2) * Math.sin(dLo / 2) ** 2;
perim += 2 * R * Math.asin(Math.sqrt(Math.min(1, a)));
}
area = Math.abs(area * R * R / 2);
state.boundary.area = area;
state.boundary.perimeter = perim;
setText('metric-area', area.toFixed(2));
setText('metric-perimeter', perim.toFixed(2));
document.getElementById('btn-subdivide-mode').disabled = false;
checkRegisterReady();
}
function checkEncroachment() {
if (!state.neighbor.active || state.boundary.points.length < 3) {
document.getElementById('encroachment-warning').classList.add('hidden');
return;
}
const neighborPts = state.neighbor.polygon.getLatLngs()[0];
let intersects = false;
const myPts = state.boundary.points;
for (let i = 0; i < myPts.length; i++) {
const a1 = myPts[i];
const a2 = myPts[(i + 1) % myPts.length];
for (let j = 0; j < neighborPts.length; j++) {
const b1 = neighborPts[j];
const b2 = neighborPts[(j + 1) % neighborPts.length];
if (checkLineIntersection(a1, a2, b1, b2)) {
intersects = true;
break;
}
}
if (intersects) break;
}
document.getElementById('encroachment-warning').classList.toggle('hidden', !intersects);
}
// ============================================================
// MAP CONTROLS
// ============================================================
function bindMapControls() {
document.getElementById('btn-toggle-grid').addEventListener('click', () => {
showToast('Grid snap toggled.', 'info');
});
document.getElementById('btn-clear-plot').addEventListener('click', () => {
if (!state.boundary.points.length) return;
if (!confirm('Clear all boundary points?')) return;
clearBoundary();
});
document.getElementById('btn-undo-point').addEventListener('click', undoPoint);
document.getElementById('btn-simulate-rover-track').addEventListener('click', () => {
if (!state.rover.connected) { showToast('Connect a GNSS rover first.', 'warning'); return; }
simulateRoverWalk();
});
document.getElementById('btn-mock-overlap').addEventListener('click', toggleNeighborLot);
}
function simulateRoverWalk() {
clearBoundary();
const c = state.map.instance.getCenter();
const offsets = [
[0.00022, 0.00004],
[0.00020, 0.00030],
[0.00002, 0.00032],
[-0.00003, 0.00025],
[0.00001, 0.00002],
];
let i = 0;
const iv = setInterval(() => {
if (i >= offsets.length) {
clearInterval(iv);
showToast('RTK rover walk complete! Boundary plotted.', 'success');
advanceTo(3);
return;
}
addBoundaryPoint(L.latLng(c.lat + offsets[i][0], c.lng + offsets[i][1]));
i++;
}, 500);
}
function toggleNeighborLot() {
if (state.neighbor.active) {
state.map.instance.removeLayer(state.neighbor.polygon);
state.neighbor.active = false;
state.neighbor.polygon = null;
showToast('Neighbor lot removed.', 'info');
return;
}
const c = state.map.instance.getCenter();
state.neighbor.polygon = L.polygon([
[c.lat + 0.00015, c.lng + 0.00016],
[c.lat + 0.00016, c.lng + 0.00040],
[c.lat - 0.00010, c.lng + 0.00038],
[c.lat - 0.00009, c.lng + 0.00014],
], {
color: '#3B82F6',
fillColor: 'rgba(59,130,246,0.10)',
weight: 2,
dashArray: '8 4',
}).bindTooltip('<b>Neighbor Lot (Registered)</b><br>Lot No. NCR-2024-0042', { permanent: false })
.addTo(state.map.instance);
state.neighbor.active = true;
checkEncroachment();
showToast('Neighbor lot loaded. Watch for encroachment.', 'info');
}
// ============================================================
// AI TITLE VECTORIZER
// ============================================================
function bindTitleVectorizer() {
const zone = document.getElementById('title-file-uploader');
const input = Object.assign(document.createElement('input'), { type: 'file', accept: 'image/*,.pdf', style: 'display:none' });
document.body.appendChild(input);
zone.addEventListener('click', () => input.click());
zone.addEventListener('dragover', e => { e.preventDefault(); zone.style.borderColor = 'var(--cheese-gold)'; });
zone.addEventListener('dragleave', () => { zone.style.borderColor = ''; });
zone.addEventListener('drop', e => {
e.preventDefault();
zone.style.borderColor = '';
if (e.dataTransfer.files[0]) processTitleFile(e.dataTransfer.files[0]);
});
input.addEventListener('change', () => { if (input.files[0]) processTitleFile(input.files[0]); });
}
async function processTitleFile(file) {
const out = document.getElementById('title-vectorizer-output');
out.classList.remove('hidden');
out.innerHTML = `
<div class="vectorizer-loading">
<i class="fa-solid fa-wand-magic-sparkles fa-spin" style="color:var(--cheese-gold);font-size:1.25rem;"></i>
<span style="font-size:0.8rem;color:var(--text-muted);">Scanning title document…</span>
</div>`;
await sleep(2200);
// Mock metes-and-bounds extracted from the title
const parsed = {
lot: 'Lot 14-B',
block: 'Block 3',
plan: 'Psd-00-099841',
area: '342.00',
points: [
{ id: 'P1', lat: 14.59940, lng: 120.98410 },
{ id: 'P2', lat: 14.59962, lng: 120.98444 },
{ id: 'P3', lat: 14.59950, lng: 120.98470 },
{ id: 'P4', lat: 14.59924, lng: 120.98452 },
],
};
out.innerHTML = `
<div class="vectorizer-result">
<div class="v-result-head">
<i class="fa-solid fa-check-circle" style="color:var(--green-accent);"></i>
<strong>Title parsed — ${parsed.points.length} boundary nodes</strong>
</div>
<div class="v-meta">
<span>${parsed.lot} / ${parsed.block}</span>
<span>Plan: ${parsed.plan}</span>
<span>Area: ${parsed.area} sqm</span>
</div>
<div class="v-coords">
${parsed.points.map(p => `
<div class="v-coord-row">
<span style="color:var(--cheese-gold);font-weight:700;">${p.id}</span>
<span>${p.lat.toFixed(6)}°N</span>
<span>${p.lng.toFixed(6)}°E</span>
</div>`).join('')}
</div>
<button id="btn-auto-populate" class="btn-primary" style="width:100%;margin-top:0.6rem;font-size:0.75rem;">
<i class="fa-solid fa-map-pin"></i> Populate on Map
</button>
</div>`;
document.getElementById('btn-auto-populate').addEventListener('click', () => {
clearBoundary();
parsed.points.forEach(p => addBoundaryPoint(L.latLng(p.lat, p.lng)));
state.map.instance.setView([parsed.points[0].lat, parsed.points[0].lng], 19);
showToast('Boundary auto-populated from title!', 'success');
});
showToast('Title document analysed!', 'success');
}
// ============================================================
// BOUNDARY TOOLS — Export & Certificate
// ============================================================
function bindBoundaryTools() {
document.getElementById('btn-export-geojson').addEventListener('click', exportGeoJSON);
document.getElementById('btn-export-kml').addEventListener('click', exportKML);
document.getElementById('btn-download-certificate').addEventListener('click', downloadCertificate);
}
function exportGeoJSON() {
const pts = state.boundary.points;
if (pts.length < 3) { showToast('Plot at least 3 boundary points first.', 'warning'); return; }
const gj = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
parcelId: `CHZ-${Date.now()}`,
area_sqm: state.boundary.area.toFixed(2),
perimeter_m: state.boundary.perimeter.toFixed(2),
owner: state.wallet.address || 'Unknown',
timestamp: new Date().toISOString(),
},
geometry: {
type: 'Polygon',
coordinates: [[...pts.map(p => [p.lng, p.lat]), [pts[0].lng, pts[0].lat]]],
},
}],
};
triggerDownload(`blockchain-surveyor-${Date.now()}.geojson`, JSON.stringify(gj, null, 2), 'application/geo+json');
showToast('GeoJSON exported!', 'success');
}
function exportKML() {
const pts = state.boundary.points;
if (pts.length < 3) { showToast('Plot at least 3 boundary points first.', 'warning'); return; }
const coords = [...pts, pts[0]].map(p => `${p.lng},${p.lat},0`).join('\n ');
const kml = `<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Blockchain Surveyor Lot Export</name>
<Placemark>
<name>Parcel CHZ-${Date.now()}</name>
<description>Area: ${state.boundary.area.toFixed(2)} sqm | Perimeter: ${state.boundary.perimeter.toFixed(2)} m</description>
<Style>
<LineStyle><color>ff0bf5f5</color><width>2</width></LineStyle>
<PolyStyle><color>330bf5f5</color></PolyStyle>
</Style>
<Polygon>
<outerBoundaryIs><LinearRing><coordinates>
${coords}
</coordinates></LinearRing></outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>`;
triggerDownload(`blockchain-surveyor-${Date.now()}.kml`, kml, 'application/vnd.google-earth.kml+xml');
showToast('KML exported!', 'success');
}
async function downloadCertificate() {
const pts = state.boundary.points;
if (pts.length < 3) { showToast('Plot at least 3 boundary points first.', 'warning'); return; }
if (!window.jspdf) { showToast('PDF library still loading — try again in a moment.', 'warning'); return; }
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'portrait', unit: 'mm', format: 'a4' });
const parcelId = `CHZ-LOT-${Date.now().toString(36).toUpperCase()}`;
const now = new Date().toLocaleString('en-PH');
const W = 210;
// Background
doc.setFillColor(10, 10, 20);
doc.rect(0, 0, W, 297, 'F');
// Gold header bar
doc.setFillColor(245, 158, 11);
doc.rect(0, 0, W, 18, 'F');
doc.setFont('helvetica', 'bold');
doc.setFontSize(12);
doc.setTextColor(10, 10, 20);
doc.text('BLOCKCHAIN SURVEYOR — SOVEREIGN CADASTRAL CERTIFICATE', W / 2, 12, { align: 'center' });
// Title
doc.setTextColor(245, 158, 11);
doc.setFontSize(18);
doc.text('Certificate of Cadastral Registration', W / 2, 32, { align: 'center' });
doc.setDrawColor(245, 158, 11);
doc.setLineWidth(0.5);
doc.line(20, 38, W - 20, 38);
// Meta
doc.setFontSize(10);
doc.setFont('helvetica', 'normal');
doc.setTextColor(200, 200, 200);
const meta = [
['Parcel ID', parcelId],
['Date Issued', now],
['Owner (Wallet)', state.wallet.address || 'Not Connected'],
['Area', `${state.boundary.area.toFixed(4)} sqm`],
['Perimeter', `${state.boundary.perimeter.toFixed(4)} m`],
['Boundary Nodes', `${pts.length}`],
['RTK Fix Type', state.rover.fixType],
['Satellites Tracked', `${state.rover.satellites}`],
['NTRIP Caster', document.getElementById('ntrip-caster')?.value || 'N/A'],
['LGU Approval', state.cadastral.lguApproved ? 'Approved' : 'Pending'],
['Surveyor Stamp', state.cadastral.surveyorVerified ? 'Verified' : 'Pending'],
['Neighbors Signed', `${state.cadastral.neighborsSigned} / ${state.cadastral.totalNeighbors}`],
];
let y = 48;
meta.forEach(([label, val]) => {
doc.setTextColor(120, 120, 140); doc.text(label + ':', 22, y);
doc.setTextColor(240, 240, 240); doc.text(String(val), 80, y);
y += 7;
});
doc.line(20, y + 2, W - 20, y + 2);
y += 10;
// Coordinates table
doc.setTextColor(245, 158, 11);
doc.setFont('helvetica', 'bold');
doc.setFontSize(10);
doc.text('Boundary Coordinates (WGS84 — ITRF2014)', 22, y);
y += 6;
doc.setFont('helvetica', 'normal');
doc.setFontSize(9);
pts.forEach((p, i) => {
doc.setTextColor(150, 150, 170);
doc.text(`P${i + 1}`, 22, y);
doc.setTextColor(220, 220, 220);
doc.text(`${p.lat.toFixed(8)}° N`, 35, y);
doc.text(`${p.lng.toFixed(8)}° E`, 95, y);
y += 6;
if (y > 270) { doc.addPage(); doc.setFillColor(10,10,20); doc.rect(0,0,W,297,'F'); y = 20; }
});
// Footer
doc.setFontSize(7.5);
doc.setTextColor(70, 70, 90);
doc.text('This certificate is generated by the Blockchain Surveyor Sovereign Cadastral System.', W / 2, 282, { align: 'center' });
doc.text('On-chain registration does not automatically constitute legal title under Philippine law (PD 1529, RA 11057).', W / 2, 287, { align: 'center' });
doc.text('Consult a licensed Geodetic Engineer and the Register of Deeds for full legal effect.', W / 2, 292, { align: 'center' });
doc.save(`Blockchain-Surveyor-Certificate-${parcelId}.pdf`);
showToast('Survey certificate downloaded!', 'success');
}
function triggerDownload(filename, content, mime) {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([content], { type: mime }));
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
}
// ============================================================
// SUBDIVISION
// ============================================================
function bindSubdivision() {
document.getElementById('btn-subdivide-mode').addEventListener('click', () => {
state.subdivision.active = true;
state.subdivision.selectedNodes = [];
document.getElementById('btn-subdivide-mode').classList.add('hidden');
document.getElementById('btn-cancel-subdivide').classList.remove('hidden');
document.getElementById('subdivision-sibling-panel').classList.remove('hidden');
showToast('Subdivision mode: click two nodes to draw split line.', 'info');
});
document.getElementById('btn-cancel-subdivide').addEventListener('click', () => {
clearSubdivision();