+page.svelte 2.92 KB
<script>
  import { onMount } from 'svelte';
  import * as d3 from 'd3';

  let mapaData = $state(null);
  let boliviaPath = $state('');
  let deptoPath = $state('');
  let muniPath = $state('');
  let error = $state('');
  let debug = $state('');

  onMount(async () => {
    try {
      const res = await fetch('/mapa.json');
      mapaData = await res.json();

      debug = `bolivia: ${mapaData.bolivia.features.length} features, `;
      debug += `deptos: ${mapaData.departamentos.features.length}, `;
      debug += `munis: ${mapaData.municipios.features.length}`;

      // Quitar CRS y corregir winding order
      delete mapaData.bolivia.crs;
      delete mapaData.departamentos.crs;
      delete mapaData.municipios.crs;

      // Corregir winding order: invertir todos los anillos
      function fixWinding(geojson) {
        const fix = (coords, isHole) => {
          // Para geo coords: exterior debe ser counterclockwise (area < 0 en D3)
          // Simplemente invertimos todos los anillos exteriores
          coords.forEach((ring, i) => {
            if (i === 0) ring.reverse(); // exterior
            // holes quedan como están
          });
        };
        for (const feat of geojson.features || [geojson]) {
          const geom = (feat.geometry || feat);
          if (geom.type === 'Polygon') {
            fix(geom.coordinates);
          } else if (geom.type === 'MultiPolygon') {
            geom.coordinates.forEach(poly => fix(poly));
          }
        }
      }

      fixWinding(mapaData.bolivia);
      fixWinding(mapaData.departamentos);
      fixWinding(mapaData.municipios);

      const bounds = d3.geoBounds(mapaData.bolivia);
      debug += ` | bounds: ${JSON.stringify(bounds)}`;

      const projection = d3.geoMercator().fitSize([380, 480], mapaData.bolivia);
      const pathGen = d3.geoPath(projection);

      boliviaPath = pathGen(mapaData.bolivia) || '';
      debug += ` | boliviaPath length: ${boliviaPath.length}`;

      deptoPath = mapaData.departamentos.features.map(f => pathGen(f)).join(' ');
      debug += ` | deptoPath length: ${deptoPath.length}`;

      const muni = mapaData.municipios.features[0];
      muniPath = pathGen(muni) || '';
      debug += ` | muni code: ${muni.properties.codigo}`;

    } catch (e) {
      error = e.message;
    }
  });
</script>

<div style="padding: 2rem; background: #1C1C1A; color: #F5F0E8; min-height: 100vh;">
  <h1>Test Mapa</h1>
  <p style="font-size: 12px; color: #888;">{debug}</p>
  {#if error}
    <p style="color: red;">{error}</p>
  {/if}

  <svg width="400" height="500" viewBox="0 0 400 500" style="border: 1px solid #333;">
    {#if boliviaPath}
      <path d={boliviaPath} fill="none" stroke="#555" stroke-width="1" />
    {/if}
    {#if deptoPath}
      <path d={deptoPath} fill="rgba(255,255,255,0.1)" stroke="#777" stroke-width="0.5" />
    {/if}
    {#if muniPath}
      <path d={muniPath} fill="#D4A574" stroke="#D4A574" stroke-width="1" />
    {/if}
  </svg>
</div>