Leaflet과 folium: 가볍고 확실한 웹 지도
Leaflet은 가장 널리 쓰이는 가벼운 웹 지도 자바스크립트 라이브러리이고, folium은 그것을 파이썬에서 쓰게 해 줍니다(11장). 이번에는 folium 없이 Leaflet HTML을 직접 써서, 무엇이 어떻게 그려지는지 봅니다.
버전
안정판은 Leaflet 1.9.4(npm latest)입니다. Leaflet 2.0은 아직 베타(2.0.0-beta.5)라 강의·업무에는 1.9.4를 권합니다.
출처(2026-11-24 확인): leafletjs.com · Leaflet 릴리스
실습: HTML 한 파일
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Leaflet 1.9.4로 그린 서울 행정동</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<style>html, body, #map { height: 100%; margin: 0; } .lg { background: #fff; padding: 6px 8px; font: 12px sans-serif; border-radius: 6px; }</style>
</head>
<body>
<div id="map"></div>
<script>
// 1) 지도와 배경 타일 (OSM 공식 타일: 소규모·학습용만, 출처 표시 필수)
const map = L.map('map').setView([37.5665, 126.978], 11);
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18, attribution: '© OpenStreetMap contributors'
}).addTo(map);
// 2) 값 → 색 (역 800m 커버율)
const color = c => c < 20 ? '#49006a' : c < 40 ? '#ae017e' : c < 60 ? '#f768a1' : c < 80 ? '#fbb4c3' : '#fde8ec';
// 3) GeoJSON 불러와 칠하고, 누르면 값 보이기
fetch('seoul_dong_web.geojson').then(r => r.json()).then(gj => {
L.geoJSON(gj, {
style: f => ({ fillColor: color(f.properties.cov), fillOpacity: 0.75, color: '#fff', weight: 0.6 }),
onEachFeature: (f, layer) => layer.bindPopup(
`<b>${f.properties.name}</b><br>인구 ${f.properties.pop.toLocaleString()}명<br>역 800m 안 ${f.properties.cov}%`)
}).addTo(map);
});
// 4) 범례
const lg = L.control({ position: 'bottomright' });
lg.onAdd = () => { const d = L.DomUtil.create('div', 'lg'); d.innerHTML = '동 면적 중 역 800m 안 비율<br>진할수록 역에서 먼 동'; return d; };
lg.addTo(map);
</script>
</body>
</html>
같은 폴더에 seoul_dong_web.geojson(키트의 tools/t_geojson_web.py가 만듦, 246KB)을 두고 간단한 웹 서버로 엽니다. 파일을 더블클릭해 열면 브라우저 보안 때문에 fetch가 막히니 python -m http.server로 여세요.
예제의 tile.openstreetmap.org는 학습·소규모용입니다. 사용 정책상 출처 표시와 올바른 User-Agent·Referer가 필요하고, 대량 다운로드·미리 받기·오프라인 사용이 금지되며 예고 없이 차단될 수 있습니다. 서비스라면 유료 타일이나 자체 타일 서버를 쓰세요(A08).
출처(2026-11-24 확인): OSM 타일 사용 정책
folium과의 관계
11장의 folium 코드는 결국 이런 Leaflet HTML을 만들어 줍니다. 파이썬 안에서 끝내려면 folium, 웹사이트에 넣고 세밀하게 고치려면 Leaflet을 직접 씁니다.
Leaflet = 타일 + GeoJSON + 팝업. 30줄짜리 HTML 하나로 클릭되는 지도를 만들 수 있습니다.