]> git.openstreetmap.org Git - nominatim-ui.git/blob - src/lib/helpers.js
dfe4cedc760229c65a068030287539d442eb9aeb
[nominatim-ui.git] / src / lib / helpers.js
1 import escapeHtml from 'escape-html';
2
3 export function formatOSMType(sType, bExcludeExternal) {
4   if (sType === 'N') return 'node';
5   if (sType === 'W') return 'way';
6   if (sType === 'R') return 'relation';
7
8   if (!bExcludeExternal) return '';
9
10   if (sType === 'T') return 'way';
11   if (sType === 'I') return 'way';
12
13   return '';
14 }
15
16 export function osmLink(aPlace) {
17   if (!aPlace.osm_type) return '';
18   var sOSMType = formatOSMType(aPlace.osm_type, false);
19   if (!sOSMType) return '';
20
21   return '<a href="https://www.openstreetmap.org/' + sOSMType + '/' + aPlace.osm_id + '">' + sOSMType + ' ' + aPlace.osm_id + '</a>';
22 }
23
24 export function formatLabel(aPlace) {
25   if (aPlace.label) return aPlace.label;
26
27   function capitalize(s) {
28     return s && s[0].toUpperCase() + s.slice(1);
29   }
30
31   if (aPlace.type && aPlace.type === 'yes' && aPlace.category) {
32     return capitalize(aPlace.category.replace(/_/g, ' '));
33   }
34   if (aPlace.type) {
35     return capitalize(aPlace.type.replace(/_/g, ' '));
36   }
37   return '';
38 }
39
40 /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */
41 export function wikipediaLink(aPlace) {
42   if (!aPlace.calculated_wikipedia) return '';
43
44   var parts = aPlace.calculated_wikipedia.split(':', 2);
45
46   var sTitle = escapeHtml(aPlace.calculated_wikipedia);
47   var sLanguage = escapeHtml(parts[0]);
48   var sArticle = escapeHtml(parts[1]);
49
50   return '<a href="https://' + sLanguage + '.wikipedia.org/wiki/' + sArticle + '" target="_blank">' + sTitle + '</a>';
51 }
52
53 export function coverageType(aPlace) {
54   return (aPlace.isarea ? 'Polygon' : 'Point');
55 }
56
57 export function isAdminBoundary(aPlace) {
58   return aPlace.category === 'boundary' && aPlace.type === 'administrative';
59 }
60
61 export function formatAddressRank(iRank) {
62   if (iRank < 4) return 'other';
63   if (iRank < 6) return 'country';
64   if (iRank < 8) return 'region';
65   if (iRank < 10) return 'state';
66   if (iRank < 12) return 'state district';
67   if (iRank < 14) return 'county';
68   if (iRank < 16) return 'municipality';
69   if (iRank < 18) return 'city / town / village';
70   if (iRank < 20) return 'city / village district';
71   if (iRank < 22) return 'suburb / hamlet';
72   if (iRank < 24) return 'neighbourhood';
73   if (iRank < 26) return 'city block / square';
74   if (iRank === 26) return 'major street';
75   if (iRank === 27) return 'minory street / path';
76   if (iRank <= 30) return 'house / building';
77   return 'other';
78 }
79
80 export function formatPlaceType(aPlace) {
81   var sOut = aPlace.class + ':' + aPlace.type;
82   if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) {
83     sOut = sOut + ' (' + aPlace.place_type + ')';
84   }
85   return escapeHtml(sOut);
86 }
87
88 // Any over 15 are invalid data in OSM anyway
89 export function formatAdminLevel(iLevel) {
90   return (iLevel && iLevel < 15 ? iLevel : '');
91 }
92
93 export function formatDistance(fDistance, bInMeters) {
94   if (bInMeters) {
95     if (fDistance < 1) return '0';
96     var sFormatted = (fDistance >= 1000)
97       ? Math.round(fDistance / 1000, 1) + ' km'
98       : Math.round(fDistance, 0) + ' m';
99
100     return '<abbr class="distance" title="' + fDistance + ' meters">~' + sFormatted + '</abbr>';
101   }
102
103   // spheric distance, http://postgis.net/docs/ST_Distance_Spheroid.html
104   if (fDistance === 0) return '0';
105
106   return '<abbr class="distance" title="spheric distance ' + fDistance + '">~'
107       + (Math.round(fDistance * 1000, 4) / 1000)
108       + '</abbr>';
109 }
110
111 // mark partial tokens (those starting with a space) with a star for readability
112 export function formatKeywordToken(sToken) {
113   return (sToken[0] === ' ' ? '*' : '') + escapeHtml(sToken);
114 }
115
116 export function zoomLevels() {
117   const aZoomLevels = [
118     /*  0 */ 'Continent / Sea',
119     /*  1 */ '',
120     /*  2 */ '',
121     /*  3 */ 'Country',
122     /*  4 */ '',
123     /*  5 */ 'State',
124     /*  6 */ 'Region',
125     /*  7 */ '',
126     /*  8 */ 'County',
127     /*  9 */ '',
128     /* 10 */ 'City',
129     /* 11 */ '',
130     /* 12 */ 'Town / Village',
131     /* 13 */ '',
132     /* 14 */ 'Suburb',
133     /* 15 */ '',
134     /* 16 */ 'Street',
135     /* 17 */ '',
136     /* 18 */ 'Building',
137     /* 19 */ '',
138     /* 20 */ '',
139     /* 21 */ ''
140   ];
141   return aZoomLevels;
142 }