on master help sign in
[enh][oscar_template] loading map informations from overpass-api
commit4b75d41f864b9d127aa88c962b7ce849ee3b0b51 authorThomas Pointhuber <thomas.pointhuber@gmx.at> dateWed 19 Nov 2014, 14:59 (12 years ago) parenta9b4f45
4 files changed+120−4
searx/engines/openstreetmap.py+4 searx/static/oscar/img/loader.gif searx/static/oscar/js/scripts.js+83−1 searx/templates/oscar/result_templates/map.html+33−3
searx/engines/openstreetmap.py+4
@@ -39,6 +39,9 @@ def response(resp):
3939 url = result_base_url.format(osm_type=osm_type,
4040 osm_id=r['osm_id'])
4141
42+ osm = {'type':osm_type,
43+ 'id':r['osm_id']}
44+
4245 geojson = r.get('geojson')
4346
4447 # if no geojson is found and osm_type is a node, add geojson Point
@@ -82,6 +85,7 @@ def response(resp):
8285 'boundingbox': r['boundingbox'],
8386 'geojson': geojson,
8487 'address': address,
88+ 'osm': osm,
8589 'url': url})
8690
8791 # return results
searx/static/oscar/img/loader.gifnew file

Binary file not shown.

searx/static/oscar/js/scripts.js+83−1
@@ -68,6 +68,88 @@ $(document).ready(function(){
6868 source: searx.searchResults.ttAdapter()
6969 });
7070 }
71+
72+ $(".searx_overpass_request").on( "click", function( event ) {
73+ var overpass_url = "http://overpass-api.de/api/interpreter?data=";
74+ var query_start = overpass_url + "[out:json][timeout:25];(";
75+ var query_end = ");out meta;";
76+
77+ var osm_id = $(this).data('osm-id');
78+ var osm_type = $(this).data('osm-type');
79+ var result_table = $(this).data('result-table');
80+ var result_table_loadicon = "#" + $(this).data('result-table-loadicon');
81+
82+ // tags which can be ignored
83+ var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ]
84+
85+ if(osm_id && osm_type && result_table) {
86+ result_table = "#" + result_table;
87+ var query = null;
88+ switch(osm_type) {
89+ case 'node':
90+ query = query_start + "node(" + osm_id + ");" + query_end;
91+ break;
92+ case 'way':
93+ query = query_start + "way(" + osm_id + ");" + query_end;
94+ break;
95+ case 'relation':
96+ query = query_start + "relation(" + osm_id + ");" + query_end;
97+ break;
98+ default:
99+ break;
100+ }
101+ if(query) {
102+ //alert(query);
103+ var ajaxRequest = $.ajax( query )
104+ .done(function( html) {
105+ if(html && html['elements'] && html['elements'][0]) {
106+ var element = html['elements'][0];
107+ var newHtml = $(result_table).html();
108+ for (var row in element.tags) {
109+ if(element.tags["name"] == null || osm_ignore_tags.indexOf(row) == -1) {
110+ newHtml += "<tr><td>" + row + "</td><td>";
111+ switch(row) {
112+ case "phone":
113+ case "fax":
114+ newHtml += "<a href=\"tel:" + element.tags[row].replace(/ /g,'') + "\">" + element.tags[row] + "</a>";
115+ break;
116+ case "email":
117+ newHtml += "<a href=\"mailto:" + element.tags[row] + "\">" + element.tags[row] + "</a>";
118+ break;
119+ case "website":
120+ case "url":
121+ newHtml += "<a href=\"" + element.tags[row] + "\">" + element.tags[row] + "</a>";
122+ break;
123+ case "wikidata":
124+ newHtml += "<a href=\"https://www.wikidata.org/wiki/" + element.tags[row] + "\">" + element.tags[row] + "</a>";
125+ break;
126+ case "wikipedia":
127+ if(element.tags[row].indexOf(":") != -1) {
128+ newHtml += "<a href=\"https://" + element.tags[row].substring(0,element.tags[row].indexOf(":")) + ".wikipedia.org/wiki/"
129+ + element.tags[row].substring(element.tags[row].indexOf(":")+1) + "\">" + element.tags[row] + "</a>";
130+ break;
131+ }
132+ default:
133+ newHtml += element.tags[row];
134+ break;
135+ }
136+ newHtml += "</td></tr>";
137+ }
138+ }
139+ $(result_table).html(newHtml);
140+ $(result_table).removeClass('hidden');
141+ $(result_table_loadicon).addClass('hidden');
142+ }
143+ })
144+ .fail(function() {
145+ alert( "could not load " );
146+ })
147+ }
148+ }
149+
150+ // this event occour only once per element
151+ $( this ).off( event );
152+ });
71153
72154 $(".searx_init_map").on( "click", function( event ) {
73155 var leaflet_target = $(this).data('leaflet-target');
@@ -119,7 +201,7 @@ $(document).ready(function(){
119201 map.setView(new L.LatLng(map_lat, map_lon),8);
120202 }
121203
122− map.addLayer(osmMapnik);
204+ map.addLayer(osmMapquest);
123205
124206 var baseLayers = {
125207 "OSM Mapnik": osmMapnik,
searx/templates/oscar/result_templates/map.html+33−3
@@ -3,13 +3,25 @@
33<h4 class="result_header">{% if result['favicon'] %}<img width="32" height="32" class="favicon" src="static/{{ theme }}/img/icons/{{ result['favicon'] }}.png" /> {% endif %}<a href="{{ result.url }}">{{ result.title|safe }}</a></h4>
44
55{% if result.publishedDate %}<time class="text-muted" datetime="{{ result.publishedDate }}" pubdate>{{ result.publishedDate }}</time>{% endif %}
6+
67<small><a class="text-info" href="https://web.archive.org/web/{{ result.pretty_url }}">{{ icon('link') }} {{ _('cached') }}</a></small>
8+
79{% if (result.latitude and result.longitude) or result.boundingbox %}
810 <small> &bull; <a class="text-info btn-collapse collapsed searx_init_map cursor-pointer" data-toggle="collapse" data-target="#result-map-{{ index }}" data-leaflet-target="osm-map-{{ index }}" data-map-lon="{{ result.longitude }}" data-map-lat="{{ result.latitude }}" {% if result.boundingbox %}data-map-boundingbox='{{ result.boundingbox|tojson|safe }}'{% endif %} {% if result.geojson %}data-map-geojson='{{ result.geojson|tojson|safe }}'{% endif %} data-btn-text-collapsed="{{ _('show map') }}" data-btn-text-not-collapsed="{{ _('hide map') }}">{{ icon('globe') }} {{ _('show map') }}</a></small>
911{% endif %}
1012
13+{% if result.osm and (result.osm.type and result.osm.id) %}
14+ <small> &bull; <a class="text-info btn-collapse collapsed cursor-pointer searx_overpass_request" data-toggle="collapse" data-target="#result-overpass-{{ index }}" data-osm-type="{{ result.osm.type }}" data-osm-id="{{ result.osm.id }}" data-result-table="result-overpass-table-{{ index }}" data-result-table-loadicon="result-overpass-table-loading-{{ index }}" data-btn-text-collapsed="{{ _('show details') }}" data-btn-text-not-collapsed="{{ _('hide details') }}">{{ icon('map-marker') }} {{ _('show details') }}</a></small>
15+{% endif %}
16+
17+{# {% if (result.latitude and result.longitude) %}
18+ <small> &bull; <a class="text-info btn-collapse collapsed cursor-pointer" data-toggle="collapse" data-target="#result-geodata-{{ index }}" data-btn-text-collapsed="{{ _('show geodata') }}" data-btn-text-not-collapsed="{{ _('hide geodata') }}">{{ icon('map-marker') }} {{ _('show geodata') }}</a></small>
19+{% endif %} #}
20+
21+<div class="container-fluid">
22+
1123{% if result.address %}
12−<p class="result-content result-adress" itemscope itemtype="http://schema.org/PostalAddress">
24+<p class="row result-content result-adress col-xs-12 col-sm-5 col-md-4" itemscope itemtype="http://schema.org/PostalAddress">
1325 {% if result.address.name %}
1426 <strong itemprop="name">{{ result.address.name }}</strong><br/>
1527 {% endif %}
@@ -27,11 +39,29 @@
2739 {% if result.address.country %}
2840 <span itemprop="addressCountry">{{ result.address.country }}</span>
2941 {% endif %}
30−</p><div class="clearfix"></div>
42+</p>
3143{% endif %}
3244
33−{% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
45+{% if result.osm and (result.osm.type and result.osm.id) %}
46+ <div class="row result-content collapse col-xs-12 col-sm-7 col-md-8" id="result-overpass-{{ index }}">
47+ <div class="text-center" id="result-overpass-table-loading-{{ index }}"><img src="{{ url_for('static', filename='img/loader.gif') }}" alt="Loading ..."/></div>
48+ <table class="table table-striped table-condensed hidden" id="result-overpass-table-{{ index }}">
49+ <tr><th>key</th><th>value</th></tr>
50+ </table>
51+ </div>
52+{% endif %}
53+
54+{# {% if (result.latitude and result.longitude) %}
55+ <div class="row collapse col-xs-12 col-sm-5 col-md-4" id="result-geodata-{{ index }}">
56+ <strong>Longitude:</strong> {{ result.longitude }} <br/>
57+ <strong>Latitude:</strong> {{ result.latitude }}
58+ </div>
59+{% endif %} #}
60+
61+{% if result.content %}<p class="row result-content col-xs-12 col-sm-12 col-md-12">{{ result.content|safe }}</p>{% endif %}
3462
63+</div>
64+
3565{% if (result.latitude and result.longitude) or result.boundingbox %}
3666 <div class="collapse" id="result-map-{{ index }}">
3767 <div style="height:300px; width:100%; margin: 10px 0;" id="osm-map-{{ index }}"></div>