diff --git a/proxy.conf.json b/proxy.conf.json index cc19a811554725a5abb0feb49e8f7fae9c58e77d..f5d9180245f7dc46708cd6947716af80568f6e29 100644 --- a/proxy.conf.json +++ b/proxy.conf.json @@ -25,5 +25,11 @@ "secure": false, "changeOrigin": true, "logLevel": "info" + }, + "/geocoding/photon/api": { + "target": "https://download.data.grandlyon.com", + "secure": false, + "changeOrigin": true, + "logLevel": "info" } } diff --git a/src/app/home/home.component.ts b/src/app/home/home.component.ts index 265131817965101ff76329120865a78598f3d7d0..ce2ef32e617452c32f52628c318aa39d23aaae5f 100644 --- a/src/app/home/home.component.ts +++ b/src/app/home/home.component.ts @@ -22,6 +22,8 @@ export class HomeComponent implements OnInit { public geolocation = false; public currentLocation: GeoJson; public currentStructure: Structure; + public userLatitude: number; + public userLongitude: number; constructor(private structureService: StructureService, private geoJsonService: GeojsonService) {} ngOnInit(): void { @@ -33,37 +35,84 @@ export class HomeComponent implements OnInit { } public getStructures(filters: Filter[]): void { - this.structureService.getStructures(filters).subscribe((structures) => { - if (structures) { - Promise.all( - structures.map((structure) => { - if (this.geolocation) { - structure = this.getStructurePosition(structure); - } - return this.structureService.updateOpeningStructure(structure, DateTime.local()); - }) - ).then((structureList) => { - structureList = _.sortBy(structureList, ['distance']); - this.structures = structureList; + const queryString = _.find(filters, { name: 'query' }); + if (queryString) { + if (this.isLocationRequest(queryString.value)) { + this.getCoordByAddress(queryString.value).then((res) => { + this.currentLocation = res; + this.updateStructuresdistance( + this.structures, + this.currentLocation.geometry.getLon(), + this.currentLocation.geometry.getLat() + ); }); } else { - this.structures = null; + this.structureService.getStructures(filters).subscribe((structures) => { + if (structures) { + this.updateStructuresdistance(structures, this.userLongitude, this.userLatitude); + } else { + this.structures = null; + } + }); } + } else { + this.structureService.getStructures(filters).subscribe((structures) => { + if (structures) { + this.updateStructuresdistance(structures, this.userLongitude, this.userLatitude); + } else { + this.structures = null; + } + }); + } + } + + private updateStructuresdistance(structures: Structure[], lon: number, lat: number): void { + Promise.all( + structures.map((structure) => { + if (this.geolocation) { + structure = this.getStructurePosition(structure, lon, lat); + } + return this.structureService.updateOpeningStructure(structure, DateTime.local()); + }) + ).then((structureList) => { + structureList = _.sortBy(structureList, ['distance']); + this.structures = structureList; }); } + /** + * Retrive GeoJson for a given address + * @param address string + */ + private getCoordByAddress(address: string): Promise<GeoJson> { + return new Promise((resolve) => { + this.geoJsonService.getCoord(address, '', '69000').subscribe((res) => { + resolve(res); + }); + }); + } + + /** + * Check with a regex that an address is request + * @param value string + */ + private isLocationRequest(value: string): boolean { + const regex = /^\d+\s[A-z]+\s[A-z]+/g; + if (value.match(regex)) { + return true; + } + return false; + } + /** * Get structures positions and add marker corresponding to those positons on the map + * @param structure Structure + * @param lon number + * @param lat number */ - private getStructurePosition(structure: Structure): Structure { + private getStructurePosition(structure: Structure, lon: number, lat: number): Structure { structure.distance = parseInt( - this.geoJsonService.getDistance( - structure.getLon(), - structure.getLat(), - this.currentLocation.geometry.getLon(), - this.currentLocation.geometry.getLat(), - 'M' - ), + this.geoJsonService.getDistance(structure.getLat(), structure.getLon(), lat, lon, 'M'), 10 ); return structure; @@ -72,13 +121,18 @@ export class HomeComponent implements OnInit { public getLocation(): void { navigator.geolocation.getCurrentPosition((position) => { this.geolocation = true; - const longitude = position.coords.longitude; - const latitude = position.coords.latitude; - this.getAddress(longitude, latitude); + this.userLongitude = position.coords.longitude; + this.userLatitude = position.coords.latitude; + this.getAddress(position.coords.longitude, position.coords.latitude); this.getStructures(null); }); } + /** + * Get an address by coord + * @param longitude number + * @param latitude number + */ private getAddress(longitude: number, latitude: number): void { this.geoJsonService.getAddressByCoord(longitude, latitude).subscribe( (location) => { diff --git a/src/app/map/components/map.component.ts b/src/app/map/components/map.component.ts index 6bccd2d4488c9a939e0217e72d55d327145618d8..875be42c0cd0ab14df2bc9411a601106a8861bb4 100644 --- a/src/app/map/components/map.component.ts +++ b/src/app/map/components/map.component.ts @@ -111,8 +111,8 @@ export class MapComponent implements OnChanges { structureListe.forEach((structure: Structure) => { this.mapService .createMarker( - structure.getLon(), structure.getLat(), + structure.getLon(), MarkerType.structure, structure.id, this.buildToolTip(structure) diff --git a/src/app/map/models/addressGeometry.model.ts b/src/app/map/models/addressGeometry.model.ts index 71e1ce346c39c7c7c52861356026d8fc4bba74ed..b7612f2fc5fb6cd32127ca4e6779560bccd9f103 100644 --- a/src/app/map/models/addressGeometry.model.ts +++ b/src/app/map/models/addressGeometry.model.ts @@ -7,10 +7,10 @@ export class AddressGeometry { } public getLat(): number { - return this.coordinates[0]; + return this.coordinates[1]; } public getLon(): number { - return this.coordinates[1]; + return this.coordinates[0]; } } diff --git a/src/app/models/structure.model.ts b/src/app/models/structure.model.ts index bcc5b4a08fdd369fb3f25aefd33f9463338df9ef..597234630cd1e1fd8ca06e75b5c7eaa8a0af18d0 100644 --- a/src/app/models/structure.model.ts +++ b/src/app/models/structure.model.ts @@ -112,10 +112,10 @@ export class Structure { } public getLat(): number { - return this.coord[0]; + return this.coord[1]; } public getLon(): number { - return this.coord[1]; + return this.coord[0]; } } diff --git a/src/app/services/geojson.service.ts b/src/app/services/geojson.service.ts index befafee6c17fcf7131e2642cf957f0596c45e952..5f15ae6d84f1e101b7def155dde13eb0b2035609 100644 --- a/src/app/services/geojson.service.ts +++ b/src/app/services/geojson.service.ts @@ -43,6 +43,16 @@ export class GeojsonService { .pipe(map((data: { features: any[] }) => _.map(data.features, this.parseToGeoJson))); } + /** + * Get GeoLocation with an address + * @param address Address + */ + public getCoord(numero: string, address: string, zipcode: string): Observable<GeoJson> { + return this.http + .get('/geocoding/photon/api' + '?q=' + numero + ' ' + address + ' ' + zipcode) + .pipe(map((data: { features: any[]; type: string }) => new GeoJson(data.features[0]))); + } + // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: // ::: ::: // ::: This routine calculates the distance between two points (given the :::