From afdc6b7b33c5d3bd62dbfe04de2648d062a113a8 Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Tue, 3 Jun 2025 00:03:53 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20ImportService=20to=20retrie?= =?UTF-8?q?ve=20and=20validate=20data=20from=20the=20Geoapify=20API,=20as?= =?UTF-8?q?=20well=20as=20convert=20data=20into=20POI=20objects.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/services/import.service.ts | 98 ++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/app/services/import.service.ts diff --git a/src/app/services/import.service.ts b/src/app/services/import.service.ts new file mode 100644 index 0000000..12a14eb --- /dev/null +++ b/src/app/services/import.service.ts @@ -0,0 +1,98 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { Observable, throwError } from 'rxjs'; +import { catchError, map } from 'rxjs/operators'; +import { POI } from '../model/POI'; + +export interface GeoapifyFeature { + type: string; + properties: { + name: string; + country: string; + city: string; + street: string; + housenumber?: string; + postcode: string; + lon: number; + lat: number; + formatted: string; + categories: string[]; + website?: string; + contact?: { + phone?: string; + email?: string; + }; + opening_hours?: string; + [key: string]: any; + }; + geometry: { + type: string; + coordinates: [number, number]; + }; +} + +export interface GeoapifyResponse { + type: string; + features: GeoapifyFeature[]; +} + +@Injectable({ + providedIn: 'root' +}) +export class ImportService { + constructor(private http: HttpClient) {} + + fetchDataFromUrl(url: string): Observable { + return this.http.get(url).pipe( + catchError(error => { + console.error('Erreur lors de la récupération des données:', error); + return throwError(() => new Error('Impossible de récupérer les données depuis l\'URL fournie')); + }) + ); + } + + validateGeoapifyData(data: any): boolean { + return ( + data && + data.type === 'FeatureCollection' && + Array.isArray(data.features) && + data.features.length > 0 && + data.features.every((feature: any) => + feature.type === 'Feature' && + feature.properties && + feature.geometry + ) + ); + } + + convertToPOI(feature: GeoapifyFeature, fieldMappings: { [key: string]: string }): POI { + const poi: POI = { + id: '', + location: feature.geometry.coordinates, + complete_address: feature.properties.formatted || '', + title: feature.properties.name || '', + files: [], + description: '', + user_id: null, + is_poi: true, + date: undefined + }; + + // Appliquer les mappings de champs + Object.entries(fieldMappings).forEach(([targetField, sourceField]) => { + if (targetField === 'latitude') { + poi.location[0] = feature.properties[sourceField]; + } else if (targetField === 'longitude') { + poi.location[1] = feature.properties[sourceField]; + } else if (sourceField && feature.properties[sourceField]) { + (poi as any)[targetField] = feature.properties[sourceField]; + } + }); + + if (poi.description === '') { + poi.description = poi.title; + } + + return poi; + } +} \ No newline at end of file