PHP解析GeoJSON文件并按分类输出指定格式坐标
需求与解决方案
需求说明
我有一个GeoJSON文件,需要用PHP脚本根据properties中的标签(HIGH、MDT、ENH、SLGT、MRGL、TSTM)解析并拆分文件,每个分类下输出坐标(格式为“正数, 负数”),每行一个坐标,分类结束后添加End:并换行,直到所有分类处理完成。
目标输出示例
"Marginal Risk" 30.48, -80.86 30.73, -82.03 31.14, -82.82 32.74, -82.82 33.87, -83.38 34.50, -83.21 35.33, -82.23 35.64, -81.15 35.58, -80.27 34.58, -78.98 34.11, -78.18 33.84, -77.43 End: "General Risk" 29.10, -80.01 29.31, -82.96 29.84, -83.95 29.86, -85.02 30.61, -85.80 31.55, -85.96 32.90, -85.58 34.01, -85.17 35.13, -85.23 36.42, -85.35 37.10, -85.14 38.65, -83.20 39.21, -81.77 39.27, -79.95 39.74, -78.60 40.70, -77.85 42.74, -74.88 43.22, -73.25 44.08, -72.13 45.35, -70.09 45.32, -68.87 43.88, -67.73 23.92, -82.05 24.93, -80.46 26.81, -79.58 End:
现有尝试代码
查看数组结构的代码
<?php // Define the URL $url = "https://www.spc.noaa.gov/products/outlook/archive/2023/day1otlk_20230331_1630_cat.lyr.geojson"; // Use cURL to fetch the data $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); // Check if the request was successful if ($response === false) { echo "Failed to retrieve data: " . curl_error($ch); } else { // Close the cURL handle curl_close($ch); // Parse the JSON response $data = json_decode($response, true); echo '<pre>' . print_r($data, true) . '</pre>'; } ?>
初始解码代码
<?php // URL of the GeoJSON file $url = 'https://www.spc.noaa.gov/products/outlook/archive/2023/day1otlk_20230331_1630_cat.lyr.geojson'; // Get the GeoJSON data $data = file_get_contents($url); // Decode the JSON data $json_data = json_decode($data, true); // Iterate over the features in the GeoJSON data foreach ($json_data['features'] as $feature) { // Get the LABEL2 value $label2 = $feature['properties']['LABEL2']; // Get the coordinates $coordinates = $feature['geometry']['coordinates']; // Convert coordinates array to string and remove brackets $coordinates_string = json_encode($coordinates, JSON_PRETTY_PRINT ); // Print the LABEL2 value and coordinates echo 'LABEL2: ' . $label2 . ', coordinates: ' . $coordinates_string . "\n"; } ?>
修正后的PHP脚本
<?php // GeoJSON文件URL $url = 'https://www.spc.noaa.gov/products/outlook/archive/2023/day1otlk_20230331_1630_cat.lyr.geojson'; // 获取GeoJSON数据,优先用cURL兼容更多环境 $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); if ($response === false) { die("获取数据失败: " . curl_error($ch)); } curl_close($ch); // 解析JSON数据 $jsonData = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { die("JSON解析失败: " . json_last_error_msg()); } // 定义需要处理的分类标签映射(标签到显示名称) $categoryMap = [ 'HIGH' => 'High Risk', 'MDT' => 'Moderate Risk', 'ENH' => 'Enhanced Risk', 'SLGT' => 'Slight Risk', 'MRGL' => 'Marginal Risk', 'TSTM' => 'Thunderstorm Risk' ]; // 先按分类整理所有坐标 $categoryData = []; foreach ($jsonData['features'] as $feature) { $label = $feature['properties']['LABEL2']; // 只处理目标分类 if (!isset($categoryMap[$label])) { continue; } $displayName = $categoryMap[$label]; // 处理坐标:GeoJSON是[经度, 纬度],需要转成"纬度, 经度"格式 foreach ($feature['geometry']['coordinates'][0] as $coord) { $lat = $coord[1]; $lon = $coord[0]; $categoryData[$displayName][] = sprintf("%.2f, %.2f", $lat, $lon); } } // 按要求输出结果 foreach ($categoryData as $name => $coords) { echo "\"$name\"\n"; foreach ($coords as $coord) { echo "$coord\n"; } echo "End:\n\n"; } ?>
代码说明
- 使用cURL获取数据,兼容性比
file_get_contents更好,避免因服务器配置限制无法获取数据 - 先将所有分类的坐标按显示名称归类,确保输出时同一分类的坐标集中展示
- 处理坐标格式:将GeoJSON中的
[经度,纬度]转换为示例要求的纬度, 经度格式,保留两位小数 - 只处理指定的6个分类标签,自动过滤无关数据
- 严格遵循目标输出格式,每个分类结束后添加
End:并换行
内容的提问来源于stack exchange,提问作者wxmikey
相关产品推荐
相关产品推荐

