Patrick Brosi hace 5 años
padre
commit
10b00867d3

+ 20 - 0
.gitignore

@@ -0,0 +1,20 @@
1
+Makefile
2
+CMakeCache.txt
3
+*.o
4
+*.a
5
+cmake_install.cmake
6
+*~
7
+CMakeFiles
8
+*.cfg
9
+*.cmake
10
+/build/
11
+*.pyc
12
+compile_commands.json
13
+.vimrc
14
+.clang_complete
15
+/.idea
16
+*.vs
17
+[._]*.s[a-w][a-z]
18
+[._]s[a-w][a-z]
19
+*.cppr
20
+*.hr

+ 18 - 1
src/osmfixer/FixerMain.cpp

@@ -3,6 +3,13 @@
3 3
 // Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
4 4
 
5 5
 #include <iostream>
6
+#include "osmfixer/index/StatIdx.h"
7
+#include "osmfixer/server/StatServer.h"
8
+#include "util/http/Server.h"
9
+#include "util/log/Log.h"
10
+
11
+using osmfixer::StatServer;
12
+using osmfixer::StatIdx;
6 13
 
7 14
 // _____________________________________________________________________________
8 15
 int main(int argc, char** argv) {
@@ -12,6 +19,16 @@ int main(int argc, char** argv) {
12 19
   // initialize randomness
13 20
   srand(time(NULL) + rand());  // NOLINT
14 21
 
22
+  if (argc < 2) {
23
+    LOG(ERROR) << "Missing path to stations file.";
24
+    exit(1);
25
+  }
26
+
27
+  StatIdx idx;
28
+  idx.readFromFile(argv[1]);
29
+
30
+  // start server
15 31
 
16
-  std::cout << "Hello." << std::endl;
32
+  StatServer serv;
33
+  util::http::HttpServer(9090, &serv).run();
17 34
 }

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

@@ -0,0 +1,51 @@
1
+// Copyright 2019, University of Freiburg,
2
+// Chair of Algorithms and Data Structures.
3
+// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
4
+
5
+#include <fstream>
6
+#include <iostream>
7
+#include <sstream>
8
+#include <string>
9
+#include "osmfixer/index/StatIdx.h"
10
+
11
+using osmfixer::StatIdx;
12
+using osmfixer::OsmAttrs;
13
+using osmfixer::Station;
14
+
15
+// _____________________________________________________________________________
16
+void StatIdx::readFromFile(const std::string& path) {
17
+  std::ifstream f(path);
18
+
19
+  std::string line;
20
+
21
+  // first, parse nodes
22
+  while (std::getline(f, line)) {
23
+    // empty line is separator between blocks
24
+    if (line.size() == 0) break;
25
+
26
+    std::stringstream rec(line);
27
+    size_t osmid;
28
+    double lat, lng;
29
+    OsmAttrs attrs;
30
+
31
+    rec >> osmid;
32
+    rec >> lat;
33
+    rec >> lng;
34
+
35
+    addStation(osmid, lat, lng, attrs);
36
+  }
37
+
38
+  std::cout << util::geo::getWKT(_bbox) << std::endl;
39
+}
40
+
41
+// _____________________________________________________________________________
42
+void StatIdx::addStation(size_t osmid, double lat, double lng,
43
+                         const OsmAttrs& attrs) {
44
+  std::cout << "Record: " << osmid << " " << lat << " " << lng << std::endl;
45
+  auto point = util::geo::latLngToWebMerc<double>(lat, lng);
46
+
47
+  _stations.emplace_back(Station{osmid, point, attrs});
48
+
49
+  // extend bounding box
50
+  _bbox = util::geo::extendBox(point, _bbox);
51
+}

+ 39 - 0
src/osmfixer/index/StatIdx.h

@@ -0,0 +1,39 @@
1
+// Copyright 2019, University of Freiburg,
2
+// Chair of Algorithms and Data Structures.
3
+// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
4
+
5
+#ifndef OSMFIXER_INDEX_STATIDX_H_
6
+#define OSMFIXER_INDEX_STATIDX_H_
7
+
8
+#include <map>
9
+#include <string>
10
+#include <vector>
11
+#include "util/geo/Geo.h"
12
+
13
+namespace osmfixer {
14
+
15
+typedef std::map<std::string, std::vector<std::string>> OsmAttrs;
16
+
17
+struct Station{
18
+  size_t osmid;
19
+  util::geo::DPoint pos;
20
+  OsmAttrs attrs;
21
+};
22
+
23
+class StatIdx {
24
+ public:
25
+  StatIdx() {}
26
+
27
+  void readFromFile(const std::string& path);
28
+
29
+ private:
30
+  void addStation(size_t id, double lat, double lng, const OsmAttrs& attrs);
31
+
32
+  void initIndex();
33
+
34
+  std::vector<Station> _stations;
35
+  util::geo::DBox _bbox;
36
+};
37
+}
38
+
39
+#endif

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

@@ -0,0 +1,64 @@
1
+// Copyright 2019, University of Freiburg,
2
+// Chair of Algorithms and Data Structures.
3
+// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
4
+
5
+#include "osmfixer/server/StatServer.h"
6
+#include "util/Misc.h"
7
+#include "util/String.h"
8
+
9
+using osmfixer::StatServer;
10
+using osmfixer::Params;
11
+
12
+// _____________________________________________________________________________
13
+util::http::Answer StatServer::handle(const util::http::Req& req,
14
+                                      int con) const {
15
+  UNUSED(con);
16
+  util::http::Answer a;
17
+  try {
18
+    Params params;
19
+    auto cmd = parseUrl(req.url, req.payload, &params);
20
+
21
+    if (cmd == "/") {
22
+      a = util::http::Answer("200 OK", "osmfixer");
23
+    } else {
24
+      a = util::http::Answer("404 Not Found", "dunno");
25
+    }
26
+  } catch (std::runtime_error e) {
27
+    a = util::http::Answer("400 Bad Request", e.what());
28
+  } catch (std::invalid_argument e) {
29
+    a = util::http::Answer("400 Bad Request", e.what());
30
+  } catch (...) {
31
+    a = util::http::Answer("500 Internal Server Error",
32
+                           "Internal Server Error.");
33
+  }
34
+
35
+  a.params["Access-Control-Allow-Origin"] = "*";
36
+  a.params["Server"] = "osmfixer";
37
+
38
+  return a;
39
+}
40
+
41
+// _____________________________________________________________________________
42
+std::string StatServer::parseUrl(std::string u, std::string pl,
43
+                                 std::map<std::string, std::string>* params) {
44
+  auto parts = util::split(u, '?');
45
+
46
+  if (parts.size() > 1) {
47
+    auto kvs = util::split(parts[1], '&');
48
+    for (auto kv : kvs) {
49
+      auto kvp = util::split(kv, '=');
50
+      if (kvp.size() == 1) kvp.push_back("");
51
+      (*params)[util::urlDecode(kvp[0])] = kvp[1];
52
+    }
53
+  }
54
+
55
+  // also parse post data
56
+  auto kvs = util::split(pl, '&');
57
+  for (auto kv : kvs) {
58
+    auto kvp = util::split(kv, '=');
59
+    if (kvp.size() == 1) kvp.push_back("");
60
+    (*params)[util::urlDecode(kvp[0])] = kvp[1];
61
+  }
62
+
63
+  return util::urlDecode(parts.front());
64
+}

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

@@ -0,0 +1,26 @@
1
+// Copyright 2019, University of Freiburg,
2
+// Chair of Algorithms and Data Structures.
3
+// Authors: Patrick Brosi <brosi@informatik.uni-freiburg.de>
4
+
5
+#ifndef OSMFIXER_SERVER_STATSERVER_H_
6
+#define OSMFIXER_SERVER_STATSERVER_H_
7
+
8
+#include "util/http/Server.h"
9
+
10
+namespace osmfixer {
11
+
12
+typedef std::map<std::string, std::string> Params;
13
+
14
+class StatServer : public util::http::Handler {
15
+ public:
16
+  StatServer() {}
17
+
18
+  virtual util::http::Answer handle(const util::http::Req& request,
19
+                                    int connection) const;
20
+
21
+ private:
22
+  static std::string parseUrl(std::string u, std::string pl, Params* params);
23
+};
24
+}
25
+
26
+#endif