Added ImportService to retrieve and validate data from the Geoapify API, as well as convert data into POI objects.

master
Alix JEUDI--LEMOINE 5 days ago
parent 2f402710f0
commit afdc6b7b33

@ -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<GeoapifyResponse> {
return this.http.get<GeoapifyResponse>(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;
}
}
Loading…
Cancel
Save