提问人:wxmikey 提问时间:11/7/2023 最后编辑:mickmackusawxmikey 更新时间:11/9/2023 访问量:38
解析 geojson 文件,然后以新格式输出数据 [duplicate]
Parse a geojson file then output data in a new format [duplicate]
问:
我有一个 .geojson 文件,我正在尝试根据每个数组属性中的标签使用 PHP 脚本对其进行解码。例如,有六个类别被标记:
高 LD5型 ENH公司 SLGT系列 磁共振成像 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:
URL 如下: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";
}
?>
答:
1赞
Tipu Sultan Eiko
11/7/2023
#1
这是我的尝试
<?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);
?>
评论