Skip to content
Snippets Groups Projects
Commit 5f840092 authored by Hugo NOUTS's avatar Hugo NOUTS
Browse files

Merge branch 'fix/issue-8-structures-adresse' into 'dev'

fixed autocomplete checks ban & bal photon

See merge request web-et-numerique/pamn_plateforme-des-acteurs-de-la-mediation-numerique/pamn_server!130
parents 09a41d33 c02c650e
Branches
Tags
3 merge requests!1371.16 into 2.0,!134Dev,!130fixed autocomplete checks ban & bal photon
...@@ -346,8 +346,33 @@ export class StructuresService { ...@@ -346,8 +346,33 @@ export class StructuresService {
*/ */
private getStructurePosition(structure: Structure): Promise<Structure> { private getStructurePosition(structure: Structure): Promise<Structure> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.getCoord(structure.address.numero, structure.address.street, structure.address.commune).subscribe( this.getCoord(structure.address.numero, structure.address.street, structure.address.commune, 'photon').subscribe(
(res) => { (res) => {
this.getCoord(
structure.address.numero,
structure.address.street,
structure.address.commune,
'photon-bal'
).subscribe((resbal) => {
// check if photon-bal is more precise than photon ban
if (resbal.data.features.length > 0) {
resbal.data.features = resbal.data.features.sort((a, b) => {
return b.properties.housenumber ? 1 : -1;
});
const address = resbal.data.features[0];
if (address && address.geometry) {
structure.coord = address.geometry.coordinates;
} else {
Logger.error(
`No coord found for: ${structure.address.numero} ${structure.address.street} ${structure.address.commune}`,
'StructureService'
);
structure.coord = [];
}
resolve(structure);
// else pick structure from photonban if it exists or throw error
} else {
if (res.data.features.length > 0) {
const address = res.data.features[0]; const address = res.data.features[0];
if (address && address.geometry) { if (address && address.geometry) {
structure.coord = address.geometry.coordinates; structure.coord = address.geometry.coordinates;
...@@ -359,10 +384,19 @@ export class StructuresService { ...@@ -359,10 +384,19 @@ export class StructuresService {
structure.coord = []; structure.coord = [];
} }
resolve(structure); resolve(structure);
} else {
Logger.error(
`No structure found for: ${structure.address.numero} ${structure.address.street} ${structure.address.commune}`,
'StructureService'
);
}
}
});
}, },
(err) => { (err) => {
Logger.error(`Request error: ${err.config.url}`, 'StructureService'); Logger.error(`Request error: ${err.config.url}`, 'StructureService');
Logger.error(err); Logger.error(err);
reject(err);
} }
); );
}); });
...@@ -383,13 +417,17 @@ export class StructuresService { ...@@ -383,13 +417,17 @@ export class StructuresService {
*/ */
public async searchAddress(data: { public async searchAddress(data: {
searchQuery: string; searchQuery: string;
}): Promise<AxiosResponse<{ features: { geometry: {}; type: string; properties: {} }[] }>> { }): Promise<{ features: { geometry: {}; type: string; properties: {} }[] }> {
const req = `https://download.data.grandlyon.com/geocoding/photon/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`; const reqBan = `https://download.data.grandlyon.com/geocoding/photon/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`;
Logger.debug(`Search request: ${encodeURI(req)}`, 'StructureService');
return new Promise((resolve, reject) => { const reqBal = `https://download.data.grandlyon.com/geocoding/photon-bal/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`;
this.httpService
const requestGroup = (url): Promise<{ features: { geometry: {}; type: string; properties: {} }[] }> =>
new Promise((resolve, reject) => {
Logger.debug(`Search request: ${encodeURI(url)}`, 'StructureService');
return this.httpService
.request({ .request({
url: encodeURI(req), url: encodeURI(url),
method: 'GET', method: 'GET',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
}) })
...@@ -409,6 +447,26 @@ export class StructuresService { ...@@ -409,6 +447,26 @@ export class StructuresService {
} }
); );
}); });
const [reqBanRes, reqBalRes] = await Promise.all([requestGroup(reqBan), requestGroup(reqBal)]);
const mergedArray = [...reqBalRes['features'], ...reqBanRes['features']];
const duplicateFreeArray = _.unionWith(mergedArray, function (a, b) {
return (
// excludes structures from photon-BAN if they share postcode && street or name && housenumber
// checking for name and street existance asserts that we will not compare 2 undefined fields
a.properties.postcode === b.properties.postcode &&
((a.properties.name &&
(a.properties.name === b.properties.name || a.properties.name === b.properties.street)) ||
(a.properties.street &&
(a.properties.street === b.properties.name || a.properties.street === b.properties.street))) &&
(a.properties.housenumber === b.properties.housenumber ||
(!a.properties.housenumber && !b.properties.housenumber))
);
});
return {
features: duplicateFreeArray,
};
} }
/** /**
...@@ -447,9 +505,11 @@ export class StructuresService { ...@@ -447,9 +505,11 @@ export class StructuresService {
); );
} }
public getCoord(numero: string, address: string, zipcode: string): Observable<AxiosResponse<any>> { public getCoord(numero: string, address: string, zipcode: string, scope: string): Observable<AxiosResponse<any>> {
const req = 'https://download.data.grandlyon.com/geocoding/photon/api?q=' + numero + ' ' + address + ' ' + zipcode; const req =
Logger.log(`getCoord - Request : ${req}`, 'StructureService'); `https://download.data.grandlyon.com/geocoding/${scope}/api?q=` + numero + ' ' + address + ' ' + zipcode;
Logger.debug('Print getCoord' + req);
return this.httpService.get(encodeURI(req)); return this.httpService.get(encodeURI(req));
} }
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment