base de données okay

This commit is contained in:
Fred Tempez 2023-07-14 11:32:33 +02:00
parent 1f7dfe0f92
commit 85b6c29b95
3 changed files with 22 additions and 19 deletions

1
cities.json Normal file
View File

@ -0,0 +1 @@
{"Annezin":{"lat":"50.5415","lon":"2.6193"},"Auchy les Hesdin":{"lat":"50.3988228","lon":"2.1019554"},"Bavay":{"lat":"50.2978864","lon":"3.7949291"},"Bois Bernard":{"lat":"50.3815339","lon":"2.911883"},"Bourbourg":{"lat":"50.9466619","lon":"2.1973702"},"Busigny":{"lat":"50.0350792","lon":"3.4692426"},"Calais":{"lat":"50.9524769","lon":"1.8538446"},"Camblain Chatelain":{"lat":"50.4822875","lon":"2.4637444"},"Chereng":{"lat":"50.6111045","lon":"3.2022404"},"Colleret":{"lat":"50.2563884","lon":"4.075551"},"Comines":{"lat":"50.7653265","lon":"3.0066319"},"Coulogne":{"lat":"50.9257432","lon":"1.8840904"},"Cysoing":{"lat":"50.5677767","lon":"3.215489"},"Ecquedecques":{"lat":"50.5621192","lon":"2.4487029"},"Estaires":{"lat":"50.6440257","lon":"2.7226506"},"Ferques":{"lat":"50.8310488","lon":"1.7677496"},"Feuchy":{"lat":"50.2944503","lon":"2.8449543"},"Fressain":{"lat":"50.2851168","lon":"3.1921922"},"Grand Fort Philippe":{"lat":"51.0014456","lon":"2.1045965"},"Houlle":{"lat":"50.7965913","lon":"2.1742979"},"La Herli\u00e8re":{"lat":"50.2077561","lon":"2.5593211"},"Labourse":{"lat":"50.4972978","lon":"2.679865"},"Laires":{"lat":"50.539137","lon":"2.2553139"},"Lapugnoy":{"lat":"50.5204","lon":"2.5342"},"Lecluse":{"lat":"50.2765792","lon":"3.0388851"},"Lens":{"lat":"50.4291723","lon":"2.8319805"},"Lewarde":{"lat":"50.340485","lon":"3.1677313"},"Loos":{"lat":"50.6092082","lon":"3.0162523"},"Malo les Bains":{"lat":"51.0466792","lon":"2.3984694"},"Marcq en Baroeul":{"lat":"50.6767018","lon":"3.1043032"},"Maubeuge":{"lat":"50.2796417","lon":"3.9674127"},"Mencas":{"lat":"50.5598051","lon":"2.1384847"},"Montreuil":{"lat":"50.4638918","lon":"1.7631125"},"Mortagne du Nord":{"lat":"50.5036803","lon":"3.4552401"},"Mouvaux":{"lat":"50.7029","lon":"3.13689"},"Noeux Les Mines":{"lat":"50.4808898","lon":"2.6620888"},"Noyelles sous Lens":{"lat":"50.4307524","lon":"2.8760012"},"Phalempin":{"lat":"50.5172168","lon":"3.0180377"},"Provin":{"lat":"50.5151406","lon":"2.9133145"},"Quarouble":{"lat":"50.3863821","lon":"3.6244948"},"Raches":{"lat":"50.4149446","lon":"3.136211"},"Rinxent":{"lat":"50.806377999999995","lon":"1.7409815771484327"},"Rumilly en Cambresis":{"lat":"50.1287091","lon":"3.2188323"},"St Marie Kerque":{"lat":"50.9005923","lon":"2.1374585"},"Thun Saint Martin":{"lat":"50.2213563","lon":"3.3053776"},"Vendin le Vieil":{"lat":"50.4756482","lon":"2.8671553"},"Achiet le Grand":{"lat":"50.1320114","lon":"2.7843609"},"Annequin":{"lat":"50.5091797","lon":"2.72736"},"Bailleul":{"lat":"50.7396668","lon":"2.7349286"},"Bienvillers au Bois":{"lat":"50.1748294","lon":"2.6202668"},"Billy Berclau":{"lat":"50.5189176","lon":"2.8674884"},"Calonne Ricouart":{"lat":"50.4869689","lon":"2.481762"},"Cambrin":{"lat":"50.5114029","lon":"2.7403226"},"Dentergem":{"lat":"50.96616075","lon":"3.401986033802248"},"Douai":{"lat":"50.3675677","lon":"3.0804641"},"Ebblinghem":{"lat":"50.7333102","lon":"2.4094016"},"Estrees":{"lat":"49.9666304","lon":"3.2873583"},"Ferriere La Grande":{"lat":"50.2560051","lon":"3.9942662"},"Grenay":{"lat":"50.4482588","lon":"2.7396733"},"Lomme":{"lat":"50.6457022","lon":"2.9870927"},"Merlimont":{"lat":"50.4553061","lon":"1.614809"},"Rexpoede":{"lat":"50.939354","lon":"2.5388577"}}

