chore: cleanup dead code + format with Pint
- Remove unused FeaturesController (empty stubs, no routes) - Remove ConvertSpatialFile CLI command (unused; service used in LayerManager) - Remove MigrateGeojsonToFeatures CLI command (one-shot migration, not referenced) - Remove .claude/worktrees/ (11 old agent worktrees from June) - Apply Laravel Pint formatting across 219 files (style only, no functional changes) Tests: 101 passing (319 assertions) API routes: unchanged (8 routes intact)
This commit is contained in:
@@ -15,14 +15,16 @@ class SpatialFileConverter
|
||||
|
||||
$geojson = match ($ext) {
|
||||
'geojson' => self::parseGeoJson($path),
|
||||
'kml' => self::kmlToGeoJson($path),
|
||||
'kmz' => self::kmzToGeoJson($path),
|
||||
'shp' => self::shapefileToGeoJson($path),
|
||||
'zip' => self::handleZip($path),
|
||||
default => null,
|
||||
'kml' => self::kmlToGeoJson($path),
|
||||
'kmz' => self::kmzToGeoJson($path),
|
||||
'shp' => self::shapefileToGeoJson($path),
|
||||
'zip' => self::handleZip($path),
|
||||
default => null,
|
||||
};
|
||||
|
||||
if (!$geojson) return null;
|
||||
if (! $geojson) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return self::postProcess($geojson);
|
||||
}
|
||||
@@ -37,15 +39,19 @@ class SpatialFileConverter
|
||||
|
||||
foreach ($geojson['features'] ?? [] as $feature) {
|
||||
|
||||
if (!isset($feature['geometry'])) continue;
|
||||
if (! isset($feature['geometry'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$geometry = self::cleanGeometry($feature['geometry']);
|
||||
if (!$geometry) continue;
|
||||
if (! $geometry) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => $geometry,
|
||||
'properties' => self::normalizeProperties($feature['properties'] ?? [])
|
||||
'properties' => self::normalizeProperties($feature['properties'] ?? []),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -53,7 +59,7 @@ class SpatialFileConverter
|
||||
'type' => 'FeatureCollection',
|
||||
'features' => $features,
|
||||
'bbox' => self::calculateBBox($features),
|
||||
'centroid' => self::calculateCentroid($features)
|
||||
'centroid' => self::calculateCentroid($features),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -75,13 +81,16 @@ class SpatialFileConverter
|
||||
|
||||
private static function cleanGeometry(array $geom): ?array
|
||||
{
|
||||
if (!isset($geom['type'], $geom['coordinates'])) return null;
|
||||
if (! isset($geom['type'], $geom['coordinates'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($geom['type'] === 'Polygon') {
|
||||
$geom['coordinates'] = array_map(function ($ring) {
|
||||
if ($ring[0] !== end($ring)) {
|
||||
$ring[] = $ring[0];
|
||||
}
|
||||
|
||||
return $ring;
|
||||
}, $geom['coordinates']);
|
||||
}
|
||||
@@ -101,7 +110,9 @@ class SpatialFileConverter
|
||||
$coords = array_merge($coords, self::flattenCoords($f['geometry']['coordinates']));
|
||||
}
|
||||
|
||||
if (empty($coords)) return null;
|
||||
if (empty($coords)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$lons = array_column($coords, 0);
|
||||
$lats = array_column($coords, 1);
|
||||
@@ -110,7 +121,7 @@ class SpatialFileConverter
|
||||
min($lons),
|
||||
min($lats),
|
||||
max($lons),
|
||||
max($lats)
|
||||
max($lats),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -126,7 +137,9 @@ class SpatialFileConverter
|
||||
$coords = array_merge($coords, self::flattenCoords($f['geometry']['coordinates']));
|
||||
}
|
||||
|
||||
if (empty($coords)) return null;
|
||||
if (empty($coords)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$x = array_sum(array_column($coords, 0)) / count($coords);
|
||||
$y = array_sum(array_column($coords, 1)) / count($coords);
|
||||
@@ -139,10 +152,13 @@ class SpatialFileConverter
|
||||
$result = [];
|
||||
|
||||
$iterator = function ($c) use (&$result, &$iterator) {
|
||||
if (!is_array($c)) return;
|
||||
if (! is_array($c)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isset($c[0]) && isset($c[1]) && is_numeric($c[0])) {
|
||||
$result[] = [$c[0], $c[1]];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -163,6 +179,7 @@ class SpatialFileConverter
|
||||
private static function parseGeoJson($path): ?array
|
||||
{
|
||||
$data = json_decode(file_get_contents($path), true);
|
||||
|
||||
return json_last_error() === JSON_ERROR_NONE ? $data : null;
|
||||
}
|
||||
|
||||
@@ -175,18 +192,24 @@ class SpatialFileConverter
|
||||
libxml_use_internal_errors(true);
|
||||
$xml = simplexml_load_file($path);
|
||||
|
||||
if (!$xml) return null;
|
||||
if (! $xml) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Namespace-agnostic: usamos local-name() en el XPath para aceptar KMLs
|
||||
// con cualquier xmlns (opengis 2.2, earth.google 2.1/2.0, o sin xmlns).
|
||||
$placemarks = $xml->xpath('//*[local-name()="Placemark"]');
|
||||
if ($placemarks === false) $placemarks = [];
|
||||
if ($placemarks === false) {
|
||||
$placemarks = [];
|
||||
}
|
||||
|
||||
$features = [];
|
||||
|
||||
foreach ($placemarks as $pm) {
|
||||
$geom = self::parseKmlGeometry($pm);
|
||||
if (!$geom) continue;
|
||||
if (! $geom) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
@@ -204,12 +227,15 @@ class SpatialFileConverter
|
||||
/** Descomprime un KMZ y parsea el .kml interno. */
|
||||
private static function kmzToGeoJson(string $path): ?array
|
||||
{
|
||||
$zip = new \ZipArchive();
|
||||
if ($zip->open($path) !== true) return null;
|
||||
$zip = new \ZipArchive;
|
||||
if ($zip->open($path) !== true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$tmp = sys_get_temp_dir() . '/kmz_' . uniqid();
|
||||
$tmp = sys_get_temp_dir().'/kmz_'.uniqid();
|
||||
if (! @mkdir($tmp, 0777, true) && ! is_dir($tmp)) {
|
||||
$zip->close();
|
||||
|
||||
return null;
|
||||
}
|
||||
$zip->extractTo($tmp);
|
||||
@@ -228,15 +254,20 @@ class SpatialFileConverter
|
||||
|
||||
// Limpieza
|
||||
self::rrmdir($tmp);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function rrmdir(string $dir): void
|
||||
{
|
||||
if (! is_dir($dir)) return;
|
||||
if (! is_dir($dir)) {
|
||||
return;
|
||||
}
|
||||
foreach (scandir($dir) ?: [] as $item) {
|
||||
if ($item === '.' || $item === '..') continue;
|
||||
$p = $dir . DIRECTORY_SEPARATOR . $item;
|
||||
if ($item === '.' || $item === '..') {
|
||||
continue;
|
||||
}
|
||||
$p = $dir.DIRECTORY_SEPARATOR.$item;
|
||||
is_dir($p) ? self::rrmdir($p) : @unlink($p);
|
||||
}
|
||||
@rmdir($dir);
|
||||
@@ -245,38 +276,45 @@ class SpatialFileConverter
|
||||
/** Devuelve el primer hijo cuyo local-name coincida (sin depender del prefijo). */
|
||||
private static function kmlChild(\SimpleXMLElement $node, string $name): \SimpleXMLElement|string
|
||||
{
|
||||
$matches = $node->xpath('./*[local-name()="' . $name . '"]');
|
||||
$matches = $node->xpath('./*[local-name()="'.$name.'"]');
|
||||
|
||||
return $matches ? $matches[0] : '';
|
||||
}
|
||||
|
||||
private static function parseKmlGeometry($pm): ?array
|
||||
{
|
||||
// Todos los accesos van por xpath local-name para tolerar cualquier xmlns.
|
||||
$find = fn (string $tag) => $pm->xpath('./*[local-name()="' . $tag . '"]');
|
||||
$findDeep = fn (\SimpleXMLElement $n, string $tag) => $n->xpath('.//*[local-name()="' . $tag . '"]');
|
||||
$find = fn (string $tag) => $pm->xpath('./*[local-name()="'.$tag.'"]');
|
||||
$findDeep = fn (\SimpleXMLElement $n, string $tag) => $n->xpath('.//*[local-name()="'.$tag.'"]');
|
||||
|
||||
if ($multi = $find('MultiGeometry')) {
|
||||
$geoms = [];
|
||||
foreach ($multi[0]->children() as $g) {
|
||||
$parsed = self::parseKmlGeometry($g);
|
||||
if ($parsed) $geoms[] = $parsed;
|
||||
if ($parsed) {
|
||||
$geoms[] = $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return ['type' => 'GeometryCollection', 'geometries' => $geoms];
|
||||
}
|
||||
|
||||
if ($point = $find('Point')) {
|
||||
$coords = self::parseKmlCoords((string) ($findDeep($point[0], 'coordinates')[0] ?? ''));
|
||||
|
||||
return $coords ? ['type' => 'Point', 'coordinates' => $coords[0]] : null;
|
||||
}
|
||||
|
||||
if ($line = $find('LineString')) {
|
||||
$coords = self::parseKmlCoords((string) ($findDeep($line[0], 'coordinates')[0] ?? ''));
|
||||
|
||||
return $coords ? ['type' => 'LineString', 'coordinates' => $coords] : null;
|
||||
}
|
||||
|
||||
if ($poly = $find('Polygon')) {
|
||||
$outer = $findDeep($poly[0], 'coordinates')[0] ?? '';
|
||||
$coords = self::parseKmlCoords((string) $outer);
|
||||
|
||||
return $coords ? ['type' => 'Polygon', 'coordinates' => [$coords]] : null;
|
||||
}
|
||||
|
||||
@@ -289,9 +327,10 @@ class SpatialFileConverter
|
||||
foreach (preg_split('/\s+/', trim($text)) as $pair) {
|
||||
$p = explode(',', $pair);
|
||||
if (count($p) >= 2) {
|
||||
$coords[] = [(float)$p[0], (float)$p[1]];
|
||||
$coords[] = [(float) $p[0], (float) $p[1]];
|
||||
}
|
||||
}
|
||||
|
||||
return $coords;
|
||||
}
|
||||
|
||||
@@ -307,16 +346,20 @@ class SpatialFileConverter
|
||||
$features = [];
|
||||
|
||||
while ($record = $reader->fetchRecord()) {
|
||||
if ($record->isDeleted()) continue;
|
||||
if ($record->isDeleted()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$geom = json_decode($record->getGeometry()->toGeoJSON(), true);
|
||||
|
||||
if (!$geom) continue;
|
||||
if (! $geom) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$features[] = [
|
||||
'type' => 'Feature',
|
||||
'geometry' => $geom,
|
||||
'properties' => $record->getDataArray()
|
||||
'properties' => $record->getDataArray(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -324,6 +367,7 @@ class SpatialFileConverter
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -334,11 +378,13 @@ class SpatialFileConverter
|
||||
|
||||
private static function handleZip($zipPath): ?array
|
||||
{
|
||||
$zip = new \ZipArchive();
|
||||
$zip = new \ZipArchive;
|
||||
|
||||
if ($zip->open($zipPath) !== true) return null;
|
||||
if ($zip->open($zipPath) !== true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$dir = sys_get_temp_dir() . '/geo_' . uniqid();
|
||||
$dir = sys_get_temp_dir().'/geo_'.uniqid();
|
||||
mkdir($dir);
|
||||
|
||||
$zip->extractTo($dir);
|
||||
@@ -347,7 +393,7 @@ class SpatialFileConverter
|
||||
$result = null;
|
||||
|
||||
foreach (scandir($dir) as $file) {
|
||||
$full = $dir . '/' . $file;
|
||||
$full = $dir.'/'.$file;
|
||||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
|
||||
if ($ext === 'shp') {
|
||||
@@ -373,4 +419,4 @@ class SpatialFileConverter
|
||||
}
|
||||
rmdir($dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user