MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
boundingbox.h
Go to the documentation of this file.
1/* Copyright 2014 MongoDB Inc.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
18#pragma once
19
20#include <vector>
21
22#include "mongo/db/jsobj.h"
23#include "mongo/geo/constants.h"
24#include "mongo/util/assert_util.h"
25
26namespace mongo {
27namespace geo {
28
66template <typename TCoordinates>
68public:
81 BoundingBox(const TCoordinates& min, const TCoordinates& max);
82
104 explicit BoundingBox(const BSONObj& bson);
105
112 TCoordinates getMin() const {
113 return _min;
114 }
115
122 TCoordinates getMax() const {
123 return _max;
124 }
125
136 BSONObj toBSON() const;
137
149 BSONArray toBSONArray() const;
150
163
164private:
165 TCoordinates _min;
166 TCoordinates _max;
167};
168
169template <typename TCoordinates>
170BoundingBox<TCoordinates>::BoundingBox(const TCoordinates& min, const TCoordinates& max)
171 : _min(min), _max(max) {}
172
173template <typename TCoordinates>
176
177 // Ensure bson defines a bounding box.
178 uassert(0, "bson argument to BoundingBox ctor must define the field \"bbox\"", !bbox.eoo());
179
180 std::vector<BSONElement> bboxCoords = bbox.Array();
181
182 // The GeoJSON spec dictates that bboxCoords is an array of
183 // length 2*n, where n is the number of dimensions represented
184 // in the contained geometries.
185 const size_t n = TCoordinates::dimensionality();
186 uassert(0,
187 "bbox field must have exactly 2 * n elements, where n is the number of dimensions "
188 "in the coordinate system",
189 bboxCoords.size() == 2 * n);
190
191 // Construct _min with the first n elements and _max with the second n elements.
192 std::vector<double> minCoords, maxCoords;
193 for (size_t i = 0; i < n; ++i) {
194 minCoords.push_back(bboxCoords[i].Double());
195 maxCoords.push_back(bboxCoords[n + i].Double());
196 }
197 _min = TCoordinates(minCoords);
198 _max = TCoordinates(maxCoords);
199}
200
201template <typename TCoordinates>
203 return BSON(kBoundingBoxFieldName << toBSONArray());
204}
205
206template <typename TCoordinates>
209 std::vector<double> minCoords = _min.getValues();
210 std::vector<double> maxCoords = _max.getValues();
211 for (size_t i = 0; i < minCoords.size(); ++i)
212 bab.append(minCoords[i]);
213 for (size_t i = 0; i < maxCoords.size(); ++i)
214 bab.append(maxCoords[i]);
215 return bab.arr();
216}
217
218template <typename TCoordinates>
220 BSONArrayBuilder minBab, maxBab;
221 std::vector<double> minCoords = _min.getValues();
222 std::vector<double> maxCoords = _max.getValues();
223 for (size_t i = 0; i < minCoords.size(); ++i)
224 minBab.append(minCoords[i]);
225 for (size_t i = 0; i < maxCoords.size(); ++i)
226 maxBab.append(maxCoords[i]);
227 return BSON_ARRAY(minBab.arr() << maxBab.arr());
228}
229
230} // namespace geo
231} // namespace mongo
Definition bsonobjbuilder.h:765
BSONArray arr()
destructive - ownership moves to returned BSONArray
Definition bsonobjbuilder.h:804
BSONElement represents an "element" in a BSONObj.
Definition bsonelement.h:55
bool eoo() const
Indicates if it is the end-of-object element, which is present at the end of every BSON object.
Definition bsonelement.h:172
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary representa...
Definition bsonobj.h:78
BSONElement getField(const StringData &name) const
Get the field of the specified name.
Represents a bounding box.
Definition boundingbox.h:67
TCoordinates getMin() const
Get the minimum coordinates of this bounding box.
Definition boundingbox.h:112
TCoordinates getMax() const
Get the maximum coordinates of this bounding box.
Definition boundingbox.h:122
BSONArray toBSONArray() const
Obtain a flat BSONArray representation of the coordinate values defined by this bounding box.
Definition boundingbox.h:207
BSONArray toNestedBSONArray() const
Obtain a nested BSONArray representation of the coordinate values defined by this bounding box.
Definition boundingbox.h:219
BSONObj toBSON() const
Obtain a BSON representation of this bounding box.
Definition boundingbox.h:202
BoundingBox(const TCoordinates &min, const TCoordinates &max)
BoundingBox constructor.
Definition boundingbox.h:170
const char kBoundingBoxFieldName[]
The special field names defined by the GeoJSON specification.
Definition constants.h:26
BSON classes.
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20
Definition bsonobj.h:581