185 lines
No EOL
6.2 KiB
Python
185 lines
No EOL
6.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Exemples d'utilisation de la fonctionnalité de sauvegarde du wrapper AtmoDataClient
|
|
"""
|
|
|
|
from atmo_data_wrapper import AtmoDataClient, AtmoDataException
|
|
from atmo_data_wrapper import AASQA_CODES, POLLUANTS
|
|
from datetime import datetime, timedelta
|
|
import os
|
|
|
|
def main():
|
|
"""Exemples de sauvegarde de données"""
|
|
print("=== Exemples de sauvegarde de données API Atmo ===\n")
|
|
|
|
# Initialisation du client
|
|
client = AtmoDataClient()
|
|
|
|
# Note: Pour ces exemples, nous utilisons des données factices
|
|
# car nous n'avons pas d'authentification réelle
|
|
|
|
# Données d'exemple au format GeoJSON (structure typique de l'API)
|
|
sample_geojson_data = {
|
|
"type": "FeatureCollection",
|
|
"features": [
|
|
{
|
|
"type": "Feature",
|
|
"geometry": {
|
|
"type": "Point",
|
|
"coordinates": [2.3522, 48.8566] # Paris
|
|
},
|
|
"properties": {
|
|
"code_zone": "75056",
|
|
"nom_zone": "Paris",
|
|
"aasqa": "11",
|
|
"nom_aasqa": "Airparif",
|
|
"date": "2024-07-07",
|
|
"code_qualificatif": "2",
|
|
"qualificatif": "Moyen",
|
|
"polluant_principal": "PM10",
|
|
"valeur": 45.2
|
|
}
|
|
},
|
|
{
|
|
"type": "Feature",
|
|
"geometry": {
|
|
"type": "Point",
|
|
"coordinates": [2.2945, 48.8584] # Boulogne
|
|
},
|
|
"properties": {
|
|
"code_zone": "92012",
|
|
"nom_zone": "Boulogne-Billancourt",
|
|
"aasqa": "11",
|
|
"nom_aasqa": "Airparif",
|
|
"date": "2024-07-07",
|
|
"code_qualificatif": "3",
|
|
"qualificatif": "Dégradé",
|
|
"polluant_principal": "NO2",
|
|
"valeur": 52.8
|
|
}
|
|
}
|
|
]
|
|
}
|
|
|
|
# Exemple 1: Sauvegarde en JSON
|
|
print("1. Sauvegarde en format JSON...")
|
|
try:
|
|
json_file = client.save_to_file(
|
|
data=sample_geojson_data,
|
|
filename="data/indices_atmo",
|
|
file_format="json"
|
|
)
|
|
print(f"✅ Fichier JSON sauvegardé: {json_file}")
|
|
except Exception as e:
|
|
print(f"❌ Erreur JSON: {e}")
|
|
|
|
# Exemple 2: Sauvegarde en GeoJSON
|
|
print("\n2. Sauvegarde en format GeoJSON...")
|
|
try:
|
|
geojson_file = client.save_to_file(
|
|
data=sample_geojson_data,
|
|
filename="data/indices_atmo_geo",
|
|
file_format="geojson"
|
|
)
|
|
print(f"✅ Fichier GeoJSON sauvegardé: {geojson_file}")
|
|
except Exception as e:
|
|
print(f"❌ Erreur GeoJSON: {e}")
|
|
|
|
# Exemple 3: Sauvegarde en CSV
|
|
print("\n3. Sauvegarde en format CSV...")
|
|
try:
|
|
csv_file = client.save_to_file(
|
|
data=sample_geojson_data,
|
|
filename="data/indices_atmo",
|
|
file_format="csv"
|
|
)
|
|
print(f"✅ Fichier CSV sauvegardé: {csv_file}")
|
|
except Exception as e:
|
|
print(f"❌ Erreur CSV: {e}")
|
|
|
|
# Exemple 4: Sauvegarde avec chemin complet
|
|
print("\n4. Sauvegarde avec chemin personnalisé...")
|
|
try:
|
|
custom_file = client.save_to_file(
|
|
data=sample_geojson_data,
|
|
filename="exports/qualite_air/paris_2024",
|
|
file_format="json"
|
|
)
|
|
print(f"✅ Fichier personnalisé sauvegardé: {custom_file}")
|
|
except Exception as e:
|
|
print(f"❌ Erreur sauvegarde personnalisée: {e}")
|
|
|
|
# Exemple 5: Gestion d'erreurs - format invalide
|
|
print("\n5. Test de validation - format invalide...")
|
|
try:
|
|
client.save_to_file(
|
|
data=sample_geojson_data,
|
|
filename="test",
|
|
file_format="xml" # Format non supporté
|
|
)
|
|
print("❌ Validation échouée - format invalide accepté")
|
|
except ValueError as e:
|
|
print(f"✅ Validation réussie: {e}")
|
|
|
|
# Exemple 6: Workflow complet avec récupération de données
|
|
print("\n6. Exemple de workflow complet (simulation)...")
|
|
try:
|
|
# Simulation d'un appel API réel
|
|
print(" - Récupération des indices ATMO (simulé)...")
|
|
# indices = client.get_indices_atmo(aasqa="11", date="2024-07-07")
|
|
|
|
# Utilisation des données d'exemple
|
|
print(" - Sauvegarde des données...")
|
|
workflow_file = client.save_to_file(
|
|
data=sample_geojson_data,
|
|
filename=f"exports/daily/indices_atmo_{datetime.now().strftime('%Y%m%d')}",
|
|
file_format="csv"
|
|
)
|
|
print(f"✅ Workflow terminé: {workflow_file}")
|
|
|
|
# Affichage des informations sur le fichier
|
|
if os.path.exists(workflow_file):
|
|
size = os.path.getsize(workflow_file)
|
|
print(f" - Taille du fichier: {size} bytes")
|
|
print(f" - Nombre de lignes: {len(sample_geojson_data['features']) + 1}") # +1 pour l'en-tête
|
|
|
|
except Exception as e:
|
|
print(f"❌ Erreur workflow: {e}")
|
|
|
|
# Exemple 7: Données sans géométrie (pour test CSV)
|
|
print("\n7. Test CSV sans coordonnées...")
|
|
sample_data_no_geom = {
|
|
"type": "FeatureCollection",
|
|
"features": [
|
|
{
|
|
"type": "Feature",
|
|
"geometry": None,
|
|
"properties": {
|
|
"code_zone": "75056",
|
|
"nom_zone": "Paris",
|
|
"date": "2024-07-07",
|
|
"indice_atmo": 3,
|
|
"qualificatif": "Dégradé"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
|
|
try:
|
|
no_geom_file = client.save_to_file(
|
|
data=sample_data_no_geom,
|
|
filename="data/indices_sans_coordonnees",
|
|
file_format="csv"
|
|
)
|
|
print(f"✅ CSV sans géométrie sauvegardé: {no_geom_file}")
|
|
except Exception as e:
|
|
print(f"❌ Erreur CSV sans géométrie: {e}")
|
|
|
|
print("\n=== Exemples terminés ===")
|
|
print("\nFichiers créés dans:")
|
|
print("- ./data/")
|
|
print("- ./exports/")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |