浏览代码

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

Thomas Pointhuber 10 年前
父节点
当前提交
4b75d41f86

+ 4
- 0
searx/engines/openstreetmap.py 查看文件

@@ -39,6 +39,9 @@ def response(resp):
39 39
         url = result_base_url.format(osm_type=osm_type,
40 40
                                      osm_id=r['osm_id'])
41 41
 
42
+        osm = {'type':osm_type,
43
+               'id':r['osm_id']}
44
+
42 45
         geojson =  r.get('geojson')
43 46
 
44 47
         # if no geojson is found and osm_type is a node, add geojson Point
@@ -82,6 +85,7 @@ def response(resp):
82 85
                         'boundingbox': r['boundingbox'],
83 86
                         'geojson': geojson,
84 87
                         'address': address,
88
+                        'osm': osm,
85 89
                         'url': url})
86 90
 
87 91
     # return results

二进制
searx/static/oscar/img/loader.gif 查看文件


+ 83
- 1
searx/static/oscar/js/scripts.js 查看文件

@@ -68,6 +68,88 @@ $(document).ready(function(){
68 68
             source: searx.searchResults.ttAdapter()
69 69
         });
70 70
     }
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
+    });
71 153
 
72 154
     $(".searx_init_map").on( "click", function( event ) {
73 155
         var leaflet_target = $(this).data('leaflet-target');
@@ -119,7 +201,7 @@ $(document).ready(function(){
119 201
                     map.setView(new L.LatLng(map_lat, map_lon),8);
120 202
             }
121 203
 
122
-	        map.addLayer(osmMapnik);
204
+	        map.addLayer(osmMapquest);
123 205
 	        
124 206
 	        var baseLayers = {
125 207
              "OSM Mapnik": osmMapnik,

+ 33
- 3
searx/templates/oscar/result_templates/map.html 查看文件

@@ -3,13 +3,25 @@
3 3
 <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>
4 4
 
5 5
 {% if result.publishedDate %}<time class="text-muted" datetime="{{ result.publishedDate }}" pubdate>{{ result.publishedDate }}</time>{% endif %}
6
+
6 7
 <small><a class="text-info" href="https://web.archive.org/web/{{ result.pretty_url }}">{{ icon('link') }} {{ _('cached') }}</a></small>
8
+
7 9
 {% if (result.latitude and result.longitude) or result.boundingbox %}
8 10
     <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>
9 11
 {% endif %}
10 12
 
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
+
11 23
 {% 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">
13 25
     {% if result.address.name %}
14 26
         <strong itemprop="name">{{ result.address.name }}</strong><br/>
15 27
     {% endif %}
@@ -27,11 +39,29 @@
27 39
     {% if result.address.country %}
28 40
         <span itemprop="addressCountry">{{ result.address.country }}</span>
29 41
     {% endif %}
30
-</p><div class="clearfix"></div>
42
+</p>
31 43
 {% endif %}
32 44
 
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 %}
34 62
 
63
+</div>
64
+    
35 65
 {% if (result.latitude and result.longitude) or result.boundingbox %}
36 66
     <div class="collapse" id="result-map-{{ index }}">
37 67
         <div style="height:300px; width:100%; margin: 10px 0;" id="osm-map-{{ index }}"></div>