Aplikasi Map Editor Converter
Aplikasi Map Editor Converter
DEMO :
http://yanzen.web.id/xoxo-map-editor-converter.html
Below is a **small single-file browser app** that can:
– Open/import **GeoJSON / JSON**
– Open/import **KML**
– Open/import **zipped Shapefile**: `.zip` containing `.shp`, `.shx`, `.dbf`, `.prj`
– Draw/edit/delete polygons, lines, rectangles, markers
– Export to:
– **GeoJSON**
– **KML**
– **Shapefile ZIP**
It uses **Leaflet** as the map editor. No backend is required.
—
## 1. Save as `map-editor-converter.html`
“`html
<!DOCTYPE html>
<html>
<head>
<meta charset=”utf-8″ />
<title>Small Map Polygon Editor and Converter</title>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<!– Leaflet –>
<link
rel=”stylesheet”
href=”https://unpkg.com/leaflet@1.9.4/dist/leaflet.css”
/>
<script src=”https://unpkg.com/leaflet@1.9.4/dist/leaflet.js”></script>
<!– Leaflet Draw –>
<link
rel=”stylesheet”
href=”https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.css”
/>
<script src=”https://unpkg.com/leaflet-draw@1.0.4/dist/leaflet.draw.js”></script>
<!– KML to GeoJSON –>
<script src=”https://unpkg.com/@tmcw/togeojson@5.8.1/dist/togeojson.umd.js”></script>
<!– GeoJSON to KML –>
<script src=”https://unpkg.com/tokml@0.4.0/tokml.js”></script>
<!– Shapefile ZIP to GeoJSON –>
<script src=”https://unpkg.com/shpjs@latest/dist/shp.min.js”></script>
<!– GeoJSON to Shapefile ZIP –>
<script src=”https://unpkg.com/shp-write@latest/shpwrite.js”></script>
<style>
html, body {
height: 100%;
margin: 0;
font-family: Arial, sans-serif;
}
#toolbar {
height: auto;
padding: 10px;
background: #f5f5f5;
border-bottom: 1px solid #ccc;
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
z-index: 9999;
}
#map {
height: calc(100% – 64px);
width: 100%;
}
button, input, select {
padding: 6px 10px;
font-size: 14px;
}
button {
cursor: pointer;
}
.info {
font-size: 13px;
color: #333;
}
</style>
</head>
<body>
<div id=”toolbar”>
<input type=”file” id=”fileInput” accept=”.json,.geojson,.kml,.zip” />
<button onclick=”exportGeoJSON()”>Export GeoJSON</button>
<button onclick=”exportKML()”>Export KML</button>
<button onclick=”exportSHP()”>Export SHP ZIP</button>
<button onclick=”clearMap()”>Clear</button>
<button onclick=”zoomToData()”>Zoom to Data</button>
<span class=”info”>
Import: GeoJSON, KML, zipped SHP | Draw/edit polygons on map
</span>
</div>
<div id=”map”></div>
<script>
// —————————–
// Map setup
// —————————–
const map = L.map(‘map’).setView([20, 0], 2);
L.tileLayer(‘https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png’, {
maxZoom: 20,
attribution: ‘© OpenStreetMap contributors’
}).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(L.Draw.Event.CREATED, function (event) {
const layer = event.layer;
if (!layer.feature) {
layer.feature = {
type: “Feature”,
properties: {}
};
}
drawnItems.addLayer(layer);
});
// —————————–
// Import files
// —————————–
document.getElementById(‘fileInput’).addEventListener(‘change’, async function (e) {
const file = e.target.files[0];
if (!file) return;
const name = file.name.toLowerCase();
try {
if (name.endsWith(‘.geojson’) || name.endsWith(‘.json’)) {
const text = await file.text();
const geojson = JSON.parse(text);
addGeoJSONToMap(geojson);
}
else if (name.endsWith(‘.kml’)) {
const text = await file.text();
const parser = new DOMParser();
const kml = parser.parseFromString(text, ‘text/xml’);
const geojson = toGeoJSON.kml(kml);
addGeoJSONToMap(geojson);
}
else if (name.endsWith(‘.zip’)) {
const arrayBuffer = await file.arrayBuffer();
// shpjs reads zipped shapefile and returns GeoJSON
const geojson = await shp(arrayBuffer);
// shpjs may return either FeatureCollection or array/layer object
if (geojson.type === “FeatureCollection”) {
addGeoJSONToMap(geojson);
} else if (Array.isArray(geojson)) {
geojson.forEach(g => addGeoJSONToMap(g));
} else {
addGeoJSONToMap(geojson);
}
}
else {
alert(“Unsupported file type. Use GeoJSON, JSON, KML, or zipped SHP.”);
}
} catch (err) {
console.error(err);
alert(“Could not load file: ” + err.message);
}
e.target.value = “”;
});
function addGeoJSONToMap(geojson) {
const layer = L.geoJSON(geojson, {
onEachFeature: function (feature, layer) {
if (!feature.properties) {
feature.properties = {};
}
layer.feature = feature;
const popupHtml = buildPopupHtml(feature);
layer.bindPopup(popupHtml);
}
});
layer.eachLayer(function (l) {
drawnItems.addLayer(l);
});
zoomToData();
}
// —————————–
// Simple property popup
// —————————–
function buildPopupHtml(feature) {
const props = feature.properties || {};
let html = “<b>Properties</b><br>”;
const keys = Object.keys(props);
if (keys.length === 0) {
html += “<i>No properties</i><br>”;
} else {
keys.forEach(k => {
html += `<b>${escapeHtml(k)}</b>: ${escapeHtml(String(props[k]))}<br>`;
});
}
html += `
<hr>
<button onclick=”editProperties(${L.stampForPopup || 0})” disabled>
Property editor disabled in popup
</button>
`;
html += `<small>You can edit geometry with map tools.</small>`;
return html;
}
function escapeHtml(str) {
return str.replace(/[&<>”‘]/g, function (m) {
return ({
‘&’: ‘&’,
‘<‘: ‘<’,
‘>’: ‘>’,
‘”‘: ‘"’,
“‘”: ‘'’
})[m];
});
}
// —————————–
// Export helpers
// —————————–
function getFeatureCollection() {
const geojson = drawnItems.toGeoJSON();
// Ensure properties exist
geojson.features.forEach(f => {
if (!f.properties) f.properties = {};
});
return geojson;
}
function downloadText(filename, text, mimeType) {
const blob = new Blob([text], { type: mimeType });
const url = URL.createObjectURL(blob);
const a = document.createElement(‘a’);
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
function exportGeoJSON() {
const geojson = getFeatureCollection();
downloadText(
“map-data.geojson”,
JSON.stringify(geojson, null, 2),
“application/geo+json”
);
}
function exportKML() {
const geojson = getFeatureCollection();
if (geojson.features.length === 0) {
alert(“No data to export.”);
return;
}
const kml = tokml(geojson);
downloadText(“map-data.kml”, kml, “application/vnd.google-earth.kml+xml”);
}
function exportSHP() {
const geojson = getFeatureCollection();
if (geojson.features.length === 0) {
alert(“No data to export.”);
return;
}
/*
shp-write creates a zipped shapefile.
Output coordinate system is normally WGS84 EPSG:4326.
*/
shpwrite.download(geojson, {
folder: “map-data”,
types: {
point: “points”,
polygon: “polygons”,
polyline: “lines”
}
});
}
// —————————–
// Map utilities
// —————————–
function clearMap() {
if (confirm(“Clear all map data?”)) {
drawnItems.clearLayers();
}
}
function zoomToData() {
if (drawnItems.getLayers().length === 0) return;
try {
map.fitBounds(drawnItems.getBounds(), {
padding: [30, 30]
});
} catch (e) {
console.warn(e);
}
}
</script>
</body>
</html>
“`
—
## 2. How to use
1. Save the file as:
“`text
map-editor-converter.html
“`
2. Open it in your browser.
3. Click **Choose File** and upload one of:
“`text
.geojson
.json
.kml
.zip
“`
For Shapefile, upload a `.zip` containing at least:
“`text
example.shp
example.shx
example.dbf
example.prj
“`
4. Edit the map using the drawing tools on the left.
5. Export using:
– **Export GeoJSON**
– **Export KML**
– **Export SHP ZIP**
—
## Notes
– Google My Maps commonly supports **KML/KMZ** import.
– This app exports **KML**, which can be used with Google Earth or Google My Maps.
– Shapefile export may shorten field names because DBF/Shapefile has old limits.
– Coordinates are handled as standard web map coordinates, usually **WGS84 / EPSG:4326**.
– This version edits geometry. Attribute/property editing can be added if needed.