John Davidson

geojson - Extract geojosn basis country and rewrite to file in php - don't know where to start

0 comments
Message:


I obtained a very large geojson file covering (world-ski-mtn.geojson) all ski mountains of the world (310mb). I do not want the world data and just want the France data. How would I read this file and rewrite to file out with just France data in PHP? The is "FR" mentioned in each record but how to read and filter.


{"type": "FeatureCollection", "features":[
{"properties":{"activities":["nordic"],"generated":false,"id":"5d0baf79bc790209271afd09bbf3e21c923da6a2","location":{"iso3166_1Alpha2":"FR","iso3166_2":null,"localized":{"en":{"country":"France","region":null,"locality":null}}},"name":"Domaine Nordique de Pessade","runConvention":"europe","sources":[{"type":"openstreetmap","id":"relation/2754619"}],"status":"operating","type":"skiArea","websites":["http://www.tourisme-lescheires.com/les-activites-nordiques.html"],"statistics":{"runs":{"byActivity":{"nordic":{"byDifficulty":{"novice":{"count":3,"lengthInKm":2.158222657716816,"minElevation":1179.76293945312,"maxElevation":1241.02416992188,"combinedElevationChange":46.74377441407978},"easy":{"count":4,"lengthInKm":2.2528948156563886,"minElevation":1223.2634277343798,"maxElevation":1269.7160644531202,"combinedElevationChange":77.51440429685863},"other":{"count":7,"lengthInKm":8.737367497472203,"minElevation":1203.24682617188,"maxElevation":1446.0247802734398,"combinedElevationChange":366.62219238280954}}}},"minElevation":1179.76293945312,"maxElevation":1446.0247802734398},"lifts":{"byType":{}},"maxElevation":1446.0247802734398,"minElevation":1179.76293945312}},"type":"Feature","geometry":{"coordinates":[2.8720576963815803,45.62683955065789],"type":"Point"}}
,
{"properties":{"activities":["downhill","nordic"],"generated":false,"id":"f9a7b501ed966ed140fc2b5290e472c0a825d9be","location":{"iso3166_1Alpha2":"FR","iso3166_2":null,"localized":{"en":{"country":"France","region":null,"locality":null}}},"name":"Cauterets Pont d'Espagne","runConvention":"europe","sources":[{"type":"openstreetmap","id":"relation/2873910"}],"status":"operating","type":"skiArea","websites":[],"statistics":{"runs":{"byActivity":{"nordic":{"byDifficulty":{"novice":{"count":4,"lengthInKm":1.4561676423330407,"minElevation":1515.00061035156,"maxElevation":1544.46008300781,"combinedElevationChange":14.598876953139097},"easy":{"count":12,"lengthInKm":7.557498528921144,"minElevation":1531.1962890625,"maxElevation":1660.64587402344,"combinedElevationChange":236.31213378904022},"intermediate":{"count":1,"lengthInKm":1.7299713686348341,"minElevation":1645.50512695312,"maxElevation":1745.72180175781,"combinedElevationChange":96.50671386718955}}},"downhill":{"byDifficulty":{"intermediate":{"count":1,"lengthInKm":0.8539627981775926,"minElevation":1553.6240234374998,"maxElevation":1757.25048828125,"combinedElevationChange":203.62646484375023},"easy":{"count":1,"lengthInKm":1.6256735065210572,"minElevation":1515.00061035156,"maxElevation":1757.25048828125,"combinedElevationChange":241.24987792969},"novice":{"count":2,"lengthInKm":0.36506435611032134,"minElevation":1540.71752929688,"maxElevation":1571.4881591796902,"combinedElevationChange":52.396362304680224}}}},"minElevation":1515.00061035156,"maxElevation":1757.25048828125},"lifts":{"byType":{"gondola":{"count":1,"lengthInKm":0.4203286202552854,"minElevation":1480.41467285156,"maxElevation":1541.74780273438,"combinedElevationChange":55.87353515624977},"chair_lift":{"count":1,"lengthInKm":0.6211856955141307,"minElevation":1517.0473632812502,"maxElevation":1755.25048828125,"combinedElevationChange":238.20312499999977},"platter":{"count":2,"lengthInKm":0.3429539521126189,"minElevation":1541.71752929688,"maxElevation":1574.97583007812,"combinedElevationChange":54.88403320310999}},"minElevation":1480.41467285156,"maxElevation":1755.25048828125},"maxElevation":1755.25048828125,"minElevation":1515.00061035156}},"type":"Feature","geometry":{"coordinates":[-0.15893142323129938,42.84794797397566],"type":"Point"}}
]}


My Answer:
php

// Read the GeoJSON file
$geojson = file_get_contents('countries.geojson');
$data = json_decode($geojson, true);

// Extract features based on country
$country = 'United States'; // Change this to the desired country

$filteredFeatures = array_filter($data['features'], function($feature) use ($country) {
return $feature['properties']['name'] === $country;
});

$filteredData = [
'type' => 'FeatureCollection',
'features' => array_values($filteredFeatures)
];

// Write the filtered GeoJSON data to a new file
$filteredGeojson = json_encode($filteredData, JSON_PRETTY_PRINT);
file_put_contents('filtered_countries.geojson', $filteredGeojson);

echo 'Filtered GeoJSON data has been written to filtered_countries.geojson file.';
?>


In this PHP script, we read a GeoJSON file containing country data, extract features based on a specified country, and write the filtered GeoJSON data to a new file. You can change the value of the `$country` variable to extract features for a different country.

Rate this post

3 of 5 based on 1858 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us