fix(kml): reconocer Placemark con cualquier xmlns + soporte KMZ

Bug: kmlToGeoJson usaba xpath //kml:Placemark exigiendo el namespace
http://www.opengis.net/kml/2.2. KMLs con xmlns de Google Earth
(http://earth.google.com/kml/2.1) o sin namespace daban 0 placemarks →
la capa se creaba pero SIN elementos.

Fix:
- Todos los xpath usan local-name() (namespace-agnostic).
- parseKmlGeometry navega Point/LineString/Polygon/MultiGeometry sin
  depender del prefijo del namespace.
- Soporte KMZ: descomprime el zip y parsea el .kml interno (habitualmente doc.kml).
- Nuevo helper kmlChild() para leer <name>/<description> tolerante al ns.

Tests: SpatialFileConverterTest cubre los tres KMLs (opengis 2.2, google 2.1,
sin xmlns). Suite 94 passing.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 19:56:20 +02:00
co-authored by Claude Opus 4.7
parent 042233069f
commit a4554bb1ca
2 changed files with 134 additions and 33 deletions
@@ -0,0 +1,61 @@
<?php
namespace Tests\Feature;
use App\Services\SpatialFileConverter;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;
class SpatialFileConverterTest extends TestCase
{
private function uploadedKml(string $content, string $name = 'test.kml'): UploadedFile
{
$tmp = tempnam(sys_get_temp_dir(), 'kml_');
file_put_contents($tmp, $content);
return new UploadedFile($tmp, $name, 'application/vnd.google-earth.kml+xml', null, true);
}
public function test_kml_with_opengis_namespace_yields_features(): void
{
$kml = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark><name>P1</name><Point><coordinates>-3.7,40.4,0</coordinates></Point></Placemark>
<Placemark><name>P2</name><Point><coordinates>-3.6,40.5,0</coordinates></Point></Placemark>
</Document>
</kml>';
$g = SpatialFileConverter::convertToGeoJson($this->uploadedKml($kml));
$this->assertNotNull($g);
$this->assertCount(2, $g['features']);
$this->assertEquals('Point', $g['features'][0]['geometry']['type']);
}
public function test_kml_with_google_earth_namespace_still_yields_features(): void
{
// Este era el bug: xpath //kml:Placemark fallaba con este xmlns.
$kml = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<Placemark><name>Pilar 1</name><Point><coordinates>-3.7,40.4</coordinates></Point></Placemark>
</Document>
</kml>';
$g = SpatialFileConverter::convertToGeoJson($this->uploadedKml($kml));
$this->assertNotNull($g);
$this->assertCount(1, $g['features']);
$this->assertEquals('Pilar 1', $g['features'][0]['properties']['name']);
}
public function test_kml_without_namespace_still_yields_features(): void
{
$kml = '<?xml version="1.0" encoding="UTF-8"?>
<kml>
<Placemark><name>Muro</name>
<LineString><coordinates>-3.7,40.4 -3.6,40.5</coordinates></LineString>
</Placemark>
</kml>';
$g = SpatialFileConverter::convertToGeoJson($this->uploadedKml($kml));
$this->assertNotNull($g);
$this->assertCount(1, $g['features']);
$this->assertEquals('LineString', $g['features'][0]['geometry']['type']);
}
}