import requests import json import argparse parser = argparse.ArgumentParser() parser.add_argument("--insee_code", type=str, required=True) args = parser.parse_args() # Création de la structure GeoJSON geojson = { "type": "FeatureCollection", "features": [] } # URL initiale url = f"https://rnb-api.beta.gouv.fr/api/alpha/buildings/?insee_code={args.insee_code}" total_buildings = 0 # Boucle pour récupérer toutes les pages while url: print(f"Récupération de {url}") response = requests.get(url) data = response.json() # Conversion des résultats en features GeoJSON for building in data['results']: feature = { "type": "Feature", "geometry": building['shape'], # La forme du bâtiment est déjà en format GeoJSON "properties": { "rnb_id": building['rnb_id'], "status": building['status'], "addresses": building['addresses'], "ext_ids": building['ext_ids'], "is_active": building['is_active'] } } geojson['features'].append(feature) total_buildings += len(data['results']) print(f"Nombre de bâtiments traités : {total_buildings}") # Préparation de l'URL suivante url = data.get('next') # Affichage du nombre total de bâtiments print(f"\nNombre total de bâtiments extraits : {total_buildings}") # Sauvegarde du GeoJSON output_file = f'buildings_code_insee_{args.insee_code}.geojson' with open(output_file, 'w', encoding='utf-8') as f: json.dump(geojson, f, ensure_ascii=False, indent=2) print(f"\nFichier sauvegardé : {output_file}")