Search code examples
phparraysjsongeojsontext-parsing

Parse a geojson file then output data in a new format


I have a .geojson file that I am trying to decode with a PHP script based on the labelling in the properties of each array. For example there are six categories that are labelled:

HIGH MDT ENH SLGT MRGL TSTM

What I am trying to do is break this file down into each category and the associated coordinates positive comma space and then the negative then return a line and then the next coordinates until the array is complete for that category. At the end of that category, I need to place END: then a new line and start the process all over until all categories are taken care of.

Here is an example of the output:

"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:

The URL is the following: https://www.spc.noaa.gov/products/outlook/archive/2023/day1otlk_20230331_1630_cat.lyr.geojson

I wrote code to extract it out so I can see the arrays.

<?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>';
}

?>

Here is the start of my code to try to decode it:

<?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";
}
?>

Solution

  • Here is my try

    <?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);
    ?>