Browse Source

[enh][oscar_template] loading map informations from overpass-api

Thomas Pointhuber 10 years ago
parent
commit
4b75d41f86

+ 4 - 0
searx/engines/openstreetmap.py

@@ -39,6 +39,9 @@ def response(resp):
         url = result_base_url.format(osm_type=osm_type,
                                      osm_id=r['osm_id'])
 
+        osm = {'type':osm_type,
+               'id':r['osm_id']}
+
         geojson =  r.get('geojson')
 
         # if no geojson is found and osm_type is a node, add geojson Point
@@ -82,6 +85,7 @@ def response(resp):
                         'boundingbox': r['boundingbox'],
                         'geojson': geojson,
                         'address': address,
+                        'osm': osm,
                         'url': url})
 
     # return results

BIN
searx/static/oscar/img/loader.gif


+ 83 - 1
searx/static/oscar/js/scripts.js

@@ -68,6 +68,88 @@ $(document).ready(function(){
             source: searx.searchResults.ttAdapter()
         });
     }
+    
+    $(".searx_overpass_request").on( "click", function( event ) {
+        var overpass_url = "http://overpass-api.de/api/interpreter?data=";
+        var query_start = overpass_url + "[out:json][timeout:25];(";
+        var query_end = ");out meta;";
+        
+        var osm_id = $(this).data('osm-id');
+        var osm_type = $(this).data('osm-type');
+        var result_table = $(this).data('result-table');
+        var result_table_loadicon = "#" + $(this).data('result-table-loadicon');
+        
+        // tags which can be ignored
+        var osm_ignore_tags = [ "addr:city", "addr:country", "addr:housenumber", "addr:postcode", "addr:street" ]
+        
+        if(osm_id && osm_type && result_table) {
+            result_table = "#" + result_table;
+            var query = null;
+            switch(osm_type) {
+                case 'node':
+                    query = query_start + "node(" + osm_id + ");" + query_end;
+                    break;
+                case 'way':
+                    query = query_start + "way(" + osm_id + ");" + query_end;
+                    break;
+                case 'relation':
+                    query = query_start + "relation(" + osm_id + ");" + query_end;
+                    break;
+                default:
+                    break;
+            }
+            if(query) {
+                //alert(query);
+                var ajaxRequest = $.ajax( query )
+                .done(function( html) {
+                    if(html && html['elements'] && html['elements'][0]) {
+                        var element = html['elements'][0];
+                        var newHtml = $(result_table).html();
+                        for (var row in element.tags) {
+                            if(element.tags["name"] == null || osm_ignore_tags.indexOf(row) == -1) {
+                                newHtml += "<tr><td>" + row + "</td><td>";
+                                switch(row) {
+                                    case "phone":
+                                    case "fax":
+                                        newHtml += "<a href=\"tel:" + element.tags[row].replace(/ /g,'') + "\">" + element.tags[row] + "</a>";
+                                        break;
+                                    case "email":
+                                        newHtml += "<a href=\"mailto:" + element.tags[row] + "\">" + element.tags[row] + "</a>";
+                                        break;
+                                    case "website":
+                                    case "url":
+                                        newHtml += "<a href=\"" + element.tags[row] + "\">" + element.tags[row] + "</a>";
+                                        break;
+                                    case "wikidata":
+                                        newHtml += "<a href=\"https://www.wikidata.org/wiki/" + element.tags[row] + "\">" + element.tags[row] + "</a>";
+                                        break;
+                                    case "wikipedia":
+                                        if(element.tags[row].indexOf(":") != -1) {
+                                            newHtml += "<a href=\"https://" + element.tags[row].substring(0,element.tags[row].indexOf(":")) + ".wikipedia.org/wiki/" 
+                                                + element.tags[row].substring(element.tags[row].indexOf(":")+1) + "\">" + element.tags[row] + "</a>";
+                                            break;
+                                        }
+                                    default:
+                                        newHtml += element.tags[row];
+                                        break;
+                                }
+                                newHtml += "</td></tr>";
+                            }
+                        }
+                        $(result_table).html(newHtml);
+                        $(result_table).removeClass('hidden');
+                        $(result_table_loadicon).addClass('hidden');
+                    }
+                })
+                .fail(function() {
+                    alert( "could not load " );
+                })
+            }
+        }
+
+        // this event occour only once per element
+        $( this ).off( event );    
+    });
 
     $(".searx_init_map").on( "click", function( event ) {
         var leaflet_target = $(this).data('leaflet-target');
@@ -119,7 +201,7 @@ $(document).ready(function(){
                     map.setView(new L.LatLng(map_lat, map_lon),8);
             }
 
-	        map.addLayer(osmMapnik);
+	        map.addLayer(osmMapquest);
 	        
 	        var baseLayers = {
              "OSM Mapnik": osmMapnik,

+ 33 - 3
searx/templates/oscar/result_templates/map.html

@@ -3,13 +3,25 @@
 <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>
 
 {% if result.publishedDate %}<time class="text-muted" datetime="{{ result.publishedDate }}" pubdate>{{ result.publishedDate }}</time>{% endif %}
+
 <small><a class="text-info" href="https://web.archive.org/web/{{ result.pretty_url }}">{{ icon('link') }} {{ _('cached') }}</a></small>
+
 {% if (result.latitude and result.longitude) or result.boundingbox %}
     <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>
 {% endif %}
 
+{% if result.osm and (result.osm.type and result.osm.id) %}
+    <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>
+{% endif %}
+
+{# {% if (result.latitude and result.longitude) %}
+    <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>
+{% endif %} #}
+
+<div class="container-fluid">
+
 {% if result.address %}
-<p class="result-content result-adress" itemscope itemtype="http://schema.org/PostalAddress">
+<p class="row result-content result-adress col-xs-12 col-sm-5 col-md-4" itemscope itemtype="http://schema.org/PostalAddress">
     {% if result.address.name %}
         <strong itemprop="name">{{ result.address.name }}</strong><br/>
     {% endif %}
@@ -27,11 +39,29 @@
     {% if result.address.country %}
         <span itemprop="addressCountry">{{ result.address.country }}</span>
     {% endif %}
-</p><div class="clearfix"></div>
+</p>
 {% endif %}
 
-{% if result.content %}<p class="result-content">{{ result.content|safe }}</p>{% endif %}
+{% if result.osm and (result.osm.type and result.osm.id) %}
+    <div class="row result-content collapse col-xs-12 col-sm-7 col-md-8" id="result-overpass-{{ index }}">
+        <div class="text-center" id="result-overpass-table-loading-{{ index }}"><img src="{{ url_for('static', filename='img/loader.gif') }}" alt="Loading ..."/></div>
+        <table class="table table-striped table-condensed hidden" id="result-overpass-table-{{ index }}">
+            <tr><th>key</th><th>value</th></tr>
+        </table>
+    </div>
+{% endif %}
+
+{# {% if (result.latitude and result.longitude) %}
+    <div class="row collapse col-xs-12 col-sm-5 col-md-4" id="result-geodata-{{ index }}">
+        <strong>Longitude:</strong> {{ result.longitude }} <br/>
+        <strong>Latitude:</strong> {{ result.latitude }}
+    </div>
+{% endif %} #}
+
+{% if result.content %}<p class="row result-content col-xs-12 col-sm-12 col-md-12">{{ result.content|safe }}</p>{% endif %}
 
+</div>
+    
 {% if (result.latitude and result.longitude) or result.boundingbox %}
     <div class="collapse" id="result-map-{{ index }}">
         <div style="height:300px; width:100%; margin: 10px 0;" id="osm-map-{{ index }}"></div>