Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
584 lines (519 loc) · 19.6 KB
/
Copy pathApp.tsx
File metadata and controls
584 lines (519 loc) · 19.6 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
import { useEffect, useMemo, useRef, useState } from 'react';
import L from 'leaflet';
import 'leaflet.vectorgrid';
// Tipe data berikut dipakai supaya komponen React tetap jelas:
// - FeatureProperties: properti mentah dari tile PBF
// - HoverInfo: isi panel hover saat feature disentuh
// - RouteSummary: ringkasan hasil routing OSRM
// - RouteInputs: form koordinat asal/tujuan
// - RouteStatus: status proses routing
type FeatureProperties = Record<string, unknown>;
type HoverInfo = {
layerName: string;
title: string;
properties: FeatureProperties;
};
type RouteSummary = {
distance: number;
duration: number;
coordinates: [number, number][];
};
type RouteInputs = {
startLng: string;
startLat: string;
endLng: string;
endLat: string;
};
type RouteStatus = {
kind: 'idle' | 'loading' | 'success' | 'error';
message: string;
};
// Endpoint tile vector PBF yang dibaca oleh Leaflet.VectorGrid.
const TILE_URL = 'https://peta.smartsystem.id/data/pmtiles/{z}/{x}/{y}.pbf';
// Definisi rute mengikuti PANDUAN_DEVELOPER_CLIENT.md:
// - endpoint OSRM menerima urutan longitude, latitude
// - overview=full dan geometries=geojson dipakai agar hasil rute bisa langsung digambar di Leaflet
const ROUTE_URL = 'https://rute.smartsystem.id/route/v1/driving/{longitude_asal},{latitude_asal};{longitude_tujuan},{latitude_tujuan}?overview=full&geometries=geojson';
// Contoh request final yang dibangun dari input default di bawah:
const ROUTE_EXAMPLE_URL = 'https://rute.smartsystem.id/route/v1/driving/107.62678798649758,-6.929305772885831;107.69649673774937,-6.953538906008247?overview=full&geometries=geojson';
// Data rute bawaan aplikasi.
// Form sidebar memakai data ini, lalu loadRoute() mengirimkannya ke OSRM.
// Data rute bawaan aplikasi.
// Form sidebar memakai data ini, lalu loadRoute() mengirimkannya ke OSRM.
// Titik awal: PT LSKK
// Titik akhir: ITB Innovation Park
const initialInputs: RouteInputs = {
startLng: '107.62678798649758',
startLat: '-6.929305772885831',
endLng: '107.69649673774937',
endLat: '-6.953538906008247',
};
// State awal panel hover sebelum user mengarah ke feature.
const initialHover: HoverInfo = {
layerName: 'Belum ada feature',
title: 'Arahkan kursor ke feature pada peta',
properties: {},
};
// Status awal routing sebelum request OSRM dijalankan.
const initialRouteStatus: RouteStatus = {
kind: 'idle',
message: 'Isi koordinat lalu klik Muat rute.',
};
// Alur gambar rute di aplikasi ini:
// 1. User mengisi koordinat asal dan tujuan pada form sidebar.
// 2. loadRoute() membangun URL OSRM dari ROUTE_URL.
// 3. Response OSRM dibaca dari data.routes[0].geometry.coordinates.
// 4. Koordinat [lng, lat] diubah menjadi [lat, lng] agar cocok dengan Leaflet.
// 5. Garis rute digambar ulang ke routeLayer sebagai polyline merah tebal.
function escapeHtml(value: unknown): string {
// Helper sederhana untuk mencegah HTML mentah tampil di popup.
return String(value)
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"')
.replaceAll("'", ''');
}
function getTitle(properties: FeatureProperties): string {
// Ambil field paling informatif yang tersedia dari properti feature.
const titleKeys = ['name', 'kind', 'type', 'class', 'layer', 'category', 'admin_level'];
for (const key of titleKeys) {
const value = properties[key];
if (value !== undefined && value !== null && value !== '') {
return `${key}: ${value}`;
}
}
return 'Feature';
}
function parseRouteCoordinate(value: string): number {
// Form input bertipe string, jadi perlu dikonversi menjadi angka.
const parsed = Number(value.trim());
return Number.isFinite(parsed) ? parsed : Number.NaN;
}
function formatDistance(distance: number): string {
// Format jarak dari meter ke meter/km agar lebih mudah dibaca di UI.
if (distance >= 1000) {
return `${(distance / 1000).toFixed(2)} km`;
}
return `${Math.round(distance)} m`;
}
function formatDuration(duration: number): string {
// Format durasi dari detik ke jam/menit.
const totalMinutes = Math.round(duration / 60);
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
if (hours === 0) {
return `${minutes} menit`;
}
return `${hours} jam ${minutes} menit`;
}
function buildRouteUrl(routeInputs: RouteInputs): string {
// Susun URL final OSRM dengan urutan longitude, latitude.
return ROUTE_URL
.replace('{longitude_asal}', routeInputs.startLng.trim())
.replace('{latitude_asal}', routeInputs.startLat.trim())
.replace('{longitude_tujuan}', routeInputs.endLng.trim())
.replace('{latitude_tujuan}', routeInputs.endLat.trim());
}
function drawRoute(routeLayer: L.LayerGroup, latLngs: [number, number][]) {
// routeLayer adalah group khusus untuk semua elemen rute.
// Setiap kali rute baru digambar, semua layer lama dihapus dulu.
routeLayer.clearLayers();
// Marker asal dan tujuan tetap dipakai untuk menunjukkan titik endpoint rute.
L.circleMarker(latLngs[0], {
radius: 7,
color: '#0f6f45',
weight: 3,
fillColor: '#d1fae5',
fillOpacity: 1,
}).addTo(routeLayer);
L.circleMarker(latLngs[latLngs.length - 1], {
radius: 7,
color: '#b91c1c',
weight: 3,
fillColor: '#fee2e2',
fillOpacity: 1,
}).addTo(routeLayer);
// Lapisan bawah yang lebih tebal berfungsi sebagai halo agar garis rute lebih mudah dibaca.
L.polyline(latLngs, {
color: '#ffffff',
weight: 10,
opacity: 0.92,
lineCap: 'round',
lineJoin: 'round',
}).addTo(routeLayer);
// Garis utama: merah tebal, gaya seperti peta navigasi.
L.polyline(latLngs, {
color: '#d71920',
weight: 7,
opacity: 1,
lineCap: 'round',
lineJoin: 'round',
}).addTo(routeLayer);
}
function App() {
// Komponen utama aplikasi: menyatukan state React, Leaflet map, dan alur routing.
// Ref untuk akses langsung ke instance Leaflet dan elemen peta.
const mapContainerRef = useRef<HTMLDivElement | null>(null);
const mapRef = useRef<L.Map | null>(null);
const vectorLayerRef = useRef<L.VectorGrid | null>(null);
const routeLayerRef = useRef<L.LayerGroup | null>(null);
const hoverTooltipRef = useRef<L.Tooltip | null>(null);
// React state dipakai untuk data yang memengaruhi UI sidebar.
const [hoverInfo, setHoverInfo] = useState<HoverInfo>(initialHover);
const [mapStatus, setMapStatus] = useState('Memuat tile vector...');
const [layerNames, setLayerNames] = useState<string[]>([]);
const [routeInputs, setRouteInputs] = useState<RouteInputs>(initialInputs);
const [routeStatus, setRouteStatus] = useState<RouteStatus>(initialRouteStatus);
const [routeSummary, setRouteSummary] = useState<RouteSummary | null>(null);
const [opacity, setOpacity] = useState(90);
// Ubah objek properti menjadi array agar mudah dirender sebagai tabel.
const propertyEntries = useMemo(() => Object.entries(hoverInfo.properties), [hoverInfo.properties]);
const loadRoute = async (routeInputsValue: RouteInputs = routeInputs) => {
// STEP 1: baca input asal dan tujuan.
// startLng/startLat = titik asal
// endLng/endLat = titik tujuan
const startLng = parseRouteCoordinate(routeInputsValue.startLng);
const startLat = parseRouteCoordinate(routeInputsValue.startLat);
const endLng = parseRouteCoordinate(routeInputsValue.endLng);
const endLat = parseRouteCoordinate(routeInputsValue.endLat);
// STEP 2: validasi input angka.
if ([startLng, startLat, endLng, endLat].some((value) => Number.isNaN(value))) {
setRouteStatus({ kind: 'error', message: 'Koordinat rute tidak valid. Pastikan format angka benar.' });
return;
}
// STEP 3: beri feedback ke UI bahwa request routing sedang berjalan.
setRouteStatus({ kind: 'loading', message: 'Menghitung rute via OSRM...' });
setRouteSummary(null);
try {
// STEP 4: bangun URL final ke OSRM.
const url = buildRouteUrl(routeInputsValue);
// STEP 5: fetch data route dari server OSRM.
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
// STEP 6: baca JSON response.
const data = await response.json();
// Struktur data yang dipakai dari OSRM:
// data.routes[0].geometry.coordinates -> array [lng, lat]
// data.routes[0].distance -> meter
// data.routes[0].duration -> detik
const route = data.routes?.[0];
const coordinates = route?.geometry?.coordinates as [number, number][] | undefined;
if (!route || !coordinates?.length) {
throw new Error('Rute tidak ditemukan.');
}
// STEP 7: ubah format koordinat dari GeoJSON ke format Leaflet.
const latLngs = coordinates.map(([lng, lat]) => [lat, lng] as [number, number]);
const routeLayer = routeLayerRef.current;
if (routeLayer) {
// STEP 8: gambar ulang semua elemen rute ke layer khusus.
drawRoute(routeLayer, latLngs);
}
// STEP 9: simpan ringkasan hasil routing ke state.
setRouteSummary({
distance: route.distance,
duration: route.duration,
coordinates: coordinates,
});
setRouteStatus({
kind: 'success',
message: `Rute berhasil dimuat. Jarak ${formatDistance(route.distance)} dan estimasi ${formatDuration(route.duration)}.`,
});
// STEP 10: fokuskan peta ke batas rute supaya langsung terlihat.
const bounds = L.latLngBounds(latLngs);
mapRef.current?.fitBounds(bounds, { padding: [30, 30] });
} catch (error) {
setRouteStatus({
kind: 'error',
message: error instanceof Error ? error.message : 'Gagal memuat rute.',
});
}
};
useEffect(() => {
// useEffect hanya jalan sekali saat komponen dipasang.
if (!mapContainerRef.current || mapRef.current) {
return;
}
// STEP A: buat instance Leaflet map.
const map = L.map(mapContainerRef.current, {
zoomControl: true,
preferCanvas: false,
zoomSnap: 0.01,
zoomDelta: 0.25,
}).setView([-6.9175, 107.6191], 12);
// STEP B: pasang basemap OpenStreetMap sebagai basemap gambar png.
const baseLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
subdomains: 'abc',
maxZoom: 20,
attribution: '© OpenStreetMap contributors',
}).addTo(map);
// STEP C: definisikan style untuk setiap layer data vector.
const styleByLayer = {
earth: {
fill: true,
fillColor: '#dff5f0',
fillOpacity: 1,
stroke: false,
},
landcover: {
fill: true,
fillColor: '#c8ecc5',
fillOpacity: 1,
stroke: false,
},
landuse: {
fill: true,
fillColor: '#b9dfab',
fillOpacity: 1,
stroke: false,
},
water: {
fill: true,
fillColor: '#a8e8ef',
fillOpacity: 1,
stroke: false,
color: '#8ccfe0',
weight: 1,
},
boundaries: () => ({
color: '#b16be0',
weight: 1.1,
fillOpacity: 0,
opacity: 0.8,
}),
roads: (properties: FeatureProperties, zoom: number) => {
const roadClass = String(properties.class ?? properties.kind ?? '');
const isMajor = /motorway|trunk|primary/.test(roadClass);
return {
color: isMajor ? '#ff8f1f' : '#f8b94c',
weight: isMajor ? Math.max(1.3, zoom * 0.08) : 0.8,
opacity: isMajor ? 0.82 : 0.55,
};
},
buildings: {
fill: true,
fillColor: '#f4d580',
fillOpacity: 0.5,
color: '#e7be5a',
weight: 0.4,
},
places: () => ({
color: '#d43ce0',
radius: 2,
fillColor: '#d43ce0',
fillOpacity: 1,
weight: 0,
}),
pois: () => ({
color: '#f04e9b',
radius: 2.5,
fillColor: '#f04e9b',
fillOpacity: 1,
weight: 0,
}),
};
// STEP D: buat layer vector dari tile PBF.
const vectorLayer = L.vectorGrid.protobuf(TILE_URL, {
rendererFactory: L.svg.tile,
interactive: true,
maxNativeZoom: 14,
vectorTileLayerStyles: styleByLayer,
}) as unknown as L.VectorGrid;
// STEP E: sediakan group khusus untuk elemen route.
const routeLayer = L.layerGroup().addTo(map);
// STEP F: tooltip hover yang dipakai ulang, bukan dibuat baru setiap event.
const hoverTooltip = L.tooltip({
permanent: false,
sticky: true,
direction: 'top',
opacity: 0.96,
className: 'feature-tooltip',
});
// STEP G: saat hover, baca properti feature lalu tampilkan ke sidebar dan tooltip.
const updateHoverState = (event: any) => {
const properties = (event.layer?.properties || event.layer?.feature?.properties || event.properties || {}) as FeatureProperties;
const layerName = event.layer?.name || event.layer?.layerName || event.sourceLayer || 'vector tile feature';
const title = getTitle(properties);
setHoverInfo({ layerName, title, properties });
hoverTooltip
.setLatLng(event.latlng)
.setContent(`<strong>${escapeHtml(title)}</strong><br />${escapeHtml(layerName)}`);
if (!map.hasLayer(hoverTooltip)) {
hoverTooltip.addTo(map);
}
};
vectorLayer.on('loading', () => {
// Event loading dipakai untuk memberi status di sidebar.
setMapStatus('Memuat tile vector...');
});
vectorLayer.on('load', () => {
// Setelah tile termuat, ambil daftar layer yang terdeteksi.
const names = vectorLayer.getDataLayerNames();
setLayerNames(names);
setMapStatus(names.length ? `Tile berhasil dimuat. Layer: ${names.join(', ')}` : 'Tile berhasil dimuat.');
});
vectorLayer.on('mouseover', (event: any) => {
if (!event.latlng) {
return;
}
updateHoverState(event);
});
vectorLayer.on('mousemove', (event: any) => {
if (!event.latlng) {
return;
}
updateHoverState(event);
});
vectorLayer.on('mouseout', () => {
if (map.hasLayer(hoverTooltip)) {
map.removeLayer(hoverTooltip);
}
setHoverInfo(initialHover);
if (layerNames.length) {
setMapStatus(`Tile berhasil dimuat. Layer: ${layerNames.join(', ')}`);
}
});
vectorLayer.on('click', (event: any) => {
// Klik feature menampilkan popup tabel properti.
const properties = (event.layer?.properties || event.layer?.feature?.properties || event.properties || {}) as FeatureProperties;
const content = `
<div class="popup-card">
<strong>${escapeHtml(getTitle(properties))}</strong>
<div style="margin-bottom: 10px; color: #4b5563;">${escapeHtml(event.layer?.name || event.sourceLayer || 'vector feature')}</div>
<table class="popup-table">
<tbody>
${Object.entries(properties)
.map(([key, value]) => `<tr><th>${escapeHtml(key)}</th><td>${escapeHtml(value)}</td></tr>`)
.join('')}
</tbody>
</table>
</div>
`;
L.popup({ className: 'feature-popup', maxWidth: 420 })
.setLatLng(event.latlng)
.setContent(content)
.openOn(map);
});
vectorLayer.addTo(map);
// Simpan referensi instance agar state React dapat mengontrol layer Leaflet.
routeLayerRef.current = routeLayer;
hoverTooltipRef.current = hoverTooltip;
vectorLayerRef.current = vectorLayer;
mapRef.current = map;
// STEP H: set opacity awal sesuai slider.
const syncOpacity = (value: number) => {
vectorLayer.setOpacity(value / 100);
};
syncOpacity(opacity);
map.invalidateSize();
// Muat contoh rute otomatis dari URL GeoJSON yang diberikan user.
void loadRoute(initialInputs);
return () => {
map.remove();
mapRef.current = null;
vectorLayerRef.current = null;
routeLayerRef.current = null;
hoverTooltipRef.current = null;
};
}, []);
useEffect(() => {
// Ketika slider berubah, update opacity layer vector secara langsung.
vectorLayerRef.current?.setOpacity(opacity / 100);
}, [opacity]);
const handleRouteInputChange = (field: keyof RouteInputs) => (event: React.ChangeEvent<HTMLInputElement>) => {
// Handler generik agar semua input form route bisa memakai satu fungsi.
setRouteInputs((current) => ({ ...current, [field]: event.target.value }));
};
const resetRoute = () => {
// Hapus rute dan kembalikan status ke kondisi awal.
routeLayerRef.current?.clearLayers();
setRouteSummary(null);
setRouteStatus(initialRouteStatus);
};
return (
<main className="app-shell">
{/* Sidebar kiri: semua kontrol, info hover, dan dokumentasi rute. */}
<aside className="sidebar">
<div>
<p className="eyebrow">Leaflet PMTiles Viewer</p>
<h1>Peta berbasis ReactJS</h1>
<p className="intro">
Menggunakan basemap OpenStreetMap, vector tile dari
<span className="mono">https://peta.smartsystem.id/data/pmtiles/{'{z}'}/{'{x}'}/{'{y}'}.pbf</span>
, hover untuk lihat info feature, dan routing OSRM sesuai panduan developer.
</p>
</div>
<div className="panel">
{/* Slider opacity mengubah nilai state `opacity`, lalu disinkronkan ke layer vector. */}
<label htmlFor="opacityRange">Opacity layer</label>
<input
id="opacityRange"
type="range"
min="0"
max="100"
value={opacity}
onChange={(event) => setOpacity(Number(event.target.value))}
/>
<div className="panel-row">
<span>0%</span>
<span>{opacity}%</span>
<span>100%</span>
</div>
</div>
<div className="panel">
{/* Panel ini menampilkan status map dan tabel properti feature saat hover. */}
<h2>Info hover</h2>
<p>{mapStatus}</p>
<div className={`feature-info ${propertyEntries.length ? '' : 'empty'}`}>
<span className="title">{hoverInfo.title}</span>
<div style={{ marginBottom: 10, color: '#5f6f68' }}>Layer: {hoverInfo.layerName}</div>
{propertyEntries.length ? (
<table>
<tbody>
{propertyEntries.map(([key, value]) => (
<tr key={key}>
<th>{key}</th>
<td>{String(value ?? '')}</td>
</tr>
))}
</tbody>
</table>
) : (
<div>Belum ada feature yang dipilih.</div>
)}
</div>
</div>
<div className="panel">
{/* Layer vector yang terdeteksi dari tile PBF ditampilkan sebagai list. */}
<h2>Layer terdeteksi</h2>
<ul className="layer-list">
{layerNames.length ? (
layerNames.map((name) => <li key={name}>{name}</li>)
) : (
<li>Memuat...</li>
)}
</ul>
</div>
<div className="panel">
<div className={`route-status ${routeStatus.kind}`}>
{routeStatus.message}
</div>
{routeSummary ? (
<div className="route-summary">
<div><strong>Jarak:</strong> {formatDistance(routeSummary.distance)}</div>
<div><strong>Durasi:</strong> {formatDuration(routeSummary.duration)}</div>
<div><strong>Titik:</strong> {routeSummary.coordinates.length}</div>
</div>
) : null}
</div>
</aside>
{/* Area kanan: container peta Leaflet. */}
<section className="map-wrap">
<div ref={mapContainerRef} id="map" />
</section>
</main>
);
}
export default App;