-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
338 lines (298 loc) · 11.2 KB
/
index.html
File metadata and controls
338 lines (298 loc) · 11.2 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
<!DOCTYPE html>
<html>
<head>
<title>WEBGIS DIGITIZER</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Leaflet & Plugin CSS -->
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet-draw/dist/leaflet.draw.css" />
<link rel="stylesheet" href="https://unpkg.com/leaflet-control-layers/dist/leaflet-control-layers.min.css" />
<style>
html, body, #map {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#locate-button, #start-pause-button, #export-geojson, #export-shp {
position: absolute;
right: 10px;
z-index: 1000;
background: white;
padding: 6px 12px;
margin-bottom: 5px;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
cursor: pointer;
}
#export-shp { bottom: 150px; }
#export-geojson { bottom: 110px; }
#start-pause-button { bottom: 70px; }
#locate-button { bottom: 30px; }
.info {
padding: 8px 12px;
background: white;
border-radius: 4px;
box-shadow: 0 0 5px rgba(0,0,0,0.2);
font-size: 14px;
}
.popup-table {
font-size: 14px;
border-collapse: collapse;
width: 100%;
max-width: 300px;
}
.popup-table td {
padding: 6px 8px;
vertical-align: top;
word-break: break-word;
}
.popup-table td:first-child {
font-weight: bold;
white-space: nowrap;
}
.leaflet-popup-content-wrapper {
max-width: 400px !important;
}
.leaflet-popup-content {
margin: 10px 12px;
overflow-y: auto;
max-height: 300px;
}
</style>
</head>
<body>
<div id="map"></div>
<button id="export-shp">Simpan SHP</button>
<button id="export-geojson">Simpan GeoJSON</button>
<button id="start-pause-button">Mulai Tracking</button>
<button id="locate-button">Lokasi Saya</button>
<!-- Scripts -->
<script src="https://unpkg.com/shp-write/shpwrite.js"></script>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="https://unpkg.com/leaflet-draw/dist/leaflet.draw.js"></script>
<script src="https://unpkg.com/leaflet-control-layers/dist/leaflet-control-layers.min.js"></script>
<script src="https://unpkg.com/@turf/turf/turf.min.js"></script>
<script src="https://unpkg.com/leaflet-geometryutil"></script>
<script src="https://cdn.jsdelivr.net/gh/makinacorpus/Leaflet.Snap/leaflet.snap.js"></script>
<script>
const map = L.map('map').setView([-7.8, 110.3], 13);
const osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 19,
attribution: '© OpenStreetMap'
}).addTo(map);
const googleSat = L.tileLayer('https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}', { maxZoom: 20 });
const googleStreet = L.tileLayer('https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', { maxZoom: 20 });
const esriImagery = L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', { maxZoom: 20 });
const baseMaps = {
"OpenStreetMap": osm,
"Google Street": googleStreet,
"Google Satellite": googleSat,
"Esri Imagery": esriImagery
};
L.control.layers(baseMaps).addTo(map);
const drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
const drawControl = new L.Control.Draw({
edit: {
featureGroup: drawnItems,
remove: true
},
draw: {
polygon: true,
polyline: true,
rectangle: true,
marker: true,
circle: false,
circlemarker: false
}
});
map.addControl(drawControl);
map.on('draw:drawstart', function () {
L.Draw.Polygon.prototype.options.guideLayers = [drawnItems];
L.Draw.Polygon.prototype.options.snapDistance = 15;
});
function createPopup(properties) {
let popup = `<table class='popup-table'>`;
for (let key in properties) {
popup += `<tr><td>${key.toUpperCase()}</td><td>${properties[key]}</td></tr>`;
}
popup += `</table>`;
return popup;
}
map.on(L.Draw.Event.CREATED, function (event) {
const layer = event.layer;
const feature = layer.feature = layer.feature || { type: 'Feature', properties: {} };
const area = turf.area(layer.toGeoJSON());
feature.properties = { Nama: '', Luas: `${area.toFixed(2)} m²` };
function renderPopupView() {
const props = layer.feature.properties;
let html = createPopup(props) + `<br><button id='edit-attr'>Edit Atribut</button>`;
layer.bindPopup(html).openPopup();
setTimeout(() => {
const btn = document.getElementById('edit-attr');
if (btn) btn.onclick = renderEditForm;
}, 100);
}
function renderEditForm() {
const props = layer.feature.properties;
let html = `<table class='popup-table' style='width:100%'>`;
for (let key in props) {
html += `<tr><td>${key.toUpperCase()}</td><td><input name='${key}' value='${props[key]}' style='width:100%'></td></tr>`;
}
html += `</table><br><button id='save-attr'>Simpan</button>`;
layer.bindPopup(html).openPopup();
setTimeout(() => {
const saveBtn = document.getElementById('save-attr');
if (saveBtn) saveBtn.onclick = () => {
document.querySelectorAll('.leaflet-popup input').forEach(input => {
props[input.name] = input.value;
});
layer.feature.properties = props;
// FIX: reset popup dan event
layer.unbindPopup();
layer.off('click');
layer.bindPopup(createPopup(props) + `<br><button id='edit-attr'>Edit</button>`);
layer.on('click', renderPopupView);
layer.openPopup();
};
}, 100);
}
layer.on('click', renderPopupView);
drawnItems.addLayer(layer);
renderPopupView();
});
let marker, lastLatLng;
let trackPoints = [];
let routeLine = L.polyline([], { color: 'blue' }).addTo(map);
let totalDistance = 0;
let tracking = false;
let watchID = null;
const infoBox = L.control({ position: 'topleft' });
infoBox.onAdd = function () {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
infoBox.update = function (dist = 0, area = 0) {
this._div.innerHTML = `<h4>Info Tracking</h4><b>Jarak:</b> ${(dist/1000).toFixed(2)} km<br><b>Luas:</b> ${area ? area.toFixed(2)+' m² ('+(area/10000).toFixed(2)+' ha)' : '-'}`;
};
infoBox.addTo(map);
function startTracking() {
if (!navigator.geolocation) return alert("Browser tidak mendukung geolocation.");
tracking = true;
document.getElementById('start-pause-button').innerText = "Pause Tracking";
watchID = navigator.geolocation.watchPosition(pos => {
const latlng = [pos.coords.latitude, pos.coords.longitude];
lastLatLng = latlng;
if (!marker) {
marker = L.circleMarker(latlng, { radius: 8, fillColor: "#4285F4", color: "#fff", weight: 2, fillOpacity: 1 }).addTo(map);
} else {
marker.setLatLng(latlng);
}
map.setView(latlng, 18);
if (tracking) {
trackPoints.push(latlng);
routeLine.setLatLngs(trackPoints);
if (trackPoints.length > 1) {
const from = turf.point(trackPoints[trackPoints.length - 2]);
const to = turf.point(trackPoints[trackPoints.length - 1]);
totalDistance += turf.distance(from, to, { units: 'meters' });
}
const area = trackPoints.length > 2 ? turf.area(turf.polygon([[...trackPoints, trackPoints[0]]])) : 0;
infoBox.update(totalDistance, area);
}
}, err => alert("Gagal tracking lokasi: " + err.message), { enableHighAccuracy: true });
}
function pauseTracking() {
tracking = false;
navigator.geolocation.clearWatch(watchID);
document.getElementById('start-pause-button').innerText = "Mulai Tracking";
}
document.getElementById("start-pause-button").onclick = () => tracking ? pauseTracking() : startTracking();
document.getElementById("locate-button").onclick = () => {
if (!navigator.geolocation) return alert("Geolocation tidak didukung");
navigator.geolocation.getCurrentPosition(pos => {
const latlng = [pos.coords.latitude, pos.coords.longitude];
map.setView(latlng, 18);
if (!marker) {
marker = L.circleMarker(latlng, { radius: 8, fillColor: "#4285F4", color: "#fff", weight: 2, fillOpacity: 1 }).addTo(map);
} else {
marker.setLatLng(latlng);
}
}, err => alert("Gagal ambil lokasi: " + err.message), { enableHighAccuracy: true });
};
document.getElementById("export-geojson").onclick = () => {
const features = [];
if (trackPoints.length > 1) features.push(turf.lineString(trackPoints, { name: "Rute Tracking" }));
if (trackPoints.length > 2) features.push(turf.polygon([[...trackPoints, trackPoints[0]]], { name: "Area Tracking" }));
drawnItems.eachLayer(layer => layer.toGeoJSON && features.push(layer.toGeoJSON()));
if (!features.length) return alert("Belum ada data untuk disimpan.");
const geojson = { type: "FeatureCollection", features };
const blob = new Blob([JSON.stringify(geojson)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "tracking_result.geojson";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
// Fungsi mengumpulkan GeoJSON
function geojsonFromDrawnItems() {
const features = [];
if (trackPoints.length > 1) features.push(turf.lineString(trackPoints, { name: "Rute Tracking" }));
if (trackPoints.length > 2) features.push(turf.polygon([[...trackPoints, trackPoints[0]]], { name: "Area Tracking" }));
drawnItems.eachLayer(layer => {
const gj = layer.toGeoJSON();
if (gj) features.push(gj);
});
return { type: "FeatureCollection", features };
}
// Fungsi bantu: Standarisasi properti agar semua feature punya field yang sama
function standardizeProps(features) {
const allKeys = new Set();
features.forEach(f => {
for (const k in f.properties) {
allKeys.add(k);
}
});
features.forEach(f => {
allKeys.forEach(k => {
if (!(k in f.properties)) {
f.properties[k] = "";
} else {
f.properties[k] = String(f.properties[k]);
}
});
});
return features;
}
// Tombol ekspor SHP
document.getElementById("export-shp").onclick = () => {
const geojson = geojsonFromDrawnItems();
if (!geojson.features.length) return alert("Belum ada data untuk diekspor.");
const features = standardizeProps(geojson.features);
const grouped = {
Polygon: [],
LineString: [],
Point: []
};
features.forEach(f => {
const type = f.geometry?.type;
if (grouped[type]) grouped[type].push(f);
});
if (grouped.Polygon.length)
shpwrite.download({ type: "FeatureCollection", features: grouped.Polygon }, { file: "digitasi_polygon" });
if (grouped.LineString.length)
shpwrite.download({ type: "FeatureCollection", features: grouped.LineString }, { file: "digitasi_line" });
if (grouped.Point.length)
shpwrite.download({ type: "FeatureCollection", features: grouped.Point }, { file: "digitasi_point" });
if (!grouped.Polygon.length && !grouped.LineString.length && !grouped.Point.length)
alert("Tidak ada geometri valid untuk diekspor ke SHP.");
};
</script>
</body>
</html>