]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | // |
6 | ||
7 | #ifndef ROCKSDB_LITE | |
8 | ||
9 | #pragma once | |
10 | #include <algorithm> | |
11 | #include <cmath> | |
12 | #include <string> | |
13 | #include <sstream> | |
14 | #include <stdexcept> | |
15 | #include <vector> | |
16 | ||
17 | #include "rocksdb/utilities/geo_db.h" | |
18 | #include "rocksdb/utilities/stackable_db.h" | |
19 | #include "rocksdb/env.h" | |
20 | #include "rocksdb/status.h" | |
21 | ||
22 | namespace rocksdb { | |
23 | ||
24 | // A specific implementation of GeoDB | |
25 | ||
26 | class GeoDBImpl : public GeoDB { | |
27 | public: | |
28 | GeoDBImpl(DB* db, const GeoDBOptions& options); | |
29 | ~GeoDBImpl(); | |
30 | ||
31 | // Associate the GPS location with the identified by 'id'. The value | |
32 | // is a blob that is associated with this object. | |
33 | virtual Status Insert(const GeoObject& object) override; | |
34 | ||
35 | // Retrieve the value of the object located at the specified GPS | |
36 | // location and is identified by the 'id'. | |
37 | virtual Status GetByPosition(const GeoPosition& pos, const Slice& id, | |
38 | std::string* value) override; | |
39 | ||
40 | // Retrieve the value of the object identified by the 'id'. This method | |
41 | // could be potentially slower than GetByPosition | |
42 | virtual Status GetById(const Slice& id, GeoObject* object) override; | |
43 | ||
44 | // Delete the specified object | |
45 | virtual Status Remove(const Slice& id) override; | |
46 | ||
47 | // Returns a list of all items within a circular radius from the | |
48 | // specified gps location | |
49 | virtual GeoIterator* SearchRadial(const GeoPosition& pos, double radius, | |
50 | int number_of_values) override; | |
51 | ||
52 | private: | |
53 | DB* db_; | |
54 | const GeoDBOptions options_; | |
55 | const WriteOptions woptions_; | |
56 | const ReadOptions roptions_; | |
57 | ||
58 | // MSVC requires the definition for this static const to be in .CC file | |
59 | // The value of PI | |
60 | static const double PI; | |
61 | ||
62 | // convert degrees to radians | |
63 | static double radians(double x); | |
64 | ||
65 | // convert radians to degrees | |
66 | static double degrees(double x); | |
67 | ||
68 | // A pixel class that captures X and Y coordinates | |
69 | class Pixel { | |
70 | public: | |
71 | unsigned int x; | |
72 | unsigned int y; | |
73 | Pixel(unsigned int a, unsigned int b) : | |
74 | x(a), y(b) { | |
75 | } | |
76 | }; | |
77 | ||
78 | // A Tile in the geoid | |
79 | class Tile { | |
80 | public: | |
81 | unsigned int x; | |
82 | unsigned int y; | |
83 | Tile(unsigned int a, unsigned int b) : | |
84 | x(a), y(b) { | |
85 | } | |
86 | }; | |
87 | ||
88 | // convert a gps location to quad coordinate | |
89 | static std::string PositionToQuad(const GeoPosition& pos, int levelOfDetail); | |
90 | ||
91 | // arbitrary constant use for WGS84 via | |
92 | // http://en.wikipedia.org/wiki/World_Geodetic_System | |
93 | // http://mathforum.org/library/drmath/view/51832.html | |
94 | // http://msdn.microsoft.com/en-us/library/bb259689.aspx | |
95 | // http://www.tuicool.com/articles/NBrE73 | |
96 | // | |
97 | const int Detail = 23; | |
98 | // MSVC requires the definition for this static const to be in .CC file | |
99 | static const double EarthRadius; | |
100 | static const double MinLatitude; | |
101 | static const double MaxLatitude; | |
102 | static const double MinLongitude; | |
103 | static const double MaxLongitude; | |
104 | ||
105 | // clips a number to the specified minimum and maximum values. | |
106 | static double clip(double n, double minValue, double maxValue) { | |
107 | return fmin(fmax(n, minValue), maxValue); | |
108 | } | |
109 | ||
110 | // Determines the map width and height (in pixels) at a specified level | |
111 | // of detail, from 1 (lowest detail) to 23 (highest detail). | |
112 | // Returns the map width and height in pixels. | |
113 | static unsigned int MapSize(int levelOfDetail) { | |
114 | return (unsigned int)(256 << levelOfDetail); | |
115 | } | |
116 | ||
117 | // Determines the ground resolution (in meters per pixel) at a specified | |
118 | // latitude and level of detail. | |
119 | // Latitude (in degrees) at which to measure the ground resolution. | |
120 | // Level of detail, from 1 (lowest detail) to 23 (highest detail). | |
121 | // Returns the ground resolution, in meters per pixel. | |
122 | static double GroundResolution(double latitude, int levelOfDetail); | |
123 | ||
124 | // Converts a point from latitude/longitude WGS-84 coordinates (in degrees) | |
125 | // into pixel XY coordinates at a specified level of detail. | |
126 | static Pixel PositionToPixel(const GeoPosition& pos, int levelOfDetail); | |
127 | ||
128 | static GeoPosition PixelToPosition(const Pixel& pixel, int levelOfDetail); | |
129 | ||
130 | // Converts a Pixel to a Tile | |
131 | static Tile PixelToTile(const Pixel& pixel); | |
132 | ||
133 | static Pixel TileToPixel(const Tile& tile); | |
134 | ||
135 | // Convert a Tile to a quadkey | |
136 | static std::string TileToQuadKey(const Tile& tile, int levelOfDetail); | |
137 | ||
138 | // Convert a quadkey to a tile and its level of detail | |
139 | static void QuadKeyToTile(std::string quadkey, Tile* tile, | |
140 | int *levelOfDetail); | |
141 | ||
142 | // Return the distance between two positions on the earth | |
143 | static double distance(double lat1, double lon1, | |
144 | double lat2, double lon2); | |
145 | static GeoPosition displaceLatLon(double lat, double lon, | |
146 | double deltay, double deltax); | |
147 | ||
148 | // | |
149 | // Returns the top left position after applying the delta to | |
150 | // the specified position | |
151 | // | |
152 | static GeoPosition boundingTopLeft(const GeoPosition& in, double radius) { | |
153 | return displaceLatLon(in.latitude, in.longitude, -radius, -radius); | |
154 | } | |
155 | ||
156 | // | |
157 | // Returns the bottom right position after applying the delta to | |
158 | // the specified position | |
159 | static GeoPosition boundingBottomRight(const GeoPosition& in, | |
160 | double radius) { | |
161 | return displaceLatLon(in.latitude, in.longitude, radius, radius); | |
162 | } | |
163 | ||
164 | // | |
165 | // Get all quadkeys within a radius of a specified position | |
166 | // | |
167 | Status searchQuadIds(const GeoPosition& position, | |
168 | double radius, | |
169 | std::vector<std::string>* quadKeys); | |
170 | ||
171 | // | |
172 | // Create keys for accessing rocksdb table(s) | |
173 | // | |
174 | static std::string MakeKey1(const GeoPosition& pos, | |
175 | Slice id, | |
176 | std::string quadkey); | |
177 | static std::string MakeKey2(Slice id); | |
178 | static std::string MakeKey1Prefix(std::string quadkey, | |
179 | Slice id); | |
180 | static std::string MakeQuadKeyPrefix(std::string quadkey); | |
181 | }; | |
182 | ||
183 | } // namespace rocksdb | |
184 | ||
185 | #endif // ROCKSDB_LITE |