提前感谢您抽出时间协助我。
我有一个 geojson 文件,我正在尝试根据每个数组属性中的标签使用 php 脚本对其进行解码。例如,有六个类别被标记:
高 多学科治疗 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:
网址如下: https://www.spc.noaa.gov/products/outlook/archive/2023/day1otlk_20230331_1630_cat.lyr.geojson
我编写了代码将其提取出来,以便我可以看到数组。
<?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
// 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
$string="";
foreach ($json_data['features'] as $feature) {
// Get the LABEL2 value
$label2 = $feature['properties']['LABEL2'];
$string.=$label2. "\n";
foreach ($feature['geometry']['coordinates'][0][0] as $coordinates) {
$string.=$coordinates[1] .",".$coordinates[0]. "\n";
}
// Convert coordinates array to string and remove brackets
$string.= "End:"."\n\n";
}
echo $string;
file_put_contents('data.txt', $string);
?>
这是一个更新脚本,其中坐标未显示。任何帮助将不胜感激。
<?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);
// Check if JSON decoding was successful
if ($data !== null) {
$formattedCoordinates = array();
// Extract coordinates from the GeoJSON data
foreach ($data['features'] as $feature) {
$label = $feature['properties']['LABEL'];
$formattedCoordinates[] = "\"$label\"";
$geometry = $feature['geometry'];
$coordinates = extractCoordinates($geometry['coordinates']);
$formattedCoordinates[] = formatCoordinates($coordinates);
}
// End with "End:"
$formattedCoordinates[] = "End:";
// Output the formatted coordinates
echo implode("\n", $formattedCoordinates);
} else {
echo "Failed to decode JSON data.";
}
}
// Function to extract coordinates
function extractCoordinates($coordinates) {
$coords = array();
foreach ($coordinates as $coord) {
$lat = $coord[1];
$lon = $coord[0];
$coords[] = "$lat, $lon";
}
return $coords;
}
// Function to format coordinates as specified
function formatCoordinates($coordinates) {
return implode("\n", $coordinates);
}
?>