66 lines
1.9 KiB
PHP
66 lines
1.9 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
// URL for the SPC Day 1 Outlook KML data
|
|
$spc_kml_url = 'https://www.spc.noaa.gov/products/outlook/day1otlk_cat.kml';
|
|
|
|
// Fetch the KML file
|
|
$kml_content = file_get_contents($spc_kml_url);
|
|
|
|
if ($kml_content === false) {
|
|
echo json_encode(['error' => 'Failed to fetch SPC data.']);
|
|
exit;
|
|
}
|
|
|
|
// Parse the KML content
|
|
$xml = simplexml_load_string($kml_content, "SimpleXMLElement", LIBXML_NOCDATA);
|
|
if ($xml === false) {
|
|
echo json_encode(['error' => 'Failed to parse KML data.']);
|
|
exit;
|
|
}
|
|
|
|
// Register the KML namespace
|
|
$xml->registerXPathNamespace('kml', 'http://www.opengis.net/kml/2.2');
|
|
|
|
$features = [];
|
|
|
|
// Find all Placemarks in the KML
|
|
foreach ($xml->xpath('//kml:Placemark') as $placemark) {
|
|
$placemark->registerXPathNamespace('kml', 'http://www.opengis.net/kml/2.2');
|
|
$name = (string)$placemark->name;
|
|
|
|
// Look for Polygon
|
|
$polygon = $placemark->xpath('.//kml:Polygon');
|
|
if ($polygon && isset($polygon[0]->outerBoundaryIs->LinearRing->coordinates)) {
|
|
$coordinates_str = (string)$polygon[0]->outerBoundaryIs->LinearRing->coordinates;
|
|
$coordinates = parse_coordinates($coordinates_str);
|
|
if (!empty($coordinates)) {
|
|
$features[] = [
|
|
'name' => $name,
|
|
'type' => 'Polygon',
|
|
'coordinates' => $coordinates
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
echo json_encode($features);
|
|
|
|
function parse_coordinates($coordinates_str) {
|
|
$coords = [];
|
|
$pairs = explode(' ', trim($coordinates_str));
|
|
foreach ($pairs as $pair) {
|
|
$parts = explode(',', $pair);
|
|
if (count($parts) >= 2) {
|
|
$lon = floatval($parts[0]);
|
|
$lat = floatval($parts[1]);
|
|
// Ensure coordinates are valid
|
|
if (is_finite($lat) && is_finite($lon)) {
|
|
$coords[] = $lon;
|
|
$coords[] = $lat;
|
|
}
|
|
}
|
|
}
|
|
return $coords;
|
|
}
|
|
?>
|