Patrick Brosi hace 5 años
padre
commit
f8b8e054e5

+ 1 - 1
src/osmfixer/FixerMain.cpp

@@ -29,6 +29,6 @@ int main(int argc, char** argv) {
29 29
 
30 30
   // start server
31 31
 
32
-  StatServer serv;
32
+  StatServer serv(&idx);
33 33
   util::http::HttpServer(9090, &serv).run();
34 34
 }

+ 33 - 0
src/osmfixer/index/StatIdx.cpp

@@ -4,6 +4,7 @@
4 4
 
5 5
 #include <fstream>
6 6
 #include <iostream>
7
+#include <set>
7 8
 #include <sstream>
8 9
 #include <string>
9 10
 #include "osmfixer/index/StatIdx.h"
@@ -35,6 +36,8 @@ void StatIdx::readFromFile(const std::string& path) {
35 36
     addStation(osmid, lat, lng, attrs);
36 37
   }
37 38
 
39
+  initIndex();
40
+
38 41
   std::cout << util::geo::getWKT(_bbox) << std::endl;
39 42
 }
40 43
 
@@ -49,3 +52,33 @@ void StatIdx::addStation(size_t osmid, double lat, double lng,
49 52
   // extend bounding box
50 53
   _bbox = util::geo::extendBox(point, _bbox);
51 54
 }
55
+
56
+// _____________________________________________________________________________
57
+void StatIdx::initIndex() {
58
+  _grid = util::geo::Grid<size_t, util::geo::Point, double>(5000, 5000, _bbox,
59
+                                                            false);
60
+  for (size_t i = 0; i < _stations.size(); i++) {
61
+    _grid.add(_stations[i].pos, i);
62
+  }
63
+}
64
+
65
+// _____________________________________________________________________________
66
+std::vector<const Station*> StatIdx::getStations(
67
+    const util::geo::DBox bbox) const {
68
+  std::vector<const Station*> ret;
69
+  auto ll = util::geo::latLngToWebMerc<double>(bbox.getLowerLeft().getX(),
70
+                                               bbox.getLowerLeft().getY());
71
+  auto ur = util::geo::latLngToWebMerc<double>(bbox.getUpperRight().getX(),
72
+                                               bbox.getUpperRight().getY());
73
+
74
+  std::set<size_t> tmp;
75
+  auto reqBox = util::geo::DBox(ll, ur);
76
+  _grid.get(reqBox, &tmp);
77
+
78
+  for (auto i : tmp) {
79
+    if (util::geo::contains(_stations[i].pos, reqBox))
80
+      ret.push_back(&_stations[i]);
81
+  }
82
+
83
+  return ret;
84
+}

+ 6 - 1
src/osmfixer/index/StatIdx.h

@@ -9,12 +9,13 @@
9 9
 #include <string>
10 10
 #include <vector>
11 11
 #include "util/geo/Geo.h"
12
+#include "util/geo/Grid.h"
12 13
 