View File

@ -82,24 +82,20 @@ function geocode($cities)
// Tableau pour stocker les villes et leurs coordonnées // Tableau pour stocker les villes et leurs coordonnées
$citiesData = array(); $citiesData = array();
// Charger les données à partir du fichier JSON s'il existe // Charger les données existantes depuis la base JSON
if (file_exists('cities_data.json')) { $jsonData = file_get_contents('cities.json');
$citiesData = json_decode(file_get_contents('cities_data.json'), true); $existingData = json_decode($jsonData, true);
}
// Parcourir les villes et géocoder leurs coordonnées // Parcourir les villes et géocoder leurs coordonnées
foreach ($cities as $city) { foreach ($cities as $city) {
$ville = $city['name']; $ville = $city['name'];
$href = $city['href']; $href = $city['href'];
// Vérifier si les coordonnées sont déjà présentes dans le tableau // Vérifier si les coordonnées de la ville existent déjà dans la base
$existingCity = array_filter($citiesData, function ($data) use ($ville) { if (isset($existingData[$ville])) {
return $data['name'] === $ville;
});
if (!empty($existingCity)) {
// Utiliser les coordonnées existantes // Utiliser les coordonnées existantes
$citiesData[] = reset($existingCity); $latitude = $existingData[$ville]['lat'];
$longitude = $existingData[$ville]['lon'];
} else { } else {
// Requête à l'API Nominatim // Requête à l'API Nominatim
$options = array( $options = array(
@ -118,20 +114,25 @@ function geocode($cities)
$latitude = $data[0]['lat']; $latitude = $data[0]['lat'];
$longitude = $data[0]['lon']; $longitude = $data[0]['lon'];
// Stocker les coordonnées dans la base JSON
$existingData[$ville] = array(
'lat' => $latitude,
'lon' => $longitude,
);
}
}
// Ajouter la ville et ses coordonnées au tableau // Ajouter la ville et ses coordonnées au tableau
$cityData = array( $citiesData[] = array(
'name' => $ville, 'name' => $ville,
'lat' => $latitude, 'lat' => $latitude,
'lon' => $longitude, 'lon' => $longitude,
'href' => $href, 'href' => $href,
); );
$citiesData[] = $cityData; }
// Sauvegarder les données dans le fichier JSON // Enregistrer les données mises à jour dans la base JSON
file_put_contents('cities_data.json', json_encode($citiesData)); file_put_contents('cities.json', json_encode($existingData));
}
}
}
return $citiesData; return $citiesData;
} }

View File

@ -24,6 +24,7 @@
<option value="<?php echo $item['url']; ?>" <?php echo $selected; ?>><?php echo $item['node']; ?></option> <option value="<?php echo $item['url']; ?>" <?php echo $selected; ?>><?php echo $item['node']; ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
<!--<input type="submit"value="Localiser sur une carte">--> <!--<input type="submit"value="Localiser sur une carte">-->
</form> </form>
</div> </div>