]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/geodb/geodb_impl.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
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).
17 #include "rocksdb/utilities/geo_db.h"
18 #include "rocksdb/utilities/stackable_db.h"
19 #include "rocksdb/env.h"
20 #include "rocksdb/status.h"
24 // A specific implementation of GeoDB
26 class GeoDBImpl
: public GeoDB
{
28 GeoDBImpl(DB
* db
, const GeoDBOptions
& options
);
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
;
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
;
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
;
44 // Delete the specified object
45 virtual Status
Remove(const Slice
& id
) override
;
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
;
54 const GeoDBOptions options_
;
55 const WriteOptions woptions_
;
56 const ReadOptions roptions_
;
58 // MSVC requires the definition for this static const to be in .CC file
60 static const double PI
;
62 // convert degrees to radians
63 static double radians(double x
);
65 // convert radians to degrees
66 static double degrees(double x
);
68 // A pixel class that captures X and Y coordinates
73 Pixel(unsigned int a
, unsigned int b
) :
78 // A Tile in the geoid
83 Tile(unsigned int a
, unsigned int b
) :
88 // convert a gps location to quad coordinate
89 static std::string
PositionToQuad(const GeoPosition
& pos
, int levelOfDetail
);
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
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
;
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
);
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
);
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
);
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
);
128 static GeoPosition
PixelToPosition(const Pixel
& pixel
, int levelOfDetail
);
130 // Converts a Pixel to a Tile
131 static Tile
PixelToTile(const Pixel
& pixel
);
133 static Pixel
TileToPixel(const Tile
& tile
);
135 // Convert a Tile to a quadkey
136 static std::string
TileToQuadKey(const Tile
& tile
, int levelOfDetail
);
138 // Convert a quadkey to a tile and its level of detail
139 static void QuadKeyToTile(std::string quadkey
, Tile
* tile
,
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
);
149 // Returns the top left position after applying the delta to
150 // the specified position
152 static GeoPosition
boundingTopLeft(const GeoPosition
& in
, double radius
) {
153 return displaceLatLon(in
.latitude
, in
.longitude
, -radius
, -radius
);
157 // Returns the bottom right position after applying the delta to
158 // the specified position
159 static GeoPosition
boundingBottomRight(const GeoPosition
& in
,
161 return displaceLatLon(in
.latitude
, in
.longitude
, radius
, radius
);
165 // Get all quadkeys within a radius of a specified position
167 Status
searchQuadIds(const GeoPosition
& position
,
169 std::vector
<std::string
>* quadKeys
);
172 // Create keys for accessing rocksdb table(s)
174 static std::string
MakeKey1(const GeoPosition
& pos
,
176 std::string quadkey
);
177 static std::string
MakeKey2(Slice id
);
178 static std::string
MakeKey1Prefix(std::string quadkey
,
180 static std::string
MakeQuadKeyPrefix(std::string quadkey
);
183 } // namespace rocksdb
185 #endif // ROCKSDB_LITE