13 14
 namespace osmfixer {
14 15
 
15 16
 typedef std::map<std::string, std::vector<std::string>> OsmAttrs;
16 17
 
17
-struct Station{
18
+struct Station {
18 19
   size_t osmid;
19 20
   util::geo::DPoint pos;
20 21
   OsmAttrs attrs;
@@ -26,6 +27,8 @@ class StatIdx {
26 27
 
27 28
   void readFromFile(const std::string& path);
28 29
 
30
+  std::vector<const Station*> getStations(const util::geo::DBox bbox) const;
31
+
29 32
  private:
30 33
   void addStation(size_t id, double lat, double lng, const OsmAttrs& attrs);
31 34
 
@@ -33,6 +36,8 @@ class StatIdx {
33 36
 
34 37
   std::vector<Station> _stations;
35 38
   util::geo::DBox _bbox;
39
+
40
+  util::geo::Grid<size_t, util::geo::Point, double> _grid;
36 41
 };
37 42
 }
38 43
 

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

@@ -5,6 +5,9 @@
5 5
 #include "osmfixer/server/StatServer.h"
6 6
 #include "util/Misc.h"
7 7
 #include "util/String.h"
8
+#include "util/geo/Geo.h"
9
+#include "util/log/Log.h"
10
+#include "util/String.h"
8 11
 
9 12
 using osmfixer::StatServer;
10 13
 using osmfixer::Params;
@@ -20,6 +23,8 @@ util::http::Answer StatServer::handle(const util::http::Req& req,
20 23
 
21 24
     if (cmd == "/") {
22 25
       a = util::http::Answer("200 OK", "osmfixer");
26
+    } else if (cmd == "/map") {
27
+      a = handleMapReq(params);
23 28
     } else {
24 29
       a = util::http::Answer("404 Not Found", "dunno");
25 30
     }
@@ -39,6 +44,44 @@ util::http::Answer StatServer::handle(const util::http::Req& req,
39 44
 }
40 45
 
41 46
 // _____________________________________________________________________________
47
+util::http::Answer StatServer::handleMapReq(const Params& pars) const {
48
+  if (pars.count("bbox") == 0 || pars.find("bbox")->second.empty())
49
+    throw std::invalid_argument("No bbox specified.");
50
+  std::string cb;
51
+  if (pars.count("cb")) cb = pars.find("cb")->second.c_str();
52
+  auto box = util::split(pars.find("bbox")->second, ',');
53
+
54
+  if (box.size() != 4)
55
+    throw std::invalid_argument("Invalid request.");
56
+
57
+  double lat1 = atof(box[0].c_str());
58
+  double lng1 = atof(box[1].c_str());
59
+  double lat2 = atof(box[2].c_str());
60
+  double lng2 = atof(box[3].c_str());
61
+
62
+  std::cout << pars.find("bbox")->second << std::endl;
63
+
64
+  util::geo::DBox bbox(util::geo::DPoint(lng1, lat1), util::geo::DPoint(lng2, lat2));
65
+
66
+  LOG(INFO) << "Request for bounding box " << util::geo::getWKT(bbox);
67
+
68
+  std::stringstream json;
69
+
70
+  if (cb.size()) json << cb << "(";
71
+  json << "[";
72
+
73
+  // TODO: query index
74
+
75
+  json << "]";
76
+  if (cb.size()) json << ")";
77
+
78
+  auto answ = util::http::Answer("200 OK", json.str(), true);
79
+  answ.params["Content-Type"] = "application/javascript; charset=utf-8";
80
+
81
+  return answ;
82
+}
83
+
84
+// _____________________________________________________________________________
42 85
 std::string StatServer::parseUrl(std::string u, std::string pl,
43 86
                                  std::map<std::string, std::string>* params) {
44 87
   auto parts = util::split(u, '?');

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

@@ -5,6 +5,7 @@
5 5
 #ifndef OSMFIXER_SERVER_STATSERVER_H_
6 6
 #define OSMFIXER_SERVER_STATSERVER_H_
7 7
 
8
+#include "osmfixer/index/StatIdx.h"
8 9
 #include "util/http/Server.h"
9 10
 
10 11
 namespace osmfixer {
@@ -13,13 +14,17 @@ typedef std::map<std::string, std::string> Params;
13 14
 
14 15
 class StatServer : public util::http::Handler {
15 16
  public:
16
-  StatServer() {}
17
+  StatServer(const osmfixer::StatIdx* idx) : _idx(idx) {}
17 18
 
18 19
   virtual util::http::Answer handle(const util::http::Req& request,
19 20
                                     int connection) const;
20 21
 
21 22
  private:
22 23
   static std::string parseUrl(std::string u, std::string pl, Params* params);
24
+
25
+  util::http::Answer handleMapReq(const Params& pars) const;
26
+
27
+  const osmfixer::StatIdx* _idx;
23 28
 };
24 29
 }
25 30