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 !130
parents 09a41d33 c02c650e
Pipeline #22803 failed with stages
in 39 minutes and 21 seconds
......@@ -346,23 +346,57 @@ export class StructuresService {
*/
private getStructurePosition(structure: Structure): Promise<Structure> {
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) => {
const address = res.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);
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];
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 {
Logger.error(
`No structure found for: ${structure.address.numero} ${structure.address.street} ${structure.address.commune}`,
'StructureService'
);
}
}
});
},
(err) => {
Logger.error(`Request error: ${err.config.url}`, 'StructureService');
Logger.error(err);
reject(err);
}
);
});
......@@ -383,32 +417,56 @@ export class StructuresService {
*/
public async searchAddress(data: {
searchQuery: string;
}): Promise<AxiosResponse<{ 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`;
Logger.debug(`Search request: ${encodeURI(req)}`, 'StructureService');
return new Promise((resolve, reject) => {
this.httpService
.request({
url: encodeURI(req),
method: 'GET',
headers: { 'Content-Type': 'application/json' },
})
.subscribe(
(reply) => {
Logger.debug(`Search request response length : ${reply.data.features.length}`, 'StructureService');
reply.data.features = reply.data.features
.filter((doc) => doc.properties.postcode && doc.properties.postcode.match(depRegex))
.sort((a, b) => {
return b.properties.housenumber ? 1 : -1;
});
return resolve(reply.data);
},
(err) => {
Logger.error(`Search - Request error: ${err.config.url}`, 'StructureService');
Logger.error(err);
}
);
}): Promise<{ features: { geometry: {}; type: string; properties: {} }[] }> {
const reqBan = `https://download.data.grandlyon.com/geocoding/photon/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`;
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`;
const requestGroup = (url): Promise<{ features: { geometry: {}; type: string; properties: {} }[] }> =>
new Promise((resolve, reject) => {
Logger.debug(`Search request: ${encodeURI(url)}`, 'StructureService');
return this.httpService
.request({
url: encodeURI(url),
method: 'GET',
headers: { 'Content-Type': 'application/json' },
})
.subscribe(
(reply) => {
Logger.debug(`Search request response length : ${reply.data.features.length}`, 'StructureService');
reply.data.features = reply.data.features
.filter((doc) => doc.properties.postcode && doc.properties.postcode.match(depRegex))
.sort((a, b) => {
return b.properties.housenumber ? 1 : -1;
});
return resolve(reply.data);
},
(err) => {
Logger.error(`Search - Request error: ${err.config.url}`, 'StructureService');
Logger.error(err);
}
);
});
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 {
);
}
public getCoord(numero: string, address: string, zipcode: string): Observable<AxiosResponse<any>> {
const req = 'https://download.data.grandlyon.com/geocoding/photon/api?q=' + numero + ' ' + address + ' ' + zipcode;
Logger.log(`getCoord - Request : ${req}`, 'StructureService');
public getCoord(numero: string, address: string, zipcode: string, scope: string): Observable<AxiosResponse<any>> {
const req =
`https://download.data.grandlyon.com/geocoding/${scope}/api?q=` + numero + ' ' + address + ' ' + zipcode;
Logger.debug('Print getCoord' + req);
return this.httpService.get(encodeURI(req));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment