Skip to content

Commit

Permalink
Merge pull request #48 from camptocamp/Fix_Geolocation_Search
Browse files Browse the repository at this point in the history
Fix geolocation Search - use swisstopo API
  • Loading branch information
marionb authored Nov 13, 2024
2 parents a3b9667 + 8f14c55 commit 37eb1e0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
51 changes: 46 additions & 5 deletions src/app/_services/map.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,12 @@ export class MapService {
return of([]);
}
const url = new URL(urlText);
url.searchParams.append('partitionlimit', '10');
url.searchParams.append('query', inputText);
url.searchParams.append('searchText', inputText);
url.searchParams.append('limit', '5'); // TODO find a good limit for this
url.searchParams.append('geometryFormat', 'geojson');
url.searchParams.append('type', 'locations');
url.searchParams.append('sr', '2056');
url.searchParams.append('origins','district,gg25,parcel,address');
return this.httpClient.get(url.toString()).pipe(
map((featureCollectionData) => {
const featureCollection = this.geoJsonFormatter.readFeatures(featureCollectionData);
Expand All @@ -303,6 +307,32 @@ export class MapService {
);
}

public stripHtmlTags(html: string): string {
const div = document.createElement('div');
div.innerHTML = html;
return div.textContent || div.innerText || '';
}

public createPolygonFromBBOX(bboxString: string): Polygon {
const coords = bboxString
.replace('BOX(', '')
.replace(')', '')
.split(',')
.map(coord => coord.trim().split(' ').map(Number));

const [minX, minY] = coords[0];
const [maxX, maxY] = coords[1];
const polygonCoords = [
[minX, minY],
[maxX, minY],
[maxX, maxY],
[minX, maxY],
[minX, minY]
];

return new Polygon([polygonCoords]);
}

/**
* Sets two geometries on the map based on the feature returned by de geocoder:
* - The extent as an order perimeter
Expand All @@ -323,9 +353,19 @@ export class MapService {
let poly: Polygon;
const geometry = feature.getGeometry();
if (geometry instanceof Point) {
const text = boundingExtent([geometry.getCoordinates()]);
const bv = 50;
poly = fromExtent(buffer(text, bv));
// TODO if the BBOX is just a point
const bboxstring = feature.get('geom_st_box2d');

poly = this.createPolygonFromBBOX(bboxstring);
feature.setGeometry(poly);

this.drawingSource.addFeature(feature);
this.modifyInteraction.setActive(true);

this.map.getView().fit(poly, {
padding: [75, 75, 75, 75]
});

} else {
const originalExtent = feature.getGeometry()?.getExtent();
if (originalExtent) {
Expand Down Expand Up @@ -353,6 +393,7 @@ export class MapService {
return;
}
const EPSG = this.configService.config.epsg || 'EPSG2056';
// TODO is this correct or is this cauing the projection shift -> check this
proj4.defs(EPSG,
'+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333'
+ ' +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel '
Expand Down
2 changes: 1 addition & 1 deletion src/app/welcome/map/map.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<button
i18n-matTooltip matTooltip="Effacer"
i18n-aria-label aria-label="Effacer"
mat-button color="accent" *ngIf="shouldDisplayClearButton" mat-icon-button
mat-icon-button color="accent" *ngIf="shouldDisplayClearButton"
(click)="searchCtrl?.setValue('');shouldDisplayClearButton = false;">
<mat-icon>close</mat-icon>
</button>
Expand Down
42 changes: 11 additions & 31 deletions src/app/welcome/map/map.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,14 @@ import Feature from 'ol/Feature';
import {MatDialog} from '@angular/material/dialog';
import {ManualentryComponent} from './manualentry/manualentry.component';

export const nameOfCategoryForGeocoder: { [prop: string]: string; } = {
neophytes: 'Plantes invasives',
search_satac: 'N° SATAC',
search_entree_sortie: 'Entrée/sortie autoroute',
rt16_giratoires: 'Giratoires',
batiments_ofs: 'Bâtiments regBL et n° egid',
axe_mistra: 'Routes et axes',
search_arrets_tp: 'Arrêts transports publics',
ImmeublesCantonHistorique: 'Biens-fonds historiques',
point_interet: 'Points d\'intérêt',
axe_rue: 'Axes et rues',
nom_local_lieu_dit: 'Noms locaux et lieux-dits',
search_cours_eau: 'Cours d\'eau',
ImmeublesCanton: 'Biens-fonds',
search_fo_administrations: 'Administrations forestières',
search_uap_publique: 'Unité d\'aménagement publique',
adresses_sitn: 'Adresses',
recenter_to: 'Recentrer sur',
localite: 'Localité',
search_fo09: 'Secours en forêt',
search_conc_hydr: 'Concessions hydrauliques',
communes: 'Communes',
cadastres: 'Cadastres',
export const nameOfCategoryForGeocoder: { [prop: string]: string; } = { // TODO this should be translated
zipcode: 'Ortschaftenverzeichnis PLZ',
gg25: 'Gemeinden',
district: 'Bezirke',
kantone: 'Kantone',
gazetteer: 'OEV Haltestellen',
address: 'Adressen',
parcel: 'Parzellen',
};

@Component({
Expand Down Expand Up @@ -110,25 +95,20 @@ export class MapComponent implements OnInit {
this.geocoderGroupOptions = [];

for (const feature of features) {
const categoryId = feature.get('layer_name');
if (this.configService.config?.geocoderLayers) {
if (this.configService.config.geocoderLayers.indexOf(categoryId) < 0) {
continue;
}
}
const categoryId = feature.get('origin') || feature.get('origin') !== '' ? feature.get('origin') : 'Allgemein'; // TODO add to translation Allgemein

let currentCategory = this.geocoderGroupOptions.find(x => x.id === categoryId);
if (currentCategory) {
currentCategory.items.push({
label: feature.get('label'),
label: this.mapService.stripHtmlTags(feature.get('label')),
feature
});
} else {
currentCategory = {
id: categoryId,
label: nameOfCategoryForGeocoder[categoryId],
items: [{
label: feature.get('label'),
label: this.mapService.stripHtmlTags(feature.get('label')),
feature
}]
};
Expand Down
2 changes: 1 addition & 1 deletion src/assets/configs/config.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apiUrl": "${API_BASE_URL}/${API_ROOTURL}",
"mediaUrl": "${MEDIA_URL}",
"baseMapUrl": "https://wmts.geo.admin.ch/1.0.0/{layer}/{style}/current/{TileMatrixSet}/{TileMatrix}/{TileCol}/{TileRow}",
"geocoderUrl": "https://nominatim.openstreetmap.org/search",
"geocoderUrl": "https://api3.geo.admin.ch/rest/services/api/SearchServer",
"geocoderLayers": [
"adresses_sitn", "nom_local_lieu_dit", "search_cours_eau",
"batiments_ofs", "axe_rue", "ImmeublesCanton", "communes", "cadastres",
Expand Down

0 comments on commit 37eb1e0

Please sign in to comment.