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 // https://www.openstreetmap.org/relation/123 => ['R', 123]
18 export function identifyLinkInQuery(query) {
19 if (!query) return undefined;
20 const m = query.match(/\/(relation|way|node)\/(\d+)/) || query.match(/^([nwr])(\d+)$/i);
21 if (!m) return undefined;
22 return [m[1][0].toUpperCase(), Number(m[2])];
25 export function osmLink(aPlace) {
26 if (!aPlace.osm_type) return '';
27 var sOSMType = formatOSMType(aPlace.osm_type, false);
28 if (!sOSMType) return '';
30 return '<a href="https://www.openstreetmap.org/' + sOSMType + '/' + aPlace.osm_id + '">' + sOSMType + ' ' + aPlace.osm_id + '</a>';
33 export function formatLabel(aPlace) {
34 if (aPlace.label) return aPlace.label;
36 function capitalize(s) {
37 return s && s[0].toUpperCase() + s.slice(1);
40 if (aPlace.type && aPlace.type === 'yes' && aPlace.category) {
41 return capitalize(aPlace.category.replace(/_/g, ' '));
44 return capitalize(aPlace.type.replace(/_/g, ' '));
49 /* en:London_Borough_of_Redbridge => https://en.wikipedia.org/wiki/London_Borough_of_Redbridge */
50 export function wikipediaLink(aPlace) {
51 if (!aPlace.calculated_wikipedia) return '';
53 var parts = aPlace.calculated_wikipedia.split(':', 2);
55 var sTitle = escapeHtml(aPlace.calculated_wikipedia);
56 var sLanguage = escapeHtml(parts[0]);
57 var sArticle = escapeHtml(parts[1]);
59 return '<a href="https://' + sLanguage + '.wikipedia.org/wiki/' + sArticle + '" target="_blank">' + sTitle + '</a>';
62 export function coverageType(aPlace) {
63 return (aPlace.isarea ? 'Polygon' : 'Point');
66 export function isAdminBoundary(aPlace) {
67 return aPlace.category === 'boundary' && aPlace.type === 'administrative';
70 export function formatAddressRank(iRank) {
71 if (iRank < 4) return 'other';
72 if (iRank < 6) return 'country';
73 if (iRank < 8) return 'region';
74 if (iRank < 10) return 'state';
75 if (iRank < 12) return 'state district';
76 if (iRank < 14) return 'county';
77 if (iRank < 16) return 'municipality';
78 if (iRank < 18) return 'city / town / village';
79 if (iRank < 20) return 'city / village district';
80 if (iRank < 22) return 'suburb / hamlet';
81 if (iRank < 24) return 'neighbourhood';
82 if (iRank < 26) return 'city block / square';
83 if (iRank === 26) return 'major street';
84 if (iRank === 27) return 'minory street / path';
85 if (iRank <= 30) return 'house / building';
89 export function formatPlaceType(aPlace) {
90 var sOut = aPlace.class + ':' + aPlace.type;
91 if (aPlace.type && aPlace.type === 'administrative' && aPlace.place_type) {
92 sOut = sOut + ' (' + aPlace.place_type + ')';
94 return escapeHtml(sOut);
97 // Any over 15 are invalid data in OSM anyway
98 export function formatAdminLevel(iLevel) {
99 return (iLevel && iLevel < 15 ? iLevel : '');
102 export function formatDistance(fDistance, bInMeters) {
104 if (fDistance < 1) return '0';
105 var sFormatted = (fDistance >= 1000)
106 ? Math.round(fDistance / 1000, 1) + ' km'
107 : Math.round(fDistance, 0) + ' m';
109 return '<abbr class="distance" title="' + fDistance + ' meters">~' + sFormatted + '</abbr>';
112 // spheric distance, http://postgis.net/docs/ST_Distance_Spheroid.html
113 if (fDistance === 0) return '0';
115 return '<abbr class="distance" title="spheric distance ' + fDistance + '">~'
116 + (Math.round(fDistance * 1000, 4) / 1000)
120 // mark partial tokens (those starting with a space) with a star for readability
121 export function formatKeywordToken(sToken) {
122 return (sToken[0] === ' ' ? '*' : '') + escapeHtml(sToken);
125 export function zoomLevels() {
126 const aZoomLevels = [
127 /* 0 */ 'Continent / Sea',
139 /* 12 */ 'Town / Borough',
140 /* 13 */ 'Village / Suburb',
141 /* 14 */ 'Neighbourhood',
143 /* 16 */ 'Major Street',
144 /* 17 */ 'Minor Street',