1 import escapeHtml from 'escape-html';
3 export function formatOSMType(sType, bExcludeExternal) {
4 if (sType === 'N') return 'node';
5 if (sType === 'W') return 'way';
6 if (sType === 'R') return 'relation';
8 if (!bExcludeExternal) return '';
10 if (sType === 'T') return 'way';
11 if (sType === 'I') return 'way';
16 export function osmLink(aPlace) {
17 if (!aPlace.osm_type) return '';
18 var sOSMType = formatOSMType(aPlace.osm_type, false);
19 if (!sOSMType) return '';
21 return '<a href="https://www.openstreetmap.org/' + sOSMType + '/' + aPlace.osm_id + '">' + sOSMType + ' ' + aPlace.osm_id + '</a>';
24 export function formatLabel(aPlace) {
25 if (aPlace.label) return aPlace.label;
27 function capitalize(s) {
28 return s && s[0].toUpperCase() + s.slice(1);
31 if (aPlace.type && aPlace.type === 'yes' && aPlace.category) {
32 return capitalize(aPlace.category.replace(/_/g, ' '));
35 return capitalize(aPlace.type.replace(/_/g, ' '));
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 '';
44 var parts = aPlace.calculated_wikipedia.split(':', 2);
46 var sTitle = escapeHtml(aPlace.calculated_wikipedia);
47 var sLanguage = escapeHtml(parts[0]);
48 var sArticle = escapeHtml(parts[1]);
50 return '<a href="https://' + sLanguage + '.wikipedia.org/wiki/' + sArticle + '" target="_blank">' + sTitle + '</a>';
53 export function coverageType(aPlace) {
54 return (aPlace.isarea ? 'Polygon' : 'Point');
57 export function isAdminBoundary(aPlace) {
58 return aPlace.category === 'boundary' && aPlace.type === 'administrative';
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';
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 + ')';
85 return escapeHtml(sOut);
88 // Any over 15 are invalid data in OSM anyway
89 export function formatAdminLevel(iLevel) {
90 return (iLevel && iLevel < 15 ? iLevel : '');
93 export function formatDistance(fDistance, 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';
100 return '<abbr class="distance" title="' + fDistance + ' meters">~' + sFormatted + '</abbr>';
103 // spheric distance, http://postgis.net/docs/ST_Distance_Spheroid.html
104 if (fDistance === 0) return '0';
106 return '<abbr class="distance" title="spheric distance ' + fDistance + '">~'
107 + (Math.round(fDistance * 1000, 4) / 1000)
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);
116 export function zoomLevels() {
117 const aZoomLevels = [
118 /* 0 */ 'Continent / Sea',
130 /* 12 */ 'Town / Village',