worldwide-map / export_map_project.py
maringetxway's picture
Create export_map_project.py
6f36b95 verified
raw
history blame
2.07 kB
from datasets import load_dataset
import json
import os
import zipfile
# Load dataset
dataset = load_dataset("maringetxway/local")["train"]
# Create output folder
os.makedirs("map_project", exist_ok=True)
# Save data.json
with open("map_project/data.json", "w") as f:
json.dump(dataset[:], f)
# index.html
index_html = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>World Map from Hugging Face Dataset</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<style>
html, body {
margin: 0;
padding: 0;
}
#map {
height: 100vh;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
<script src="script.js"></script>
</body>
</html>
"""
with open("map_project/index.html", "w") as f:
f.write(index_html)
# script.js
script_js = """const map = L.map('map').setView([20, 0], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; OpenStreetMap contributors'
}).addTo(map);
fetch('data.json')
.then(response => response.json())
.then(data => {
data.forEach(entry => {
const lat = parseFloat(entry.latitude || entry.Latitude);
const lng = parseFloat(entry.longitude || entry.Longitude);
const name = entry.name || entry.Name || 'Unknown';
const desc = entry.description || entry.Description || '';
if (!isNaN(lat) && !isNaN(lng)) {
L.marker([lat, lng])
.addTo(map)
.bindPopup(`<strong>${name}</strong><br>${desc}`);
}
});
});
"""
with open("map_project/script.js", "w") as f:
f.write(script_js)
# Zip it
with zipfile.ZipFile("world_map_project.zip", "w") as zipf:
for foldername, subfolders, filenames in os.walk("map_project"):
for filename in filenames:
filepath = os.path.join(foldername, filename)
zipf.write(filepath, os.path.relpath(filepath, "map_project"))
print("βœ… Done! File saved as world_map_project.zip")