Patrick Brosi 4 years ago
parent
commit
3190ab7c70
3 changed files with 138 additions and 3 deletions
  1. 80 0
      src/osmfixer/server/StatServer.cpp
  2. 1 0
      src/osmfixer/server/StatServer.h
  3. 57 3
      web/script.js

+ 80 - 0
src/osmfixer/server/StatServer.cpp

@@ -29,6 +29,8 @@ util::http::Answer StatServer::handle(const util::http::Req& req,
29 29
       a = handleHeatMapReq(params);
30 30
     } else if (cmd == "/stat") {
31 31
       a = handleStatReq(params);
32
+    } else if (cmd == "/group") {
33
+      a = handleGroupReq(params);
32 34
     } else {
33 35
       a = util::http::Answer("404 Not Found", "dunno");
34 36
     }
@@ -268,6 +270,84 @@ std::string StatServer::parseUrl(std::string u, std::string pl,
268 270
 }
269 271
 
270 272
 // _____________________________________________________________________________
273
+util::http::Answer StatServer::handleGroupReq(const Params& pars) const {
274
+  if (pars.count("id") == 0 || pars.find("id")->second.empty())
275
+    throw std::invalid_argument("No ID specified.");
276
+  std::string cb;
277
+  if (pars.count("cb")) cb = pars.find("cb")->second.c_str();
278
+
279
+  size_t gid = atol(pars.find("id")->second.c_str());
280
+  auto group = _idx->getGroup(gid);
281
+
282
+  if (!group) return util::http::Answer("404 Not Found", "Group not found.");
283
+
284
+  std::stringstream json;
285
+
286
+  json << std::setprecision(10);
287
+
288
+  if (cb.size()) json << cb << "(";
289
+  json << "{\"id\":" << gid << ","
290
+       << "\"osmid\":" << group->osmid << ","
291
+       << "\"attrs\":{";
292
+
293
+  char sep = ' ';
294
+
295
+  for (const auto& par : group->attrs) {
296
+    json << sep;
297
+    sep = ',';
298
+    json << "\"" << util::jsonStringEscape(par.first) << "\":[";
299
+
300
+    char sep2 = ' ';
301
+    for (const auto& val : par.second) {
302
+      json << sep2;
303
+      sep2 = ',';
304
+      json << "\"" << util::jsonStringEscape(val) << "\"";
305
+    }
306
+    json << "]";
307
+  }
308
+
309
+  json << "},\"stations\":[";
310
+
311
+  sep = ' ';
312
+  for (const auto& sid : group->stations) {
313
+    json << sep;
314
+    sep = ',';
315
+    const auto stat = _idx->getStation(sid);
316
+    json << "{\"id\":" << sid << ","
317
+         << "\"osmid\":" << stat->osmid << ","
318
+         << "\"attrs\":{";
319
+
320
+    char sep = ' ';
321
+
322
+    for (const auto& par : stat->attrs) {
323
+      json << sep;
324
+      sep = ',';
325
+      json << "\"" << util::jsonStringEscape(par.first) << "\":[";
326
+
327
+      char sep2 = ' ';
328
+      for (const auto& val : par.second) {
329
+        json << sep2;
330
+        sep2 = ',';
331
+        json << "\"" << util::jsonStringEscape(val) << "\"";
332
+      }
333
+      json << "]";
334
+    }
335
+    json << "}}";
336
+  }
337
+
338
+  json << "]";
339
+
340
+  json << "}";
341
+
342
+  if (cb.size()) json << ")";
343
+
344
+  auto answ = util::http::Answer("200 OK", json.str(), true);
345
+  answ.params["Content-Type"] = "application/javascript; charset=utf-8";
346
+
347
+  return answ;
348
+}
349
+
350
+// _____________________________________________________________________________
271 351
 util::http::Answer StatServer::handleStatReq(const Params& pars) const {
272 352
   if (pars.count("id") == 0 || pars.find("id")->second.empty())
273 353
     throw std::invalid_argument("No ID specified.");

+ 1 - 0
src/osmfixer/server/StatServer.h

@@ -27,6 +27,7 @@ class StatServer : public util::http::Handler {
27 27
   util::http::Answer handleMapReq(const Params& pars) const;
28 28
   util::http::Answer handleHeatMapReq(const Params& pars) const;
29 29
   util::http::Answer handleStatReq(const Params& pars) const;
30
+  util::http::Answer handleGroupReq(const Params& pars) const;
30 31
 
31 32
   void printStation(const Station* stat, std::ostream* out) const;
32 33
   static void printGroup(const Group* stat, std::ostream* out);

+ 57 - 3
web/script.js

@@ -31,8 +31,8 @@ function marker(stat, z) {
31 31
 }
32 32
 
33 33
 function poly(group, z) {
34
-    if (group.new) return L.polygon(group.poly, {color: "blue", smoothFactor : 0.4, fillOpacity:0.2})
35
-    return L.polygon(group.poly, {color: '#85f385', smoothFactor : 1, fillOpacity:0.2})
34
+    if (group.new) return L.polygon(group.poly, {color: "blue", smoothFactor : 0.4, fillOpacity:0.2, id : group.id})
35
+    return L.polygon(group.poly, {color: '#85f385', smoothFactor : 1, fillOpacity:0.2, id : group.id})
36 36
 }
37 37
 
38 38
 function sugArr(sug, z) {
@@ -141,6 +141,60 @@ function openStat(id, ll) {
141 141
     xmlhttp.send();
142 142
 }
143 143
 
144
+function renderGroup(grp, ll) {
145
+    var attrrows = {};
146
+
147
+    var content = document.createElement('div');
148
+    content.setAttribute("id", "nav")
149
+    var attrTbl = document.createElement('table');
150
+     attrTbl.setAttribute("id", "attr-tbl")
151
+    var suggD = document.createElement('table');
152
+    suggD.setAttribute("id", "sugg")
153
+
154
+
155
+    content.innerHTML = "Relation <a onmouseover='groupHl( " + grp.id + ")' onmouseout='groupUnHl( " + grp.id + ")' target='_blank' href='https://www.openstreetmap.org/relation/" + grp.osmid + "'>" + grp.osmid + "</a>";
156
+
157
+    content.appendChild(attrTbl);
158
+    content.appendChild(suggD);
159
+
160
+    var tbody = document.createElement('tbody');
161
+    attrTbl.appendChild(tbody);
162
+
163
+    for (var key in grp.stations) {
164
+        //var row = document.createElement('tr');
165
+        //var col1 = document.createElement('td');
166
+        //var col2 = document.createElement('td');
167
+        //tbody.appendChild(row);
168
+        //row.appendChild(col1);
169
+        //row.appendChild(col2);
170
+        //col1.innerHTML = "<a href=\"https://wiki.openstreetmap.org/wiki/Key:" + key + "\" target=\"_blank\">" + key + "</a>";
171
+        //for (var i = 0; i <  stat.attrs[key].length; i++) col2.innerHTML += "<span class='attrval'>" + stat.attrs[key][i] + "</span>"+ "<br>";
172
+        //attrrows[key] = row;
173
+    }
174
+
175
+    if (map.getZoom() < 18) {
176
+        map.setView(ll, 18, {animate: true});
177
+    }
178
+
179
+    L.popup({opacity: 0.8})
180
+        .setLatLng(ll)
181
+        .setContent(content)
182
+        .openOn(map);
183
+}
184
+
185
+function openGroup(id, ll) {
186
+    var xmlhttp = new XMLHttpRequest();
187
+    xmlhttp.onreadystatechange = function() {
188
+        if (this.readyState == 4 && this.status == 200) {
189
+            var content = JSON.parse(this.responseText);
190
+            renderGroup(content, ll);
191
+        }
192
+    };
193
+
194
+    xmlhttp.open("GET", "http://localhost:9090/group?id=" + id, true);
195
+    xmlhttp.send();
196
+}
197
+
144 198
 function groupHl(id) {
145 199
     if (!document.groupIdx[id]) return;
146 200
     document.groupIdx[id].setStyle({
@@ -254,7 +308,7 @@ function render() {
254 308
 
255 309
                 if (map.getZoom() > 15) {
256 310
                    labelLayer.addLayer(L.featureGroup(labels));
257
-                   layer.addLayer(L.featureGroup(groups).on('click', function(a) { console.log(a.layer.options);}));
311
+                   layer.addLayer(L.featureGroup(groups).on('click', function(a) { openGroup(a.layer.options.id, a.layer.getBounds().getCenter());}));
258 312
                    layer.addLayer(L.featureGroup(suggs).on('click', function(a) { openStat(a.layer.options.id, a.layer.getBounds().getCenter());}));
259 313
                 }
260 314