boat
C++23 geospatial library
Loading...
Searching...
No Matches
catalogs.hpp
Go to the documentation of this file.
1// Andrew Naplavkov
2
3#ifndef BOAT_CATALOGS_HPP
4#define BOAT_CATALOGS_HPP
5
6#include <boat/detail/uri.hpp>
7#if __has_include(<gdal.h>)
9#endif
10#if __has_include(<curl/curl.h>)
11#include <boat/slippy.hpp>
12#endif
13#include <boat/sql/catalog.hpp>
14#include <boat/sql/commands.hpp>
15
16namespace boat {
17
18inline std::unique_ptr<db::catalog> make_catalog(
19 std::string_view address, bool update = false)
20{
21 if (is_http_url(address))
22#if __has_include(<curl/curl.h>)
23 {
24 auto ret = std::make_unique<slippy::catalog>();
25 auto u = uri::parse(address);
26 auto os = std::ostringstream{};
27 os << u.scheme << "://" << u.host_spec << '/' << u.path;
28 for (auto sep{"?"}; !u.query.empty();) {
29 auto next = u.query.find('&');
30 auto len = (next == u.query.npos) ? u.query.size() : next;
31 auto pair = u.query.substr(0, len);
32 u.query.remove_prefix(len + (next != u.query.npos));
33 if (auto eq = pair.find('='); eq != pair.npos) {
34 auto key = pair.substr(0, eq);
35 auto val = pair.substr(eq + 1);
36 if (key == "agent") {
37 ret->agent = val;
38 continue;
39 }
40 if (key == "epsg") {
41 ret->epsg = from_chars<int>(val.data(), val.size());
42 continue;
43 }
44 if (key == "zmax") {
45 ret->zmax = from_chars<int>(val.data(), val.size());
46 continue;
47 }
48 if (key == "ssl") {
49 ret->ssl = from_chars<int>(val.data(), val.size());
50 continue;
51 }
52 }
53 os << std::exchange(sep, "&") << pair;
54 }
55 ret->url = std::move(os).str();
56 return ret;
57 }
58#else
59 throw std::runtime_error("compiled without curl");
60#endif
61 if (sql::supported_url(address)) {
62 auto ret = std::make_unique<sql::catalog>();
63 ret->command = sql::make_command(address);
64 return ret;
65 }
66#if __has_include(<gdal.h>)
67 auto ret = std::make_unique<gdal::catalog>();
68 ret->dataset = gdal::open(std::string{address}.data(), update);
69 return ret;
70#else
71 throw std::runtime_error("compiled without gdal");
72#endif
73}
74
75} // namespace boat
76
77#endif // BOAT_CATALOGS_HPP
dataset_ptr open(char const *file, bool update=false)
Definition dataset.hpp:11
std::unique_ptr< db::command > make_command(std::string_view url)
Throws if the URL scheme is unknown or its client library is unavailable.
Definition commands.hpp:39
bool supported_url(std::string_view url)
Recognizes SQL URL schemes even without the corresponding client library.
Definition commands.hpp:32
Definition blob.hpp:12
std::unique_ptr< db::catalog > make_catalog(std::string_view address, bool update=false)
Definition catalogs.hpp:18